Skip to content

Commit

Permalink
refactor: sort strings with localeCompare
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <[email protected]>
  • Loading branch information
pedrolamas committed Nov 10, 2022
1 parent 935a6c5 commit b5dfff6
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 37 deletions.
6 changes: 1 addition & 5 deletions src/store/cameras/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ export const getters: GetterTree<CamerasState, RootState> = {
return [
...state.cameras,
...camerasInConfig
].sort((a, b) => {
const name1 = a.name.toLowerCase()
const name2 = b.name.toLowerCase()
return (name1 < name2) ? -1 : (name1 > name2) ? 1 : 0
})
].sort((a, b) => a.name.localeCompare(b.name))
},

getCamerasInConfig: (state, getters, rootState, rootGetters) => {
Expand Down
11 changes: 7 additions & 4 deletions src/store/config/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ export const getters: GetterTree<ConfigState, RootState> = {
},

getInstances: (state) => {
const instances = [...state.instances]
instances.sort((a, b) => {
return a.active ? -1 : (b.active ? 1 : a.name.localeCompare(b.name))
})
const instances = [
...state.instances
].sort((a, b) =>
a.active
? -1
: (b.active ? 1 : a.name.localeCompare(b.name))
)
return instances
},

Expand Down
2 changes: 1 addition & 1 deletion src/store/history/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const getters: GetterTree<HistoryState, RootState> = {
*/
getHistory: (state) => {
return (state.jobs && state.jobs.length)
? [...state.jobs].sort((a, b) => { return b.start_time - a.start_time })
? [...state.jobs].sort((a, b) => b.start_time - a.start_time)
: []
},

Expand Down
12 changes: 2 additions & 10 deletions src/store/macros/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ export const getters: GetterTree<MacrosState, RootState> = {

return r
})
.sort((a, b) => {
const name1 = a.name.toLowerCase()
const name2 = b.name.toLowerCase()
return (name1 < name2) ? -1 : (name1 > name2) ? 1 : 0
})
.sort((a, b) => a.name.localeCompare(b.name))
return macros
},

Expand Down Expand Up @@ -109,11 +105,7 @@ export const getters: GetterTree<MacrosState, RootState> = {
count
}
})
.sort((a, b) => {
const name1 = a.name.toLowerCase()
const name2 = b.name.toLowerCase()
return (name1 < name2) ? -1 : (name1 > name2) ? 1 : 0
})
.sort((a, b) => a.name.localeCompare(b.name))

return categories
}
Expand Down
15 changes: 9 additions & 6 deletions src/store/mesh/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ export const getters: GetterTree<MeshState, RootState> = {
}
}
}
return meshes.sort((a: KlipperMesh, b: KlipperMesh) => {
const name1 = a.profile_name.toLowerCase()
const name2 = b.profile_name.toLowerCase()
if (a.profile_name === 'default' || b.profile_name === 'default') return 1
return (name1 < name2) ? -1 : (name1 > name2) ? 1 : 0
})
return meshes.sort((a, b) =>
a.profile_name === 'default'
? -1
: (
b.profile_name === 'default'
? 1
: a.profile_name.localeCompare(b.profile_name)
)
)
},

/**
Expand Down
9 changes: 3 additions & 6 deletions src/store/printer/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,8 @@ export const getters: GetterTree<PrinterState, RootState> = {
})
}
})
return r.sort((a: Heater, b: Heater) => {
const name1 = a.name.toUpperCase()
const name2 = b.name.toUpperCase()
return (name1 < name2) ? -1 : (name1 > name2) ? 1 : 0
})

return r.sort((a, b) => a.name.localeCompare(b.name))
}
return []
},
Expand Down Expand Up @@ -438,7 +435,7 @@ export const getters: GetterTree<PrinterState, RootState> = {
const outputs = getters.getOutputs([
'output_pin'
])
return outputs.sort((output: OutputPin) => output.pwm ? 1 : 1)
return outputs.sort((output: OutputPin) => output.pwm ? 1 : -1)
},

/**
Expand Down
6 changes: 1 addition & 5 deletions src/store/version/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ export const getters: GetterTree<VersionState, RootState> = {
r.key = k
return r
})
.sort((a, b) => a.key.localeCompare(b.key))

o.sort((a, b) => {
const name1 = a.key.toLowerCase()
const name2 = b.key.toLowerCase()
return (name1 < name2) ? -1 : (name1 > name2) ? 1 : 0
})
return o
},

Expand Down

0 comments on commit b5dfff6

Please sign in to comment.