Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

natural-orderby

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

natural-orderby - npm Package Compare versions

Comparing version 4.0.0 to 5.0.0

7

CHANGELOG.md
# natural-orderby
## 5.0.0
### Major Changes
- feat: support numeric separators
- feat: add ability to define locale for sorting unicode
## 4.0.0

@@ -4,0 +11,0 @@

4

dist/index.d.ts

@@ -6,3 +6,5 @@ type CompareFn = (valueA: unknown, valueB: unknown) => number;

order?: OrderEnum;
locale?: Locale;
} | OrderEnum | undefined;
type Locale = string;
type IdentifierFn<T> = (value: T) => unknown;

@@ -23,4 +25,4 @@ type Identifier<T> = IdentifierFn<T> | keyof T | number;

*/
declare function orderBy<T>(collection: ReadonlyArray<T>, identifiers?: ReadonlyArray<Identifier<T>> | Identifier<T> | null, orders?: ReadonlyArray<Order> | Order | null): Array<T>;
declare function orderBy<T>(collection: ReadonlyArray<T>, identifiers?: ReadonlyArray<Identifier<T>> | Identifier<T> | null, orders?: ReadonlyArray<Order> | Order | null, locale?: Locale): Array<T>;
export { type CompareFn, type CompareOptions, type Identifier, type Order, compare, orderBy };
/**
* natural-orderby v4.0.0
* natural-orderby v5.0.0
*

@@ -21,4 +21,4 @@ * Copyright (c) Olaf Ennen

var compareUnicode = function compareUnicode(stringA, stringB) {
var result = stringA.localeCompare(stringB);
var compareUnicode = function compareUnicode(stringA, stringB, locale) {
var result = stringA.localeCompare(stringB, locale);
return result ? result / Math.abs(result) : 0;

@@ -46,3 +46,3 @@ };

var compareChunks = function compareChunks(chunksA, chunksB) {
var compareChunks = function compareChunks(chunksA, chunksB, locale) {
var lengthA = chunksA.length;

@@ -77,3 +77,3 @@ var lengthB = chunksB.length;

// use locale comparison only if one of the chunks contains unicode characters
return compareUnicode(chunkA.normalizedString, chunkB.normalizedString);
return compareUnicode(chunkA.normalizedString, chunkB.normalizedString, locale);
} else {

@@ -117,3 +117,3 @@ // use common string comparison for performance reason

var compareValues = function compareValues(valueA, valueB) {
var compareValues = function compareValues(valueA, valueB, locale) {
if (valueA.value === valueB.value) {

@@ -126,3 +126,3 @@ return 0;

if (valueA.chunks && valueB.chunks) {
return compareChunks(valueA.chunks, valueB.chunks);
return compareChunks(valueA.chunks, valueB.chunks, locale);
}

@@ -138,3 +138,3 @@ return compareOtherTypes(valueA, valueB);

if (value.length !== 0) {
var parsedNumber = Number(value);
var parsedNumber = Number(value.replace(/_/g, ''));
if (!Number.isNaN(parsedNumber)) {

@@ -264,3 +264,3 @@ return parsedNumber;

var b = getMappedValueRecord(valueB);
var result = compareValues(a, b);
var result = compareValues(a, b, options.locale);
return result * (options.order === 'desc' ? -1 : 1);

@@ -275,9 +275,16 @@ };

var order = 'asc';
var locale; // = 'en';
if (typeof customOptions === 'string' && isValidOrder(customOptions)) {
order = customOptions;
} else if (customOptions && typeof customOptions === 'object' && customOptions.order && isValidOrder(customOptions.order)) {
order = customOptions.order;
} else if (customOptions && typeof customOptions === 'object') {
if (customOptions.order && isValidOrder(customOptions.order)) {
order = customOptions.order;
}
if (customOptions.locale && customOptions.locale.length > 0) {
locale = customOptions.locale;
}
}
return {
order: order
order: order,
locale: locale
};

@@ -295,3 +302,3 @@ };

var compareMultiple = function compareMultiple(recordA, recordB, orders) {
var compareMultiple = function compareMultiple(recordA, recordB, orders, locale) {
var indexA = recordA.index,

@@ -311,3 +318,3 @@ valuesA = recordA.values;

} else {
var _result = compareValues(valuesA[i], valuesB[i]);
var _result = compareValues(valuesA[i], valuesB[i], locale);
if (_result) {

@@ -348,3 +355,3 @@ return _result * (order === 'desc' ? -1 : 1);

var baseOrderBy = function baseOrderBy(collection, identifiers, orders) {
var baseOrderBy = function baseOrderBy(collection, identifiers, orders, locale) {
var identifierFns = identifiers.length ? identifiers.map(createIdentifierFn) : [function (value) {

@@ -367,3 +374,3 @@ return value;

mappedCollection.sort(function (recordA, recordB) {
return compareMultiple(recordA, recordB, orders);
return compareMultiple(recordA, recordB, orders, locale);
});

@@ -407,3 +414,3 @@ return mappedCollection.map(function (element) {

*/
function orderBy(collection, identifiers, orders) {
function orderBy(collection, identifiers, orders, locale) {
if (!collection || !Array.isArray(collection)) {

@@ -414,5 +421,5 @@ return [];

var validatedOrders = getOrders(orders);
return baseOrderBy(collection, validatedIdentifiers, validatedOrders);
return baseOrderBy(collection, validatedIdentifiers, validatedOrders, locale);
}
export { compare, orderBy };
/**
* natural-orderby v4.0.0
* natural-orderby v5.0.0
*

@@ -4,0 +4,0 @@ * Copyright (c) Olaf Ennen

/**
* natural-orderby v4.0.0
* natural-orderby v5.0.0
*

@@ -21,4 +21,4 @@ * Copyright (c) Olaf Ennen

const compareUnicode = (stringA, stringB) => {
const result = stringA.localeCompare(stringB);
const compareUnicode = (stringA, stringB, locale) => {
const result = stringA.localeCompare(stringB, locale);
return result ? result / Math.abs(result) : 0;

@@ -46,3 +46,3 @@ };

const compareChunks = (chunksA, chunksB) => {
const compareChunks = (chunksA, chunksB, locale) => {
const lengthA = chunksA.length;

@@ -77,3 +77,3 @@ const lengthB = chunksB.length;

// use locale comparison only if one of the chunks contains unicode characters
return compareUnicode(chunkA.normalizedString, chunkB.normalizedString);
return compareUnicode(chunkA.normalizedString, chunkB.normalizedString, locale);
} else {

@@ -117,3 +117,3 @@ // use common string comparison for performance reason

const compareValues = (valueA, valueB) => {
const compareValues = (valueA, valueB, locale) => {
if (valueA.value === valueB.value) {

@@ -126,3 +126,3 @@ return 0;

if (valueA.chunks && valueB.chunks) {
return compareChunks(valueA.chunks, valueB.chunks);
return compareChunks(valueA.chunks, valueB.chunks, locale);
}

@@ -138,3 +138,3 @@ return compareOtherTypes(valueA, valueB);

if (value.length !== 0) {
const parsedNumber = Number(value);
const parsedNumber = Number(value.replace(/_/g, ''));
if (!Number.isNaN(parsedNumber)) {

@@ -247,3 +247,3 @@ return parsedNumber;

const b = getMappedValueRecord(valueB);
const result = compareValues(a, b);
const result = compareValues(a, b, options.locale);
return result * (options.order === 'desc' ? -1 : 1);

@@ -255,11 +255,16 @@ };

let order = 'asc';
let locale; // = 'en';
if (typeof customOptions === 'string' && isValidOrder(customOptions)) {
order = customOptions;
} else if (customOptions && typeof customOptions === 'object' && customOptions.order && isValidOrder(customOptions.order)) {
({
order
} = customOptions);
} else if (customOptions && typeof customOptions === 'object') {
if (customOptions.order && isValidOrder(customOptions.order)) {
order = customOptions.order;
}
if (customOptions.locale && customOptions.locale.length > 0) {
locale = customOptions.locale;
}
}
return {
order
order,
locale
};

@@ -277,3 +282,3 @@ };

const compareMultiple = (recordA, recordB, orders) => {
const compareMultiple = (recordA, recordB, orders, locale) => {
const {

@@ -299,3 +304,3 @@ index: indexA,

} else {
const result = compareValues(valuesA[i], valuesB[i]);
const result = compareValues(valuesA[i], valuesB[i], locale);
if (result) {

@@ -332,3 +337,3 @@ return result * (order === 'desc' ? -1 : 1);

const baseOrderBy = (collection, identifiers, orders) => {
const baseOrderBy = (collection, identifiers, orders, locale) => {
const identifierFns = identifiers.length ? identifiers.map(createIdentifierFn) : [value => value];

@@ -346,3 +351,3 @@

// iterate over values and compare values until a != b or last value reached
mappedCollection.sort((recordA, recordB) => compareMultiple(recordA, recordB, orders));
mappedCollection.sort((recordA, recordB) => compareMultiple(recordA, recordB, orders, locale));
return mappedCollection.map(element => getElementByIndex(collection, element.index));

@@ -379,3 +384,3 @@ };

*/
function orderBy(collection, identifiers, orders) {
function orderBy(collection, identifiers, orders, locale) {
if (!collection || !Array.isArray(collection)) {

@@ -386,5 +391,5 @@ return [];

const validatedOrders = getOrders(orders);
return baseOrderBy(collection, validatedIdentifiers, validatedOrders);
return baseOrderBy(collection, validatedIdentifiers, validatedOrders, locale);
}
export { compare, orderBy };
/**
* natural-orderby v4.0.0
* natural-orderby v5.0.0
*

@@ -11,2 +11,2 @@ * Copyright (c) Olaf Ennen

*/
const e=(e,r)=>e<r?-1:e>r?1:0,r=(e,r)=>{const n=e.localeCompare(r);return n?n/Math.abs(n):0},n=/(^0x[\da-fA-F]+$|^([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?(?!\.\d+)(?=\D|\s|$))|\d+)/g,t=/^\s+|\s+$/g,i=/\s+/g,s=/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/,o=/(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[/-]\d{1,4}[/-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,a=/^0+[1-9]{1}[0-9]*$/,u=/[^\x00-\x80]/,c=(e,r)=>e<r?-1:e>r?1:0,l=(n,t)=>n.value===t.value?0:void 0!==n.parsedNumber&&void 0!==t.parsedNumber?e(n.parsedNumber,t.parsedNumber):n.chunks&&t.chunks?((n,t)=>{const i=n.length,s=t.length,o=Math.min(i,s);for(let a=0;a<o;a++){const i=n[a],s=t[a];if(i.normalizedString!==s.normalizedString){if(""===i.normalizedString!=(""===s.normalizedString))return""===i.normalizedString?-1:1;if(void 0!==i.parsedNumber&&void 0!==s.parsedNumber){const r=e(i.parsedNumber,s.parsedNumber);return 0===r?c(i.normalizedString,s.normalizedString):r}return void 0!==i.parsedNumber||void 0!==s.parsedNumber?void 0!==i.parsedNumber?-1:1:u.test(i.normalizedString+s.normalizedString)?r(i.normalizedString,s.normalizedString):c(i.normalizedString,s.normalizedString)}}return i>o||s>o?i<=o?-1:1:0})(n.chunks,t.chunks):((e,r)=>(e.chunks?!r.chunks:r.chunks)?e.chunks?-1:1:(e.isNaN?!r.isNaN:r.isNaN)?e.isNaN?-1:1:(e.isSymbol?!r.isSymbol:r.isSymbol)?e.isSymbol?-1:1:(e.isObject?!r.isObject:r.isObject)?e.isObject?-1:1:(e.isArray?!r.isArray:r.isArray)?e.isArray?-1:1:(e.isFunction?!r.isFunction:r.isFunction)?e.isFunction?-1:1:(e.isNull?!r.isNull:r.isNull)?e.isNull?-1:1:0)(n,t),d=e=>e.replace(i," ").replace(t,""),f=e=>{if(0!==e.length){const r=Number(e);if(!Number.isNaN(r))return r}},m=(e,r,n)=>{if(s.test(e)&&(!a.test(e)||0===r||"."!==n[r-1]))return f(e)||0},p=(e,r,n)=>({parsedNumber:m(e,r,n),normalizedString:d(e)}),N=e=>{const r=(e=>e.replace(n,"\0$1\0").replace(/\0$/,"").replace(/^\0/,"").split("\0"))(e).map(p);return r},b=e=>"function"==typeof e,y=e=>Number.isNaN(e)||e instanceof Number&&Number.isNaN(e.valueOf()),g=e=>null===e,S=e=>!(null===e||"object"!=typeof e||Array.isArray(e)||e instanceof Number||e instanceof String||e instanceof Boolean||e instanceof Date),v=e=>"symbol"==typeof e,h=e=>void 0===e,A=e=>{const r=f(e);return void 0!==r?r:(e=>{try{const r=Date.parse(e);return!Number.isNaN(r)&&o.test(e)?r:void 0}catch{return}})(e)},z=e=>{if("string"==typeof e||e instanceof String||("number"==typeof e||e instanceof Number)&&!y(e)||"boolean"==typeof e||e instanceof Boolean||e instanceof Date){const r=(e=>"boolean"==typeof e||e instanceof Boolean?Number(e).toString():"number"==typeof e||e instanceof Number?e.toString():e instanceof Date?e.getTime().toString():"string"==typeof e||e instanceof String?e.toLowerCase().replace(t,""):"")(e),n=A(r);return{parsedNumber:n,chunks:N(n?`${n}`:r),value:e}}return{isArray:Array.isArray(e),isFunction:b(e),isNaN:y(e),isNull:g(e),isObject:S(e),isSymbol:v(e),isUndefined:h(e),value:e}},j=e=>"string"==typeof e&&("asc"===e||"desc"===e);function k(e){return(e=>(r,n)=>{const t=z(r),i=z(n);return l(t,i)*("desc"===e.order?-1:1)})((e=>{let r="asc";return"string"==typeof e&&j(e)?r=e:e&&"object"==typeof e&&e.order&&j(e.order)&&({order:r}=e),{order:r}})(e))}const w=e=>"function"==typeof e?e:r=>{if(Array.isArray(r)){const n=Number(e);if(Number.isInteger(n))return r[n]}else if(r&&"object"==typeof r){const n=Object.getOwnPropertyDescriptor(r,e);return n?.value}return r},x=(e,r,n)=>{const t=r.length?r.map(w):[e=>e],i=e.map(((e,r)=>({index:r,values:t.map((r=>r(e))).map(z)})));return i.sort(((e,r)=>((e,r,n)=>{const{index:t,values:i}=e,{index:s,values:o}=r,{length:a}=i,u=n.length;for(let c=0;c<a;c++){const e=c<u?n[c]:null;if(e&&"function"==typeof e){const r=e(i[c].value,o[c].value);if(r)return r}else{const r=l(i[c],o[c]);if(r)return r*("desc"===e?-1:1)}}return t-s})(e,r,n))),i.map((r=>((e,r)=>e[r])(e,r.index)))};function O(e,r,n){if(!e||!Array.isArray(e))return[];const t=(e=>{if(!e)return[];const r=Array.isArray(e)?[...e]:[e];return r.some((e=>"string"!=typeof e&&"number"!=typeof e&&"function"!=typeof e))?[]:r})(r),i=(e=>{if(!e)return[];const r=Array.isArray(e)?[...e]:[e];return r.some((e=>"asc"!==e&&"desc"!==e&&"function"!=typeof e))?[]:r})(n);return x(e,t,i)}export{k as compare,O as orderBy};
const e=(e,r)=>e<r?-1:e>r?1:0,r=(e,r,n)=>{const t=e.localeCompare(r,n);return t?t/Math.abs(t):0},n=/(^0x[\da-fA-F]+$|^([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?(?!\.\d+)(?=\D|\s|$))|\d+)/g,t=/^\s+|\s+$/g,i=/\s+/g,o=/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/,s=/(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[/-]\d{1,4}[/-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,a=/^0+[1-9]{1}[0-9]*$/,u=/[^\x00-\x80]/,c=(e,r)=>e<r?-1:e>r?1:0,l=(n,t,i)=>n.value===t.value?0:void 0!==n.parsedNumber&&void 0!==t.parsedNumber?e(n.parsedNumber,t.parsedNumber):n.chunks&&t.chunks?((n,t,i)=>{const o=n.length,s=t.length,a=Math.min(o,s);for(let l=0;l<a;l++){const o=n[l],s=t[l];if(o.normalizedString!==s.normalizedString){if(""===o.normalizedString!=(""===s.normalizedString))return""===o.normalizedString?-1:1;if(void 0!==o.parsedNumber&&void 0!==s.parsedNumber){const r=e(o.parsedNumber,s.parsedNumber);return 0===r?c(o.normalizedString,s.normalizedString):r}return void 0!==o.parsedNumber||void 0!==s.parsedNumber?void 0!==o.parsedNumber?-1:1:u.test(o.normalizedString+s.normalizedString)?r(o.normalizedString,s.normalizedString,i):c(o.normalizedString,s.normalizedString)}}return o>a||s>a?o<=a?-1:1:0})(n.chunks,t.chunks,i):((e,r)=>(e.chunks?!r.chunks:r.chunks)?e.chunks?-1:1:(e.isNaN?!r.isNaN:r.isNaN)?e.isNaN?-1:1:(e.isSymbol?!r.isSymbol:r.isSymbol)?e.isSymbol?-1:1:(e.isObject?!r.isObject:r.isObject)?e.isObject?-1:1:(e.isArray?!r.isArray:r.isArray)?e.isArray?-1:1:(e.isFunction?!r.isFunction:r.isFunction)?e.isFunction?-1:1:(e.isNull?!r.isNull:r.isNull)?e.isNull?-1:1:0)(n,t),d=e=>e.replace(i," ").replace(t,""),f=e=>{if(0!==e.length){const r=Number(e.replace(/_/g,""));if(!Number.isNaN(r))return r}},m=(e,r,n)=>{if(o.test(e)&&(!a.test(e)||0===r||"."!==n[r-1]))return f(e)||0},p=(e,r,n)=>({parsedNumber:m(e,r,n),normalizedString:d(e)}),N=e=>{const r=(e=>e.replace(n,"\0$1\0").replace(/\0$/,"").replace(/^\0/,"").split("\0"))(e).map(p);return r},b=e=>"function"==typeof e,y=e=>Number.isNaN(e)||e instanceof Number&&Number.isNaN(e.valueOf()),g=e=>null===e,S=e=>!(null===e||"object"!=typeof e||Array.isArray(e)||e instanceof Number||e instanceof String||e instanceof Boolean||e instanceof Date),v=e=>"symbol"==typeof e,h=e=>void 0===e,A=e=>{const r=f(e);return void 0!==r?r:(e=>{try{const r=Date.parse(e);return!Number.isNaN(r)&&s.test(e)?r:void 0}catch{return}})(e)},z=e=>{if("string"==typeof e||e instanceof String||("number"==typeof e||e instanceof Number)&&!y(e)||"boolean"==typeof e||e instanceof Boolean||e instanceof Date){const r=(e=>"boolean"==typeof e||e instanceof Boolean?Number(e).toString():"number"==typeof e||e instanceof Number?e.toString():e instanceof Date?e.getTime().toString():"string"==typeof e||e instanceof String?e.toLowerCase().replace(t,""):"")(e),n=A(r);return{parsedNumber:n,chunks:N(n?`${n}`:r),value:e}}return{isArray:Array.isArray(e),isFunction:b(e),isNaN:y(e),isNull:g(e),isObject:S(e),isSymbol:v(e),isUndefined:h(e),value:e}},j=e=>"string"==typeof e&&("asc"===e||"desc"===e);function k(e){return(e=>(r,n)=>{const t=z(r),i=z(n);return l(t,i,e.locale)*("desc"===e.order?-1:1)})((e=>{let r,n="asc";return"string"==typeof e&&j(e)?n=e:e&&"object"==typeof e&&(e.order&&j(e.order)&&(n=e.order),e.locale&&e.locale.length>0&&(r=e.locale)),{order:n,locale:r}})(e))}const w=e=>"function"==typeof e?e:r=>{if(Array.isArray(r)){const n=Number(e);if(Number.isInteger(n))return r[n]}else if(r&&"object"==typeof r){const n=Object.getOwnPropertyDescriptor(r,e);return n?.value}return r},x=(e,r,n,t)=>{const i=r.length?r.map(w):[e=>e],o=e.map(((e,r)=>({index:r,values:i.map((r=>r(e))).map(z)})));return o.sort(((e,r)=>((e,r,n,t)=>{const{index:i,values:o}=e,{index:s,values:a}=r,{length:u}=o,c=n.length;for(let d=0;d<u;d++){const e=d<c?n[d]:null;if(e&&"function"==typeof e){const r=e(o[d].value,a[d].value);if(r)return r}else{const r=l(o[d],a[d],t);if(r)return r*("desc"===e?-1:1)}}return i-s})(e,r,n,t))),o.map((r=>((e,r)=>e[r])(e,r.index)))};function O(e,r,n,t){if(!e||!Array.isArray(e))return[];const i=(e=>{if(!e)return[];const r=Array.isArray(e)?[...e]:[e];return r.some((e=>"string"!=typeof e&&"number"!=typeof e&&"function"!=typeof e))?[]:r})(r),o=(e=>{if(!e)return[];const r=Array.isArray(e)?[...e]:[e];return r.some((e=>"asc"!==e&&"desc"!==e&&"function"!=typeof e))?[]:r})(n);return x(e,i,o,t)}export{k as compare,O as orderBy};
/**
* natural-orderby v4.0.0
* natural-orderby v5.0.0
*

@@ -27,4 +27,4 @@ * Copyright (c) Olaf Ennen

var compareUnicode = function compareUnicode(stringA, stringB) {
var result = stringA.localeCompare(stringB);
var compareUnicode = function compareUnicode(stringA, stringB, locale) {
var result = stringA.localeCompare(stringB, locale);
return result ? result / Math.abs(result) : 0;

@@ -52,3 +52,3 @@ };

var compareChunks = function compareChunks(chunksA, chunksB) {
var compareChunks = function compareChunks(chunksA, chunksB, locale) {
var lengthA = chunksA.length;

@@ -83,3 +83,3 @@ var lengthB = chunksB.length;

// use locale comparison only if one of the chunks contains unicode characters
return compareUnicode(chunkA.normalizedString, chunkB.normalizedString);
return compareUnicode(chunkA.normalizedString, chunkB.normalizedString, locale);
} else {

@@ -123,3 +123,3 @@ // use common string comparison for performance reason

var compareValues = function compareValues(valueA, valueB) {
var compareValues = function compareValues(valueA, valueB, locale) {
if (valueA.value === valueB.value) {

@@ -132,3 +132,3 @@ return 0;

if (valueA.chunks && valueB.chunks) {
return compareChunks(valueA.chunks, valueB.chunks);
return compareChunks(valueA.chunks, valueB.chunks, locale);
}

@@ -144,3 +144,3 @@ return compareOtherTypes(valueA, valueB);

if (value.length !== 0) {
var parsedNumber = Number(value);
var parsedNumber = Number(value.replace(/_/g, ''));
if (!Number.isNaN(parsedNumber)) {

@@ -270,3 +270,3 @@ return parsedNumber;

var b = getMappedValueRecord(valueB);
var result = compareValues(a, b);
var result = compareValues(a, b, options.locale);
return result * (options.order === 'desc' ? -1 : 1);

@@ -281,9 +281,16 @@ };

var order = 'asc';
var locale; // = 'en';
if (typeof customOptions === 'string' && isValidOrder(customOptions)) {
order = customOptions;
} else if (customOptions && typeof customOptions === 'object' && customOptions.order && isValidOrder(customOptions.order)) {
order = customOptions.order;
} else if (customOptions && typeof customOptions === 'object') {
if (customOptions.order && isValidOrder(customOptions.order)) {
order = customOptions.order;
}
if (customOptions.locale && customOptions.locale.length > 0) {
locale = customOptions.locale;
}
}
return {
order: order
order: order,
locale: locale
};

@@ -301,3 +308,3 @@ };

var compareMultiple = function compareMultiple(recordA, recordB, orders) {
var compareMultiple = function compareMultiple(recordA, recordB, orders, locale) {
var indexA = recordA.index,

@@ -317,3 +324,3 @@ valuesA = recordA.values;

} else {
var _result = compareValues(valuesA[i], valuesB[i]);
var _result = compareValues(valuesA[i], valuesB[i], locale);
if (_result) {

@@ -354,3 +361,3 @@ return _result * (order === 'desc' ? -1 : 1);

var baseOrderBy = function baseOrderBy(collection, identifiers, orders) {
var baseOrderBy = function baseOrderBy(collection, identifiers, orders, locale) {
var identifierFns = identifiers.length ? identifiers.map(createIdentifierFn) : [function (value) {

@@ -373,3 +380,3 @@ return value;

mappedCollection.sort(function (recordA, recordB) {
return compareMultiple(recordA, recordB, orders);
return compareMultiple(recordA, recordB, orders, locale);
});

@@ -413,3 +420,3 @@ return mappedCollection.map(function (element) {

*/
function orderBy(collection, identifiers, orders) {
function orderBy(collection, identifiers, orders, locale) {
if (!collection || !Array.isArray(collection)) {

@@ -420,3 +427,3 @@ return [];

var validatedOrders = getOrders(orders);
return baseOrderBy(collection, validatedIdentifiers, validatedOrders);
return baseOrderBy(collection, validatedIdentifiers, validatedOrders, locale);
}

@@ -423,0 +430,0 @@

/**
* natural-orderby v4.0.0
* natural-orderby v5.0.0
*

@@ -11,2 +11,2 @@ * Copyright (c) Olaf Ennen

*/
!function(r,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((r="undefined"!=typeof globalThis?globalThis:r||self).naturalOrderBy={})}(this,(function(r){"use strict";var n=function(r,n){return r<n?-1:r>n?1:0},e=function(r,n){var e=r.localeCompare(n);return e?e/Math.abs(e):0},t=/(^0x[\da-fA-F]+$|^([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?(?!\.\d+)(?=\D|\s|$))|\d+)/g,i=/^\s+|\s+$/g,u=/\s+/g,o=/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/,a=/(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[/-]\d{1,4}[/-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,s=/^0+[1-9]{1}[0-9]*$/,f=/[^\x00-\x80]/,c=function(r,n){return r<n?-1:r>n?1:0},d=function(r,t){return r.value===t.value?0:void 0!==r.parsedNumber&&void 0!==t.parsedNumber?n(r.parsedNumber,t.parsedNumber):r.chunks&&t.chunks?function(r,t){for(var i=r.length,u=t.length,o=Math.min(i,u),a=0;a<o;a++){var s=r[a],d=t[a];if(s.normalizedString!==d.normalizedString){if(""===s.normalizedString!=(""===d.normalizedString))return""===s.normalizedString?-1:1;if(void 0!==s.parsedNumber&&void 0!==d.parsedNumber){var l=n(s.parsedNumber,d.parsedNumber);return 0===l?c(s.normalizedString,d.normalizedString):l}return void 0!==s.parsedNumber||void 0!==d.parsedNumber?void 0!==s.parsedNumber?-1:1:f.test(s.normalizedString+d.normalizedString)?e(s.normalizedString,d.normalizedString):c(s.normalizedString,d.normalizedString)}}return i>o||u>o?i<=o?-1:1:0}(r.chunks,t.chunks):function(r,n){return(r.chunks?!n.chunks:n.chunks)?r.chunks?-1:1:(r.isNaN?!n.isNaN:n.isNaN)?r.isNaN?-1:1:(r.isSymbol?!n.isSymbol:n.isSymbol)?r.isSymbol?-1:1:(r.isObject?!n.isObject:n.isObject)?r.isObject?-1:1:(r.isArray?!n.isArray:n.isArray)?r.isArray?-1:1:(r.isFunction?!n.isFunction:n.isFunction)?r.isFunction?-1:1:(r.isNull?!n.isNull:n.isNull)?r.isNull?-1:1:0}(r,t)},l=function(r){return r.replace(u," ").replace(i,"")},m=function(r){if(0!==r.length){var n=Number(r);if(!Number.isNaN(n))return n}},p=function(r,n,e){if(o.test(r)&&(!s.test(r)||0===n||"."!==e[n-1]))return m(r)||0},y=function(r,n,e){return{parsedNumber:p(r,n,e),normalizedString:l(r)}},b=function(r){var n=function(r){return r.replace(t,"\0$1\0").replace(/\0$/,"").replace(/^\0/,"").split("\0")}(r).map(y);return n},N=function(r){return"function"==typeof r},v=function(r){return Number.isNaN(r)||r instanceof Number&&Number.isNaN(r.valueOf())},g=function(r){return null===r},S=function(r){return!(null===r||"object"!=typeof r||Array.isArray(r)||r instanceof Number||r instanceof String||r instanceof Boolean||r instanceof Date)},h=function(r){return"symbol"==typeof r},A=function(r){return void 0===r},z=function(r){var n=m(r);return void 0!==n?n:function(r){try{var n=Date.parse(r);return!Number.isNaN(n)&&a.test(r)?n:void 0}catch(r){return}}(r)},j=function(r){if("string"==typeof r||r instanceof String||("number"==typeof r||r instanceof Number)&&!v(r)||"boolean"==typeof r||r instanceof Boolean||r instanceof Date){var n=function(r){return"boolean"==typeof r||r instanceof Boolean?Number(r).toString():"number"==typeof r||r instanceof Number?r.toString():r instanceof Date?r.getTime().toString():"string"==typeof r||r instanceof String?r.toLowerCase().replace(i,""):""}(r),e=z(n);return{parsedNumber:e,chunks:b(e?""+e:n),value:r}}return{isArray:Array.isArray(r),isFunction:N(r),isNaN:v(r),isNull:g(r),isObject:S(r),isSymbol:h(r),isUndefined:A(r),value:r}},x=function(r){return"string"==typeof r&&("asc"===r||"desc"===r)};var k=function(r){return"function"==typeof r?r:function(n){if(Array.isArray(n)){var e=Number(r);if(Number.isInteger(e))return n[e]}else if(n&&"object"==typeof n){var t=Object.getOwnPropertyDescriptor(n,r);return null==t?void 0:t.value}return n}},w=function(r,n,e){var t=n.length?n.map(k):[function(r){return r}],i=r.map((function(r,n){return{index:n,values:t.map((function(n){return n(r)})).map(j)}}));return i.sort((function(r,n){return function(r,n,e){for(var t=r.index,i=r.values,u=n.index,o=n.values,a=i.length,s=e.length,f=0;f<a;f++){var c=f<s?e[f]:null;if(c&&"function"==typeof c){var l=c(i[f].value,o[f].value);if(l)return l}else{var m=d(i[f],o[f]);if(m)return m*("desc"===c?-1:1)}}return t-u}(r,n,e)})),i.map((function(n){return function(r,n){return r[n]}(r,n.index)}))};r.compare=function(r){var n,e;return function(r){return function(n,e){var t=j(n),i=j(e);return d(t,i)*("desc"===r.order?-1:1)}}((e="asc","string"==typeof(n=r)&&x(n)?e=n:n&&"object"==typeof n&&n.order&&x(n.order)&&(e=n.order),{order:e}))},r.orderBy=function(r,n,e){if(!r||!Array.isArray(r))return[];var t=function(r){if(!r)return[];var n=Array.isArray(r)?[].concat(r):[r];return n.some((function(r){return"string"!=typeof r&&"number"!=typeof r&&"function"!=typeof r}))?[]:n}(n),i=function(r){if(!r)return[];var n=Array.isArray(r)?[].concat(r):[r];return n.some((function(r){return"asc"!==r&&"desc"!==r&&"function"!=typeof r}))?[]:n}(e);return w(r,t,i)}}));
!function(r,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((r="undefined"!=typeof globalThis?globalThis:r||self).naturalOrderBy={})}(this,(function(r){"use strict";var n=function(r,n){return r<n?-1:r>n?1:0},e=function(r,n,e){var t=r.localeCompare(n,e);return t?t/Math.abs(t):0},t=/(^0x[\da-fA-F]+$|^([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?(?!\.\d+)(?=\D|\s|$))|\d+)/g,i=/^\s+|\s+$/g,o=/\s+/g,u=/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/,a=/(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[/-]\d{1,4}[/-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,s=/^0+[1-9]{1}[0-9]*$/,c=/[^\x00-\x80]/,f=function(r,n){return r<n?-1:r>n?1:0},l=function(r,t,i){return r.value===t.value?0:void 0!==r.parsedNumber&&void 0!==t.parsedNumber?n(r.parsedNumber,t.parsedNumber):r.chunks&&t.chunks?function(r,t,i){for(var o=r.length,u=t.length,a=Math.min(o,u),s=0;s<a;s++){var l=r[s],d=t[s];if(l.normalizedString!==d.normalizedString){if(""===l.normalizedString!=(""===d.normalizedString))return""===l.normalizedString?-1:1;if(void 0!==l.parsedNumber&&void 0!==d.parsedNumber){var m=n(l.parsedNumber,d.parsedNumber);return 0===m?f(l.normalizedString,d.normalizedString):m}return void 0!==l.parsedNumber||void 0!==d.parsedNumber?void 0!==l.parsedNumber?-1:1:c.test(l.normalizedString+d.normalizedString)?e(l.normalizedString,d.normalizedString,i):f(l.normalizedString,d.normalizedString)}}return o>a||u>a?o<=a?-1:1:0}(r.chunks,t.chunks,i):function(r,n){return(r.chunks?!n.chunks:n.chunks)?r.chunks?-1:1:(r.isNaN?!n.isNaN:n.isNaN)?r.isNaN?-1:1:(r.isSymbol?!n.isSymbol:n.isSymbol)?r.isSymbol?-1:1:(r.isObject?!n.isObject:n.isObject)?r.isObject?-1:1:(r.isArray?!n.isArray:n.isArray)?r.isArray?-1:1:(r.isFunction?!n.isFunction:n.isFunction)?r.isFunction?-1:1:(r.isNull?!n.isNull:n.isNull)?r.isNull?-1:1:0}(r,t)},d=function(r){return r.replace(o," ").replace(i,"")},m=function(r){if(0!==r.length){var n=Number(r.replace(/_/g,""));if(!Number.isNaN(n))return n}},p=function(r,n,e){if(u.test(r)&&(!s.test(r)||0===n||"."!==e[n-1]))return m(r)||0},y=function(r,n,e){return{parsedNumber:p(r,n,e),normalizedString:d(r)}},b=function(r){var n=function(r){return r.replace(t,"\0$1\0").replace(/\0$/,"").replace(/^\0/,"").split("\0")}(r).map(y);return n},N=function(r){return"function"==typeof r},v=function(r){return Number.isNaN(r)||r instanceof Number&&Number.isNaN(r.valueOf())},g=function(r){return null===r},S=function(r){return!(null===r||"object"!=typeof r||Array.isArray(r)||r instanceof Number||r instanceof String||r instanceof Boolean||r instanceof Date)},h=function(r){return"symbol"==typeof r},A=function(r){return void 0===r},z=function(r){var n=m(r);return void 0!==n?n:function(r){try{var n=Date.parse(r);return!Number.isNaN(n)&&a.test(r)?n:void 0}catch(r){return}}(r)},j=function(r){if("string"==typeof r||r instanceof String||("number"==typeof r||r instanceof Number)&&!v(r)||"boolean"==typeof r||r instanceof Boolean||r instanceof Date){var n=function(r){return"boolean"==typeof r||r instanceof Boolean?Number(r).toString():"number"==typeof r||r instanceof Number?r.toString():r instanceof Date?r.getTime().toString():"string"==typeof r||r instanceof String?r.toLowerCase().replace(i,""):""}(r),e=z(n);return{parsedNumber:e,chunks:b(e?""+e:n),value:r}}return{isArray:Array.isArray(r),isFunction:N(r),isNaN:v(r),isNull:g(r),isObject:S(r),isSymbol:h(r),isUndefined:A(r),value:r}},x=function(r){return"string"==typeof r&&("asc"===r||"desc"===r)};var k=function(r){return"function"==typeof r?r:function(n){if(Array.isArray(n)){var e=Number(r);if(Number.isInteger(e))return n[e]}else if(n&&"object"==typeof n){var t=Object.getOwnPropertyDescriptor(n,r);return null==t?void 0:t.value}return n}},w=function(r,n,e,t){var i=n.length?n.map(k):[function(r){return r}],o=r.map((function(r,n){return{index:n,values:i.map((function(n){return n(r)})).map(j)}}));return o.sort((function(r,n){return function(r,n,e,t){for(var i=r.index,o=r.values,u=n.index,a=n.values,s=o.length,c=e.length,f=0;f<s;f++){var d=f<c?e[f]:null;if(d&&"function"==typeof d){var m=d(o[f].value,a[f].value);if(m)return m}else{var p=l(o[f],a[f],t);if(p)return p*("desc"===d?-1:1)}}return i-u}(r,n,e,t)})),o.map((function(n){return function(r,n){return r[n]}(r,n.index)}))};r.compare=function(r){var n,e,t;return function(r){return function(n,e){var t=j(n),i=j(e);return l(t,i,r.locale)*("desc"===r.order?-1:1)}}((t="asc","string"==typeof(n=r)&&x(n)?t=n:n&&"object"==typeof n&&(n.order&&x(n.order)&&(t=n.order),n.locale&&n.locale.length>0&&(e=n.locale)),{order:t,locale:e}))},r.orderBy=function(r,n,e,t){if(!r||!Array.isArray(r))return[];var i=function(r){if(!r)return[];var n=Array.isArray(r)?[].concat(r):[r];return n.some((function(r){return"string"!=typeof r&&"number"!=typeof r&&"function"!=typeof r}))?[]:n}(n),o=function(r){if(!r)return[];var n=Array.isArray(r)?[].concat(r):[r];return n.some((function(r){return"asc"!==r&&"desc"!==r&&"function"!=typeof r}))?[]:n}(e);return w(r,i,o,t)}}));
{
"name": "natural-orderby",
"version": "4.0.0",
"version": "5.0.0",
"description": "Lightweight and performant natural sorting of arrays and collections by differentiating between unicode characters, numbers, dates, etc.",

@@ -5,0 +5,0 @@ "sideEffects": false,

@@ -147,2 +147,3 @@ # Welcome to 🌲 natural-orderby

orders?: ReadonlyArray<Order> | Order | null
locale?: string
): Array<T>

@@ -160,3 +161,3 @@ ```

If `collection` is an array of primitives, `identifiers` may be unspecified. Otherwise, you should specify `identifiers` to sort by or `collection` will be returned unsorted. An identifier can beexpressed by:
If `collection` is an array of primitives, `identifiers` may be unspecified. Otherwise, you should specify `identifiers` to sort by or `collection` will be returned unsorted. An identifier can be expressed by:

@@ -169,2 +170,4 @@ - an index position, if `collection` is a nested array,

If you want to sort unicode strings according to a specific locale, please provide a string value for `locale` with a BCP 47 language tag. If the argument is unspecified, the host locale setting will be used.
> Note: `orderBy()` always returns a new array, even if the original was already sorted.

@@ -345,6 +348,6 @@

| Type | Value |
| :--------------- | :-------------------------------------------------------- |
| `CompareOptions` | <code>{ order?: 'asc' &#124; 'desc' }</code> |
| `CompareFn` | <code>(valueA: unknown, valueB: unknown) => number</code> |
| Type | Value |
| :--------------- | :------------------------------------------------------------ |
| `CompareOptions` | <code>{ order?: 'asc' &#124; 'desc', locale?: string }</code> |
| `CompareFn` | <code>(valueA: unknown, valueB: unknown) => number</code> |

@@ -356,2 +359,3 @@ #### Description

If `options` or its property `order` is unspecified, values are sorted in ascending sort order. Otherwise, specify an order of `'desc'` for descending or `'asc'` for ascending sort order of values.
If unicode strings should be ordered corresponding to a specific locale setting, specify the according value for the options property `locale`. It must be a string with a BCP 47 language tag. If the argument is unspecified, the host locale setting will be used.

@@ -358,0 +362,0 @@ #### Examples

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc