package/package.json000644 000765 000024 0000001711 13155535706013027 0ustar00000000 000000 { "name": "default-compare", "description": "Basic sort algorithm that has similar behavior to Array.prototype.sort for null and undefined, but also allows sorting by an object property.", "version": "1.0.0", "homepage": "https://github.com/doowb/default-compare", "author": "Brian Woodward (https://doowb.com)", "repository": "doowb/default-compare", "bugs": { "url": "https://github.com/doowb/default-compare/issues" }, "license": "MIT", "files": [ "index.js" ], "main": "index.js", "engines": { "node": ">=0.10.0" }, "scripts": { "test": "mocha" }, "dependencies": { "kind-of": "^5.0.2" }, "devDependencies": { "gulp-format-md": "^1.0.0", "mocha": "^3.5.2" }, "keywords": [ "compare", "default" ], "verb": { "toc": false, "layout": "default", "tasks": [ "readme" ], "plugins": [ "gulp-format-md" ], "lint": { "reflinks": true } } } package/README.md000644 000765 000024 0000007064 13155535437012030 0ustar00000000 000000 # default-compare [![NPM version](https://img.shields.io/npm/v/default-compare.svg?style=flat)](https://www.npmjs.com/package/default-compare) [![NPM monthly downloads](https://img.shields.io/npm/dm/default-compare.svg?style=flat)](https://npmjs.org/package/default-compare) [![NPM total downloads](https://img.shields.io/npm/dt/default-compare.svg?style=flat)](https://npmjs.org/package/default-compare) [![Linux Build Status](https://img.shields.io/travis/doowb/default-compare.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/default-compare) [![Windows Build Status](https://img.shields.io/appveyor/ci/doowb/default-compare.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/doowb/default-compare) > Basic sort algorithm that has similar behavior to Array.prototype.sort for null and undefined, but also allows sorting by an object property. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save default-compare ``` Install with [yarn](https://yarnpkg.com): ```sh $ yarn add default-compare ``` ## Usage ```js var defaultCompare = require('default-compare'); ``` **basic array** ```js var arr = ['c', 'a', undefined, 'b', 'd', null, 'e']; console.log(arr.sort(defaultCompare)); //=> ['a', 'b', 'c', 'd', 'e', null, undefined] ``` **objects sorted by their "name" property** ```js var arr = [ {name: 'c', title: 'C'}, {name: 'a', title: 'A'}, {title: 'G'}, {name: 'b', title: 'B'}, {name: 'd', title: 'D'}, {name: null, title: 'F'}, {name: 'e', title: 'E'} ]; arr.sort(function(a, b) { return defaultCompare(a, b, 'name'); }); console.log(arr); //=> [ //=> {name: 'a', title: 'A'}, //=> {name: 'b', title: 'B'}, //=> {name: 'c', title: 'C'}, //=> {name: 'd', title: 'D'}, //=> {name: 'e', title: 'E'}, //=> {name: null, title: 'F'}, //=> {title: 'G'} //=> ]; ``` ## API ### [defaultCompare](index.js#L16) Basic sort algorithm that has similar behavior to `Array.prototype.sort` for null and undefined, but also allows sorting by an object property. **Params** * `a` **{Mixed}**: First value to compare. * `b` **{Mixed}**: Second value to compare. * `prop` **{String}**: Optional property to use when comparing objects. If specified must be a string. * `returns` **{Number}**: Returns 1 when `a` should come after `b`, -1 when `a` should come before `b`, and 0 when `a` and `b` are equal. ## About ### Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards. ### 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 **Brian Woodward** * [github/doowb](https://github.com/doowb) * [twitter/doowb](https://twitter.com/doowb) ### License Copyright © 2017, [Brian Woodward](https://doowb.com). 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._package/LICENSE000644 000765 000024 0000002072 13045254405011537 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2017, Brian Woodward. 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.package/index.js000644 000765 000024 0000002425 13155534510012201 0ustar00000000 000000 'use strict'; var typeOf = require('kind-of'); /** * Basic sort algorithm that has similar behavior to `Array.prototype.sort` * for null and undefined, but also allows sorting by an object property. * * @param {Mixed} `a` First value to compare. * @param {Mixed} `b` Second value to compare. * @param {String} `prop` Optional property to use when comparing objects. If specified must be a string. * @return {Number} Returns 1 when `a` should come after `b`, -1 when `a` should come before `b`, and 0 when `a` and `b` are equal. * @api public */ module.exports = function defaultCompare(a, b, prop) { if (prop != null && typeOf(prop) !== 'string') { throw new TypeError('expected "prop" to be undefined or a string'); } var typeA = typeOf(a); var typeB = typeOf(b); if (prop) { if (typeA === 'object') { a = a[prop]; typeA = typeOf(a); } if (typeB === 'object') { b = b[prop]; typeB = typeOf(b); } } if (typeA === 'null') { return typeB === 'null' ? 0 : (typeB === 'undefined' ? -1 : 1); } else if (typeA === 'undefined') { return typeB === 'null' ? 1 : (typeB === 'undefined' ? 0 : 1); } else if (typeB === 'null' || typeB === 'undefined') { return -1; } else { return a < b ? -1 : (a > b ? 1 : 0); } };