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

fast-sort

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-sort - npm Package Compare versions

Comparing version 1.4.0 to 1.5.0

2

package.json

@@ -1,1 +0,1 @@

{"name":"fast-sort","version":"1.4.0","description":"Blazing fast array sorting.","main":"sort.js","author":"Stefan Novakovic <stefan.novakovich@gmail.com>","license":"MIT","homepage":"https://github.com/snovakovic/fast-sort#readme","scripts":{"test":"node integration_test/index.js","publish":"node publish"},"repository":{"type":"git","url":"git+https://github.com/snovakovic/js-flock.git"},"bugs":{"url":"https://github.com/snovakovic/js-flock/issues"},"keywords":["sort","sortBy","order","orderBy"]}
{"name":"fast-sort","version":"1.5.0","description":"Blazing fast array sorting.","main":"sort.js","author":"Stefan Novakovic <stefan.novakovich@gmail.com>","license":"MIT","homepage":"https://github.com/snovakovic/fast-sort#readme","scripts":{"test":"node integration_test/index.js","publish":"node publish"},"repository":{"type":"git","url":"git+https://github.com/snovakovic/js-flock.git"},"bugs":{"url":"https://github.com/snovakovic/js-flock/issues"},"keywords":["sort","sortBy","order","orderBy"],"dependencies":{"js-flock":"^3.5.1"}}

@@ -58,3 +58,3 @@ # fast-sort

// Sort in multiple directions
// NOTE: Available from version [1.4.0]
// NOTE: Available from version [1.5.0]
sort(persons).by([

@@ -61,0 +61,0 @@ { asc: 'name' }

@@ -7,37 +7,127 @@ (function (global, factory) {

// Internals
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var sorter = function sorter(direction, sortBy, thenBy, depth, a, b) {
var valA = sortBy(a);
var valB = sortBy(b);
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
if (valA === valB) {
if (thenBy && thenBy.length > depth) {
return sorter(direction, thenBy[depth], thenBy, depth + 1, a, b);
}
return 0;
}
/* eslint no-use-before-define: 0 */
if (valA < valB) return -direction;
if (valA == null) return 1;
if (valB == null) return -1;
// >>> SORTERS <<<
var sorter = function sorter(direction, a, b) {
if (a === b) return 0;
if (a < b) return -direction;
if (a == null) return 1;
if (b == null) return -1;
return direction;
};
var emptySortBy = function emptySortBy(a) {
return a;
/**
* stringSorter does not support nested property.
* For nested properties or value transformation (e.g toLowerCase) we should use functionSorter
* Based on benchmark testing using stringSorter is bit faster then using equivalent function sorter
* @example sort(users).asc('firstName')
*/
var stringSorter = function stringSorter(direction, sortBy, a, b) {
return sorter(direction, a[sortBy], b[sortBy]);
};
var sort = function sort(direction, ctx) {
var sortBy = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : emptySortBy;
/**
* @example sort(users).asc(p => p.address.city)
*/
var functionSorter = function functionSorter(direction, sortBy, a, b) {
return sorter(direction, sortBy(a), sortBy(b));
};
/**
* Used when we have sorting by multyple properties and when current sorter is function
* @example sort(users).asc([p => p.address.city, p => p.firstName])
*/
var multiPropFunctionSorter = function multiPropFunctionSorter(sortBy, thenBy, depth, direction, a, b) {
return multiPropEqualityHandler(sortBy(a), sortBy(b), thenBy, depth, direction, a, b);
};
/**
* Used when we have sorting by multyple properties and when current sorter is string
* @example sort(users).asc(['firstName', 'lastName'])
*/
var multiPropStringSorter = function multiPropStringSorter(sortBy, thenBy, depth, direction, a, b) {
return multiPropEqualityHandler(a[sortBy], b[sortBy], thenBy, depth, direction, a, b);
};
/**
* Used with 'by' sorter when we have sorting in multiple direction
* @example sort(users).asc(['firstName', 'lastName'])
*/
var multiPropObjectSorter = function multiPropObjectSorter(sortByObj, thenBy, depth, _direction, a, b) {
var sortBy = sortByObj.asc || sortByObj.desc;
var direction = sortByObj.asc ? 1 : -1;
if (!sortBy) {
throw Error('sort: Invalid \'by\' sorting onfiguration.\n Expecting object with \'asc\' or \'desc\' key');
}
var multiSorter = getMultiPropertySorter(sortBy);
return multiSorter(sortBy, thenBy, depth, direction, a, b);
};
// >>> HELPERS <<<
/**
* Return multiProperty sort handler based on sortBy value
*/
var getMultiPropertySorter = function getMultiPropertySorter(sortBy) {
var type = typeof sortBy === 'undefined' ? 'undefined' : _typeof(sortBy);
if (type === 'string') {
return multiPropStringSorter;
} else if (type === 'function') {
return multiPropFunctionSorter;
}
return multiPropObjectSorter;
};
var multiPropEqualityHandler = function multiPropEqualityHandler(valA, valB, thenBy, depth, direction, a, b) {
if (valA === valB || valA == null && valB == null) {
if (thenBy.length > depth) {
var multiSorter = getMultiPropertySorter(thenBy[depth]);
return multiSorter(thenBy[depth], thenBy, depth + 1, direction, a, b);
}
return 0;
}
return sorter(direction, valA, valB);
};
/**
* Pick sorter based on provided sortBy value
*/
var sort = function sort(direction, ctx, sortBy) {
if (!Array.isArray(ctx)) return ctx;
var _sorter = Array.isArray(sortBy) ? sorter.bind(undefined, direction, sortBy.shift(), sortBy, 0) : sorter.bind(undefined, direction, sortBy, undefined, 0);
// Unwrap sortBy if array with only 1 value
if (Array.isArray(sortBy) && sortBy.length < 2) {
var _sortBy = sortBy;
var _sortBy2 = _slicedToArray(_sortBy, 1);
sortBy = _sortBy2[0];
}
var _sorter = void 0;
if (!sortBy) {
_sorter = sorter.bind(undefined, direction);
} else if (typeof sortBy === 'string') {
_sorter = stringSorter.bind(undefined, direction, sortBy);
} else if (typeof sortBy === 'function') {
_sorter = functionSorter.bind(undefined, direction, sortBy);
} else {
_sorter = getMultiPropertySorter(sortBy[0]).bind(undefined, sortBy.shift(), sortBy, 0, direction);
}
return ctx.sort(_sorter);
};
// Public
// >>> PUBLIC <<<

@@ -51,2 +141,12 @@ var sort_1 = function sort_1(ctx) {

return sort(-1, ctx, sortBy);
},
by: function by(sortBy) {
if (!Array.isArray(ctx)) return ctx;
if (!Array.isArray(sortBy) || sortBy.length < 2) {
throw Error('sort: Invalid usage of \'by\' sorter.\n \'by\' sorter should be used only for sorting in multiple directions.\n Did you mean to use \'asc\' or \'desc\' sorter instead?');
}
var _sorter = multiPropObjectSorter.bind(undefined, sortBy.shift(), sortBy, 0, undefined);
return ctx.sort(_sorter);
}

@@ -53,0 +153,0 @@ };

@@ -1,1 +0,1 @@

!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(n.sort=n.sort||{},n.sort.js=r())}(this,function(){"use strict";var n=function n(r,t,e,i,o,u){var f=t(o),d=t(u);return f===d?e&&e.length>i?n(r,e[i],e,i+1,o,u):0:f<d?-r:null==f?1:null==d?-1:r},r=function(n){return n},t=function(t,e){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:r;if(!Array.isArray(e))return e;var o=Array.isArray(i)?n.bind(void 0,t,i.shift(),i,0):n.bind(void 0,t,i,void 0,0);return e.sort(o)};return function(n){return{asc:function(r){return t(1,n,r)},desc:function(r){return t(-1,n,r)}}}});
!function(r,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(r.sort=r.sort||{},r.sort.js=n())}(this,function(){"use strict";var e=function(r,n){if(Array.isArray(r))return r;if(Symbol.iterator in Object(r))return function(r,n){var t=[],o=!0,e=!1,i=void 0;try{for(var u,f=r[Symbol.iterator]();!(o=(u=f.next()).done)&&(t.push(u.value),!n||t.length!==n);o=!0);}catch(r){e=!0,i=r}finally{try{!o&&f.return&&f.return()}finally{if(e)throw i}}return t}(r,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")},t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},f=function(r,n,t){return n===t?0:n<t?-r:null==n?1:null==t?-1:r},o=function(r,n,t,o,e,i){return c(r(e),r(i),n,t,o,e,i)},i=function(r,n,t,o,e,i){return c(e[r],i[r],n,t,o,e,i)},u=function(r,n,t,o,e,i){var u=r.asc||r.desc,f=r.asc?1:-1;if(!u)throw Error("sort: Invalid 'by' sorting onfiguration.\n Expecting object with 'asc' or 'desc' key");return s(u)(u,n,t,f,e,i)},s=function(r){var n=void 0===r?"undefined":t(r);return"string"===n?i:"function"===n?o:u},c=function(r,n,t,o,e,i,u){return r===n||null==r&&null==n?t.length>o?s(t[o])(t[o],t,o+1,e,i,u):0:f(e,r,n)},n=function(r,n,t){if(!Array.isArray(n))return n;Array.isArray(t)&&t.length<2&&(t=e(t,1)[0]);var o=void 0;return o=t?"string"==typeof t?function(r,n,t,o){return f(r,t[n],o[n])}.bind(void 0,r,t):"function"==typeof t?function(r,n,t,o){return f(r,n(t),n(o))}.bind(void 0,r,t):s(t[0]).bind(void 0,t.shift(),t,0,r):f.bind(void 0,r),n.sort(o)};return function(t){return{asc:function(r){return n(1,t,r)},desc:function(r){return n(-1,t,r)},by:function(r){if(!Array.isArray(t))return t;if(!Array.isArray(r)||r.length<2)throw Error("sort: Invalid usage of 'by' sorter.\n 'by' sorter should be used only for sorting in multiple directions.\n Did you mean to use 'asc' or 'desc' sorter instead?");var n=u.bind(void 0,r.shift(),r,0,void 0);return t.sort(n)}}}});

@@ -1,10 +0,85 @@

// Internals
/* eslint no-use-before-define: 0 */
const sorter = function(direction, sortBy, thenBy, depth, a, b) {
const valA = sortBy(a);
const valB = sortBy(b);
// >>> SORTERS <<<
if (valA === valB) {
if (thenBy && thenBy.length > depth) {
return sorter(direction, thenBy[depth], thenBy, depth + 1, a, b);
const sorter = function(direction, a, b) {
if (a === b) return 0;
if (a < b) return -direction;
if (a == null) return 1;
if (b == null) return -1;
return direction;
};
/**
* stringSorter does not support nested property.
* For nested properties or value transformation (e.g toLowerCase) we should use functionSorter
* Based on benchmark testing using stringSorter is bit faster then using equivalent function sorter
* @example sort(users).asc('firstName')
*/
const stringSorter = function(direction, sortBy, a, b) {
return sorter(direction, a[sortBy], b[sortBy]);
};
/**
* @example sort(users).asc(p => p.address.city)
*/
const functionSorter = function(direction, sortBy, a, b) {
return sorter(direction, sortBy(a), sortBy(b));
};
/**
* Used when we have sorting by multyple properties and when current sorter is function
* @example sort(users).asc([p => p.address.city, p => p.firstName])
*/
const multiPropFunctionSorter = function(sortBy, thenBy, depth, direction, a, b) {
return multiPropEqualityHandler(sortBy(a), sortBy(b), thenBy, depth, direction, a, b);
};
/**
* Used when we have sorting by multyple properties and when current sorter is string
* @example sort(users).asc(['firstName', 'lastName'])
*/
const multiPropStringSorter = function(sortBy, thenBy, depth, direction, a, b) {
return multiPropEqualityHandler(a[sortBy], b[sortBy], thenBy, depth, direction, a, b);
};
/**
* Used with 'by' sorter when we have sorting in multiple direction
* @example sort(users).asc(['firstName', 'lastName'])
*/
const multiPropObjectSorter = function(sortByObj, thenBy, depth, _direction, a, b) {
const sortBy = sortByObj.asc || sortByObj.desc;
const direction = sortByObj.asc ? 1 : -1;
if (!sortBy) {
throw Error(`sort: Invalid 'by' sorting onfiguration.
Expecting object with 'asc' or 'desc' key`);
}
const multiSorter = getMultiPropertySorter(sortBy);
return multiSorter(sortBy, thenBy, depth, direction, a, b);
};
// >>> HELPERS <<<
/**
* Return multiProperty sort handler based on sortBy value
*/
const getMultiPropertySorter = function(sortBy) {
const type = typeof sortBy;
if (type === 'string') {
return multiPropStringSorter;
} else if (type === 'function') {
return multiPropFunctionSorter;
}
return multiPropObjectSorter;
};
const multiPropEqualityHandler = function(valA, valB, thenBy, depth, direction, a, b) {
if (valA === valB || (valA == null && valB == null)) {
if (thenBy.length > depth) {
const multiSorter = getMultiPropertySorter(thenBy[depth]);
return multiSorter(thenBy[depth], thenBy, depth + 1, direction, a, b);
}

@@ -14,22 +89,33 @@ return 0;

if (valA < valB) return -direction;
if (valA == null) return 1;
if (valB == null) return -1;
return direction;
return sorter(direction, valA, valB);
};
const emptySortBy = (a) => a;
const sort = function(direction, ctx, sortBy = emptySortBy) {
/**
* Pick sorter based on provided sortBy value
*/
const sort = function(direction, ctx, sortBy) {
if (!Array.isArray(ctx)) return ctx;
const _sorter = Array.isArray(sortBy)
? sorter.bind(undefined, direction, sortBy.shift(), sortBy, 0)
: sorter.bind(undefined, direction, sortBy, undefined, 0);
// Unwrap sortBy if array with only 1 value
if (Array.isArray(sortBy) && sortBy.length < 2) {
[sortBy] = sortBy;
}
let _sorter;
if (!sortBy) {
_sorter = sorter.bind(undefined, direction);
} else if (typeof sortBy === 'string') {
_sorter = stringSorter.bind(undefined, direction, sortBy);
} else if (typeof sortBy === 'function') {
_sorter = functionSorter.bind(undefined, direction, sortBy);
} else {
_sorter = getMultiPropertySorter(sortBy[0])
.bind(undefined, sortBy.shift(), sortBy, 0, direction);
}
return ctx.sort(_sorter);
};
// Public
// >>> PUBLIC <<<

@@ -39,4 +125,16 @@ module.exports = function(ctx) {

asc: (sortBy) => sort(1, ctx, sortBy),
desc: (sortBy) => sort(-1, ctx, sortBy)
desc: (sortBy) => sort(-1, ctx, sortBy),
by: (sortBy) => {
if (!Array.isArray(ctx)) return ctx;
if (!Array.isArray(sortBy) || sortBy.length < 2) {
throw Error(`sort: Invalid usage of 'by' sorter.
'by' sorter should be used only for sorting in multiple directions.
Did you mean to use 'asc' or 'desc' sorter instead?`);
}
const _sorter = multiPropObjectSorter.bind(undefined, sortBy.shift(), sortBy, 0, undefined);
return ctx.sort(_sorter);
}
};
};
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