pax_global_header00006660000000000000000000000064131555357150014524gustar00rootroot0000000000000052 comment=2f56e4346d80672cf92aa724fee8307aa22f8b5f default-compare-1.0.0/000077500000000000000000000000001315553571500145725ustar00rootroot00000000000000default-compare-1.0.0/.editorconfig000066400000000000000000000004051315553571500172460ustar00rootroot00000000000000root = 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 = falsedefault-compare-1.0.0/.eslintrc.json000066400000000000000000000067751315553571500174050ustar00rootroot00000000000000{ "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"] } } default-compare-1.0.0/.gitattributes000066400000000000000000000000141315553571500174600ustar00rootroot00000000000000* text=auto default-compare-1.0.0/.github/000077500000000000000000000000001315553571500161325ustar00rootroot00000000000000default-compare-1.0.0/.github/contributing.md000066400000000000000000000113171315553571500211660ustar00rootroot00000000000000# Contributing to default-compare First and foremost, thank you! We appreciate that you want to contribute to default-compare, your time is valuable, and your contributions mean a lot to us. ## Important! By contributing to this project, you: * Agree that you have authored 100% of the content * Agree that you have the necessary rights to the content * Agree that you have received the necessary permissions from your employer to make the contributions (if applicable) * Agree that the content you contribute may be provided under the Project license(s) ## Getting started **What does "contributing" mean?** Creating an issue is the simplest form of contributing to a project. But there are many ways to contribute, including the following: - Updating or correcting documentation - Feature requests - Bug reports If you'd like to learn more about contributing in general, the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) has a lot of useful information. **Showing support for default-compare** Please keep in mind that open source software is built by people like you, who spend their free time creating things the rest the community can use. Don't have time to contribute? No worries, here are some other ways to show your support for default-compare: - star the [project](https://github.com/doowb/default-compare) - tweet your support for default-compare ## Issues ### Before creating an issue Please try to determine if the issue is caused by an underlying library, and if so, create the issue there. Sometimes this is difficult to know. We only ask that you attempt to give a reasonable attempt to find out. Oftentimes the readme will have advice about where to go to create issues. Try to follow these guidelines - **Avoid creating issues for implementation help**. It's much better for discoverability, SEO, and semantics - to keep the issue tracker focused on bugs and feature requests - to ask implementation-related questions on [stackoverflow.com][so] - **Investigate the issue**: - **Check the readme** - oftentimes you will find notes about creating issues, and where to go depending on the type of issue. - Create the issue in the appropriate repository. ### Creating an issue Please be as descriptive as possible when creating an issue. Give us the information we need to successfully answer your question or address your issue by answering the following in your issue: - **version**: please note the version of default-compare are you using - **extensions, plugins, helpers, etc** (if applicable): please list any extensions you're using - **error messages**: please paste any error messages into the issue, or a [gist](https://gist.github.com/) ### Closing issues The original poster or the maintainer's of default-compare may close an issue at any time. Typically, but not exclusively, issues are closed when: - The issue is resolved - The project's maintainers have determined the issue is out of scope - An issue is clearly a duplicate of another issue, in which case the duplicate issue will be linked. - A discussion has clearly run its course ## Next steps **Tips for creating idiomatic issues** Spending just a little extra time to review best practices and brush up on your contributing skills will, at minimum, make your issue easier to read, easier to resolve, and more likely to be found by others who have the same or similar issue in the future. At best, it will open up doors and potential career opportunities by helping you be at your best. The following resources were hand-picked to help you be the most effective contributor you can be: - The [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) is a great place for newcomers to start, but there is also information for experienced contributors there. - Take some time to learn basic markdown. We can't stress this enough. Don't start pasting code into GitHub issues before you've taken a moment to review this [markdown cheatsheet](https://gist.github.com/jonschlinkert/5854601) - The GitHub guide to [basic markdown](https://help.github.com/articles/markdown-basics/) is another great markdown resource. - Learn about [GitHub Flavored Markdown](https://help.github.com/articles/github-flavored-markdown/). And if you want to really go above and beyond, read [mastering markdown](https://guides.github.com/features/mastering-markdown/). At the very least, please try to: - Use backticks to wrap code. This ensures that it retains its formatting and isn't modified when it's rendered by GitHub, and makes the code more readable to others - When applicable, use syntax highlighting by adding the correct language name after the first "code fence" [so]: http://stackoverflow.com/questions/tagged/default-compare default-compare-1.0.0/.gitignore000066400000000000000000000004401315553571500165600ustar00rootroot00000000000000# 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.jsondefault-compare-1.0.0/.travis.yml000066400000000000000000000002041315553571500166770ustar00rootroot00000000000000sudo: false os: - linux - osx language: node_js node_js: - node - '8' - '7' - '6' - '5' - '4' - '0.12' - '0.10' default-compare-1.0.0/.verb.md000066400000000000000000000015031315553571500161270ustar00rootroot00000000000000## Usage ```js var defaultCompare = require('{%= name %}'); ``` **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 {%= apidocs('index.js') %}default-compare-1.0.0/LICENSE000066400000000000000000000020721315553571500156000ustar00rootroot00000000000000The 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.default-compare-1.0.0/README.md000066400000000000000000000070641315553571500160600ustar00rootroot00000000000000# 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._default-compare-1.0.0/appveyor.yml000066400000000000000000000005211315553571500171600ustar00rootroot00000000000000environment: matrix: - nodejs_version: '8' - nodejs_version: '7' - nodejs_version: '6' - nodejs_version: '4' - nodejs_version: '0.12' - nodejs_version: '0.10' install: - ps: 'Install-Product node $env:nodejs_version' - npm install test_script: - node --version - npm --version - npm test build: 'off' default-compare-1.0.0/index.js000066400000000000000000000024251315553571500162420ustar00rootroot00000000000000'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); } }; default-compare-1.0.0/package.json000066400000000000000000000017111315553571500170600ustar00rootroot00000000000000{ "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 } } } default-compare-1.0.0/test.js000066400000000000000000000027351315553571500161160ustar00rootroot00000000000000'use strict'; require('mocha'); var assert = require('assert'); var defaultCompare = require('./'); describe('default-compare', function() { it('should export a function', function() { assert.equal(typeof defaultCompare, 'function'); }); it('should throw an error when invalid args are passed', function(cb) { try { defaultCompare('a', 'b', {foo: 'bar'}); cb(new Error('expected an error')); } catch (err) { assert(err); assert.equal(err.message, 'expected "prop" to be undefined or a string'); cb(); } }); it('should sort an array with null and undefined values', function() { var arr = ['c', 'a', undefined, 'b', 'd', null, 'e']; assert.deepEqual(arr.sort(defaultCompare), ['a', 'b', 'c', 'd', 'e', null, undefined]); }); it('should sort an array of objects with null and undefined values', function() { var arr = [ {name: 'c', title: 'C'}, undefined, {name: 'a', title: 'A'}, {title: 'G'}, {name: 'b', title: 'B'}, null, {name: 'd', title: 'D'}, {name: null, title: 'F'}, {name: 'e', title: 'E'} ]; arr.sort(function(a, b) { return defaultCompare(a, b, 'name'); }); assert.deepEqual(arr, [ {name: 'a', title: 'A'}, {name: 'b', title: 'B'}, {name: 'c', title: 'C'}, {name: 'd', title: 'D'}, {name: 'e', title: 'E'}, null, {name: null, title: 'F'}, {title: 'G'}, undefined ]); }); });