Skip to content

Commit

Permalink
Add support for Iterable, require Node.js 8, add TypeScript definit…
Browse files Browse the repository at this point in the history
…ion (#6)
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 5, 2019
1 parent 087edee commit 8d6734f
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 68 deletions.
5 changes: 1 addition & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
sudo: false
language: node_js
node_js:
- 'stable'
- '0.12'
- '10'
- '8'
32 changes: 32 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
Convert a value to an array.
_Supplying `null` or `undefined` results in an empty array._
@example
```
import arrify = require('arrify');
arrify('🦄');
//=> ['🦄']
arrify(['🦄']);
//=> ['🦄']
arrify(new Set(['🦄']));
//=> ['🦄']
arrify(null);
//=> []
arrify(undefined);
//=> []
```
*/
declare function arrify(value: null | undefined): [];
declare function arrify(value: string): [string];
declare function arrify<ValueType>(value: ValueType[]): ValueType[];
declare function arrify<ValueType>(value: Iterable<ValueType>): ValueType[];
declare function arrify<ValueType>(value: ValueType): [ValueType];

export = arrify;
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
'use strict';
module.exports = function (val) {
if (val === null || val === undefined) {

const arrify = value => {
if (value === null || value === undefined) {
return [];
}

return Array.isArray(val) ? val : [val];
if (Array.isArray(value)) {
return value;
}

if (typeof value === 'string') {
return [value];
}

if (typeof value[Symbol.iterator] === 'function') {
return [...value];
}

return [value];
};

module.exports = arrify;
14 changes: 14 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {expectType} from 'tsd';
import arrify = require('.');

expectType<[]>(arrify(null));
expectType<[]>(arrify(undefined));
expectType<[string]>(arrify('🦄'));
expectType<string[]>(arrify(['🦄']));
expectType<[boolean]>(arrify(true));
expectType<[number]>(arrify(1));
expectType<[{}]>(arrify({}));
expectType<([string | number, string | number])[]>(
arrify(new Map<string | number, string | number>([[1, 2], ['a', 'b']]))
);
expectType<number[]>(arrify(new Set([1, 2])));
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

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:
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 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.
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.
64 changes: 33 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
{
"name": "arrify",
"version": "1.0.1",
"description": "Convert a value to an array",
"license": "MIT",
"repository": "sindresorhus/arrify",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"array",
"arr",
"arrify",
"arrayify",
"convert",
"value"
],
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "arrify",
"version": "1.0.1",
"description": "Convert a value to an array",
"license": "MIT",
"repository": "sindresorhus/arrify",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"array",
"arr",
"arrify",
"arrayify",
"convert",
"value"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
11 changes: 7 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ $ npm install --save arrify
```js
const arrify = require('arrify');

arrify('unicorn');
//=> ['unicorn']
arrify('🦄');
//=> ['🦄']

arrify(['unicorn']);
//=> ['unicorn']
arrify(['🦄']);
//=> ['🦄']

arrify(new Set(['🦄']));
//=> ['🦄']

arrify(null);
//=> []
Expand Down
16 changes: 10 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import test from 'ava';
import fn from './';
import arrify from '.';

test(t => {
t.is(fn('foo')[0], 'foo');
t.is(fn(['foo'])[0], 'foo');
t.is(fn(null)[0], undefined);
t.is(fn(undefined)[0], undefined);
test('main', t => {
t.deepEqual(arrify('foo'), ['foo']);
t.deepEqual(arrify(new Map([[1, 2], ['a', 'b']])), [[1, 2], ['a', 'b']]);
t.deepEqual(arrify(new Set([1, 2])), [1, 2]);
t.deepEqual(arrify(null), []);
t.deepEqual(arrify(undefined), []);

const fooArray = ['foo'];
t.is(arrify(fooArray), fooArray);
});

0 comments on commit 8d6734f

Please sign in to comment.