diff --git a/.editorconfig b/.editorconfig index 8311fe1..98a761d 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,4 +1,3 @@ -# editorconfig.org root = true [*] @@ -8,9 +7,6 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[package.json] +[{package.json,*.yml}] indent_style = space indent_size = 2 - -[*.md] -trim_trailing_whitespace = false diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 804f8af..0000000 --- a/.jshintrc +++ /dev/null @@ -1,13 +0,0 @@ -{ - "node": true, - "esnext": true, - "bitwise": true, - "camelcase": true, - "curly": true, - "immed": true, - "newcap": true, - "noarg": true, - "undef": true, - "unused": "vars", - "strict": true -} diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index dedfc07..c9c9848 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,4 @@ sudo: false language: node_js node_js: - - 'iojs' - - '0.12' - - '0.10' + - '6' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..12a94e1 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,15 @@ +/** + * Create an array with values that are present in the first array but not additional ones. + * + * @param array - The array to compare against. + * @param values - The arrays with values to be excluded. + * @returns A new array of filtered values. + * + * @example + * + * import arrayDiffer from 'array-differ'; + * + * arrayDiffer([2, 3, 4], [3, 50]); + * //=> [2, 4] + */ +export default function arrayDiffer(array: ArrayLike, ...values: ArrayLike[]): T[]; diff --git a/index.js b/index.js index fbe2ed2..1ef9418 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,9 @@ 'use strict'; -module.exports = function (arr) { - var rest = [].concat.apply([], [].slice.call(arguments, 1)); - return arr.filter(function (el) { - return rest.indexOf(el) === -1; - }); + +const arrayDiffer = (array, ...values) => { + const rest = new Set([].concat(...values)); + return array.filter(x => !rest.has(x)); }; + +module.exports = arrayDiffer; +module.exports.default = arrayDiffer; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..6ad424e --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,5 @@ +import {expectType} from 'tsd-check'; +import arrayDiffer from '.'; + +expectType(arrayDiffer(['a', 'b', 'c'], ['b'], ['c'])); +expectType(arrayDiffer([1, 2, 3], [2], [3])); diff --git a/package.json b/package.json index a8c4683..371229e 100644 --- a/package.json +++ b/package.json @@ -1,32 +1,35 @@ { - "name": "array-differ", - "version": "1.0.0", - "description": "Create an array with values that are present in the first input array but not additional ones", - "license": "MIT", - "repository": "sindresorhus/array-differ", - "author": { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "http://sindresorhus.com" - }, - "engines": { - "node": ">=0.10.0" - }, - "scripts": { - "test": "mocha" - }, - "files": [ - "index.js" - ], - "keywords": [ - "array", - "difference", - "diff", - "differ", - "filter", - "exclude" - ], - "devDependencies": { - "mocha": "*" - } + "name": "array-differ", + "version": "2.1.0", + "description": "Create an array with values that are present in the first input array but not additional ones", + "license": "MIT", + "repository": "sindresorhus/array-differ", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=6" + }, + "scripts": { + "test": "xo && ava && tsd-check" + }, + "files": [ + "index.js", + "index.d.ts" + ], + "keywords": [ + "array", + "difference", + "diff", + "differ", + "filter", + "exclude" + ], + "devDependencies": { + "ava": "^1.2.1", + "tsd-check": "^0.3.0", + "xo": "^0.24.0" + } } diff --git a/readme.md b/readme.md index 68f5d36..b98cc06 100644 --- a/readme.md +++ b/readme.md @@ -5,15 +5,15 @@ ## Install -```sh -$ npm install --save array-differ +``` +$ npm install array-differ ``` ## Usage ```js -var arrayDiffer = require('array-differ'); +const arrayDiffer = require('array-differ'); arrayDiffer([2, 3, 4], [3, 50]); //=> [2, 4] @@ -21,21 +21,21 @@ arrayDiffer([2, 3, 4], [3, 50]); ## API -### arrayDiffer(input, values, [values, ...]) +### arrayDiffer(input, ...values) -Returns the new array. +Returns a new array. #### input -Type: `array` +Type: `unknown[]` #### values -Type: `array` +Type: `unknown[]` Arrays of values to exclude. ## License -MIT © [Sindre Sorhus](http://sindresorhus.com) +MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/test.js b/test.js index d3f3858..5375bbf 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,7 @@ -'use strict'; -var assert = require('assert'); -var arrayDifference = require('./'); +import test from 'ava'; +import arrayDiffer from '.'; -it('should filter out the difference', function () { - assert.deepEqual(arrayDifference([2, 3, 4], [3, 50, 60]), [2, 4]); +test('main', t => { + t.deepEqual(arrayDiffer([2, 3, 4], [3, 50, 60]), [2, 4]); + t.deepEqual(arrayDiffer([2, 3, 4], [3, 50], [2, 60]), [4]); });