pax_global_header00006660000000000000000000000064132120443150014505gustar00rootroot0000000000000052 comment=a7913f8ec9b3891a01eaf71636a7acc7e638f9a0 array-sort-1.0.0/000077500000000000000000000000001321204431500136065ustar00rootroot00000000000000array-sort-1.0.0/.editorconfig000066400000000000000000000004051321204431500162620ustar00rootroot00000000000000root = true [*] indent_style = space end_of_line = lf charset = utf-8 indent_size = 2 trim_trailing_whitespace = true insert_final_newline = true [{**/{actual,fixtures,expected,templates}/**,*.md}] trim_trailing_whitespace = false insert_final_newline = falsearray-sort-1.0.0/.eslintrc.json000066400000000000000000000071251321204431500164070ustar00rootroot00000000000000{ "ecmaFeatures": { "modules": true, "experimentalObjectRestSpread": true }, "env": { "browser": false, "es6": true, "node": true, "mocha": true }, "globals": { "document": false, "navigator": false, "window": false }, "rules": { "accessor-pairs": 2, "arrow-spacing": [2, { "before": true, "after": true }], "block-spacing": [2, "always"], "brace-style": [2, "1tbs", { "allowSingleLine": true }], "comma-dangle": [2, "never"], "comma-spacing": [2, { "before": false, "after": true }], "comma-style": [2, "last"], "constructor-super": 2, "curly": [2, "multi-line"], "dot-location": [2, "property"], "eol-last": 2, "eqeqeq": [2, "allow-null"], "generator-star-spacing": [2, { "before": true, "after": true }], "handle-callback-err": [2, "^(err|error)$" ], "indent": [2, 2, { "SwitchCase": 1 }], "key-spacing": [2, { "beforeColon": false, "afterColon": true }], "keyword-spacing": [2, { "before": true, "after": true }], "new-cap": [2, { "newIsCap": true, "capIsNew": false }], "new-parens": 2, "no-array-constructor": 2, "no-caller": 2, "no-class-assign": 2, "no-cond-assign": 2, "no-const-assign": 2, "no-control-regex": 2, "no-debugger": 2, "no-delete-var": 2, "no-dupe-args": 2, "no-dupe-class-members": 2, "no-dupe-keys": 2, "no-duplicate-case": 2, "no-empty-character-class": 2, "no-eval": 2, "no-ex-assign": 2, "no-extend-native": 2, "no-extra-bind": 2, "no-extra-boolean-cast": 2, "no-extra-parens": [2, "functions"], "no-fallthrough": 2, "no-floating-decimal": 2, "no-func-assign": 2, "no-implied-eval": 2, "no-inner-declarations": [2, "functions"], "no-invalid-regexp": 2, "no-irregular-whitespace": 2, "no-iterator": 2, "no-label-var": 2, "no-labels": 2, "no-lone-blocks": 2, "no-mixed-spaces-and-tabs": 2, "no-multi-spaces": 2, "no-multi-str": 2, "no-multiple-empty-lines": [2, { "max": 1 }], "no-native-reassign": 0, "no-negated-in-lhs": 2, "no-new": 2, "no-new-func": 2, "no-new-object": 2, "no-new-require": 2, "no-new-wrappers": 2, "no-obj-calls": 2, "no-octal": 2, "no-octal-escape": 2, "no-proto": 0, "no-redeclare": 2, "no-regex-spaces": 2, "no-return-assign": 2, "no-self-compare": 2, "no-sequences": 2, "no-shadow-restricted-names": 2, "no-spaced-func": 2, "no-sparse-arrays": 2, "no-this-before-super": 2, "no-throw-literal": 2, "no-trailing-spaces": 0, "no-undef": 2, "no-undef-init": 2, "no-unexpected-multiline": 2, "no-unneeded-ternary": [2, { "defaultAssignment": false }], "no-unreachable": 2, "no-unused-vars": [2, { "vars": "all", "args": "none" }], "no-useless-call": 0, "no-with": 2, "one-var": [0, { "initialized": "never" }], "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], "padded-blocks": [0, "never"], "quotes": [2, "single", "avoid-escape"], "radix": 2, "semi": [2, "always"], "semi-spacing": [2, { "before": false, "after": true }], "space-before-blocks": [2, "always"], "space-before-function-paren": [2, "never"], "space-in-parens": [2, "never"], "space-infix-ops": 2, "space-unary-ops": [2, { "words": true, "nonwords": false }], "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], "use-isnan": 2, "valid-typeof": 2, "wrap-iife": [2, "any"], "yoda": [2, "never"] } } array-sort-1.0.0/.gitattributes000066400000000000000000000002001321204431500164710ustar00rootroot00000000000000# Enforce Unix newlines * text eol=lf # binaries *.ai binary *.psd binary *.jpg binary *.gif binary *.png binary *.jpeg binary array-sort-1.0.0/.gitignore000066400000000000000000000004531321204431500156000ustar00rootroot00000000000000# always ignore files *.DS_Store *.sublime-* # test related, or directories generated by tests test/actual actual coverage .nyc* # npm node_modules npm-debug.log # yarn yarn.lock yarn-error.log # misc _gh_pages _draft _drafts bower_components vendor temp tmp TODO.md package-lock.json benchmark array-sort-1.0.0/.travis.yml000066400000000000000000000002401321204431500157130ustar00rootroot00000000000000sudo: false os: - linux - osx language: node_js node_js: - node - '8' - '7' - '6' - '5' - '4' - '0.12' - '0.10' matrix: fast_finish: true array-sort-1.0.0/.verb.md000066400000000000000000000063361321204431500151540ustar00rootroot00000000000000## Usage Sort an array by the given object property: ```js var arraySort = require('{%= name %}'); arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo'); //=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}] ``` **Reverse order** ```js arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo', {reverse: true}); //=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}] ``` ## Params ```js arraySort(array, comparisonArgs); ``` - `array`: **{Array}** The array to sort - `comparisonArgs`: **{Function|String|Array}**: One or more functions or object paths to use for sorting. ## Examples **[Sort blog posts](examples/blog-posts.js)** ```js var arraySort = require('{%= name %}'); var posts = [ { path: 'c.md', locals: { date: '2014-01-09' } }, { path: 'a.md', locals: { date: '2014-01-02' } }, { path: 'b.md', locals: { date: '2013-05-06' } }, ]; // sort by `locals.date` console.log(arraySort(posts, 'locals.date')); // sort by `path` console.log(arraySort(posts, 'path')); ``` **[Sort by multiple properties](examples/multiple-props.js)** ```js var arraySort = require('{%= name %}'); var posts = [ { locals: { foo: 'bbb', date: '2013-05-06' }}, { locals: { foo: 'aaa', date: '2012-01-02' }}, { locals: { foo: 'ccc', date: '2014-01-02' }}, { locals: { foo: 'ccc', date: '2015-01-02' }}, { locals: { foo: 'bbb', date: '2014-06-01' }}, { locals: { foo: 'aaa', date: '2014-02-02' }}, ]; // sort by `locals.foo`, then `locals.date` var result = arraySort(posts, ['locals.foo', 'locals.date']); console.log(result); // [ { locals: { foo: 'aaa', date: '2012-01-02' } }, // { locals: { foo: 'aaa', date: '2014-02-02' } }, // { locals: { foo: 'bbb', date: '2013-05-06' } }, // { locals: { foo: 'bbb', date: '2014-06-01' } }, // { locals: { foo: 'ccc', date: '2014-01-02' } }, // { locals: { foo: 'ccc', date: '2015-01-02' } } ] ``` **[Custom function](examples/custom-function.js)** If custom functions are supplied, array elements are sorted according to the return value of the compare function. See the [docs for `Array.sort()`][mozilla] for more details. ```js var arr = [ {one: 'w', two: 'b'}, {one: 'z', two: 'a'}, {one: 'x', two: 'c'}, {one: 'y', two: 'd'}, ]; function compare(prop) { return function (a, b) { return a[prop].localeCompare(b[prop]); }; } var result = arraySort(arr, function (a, b) { return a.two.localeCompare(b.two); }); console.log(result); // [ { one: 'z', two: 'a' }, // { one: 'w', two: 'b' }, // { one: 'x', two: 'c' }, // { one: 'y', two: 'd' } ] ``` **[Multiple custom functions](examples/custom-functions.js)** ```js var arr = [ {foo: 'w', bar: 'y', baz: 'w'}, {foo: 'x', bar: 'y', baz: 'w'}, {foo: 'x', bar: 'y', baz: 'z'}, {foo: 'x', bar: 'x', baz: 'w'}, ]; // reusable compare function function compare(prop) { return function (a, b) { return a[prop].localeCompare(b[prop]); }; } // the `compare` functions can be a list or array var result = arraySort(arr, compare('foo'), compare('bar'), compare('baz')); console.log(result); // [ { foo: 'w', bar: 'y', baz: 'w' }, // { foo: 'x', bar: 'x', baz: 'w' }, // { foo: 'x', bar: 'y', baz: 'w' }, // { foo: 'x', bar: 'y', baz: 'z' } ] ``` [mozilla]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort array-sort-1.0.0/LICENSE000066400000000000000000000020771321204431500146210ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015-2017, Jon Schlinkert. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.array-sort-1.0.0/README.md000066400000000000000000000153161321204431500150730ustar00rootroot00000000000000# array-sort [![NPM version](https://img.shields.io/npm/v/array-sort.svg?style=flat)](https://www.npmjs.com/package/array-sort) [![NPM monthly downloads](https://img.shields.io/npm/dm/array-sort.svg?style=flat)](https://npmjs.org/package/array-sort) [![NPM total downloads](https://img.shields.io/npm/dt/array-sort.svg?style=flat)](https://npmjs.org/package/array-sort) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/array-sort.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/array-sort) [![Windows Build Status](https://img.shields.io/appveyor/ci/jonschlinkert/array-sort.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/jonschlinkert/array-sort) > Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save array-sort ``` Install with [yarn](https://yarnpkg.com): ```sh $ yarn add array-sort ``` ## Usage Sort an array by the given object property: ```js var arraySort = require('array-sort'); arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo'); //=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}] ``` **Reverse order** ```js arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo', {reverse: true}); //=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}] ``` ## Params ```js arraySort(array, comparisonArgs); ``` * `array`: **{Array}** The array to sort * `comparisonArgs`: **{Function|String|Array}**: One or more functions or object paths to use for sorting. ## Examples **[Sort blog posts](examples/blog-posts.js)** ```js var arraySort = require('array-sort'); var posts = [ { path: 'c.md', locals: { date: '2014-01-09' } }, { path: 'a.md', locals: { date: '2014-01-02' } }, { path: 'b.md', locals: { date: '2013-05-06' } }, ]; // sort by `locals.date` console.log(arraySort(posts, 'locals.date')); // sort by `path` console.log(arraySort(posts, 'path')); ``` **[Sort by multiple properties](examples/multiple-props.js)** ```js var arraySort = require('array-sort'); var posts = [ { locals: { foo: 'bbb', date: '2013-05-06' }}, { locals: { foo: 'aaa', date: '2012-01-02' }}, { locals: { foo: 'ccc', date: '2014-01-02' }}, { locals: { foo: 'ccc', date: '2015-01-02' }}, { locals: { foo: 'bbb', date: '2014-06-01' }}, { locals: { foo: 'aaa', date: '2014-02-02' }}, ]; // sort by `locals.foo`, then `locals.date` var result = arraySort(posts, ['locals.foo', 'locals.date']); console.log(result); // [ { locals: { foo: 'aaa', date: '2012-01-02' } }, // { locals: { foo: 'aaa', date: '2014-02-02' } }, // { locals: { foo: 'bbb', date: '2013-05-06' } }, // { locals: { foo: 'bbb', date: '2014-06-01' } }, // { locals: { foo: 'ccc', date: '2014-01-02' } }, // { locals: { foo: 'ccc', date: '2015-01-02' } } ] ``` **[Custom function](examples/custom-function.js)** If custom functions are supplied, array elements are sorted according to the return value of the compare function. See the [docs for ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)`Array.sort()` for more details. ```js var arr = [ {one: 'w', two: 'b'}, {one: 'z', two: 'a'}, {one: 'x', two: 'c'}, {one: 'y', two: 'd'}, ]; function compare(prop) { return function (a, b) { return a[prop].localeCompare(b[prop]); }; } var result = arraySort(arr, function (a, b) { return a.two.localeCompare(b.two); }); console.log(result); // [ { one: 'z', two: 'a' }, // { one: 'w', two: 'b' }, // { one: 'x', two: 'c' }, // { one: 'y', two: 'd' } ] ``` **[Multiple custom functions](examples/custom-functions.js)** ```js var arr = [ {foo: 'w', bar: 'y', baz: 'w'}, {foo: 'x', bar: 'y', baz: 'w'}, {foo: 'x', bar: 'y', baz: 'z'}, {foo: 'x', bar: 'x', baz: 'w'}, ]; // reusable compare function function compare(prop) { return function (a, b) { return a[prop].localeCompare(b[prop]); }; } // the `compare` functions can be a list or array var result = arraySort(arr, compare('foo'), compare('bar'), compare('baz')); console.log(result); // [ { foo: 'w', bar: 'y', baz: 'w' }, // { foo: 'x', bar: 'x', baz: 'w' }, // { foo: 'x', bar: 'y', baz: 'w' }, // { foo: 'x', bar: 'y', baz: 'z' } ] ``` ## About ### Related projects * [get-value](https://www.npmjs.com/package/get-value): Use property paths (`a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value "Use property paths (`a.b.c`) to get a nested value from an object.") * [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.") * [sort-asc](https://www.npmjs.com/package/sort-asc): Sort array elements in ascending order. | [homepage](https://github.com/jonschlinkert/sort-asc "Sort array elements in ascending order.") * [sort-desc](https://www.npmjs.com/package/sort-desc): Sort array elements in descending order. | [homepage](https://github.com/jonschlinkert/sort-desc "Sort array elements in descending order.") * [sort-object](https://www.npmjs.com/package/sort-object): Sort the keys in an object. | [homepage](https://github.com/doowb/sort-object "Sort the keys in an object.") ### Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). ### Contributors | **Commits** | **Contributor** | | --- | --- | | 10 | [jonschlinkert](https://github.com/jonschlinkert) | | 4 | [doowb](https://github.com/doowb) | | 1 | [iamstolis](https://github.com/iamstolis) | | 1 | [wkevina](https://github.com/wkevina) | ### Building docs _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ To generate the readme, run the following command: ```sh $ npm install -g verbose/verb#dev verb-generate-readme && verb ``` ### Running tests Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: ```sh $ npm install && npm test ``` ### Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) ### License Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). Released under the [MIT License](LICENSE). *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 11, 2017._array-sort-1.0.0/appveyor.yml000066400000000000000000000012111321204431500161710ustar00rootroot00000000000000# Test against this version of Node.js environment: matrix: # node.js - nodejs_version: "8.0" - nodejs_version: "7.0" - nodejs_version: "6.0" - nodejs_version: "5.0" - nodejs_version: "4.0" - nodejs_version: "0.12" - nodejs_version: "0.10" # Install scripts. (runs after repo cloning) install: # Get the latest stable version of Node.js or io.js - ps: Install-Product node $env:nodejs_version # install modules - npm install # Post-install test scripts. test_script: # Output useful info for debugging. - node --version - npm --version # run tests - npm test # Don't actually build. build: off array-sort-1.0.0/examples/000077500000000000000000000000001321204431500154245ustar00rootroot00000000000000array-sort-1.0.0/examples/blog-posts.js000066400000000000000000000005061321204431500200540ustar00rootroot00000000000000var arraySort = require('..'); var posts = [ { path: 'c.md', locals: { date: '2014-01-09' } }, { path: 'a.md', locals: { date: '2014-01-02' } }, { path: 'b.md', locals: { date: '2013-05-06' } }, ]; // by `locals.date` console.log(arraySort(posts, 'locals.date')); // by `path` console.log(arraySort(posts, 'path')); array-sort-1.0.0/examples/custom-function.js000066400000000000000000000007501321204431500211210ustar00rootroot00000000000000var arraySort = require('..'); var arr = [ {one: 'w', two: 'b'}, {one: 'z', two: 'a'}, {one: 'x', two: 'c'}, {one: 'y', two: 'd'}, ]; function compare(prop) { return function (a, b) { return a[prop].localeCompare(b[prop]); }; } var result = arraySort(arr, function (a, b) { return a.two.localeCompare(b.two); }); console.log(result); // Results in: // [ { one: 'z', two: 'a' }, // { one: 'w', two: 'b' }, // { one: 'x', two: 'c' }, // { one: 'y', two: 'd' } ] array-sort-1.0.0/examples/custom-functions.js000066400000000000000000000010311321204431500212750ustar00rootroot00000000000000var arraySort = require('..'); var arr = [ {foo: 'w', bar: 'y', baz: 'w'}, {foo: 'x', bar: 'y', baz: 'w'}, {foo: 'x', bar: 'y', baz: 'z'}, {foo: 'x', bar: 'x', baz: 'w'}, ]; function compare(prop) { return function (a, b) { return a[prop].localeCompare(b[prop]); }; } console.log(arraySort(arr, compare('foo'), compare('bar'), compare('baz'))); // Results in: // [ { foo: 'w', bar: 'y', baz: 'w' }, // { foo: 'x', bar: 'x', baz: 'w' }, // { foo: 'x', bar: 'y', baz: 'w' }, // { foo: 'x', bar: 'y', baz: 'z' } ] array-sort-1.0.0/examples/multiple-props.js000066400000000000000000000014271321204431500207620ustar00rootroot00000000000000var arraySort = require('..'); var posts = [ { locals: { foo: 'bbb', date: '2013-05-06' }}, { locals: { foo: 'aaa', date: '2012-01-02' }}, { locals: { foo: 'ccc', date: '2014-01-02' }}, { locals: { foo: 'ccc', date: '2015-01-02' }}, { locals: { foo: 'bbb', date: '2014-06-01' }}, { locals: { foo: 'aaa', date: '2014-02-02' }}, ]; // sort by `locals.foo`, then `locals.date` var result = arraySort(posts, ['locals.foo', 'locals.date']); console.log(result); // [ { locals: { foo: 'aaa', date: '2012-01-02' } }, // { locals: { foo: 'aaa', date: '2014-02-02' } }, // { locals: { foo: 'bbb', date: '2013-05-06' } }, // { locals: { foo: 'bbb', date: '2014-06-01' } }, // { locals: { foo: 'ccc', date: '2014-01-02' } }, // { locals: { foo: 'ccc', date: '2015-01-02' } } ] array-sort-1.0.0/index.js000066400000000000000000000045331321204431500152600ustar00rootroot00000000000000/*! * array-sort * * Copyright (c) 2015-2017, Jon Schlinkert. * Released under the MIT License. */ 'use strict'; var defaultCompare = require('default-compare'); var typeOf = require('kind-of'); var get = require('get-value'); /** * Sort an array of objects by one or more properties. * * @param {Array} `arr` The Array to sort. * @param {String|Array|Function} `props` One or more object paths or comparison functions. * @param {Object} `opts` Pass `{ reverse: true }` to reverse the sort order. * @return {Array} Returns a sorted array. * @api public */ function arraySort(arr, props, opts) { if (arr == null) { return []; } if (!Array.isArray(arr)) { throw new TypeError('array-sort expects an array.'); } if (arguments.length === 1) { return arr.sort(); } var args = flatten([].slice.call(arguments, 1)); // if the last argument appears to be a plain object, // it's not a valid `compare` arg, so it must be options. if (typeOf(args[args.length - 1]) === 'object') { opts = args.pop(); } return arr.sort(sortBy(args, opts)); } /** * Iterate over each comparison property or function until `1` or `-1` * is returned. * * @param {String|Array|Function} `props` One or more object paths or comparison functions. * @param {Object} `opts` Pass `{ reverse: true }` to reverse the sort order. * @return {Array} */ function sortBy(props, opts) { opts = opts || {}; return function compareFn(a, b) { var len = props.length, i = -1; var result; while (++i < len) { result = compare(props[i], a, b); if (result !== 0) { break; } } if (opts.reverse === true) { return result * -1; } return result; }; } /** * Compare `a` to `b`. If an object `prop` is passed, then * `a[prop]` is compared to `b[prop]` */ function compare(prop, a, b) { if (typeof prop === 'function') { // expose `compare` to custom function return prop(a, b, compare.bind(null, null)); } // compare object values if (prop && typeof a === 'object' && typeof b === 'object') { return compare(null, get(a, prop), get(b, prop)); } return defaultCompare(a, b); } /** * Flatten the given array. */ function flatten(arr) { return [].concat.apply([], arr); } /** * Expose `arraySort` */ module.exports = arraySort; array-sort-1.0.0/package.json000066400000000000000000000036221321204431500160770ustar00rootroot00000000000000{ "name": "array-sort", "description": "Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.", "version": "1.0.0", "homepage": "https://github.com/jonschlinkert/array-sort", "author": "Jon Schlinkert (https://github.com/jonschlinkert)", "contributors": [ "Brian Woodward (https://twitter.com/doowb)", "Jan Stola (https://github.com/iamstolis)", "Jon Schlinkert (http://twitter.com/jonschlinkert)", "Kevin Ward (https://github.com/wkevina)" ], "repository": "jonschlinkert/array-sort", "bugs": { "url": "https://github.com/jonschlinkert/array-sort/issues" }, "license": "MIT", "files": [ "index.js" ], "main": "index.js", "engines": { "node": ">=0.10.0" }, "scripts": { "test": "mocha" }, "dependencies": { "default-compare": "^1.0.0", "get-value": "^2.0.6", "kind-of": "^5.0.2" }, "devDependencies": { "ansi-bold": "^0.1.1", "benchmarked": "^0.1.5", "glob": "^7.0.3", "gulp-format-md": "^0.1.8", "lodash.sortbyorder": "^3.4.4", "mocha": "^2.4.5", "should": "^8.3.1" }, "keywords": [ "arr", "array", "asc", "ascend", "ascending", "desc", "descend", "descending", "dot", "element", "elements", "get", "multiple", "nested", "obj", "object", "order", "ordered", "path", "prop", "properties", "property", "sort", "sorted", "sorting" ], "verb": { "reflinks": [ "verb" ], "related": { "list": [ "get-value", "set-value", "sort-asc", "sort-desc", "sort-object" ] }, "toc": false, "layout": "default", "tasks": [ "readme" ], "plugins": [ "gulp-format-md" ], "lint": { "reflinks": true } } } array-sort-1.0.0/test.js000066400000000000000000000342331321204431500151300ustar00rootroot00000000000000'use strict'; require('mocha'); var should = require('should'); var get = require('get-value'); var arraySort = require('./'); describe('errors', function() { it('should throw an error when invalid args are passed:', function() { (function() { arraySort({}); }).should.throw('array-sort expects an array.'); }); }); describe('empty array', function() { it('should return an empty array when null or undefined is passed', function() { arraySort().should.eql([]); arraySort(undefined).should.eql([]); arraySort(null).should.eql([]); }) }); describe('basic sort', function() { it('should sort an array of primitives', function() { var arr = ['d', 3, 'b', 'a', 'd', 1, 0, 'z']; arraySort(arr).should.eql([ 0, 1, 3, 'a', 'b', 'd', 'd', 'z' ]); }) }); describe('arraySort', function() { var posts = [ { path: 'a.md', locals: { date: '2014-01-09' } }, { path: 'f.md', locals: { date: '2014-01-02' } }, { path: 'd.md', locals: { date: '2013-05-06' } }, { path: 'e.md', locals: { date: '2015-01-02' } }, { path: 'b.md', locals: { date: '2012-01-02' } }, { path: 'f.md', locals: { date: '2014-06-01' } }, { path: 'c.md', locals: { date: '2015-04-12' } }, { path: 'g.md', locals: { date: '2014-02-02' } }, ]; it('should sort by a property:', function() { var arr = [{key: 'y'}, {key: 'z'}, {key: 'x'}]; arraySort(arr, 'key').should.eql([ {key: 'x'}, {key: 'y'}, {key: 'z'} ]); arraySort(posts, 'path').should.eql([ { path: 'a.md', locals: { date: '2014-01-09' } }, { path: 'b.md', locals: { date: '2012-01-02' } }, { path: 'c.md', locals: { date: '2015-04-12' } }, { path: 'd.md', locals: { date: '2013-05-06' } }, { path: 'e.md', locals: { date: '2015-01-02' } }, { path: 'f.md', locals: { date: '2014-01-02' } }, { path: 'f.md', locals: { date: '2014-06-01' } }, { path: 'g.md', locals: { date: '2014-02-02' } } ]); }); it('should sort by a property with null values:', function() { var arr = [{key: null}, {key: 'z'}, {key: 'x'}]; arraySort(arr, 'key').should.eql([ {key: 'x'}, {key: 'z'}, {key: null} ]); }); it('should sort by a property with undefined values:', function() { var arr = [{}, {key: 'z'}, {key: 'x'}]; arraySort(arr, 'key').should.eql([ {key: 'x'}, {key: 'z'}, {} ]); }); it('should sort by a property with null and undefined values:', function() { var arr = [{key: null}, {key: 'z'}, {}, {key: 'x'}]; arraySort(arr, 'key').should.eql([ {key: 'x'}, {key: 'z'}, {key: null}, {} ]); }); it('should sort by a nested property:', function() { var res = arraySort(posts, 'locals.date'); res.should.eql([ { path: 'b.md', locals: { date: '2012-01-02' } }, { path: 'd.md', locals: { date: '2013-05-06' } }, { path: 'f.md', locals: { date: '2014-01-02' } }, { path: 'a.md', locals: { date: '2014-01-09' } }, { path: 'g.md', locals: { date: '2014-02-02' } }, { path: 'f.md', locals: { date: '2014-06-01' } }, { path: 'e.md', locals: { date: '2015-01-02' } }, { path: 'c.md', locals: { date: '2015-04-12' } } ]); }); it('should do nothing when the specified property is not a string:', function() { var arr = [ {a: {b: {c: 'c'}}}, {a: {b: {z: 'z'}}}, {a: {b: {u: 'u'}}}, {a: {b: {y: 'y'}}} ]; arraySort(arr, 'a.b').should.eql([ {a: {b: {c: 'c'}}}, {a: {b: {z: 'z'}}}, {a: {b: {u: 'u'}}}, {a: {b: {y: 'y'}}} ]); }); it('should sort by multiple properties:', function() { var posts = [ { foo: 'bbb', locals: { date: '2013-05-06' } }, { foo: 'aaa', locals: { date: '2012-01-02' } }, { foo: 'ddd', locals: { date: '2015-04-12' } }, { foo: 'ccc', locals: { date: '2014-01-02' } }, { foo: 'ccc', locals: { date: '2015-01-02' } }, { foo: 'ddd', locals: { date: '2014-01-09' } }, { foo: 'bbb', locals: { date: '2014-06-01' } }, { foo: 'aaa', locals: { date: '2014-02-02' } }, ]; var actual = arraySort(posts, ['foo', 'locals.date']); actual.should.eql([ { foo: 'aaa', locals: { date: '2012-01-02' } }, { foo: 'aaa', locals: { date: '2014-02-02' } }, { foo: 'bbb', locals: { date: '2013-05-06' } }, { foo: 'bbb', locals: { date: '2014-06-01' } }, { foo: 'ccc', locals: { date: '2014-01-02' } }, { foo: 'ccc', locals: { date: '2015-01-02' } }, { foo: 'ddd', locals: { date: '2014-01-09' } }, { foo: 'ddd', locals: { date: '2015-04-12' } } ]); }); it('should sort by multiple properties with null values:', function() { var posts = [ { foo: 'bbb', locals: { date: '2013-05-06' } }, { foo: 'aaa', locals: { date: '2012-01-02' } }, { foo: null, locals: { date: '2015-04-12' } }, { foo: 'ccc', locals: { date: '2014-01-02' } }, { foo: null, locals: { date: '2015-01-02' } }, { foo: 'ddd', locals: { date: '2014-01-09' } }, { foo: 'bbb', locals: { date: null } }, { foo: 'aaa', locals: { date: '2014-02-02' } }, ]; var actual = arraySort(posts, ['foo', 'locals.date']); actual.should.eql([ { foo: 'aaa', locals: { date: '2012-01-02' } }, { foo: 'aaa', locals: { date: '2014-02-02' } }, { foo: 'bbb', locals: { date: '2013-05-06' } }, { foo: 'bbb', locals: { date: null } }, { foo: 'ccc', locals: { date: '2014-01-02' } }, { foo: 'ddd', locals: { date: '2014-01-09' } }, { foo: null, locals: { date: '2015-01-02' } }, { foo: null, locals: { date: '2015-04-12' } } ]); }); it('should sort by multiple properties with undefined values:', function() { var posts = [ { foo: 'bbb', locals: { date: '2013-05-06' } }, { foo: 'aaa', locals: { date: '2012-01-02' } }, { locals: { date: '2015-04-12' } }, { foo: 'ccc', locals: { date: '2014-01-02' } }, { locals: { date: '2015-01-02' } }, { foo: 'ddd', locals: { date: '2014-01-09' } }, { foo: 'bbb', locals: {} }, { foo: 'aaa', locals: { date: '2014-02-02' } }, ]; var actual = arraySort(posts, ['foo', 'locals.date']); actual.should.eql([ { foo: 'aaa', locals: { date: '2012-01-02' } }, { foo: 'aaa', locals: { date: '2014-02-02' } }, { foo: 'bbb', locals: { date: '2013-05-06' } }, { foo: 'bbb', locals: {} }, { foo: 'ccc', locals: { date: '2014-01-02' } }, { foo: 'ddd', locals: { date: '2014-01-09' } }, { locals: { date: '2015-01-02' } }, { locals: { date: '2015-04-12' } } ]); }); it('should sort by multiple properties with null and undefined values:', function() { var posts = [ { foo: 'bbb', locals: { date: '2013-05-06' } }, { foo: 'aaa', locals: { date: null } }, { locals: { date: '2015-04-12' } }, { foo: 'ccc', locals: { date: '2014-01-02' } }, { locals: { date: '2015-01-02' } }, { foo: 'ddd', locals: { date: '2014-01-09' } }, { foo: null, locals: {} }, { foo: 'aaa', locals: { date: '2014-02-02' } }, ]; var actual = arraySort(posts, ['foo', 'locals.date']); actual.should.eql([ { foo: 'aaa', locals: { date: '2014-02-02' } }, { foo: 'aaa', locals: { date: null } }, { foo: 'bbb', locals: { date: '2013-05-06' } }, { foo: 'ccc', locals: { date: '2014-01-02' } }, { foo: 'ddd', locals: { date: '2014-01-09' } }, { foo: null, locals: {} }, { locals: { date: '2015-01-02' } }, { locals: { date: '2015-04-12' } } ]); }); it('should sort with a function:', function() { var arr = [{key: 'y'}, {key: 'z'}, {key: 'x'}]; var actual = arraySort(arr, function(a, b) { return a.key < b.key ? -1 : (a.key > b.key ? 1 : 0); }); actual.should.eql([ {key: 'x'}, {key: 'y'}, {key: 'z'} ]); }); it('should support sorting with a list of function:', function() { var arr = [ {foo: 'w', bar: 'y', baz: 'w', quux: 'a'}, {foo: 'x', bar: 'y', baz: 'w', quux: 'b'}, {foo: 'x', bar: 'y', baz: 'z', quux: 'c'}, {foo: 'x', bar: 'x', baz: 'w', quux: 'd'}, ]; var compare = function(prop) { return function(a, b) { return a[prop].localeCompare(b[prop]); }; }; var actual = arraySort(arr, compare('foo'), compare('bar'), compare('baz'), compare('quux')); actual.should.eql([ { foo: 'w', bar: 'y', baz: 'w', quux: 'a' }, { foo: 'x', bar: 'x', baz: 'w', quux: 'd' }, { foo: 'x', bar: 'y', baz: 'w', quux: 'b' }, { foo: 'x', bar: 'y', baz: 'z', quux: 'c' } ]); }); it('should support sorting with an array of function:', function() { var arr = [ {foo: 'w', bar: 'y', baz: 'w', quux: 'a'}, {foo: 'x', bar: 'y', baz: 'w', quux: 'b'}, {foo: 'x', bar: 'y', baz: 'z', quux: 'c'}, {foo: 'x', bar: 'x', baz: 'w', quux: 'd'}, ]; var compare = function(prop) { return function(a, b) { return a[prop].localeCompare(b[prop]); }; }; var actual = arraySort(arr, [ compare('foo'), compare('bar'), compare('baz'), compare('quux') ]); actual.should.eql([ { foo: 'w', bar: 'y', baz: 'w', quux: 'a' }, { foo: 'x', bar: 'x', baz: 'w', quux: 'd' }, { foo: 'x', bar: 'y', baz: 'w', quux: 'b' }, { foo: 'x', bar: 'y', baz: 'z', quux: 'c' } ]); }); it('should support sorting with any combination of functions and properties:', function() { var posts = [ { path: 'a.md', locals: { date: '2014-01-01', foo: 'zzz', bar: 1 } }, { path: 'f.md', locals: { date: '2014-01-01', foo: 'mmm', bar: 2 } }, { path: 'd.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 3 } }, { path: 'i.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 5 } }, { path: 'k.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 1 } }, { path: 'j.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 4 } }, { path: 'h.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 6 } }, { path: 'l.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 7 } }, { path: 'e.md', locals: { date: '2015-01-02', foo: 'aaa', bar: 8 } }, { path: 'b.md', locals: { date: '2012-01-02', foo: 'ccc', bar: 9 } }, { path: 'f.md', locals: { date: '2014-06-01', foo: 'rrr', bar: 10 } }, { path: 'c.md', locals: { date: '2015-04-12', foo: 'ttt', bar: 11 } }, { path: 'g.md', locals: { date: '2014-02-02', foo: 'yyy', bar: 12 } }, ]; var compare = function(prop) { return function(a, b, fn) { var valA = get(a, prop); var valB = get(b, prop); return fn(valA, valB); }; }; var actual = arraySort(posts, 'locals.date', 'doesnt.exist', compare('locals.foo'), [ compare('locals.bar') ]); actual.should.eql([ { path: 'b.md', locals: { date: '2012-01-02', foo: 'ccc', bar: 9 } }, { path: 'f.md', locals: { date: '2014-01-01', foo: 'mmm', bar: 2 } }, { path: 'k.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 1 } }, { path: 'd.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 3 } }, { path: 'j.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 4 } }, { path: 'i.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 5 } }, { path: 'h.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 6 } }, { path: 'l.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 7 } }, { path: 'a.md', locals: { date: '2014-01-01', foo: 'zzz', bar: 1 } }, { path: 'g.md', locals: { date: '2014-02-02', foo: 'yyy', bar: 12 } }, { path: 'f.md', locals: { date: '2014-06-01', foo: 'rrr', bar: 10 } }, { path: 'e.md', locals: { date: '2015-01-02', foo: 'aaa', bar: 8 } }, { path: 'c.md', locals: { date: '2015-04-12', foo: 'ttt', bar: 11 } } ]); }); it('should support reverse sorting with any combination of functions and properties:', function() { var posts = [ { path: 'a.md', locals: { date: '2014-01-01', foo: 'zzz', bar: 1 } }, { path: 'f.md', locals: { date: '2014-01-01', foo: 'mmm', bar: 2 } }, { path: 'd.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 3 } }, { path: 'i.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 5 } }, { path: 'k.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 1 } }, { path: 'j.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 4 } }, { path: 'h.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 6 } }, { path: 'l.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 7 } }, { path: 'e.md', locals: { date: '2015-01-02', foo: 'aaa', bar: 8 } }, { path: 'b.md', locals: { date: '2012-01-02', foo: 'ccc', bar: 9 } }, { path: 'f.md', locals: { date: '2014-06-01', foo: 'rrr', bar: 10 } }, { path: 'c.md', locals: { date: '2015-04-12', foo: 'ttt', bar: 11 } }, { path: 'g.md', locals: { date: '2014-02-02', foo: 'yyy', bar: 12 } }, ]; var compare = function(prop) { return function(a, b, fn) { var valA = get(a, prop); var valB = get(b, prop); return fn(valA, valB); }; }; var actual = arraySort(posts, 'locals.date', 'doesnt.exist', compare('locals.foo'), [ compare('locals.bar') ], { reverse: true }); actual.should.eql([ { path: 'c.md', locals: { date: '2015-04-12', foo: 'ttt', bar: 11 } }, { path: 'e.md', locals: { date: '2015-01-02', foo: 'aaa', bar: 8 } }, { path: 'f.md', locals: { date: '2014-06-01', foo: 'rrr', bar: 10 } }, { path: 'g.md', locals: { date: '2014-02-02', foo: 'yyy', bar: 12 } }, { path: 'a.md', locals: { date: '2014-01-01', foo: 'zzz', bar: 1 } }, { path: 'l.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 7 } }, { path: 'h.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 6 } }, { path: 'i.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 5 } }, { path: 'j.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 4 } }, { path: 'd.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 3 } }, { path: 'k.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 1 } }, { path: 'f.md', locals: { date: '2014-01-01', foo: 'mmm', bar: 2 } }, { path: 'b.md', locals: { date: '2012-01-02', foo: 'ccc', bar: 9 } } ]); }); });