@bloks/utils
Advanced tools
Comparing version 3.5.68 to 3.5.69
{ | ||
"name": "@bloks/utils", | ||
"version": "3.5.68", | ||
"version": "3.5.69", | ||
"description": "", | ||
@@ -21,3 +21,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "974f11b00bd54ebdd985bb30a9d4d2fabc0613e4" | ||
"gitHead": "b0fbc27eee82b507ce105cde2cbf1a3905f2bdf3" | ||
} |
@@ -1,18 +0,25 @@ | ||
const regex = /^([a-z]*)(\d*)/i | ||
function* zip(...args) { | ||
const iterators = args.map(x => x[Symbol.iterator]()) | ||
while (true) { | ||
const current = iterators.map(x => x.next()) | ||
if (current.some(x => x.done)) { | ||
break; | ||
} | ||
yield current.map(x => x.value) | ||
} | ||
} | ||
export function sortAlphaNum(a, b) { | ||
const _a = a.match(regex) | ||
const _b = b.match(regex) | ||
let toCompare = [...zip(a, b)] | ||
// if the alphabetic part of a is less than that of b => -1 | ||
if (_a[1] < _b[1]) return -1 | ||
// if the alphabetic part of a is greater than that of b => 1 | ||
if (_a[1] > _b[1]) return 1 | ||
for (const item of toCompare) { | ||
if (item[0] < item[1]) return -1 | ||
if (item[0] > item[1]) return 1 | ||
} | ||
// if the alphabetic parts are equal, check the number parts | ||
const _n = parseInt(_a[2]) - parseInt(_b[2]) | ||
if (_n == 0) // if the number parts are equal start a recursive test on the rest | ||
return sortFn(a.substr(_a[0].length), b.substr(_b[0].length)) | ||
// else, just sort using the numbers parts | ||
return _n | ||
} | ||
return a.length === b.length | ||
? 0 | ||
: a.length > b.length | ||
? 1 | ||
: -1 | ||
} |
965
32282