Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 28, 2021
1 parent 1f954bc commit 2838a3e
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 48 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
20 changes: 9 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ _Specifying `null` or `undefined` results in an empty array._
@example
```
import arrify = require('arrify');
import arrify from 'arrify';
arrify('🦄');
//=> ['🦄']
Expand All @@ -23,16 +23,14 @@ arrify(undefined);
//=> []
```
*/
declare function arrify<ValueType>(
export default function arrify<ValueType>(
value: ValueType
): ValueType extends (null | undefined)
? []
? [] // eslint-disable-line @typescript-eslint/ban-types
: ValueType extends string
? [string]
: ValueType extends ReadonlyArray<unknown> // TODO: Use 'readonly unknown[]' in the next major version
? ValueType
: ValueType extends Iterable<infer T>
? T[]
: [ValueType];

export = arrify;
? [string]
: ValueType extends readonly unknown[]
? ValueType
: ValueType extends Iterable<infer T>
? T[]
: [ValueType];
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const arrify = value => {
export default function arrify(value) {
if (value === null || value === undefined) {
return [];
}
Expand All @@ -18,6 +16,4 @@ const arrify = value => {
}

return [value];
};

module.exports = arrify;
}
37 changes: 19 additions & 18 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import {expectType, expectError} from 'tsd';
import arrify = require('.');
/* eslint-disable @typescript-eslint/ban-types */
import {expectType, expectError, expectAssignable} from 'tsd';
import arrify from './index.js';

expectType<[]>(arrify(null));
expectType<[]>(arrify(undefined));
expectType<[string]>(arrify('🦄'));
expectType<string[]>(arrify(['🦄']));
expectType<[boolean]>(arrify(true));
expectAssignable<[boolean]>(arrify(true));
expectType<[number]>(arrify(1));
expectType<[{}]>(arrify({}));
expectAssignable<[Record<string, unknown>]>(arrify({}));
expectType<[number, string]>(arrify([1, 'foo']));
expectType<(string | boolean)[]>(
expectType<Array<string | boolean>>(
arrify(new Set<string | boolean>(['🦄', true]))
);
expectType<number[]>(arrify(new Set([1, 2])));
expectError(arrify(['🦄'] as const).push(''));
expectType<number[] | []>(arrify(Boolean() ? [1, 2] : null));
expectType<number[] | []>(arrify(Boolean() ? [1, 2] : undefined));
expectType<number[] | [string]>(arrify(Boolean() ? [1, 2] : '🦄'));
expectType<number[] | string[]>(arrify(Boolean() ? [1, 2] : ['🦄']));
expectType<number[] | [boolean]>(arrify(Boolean() ? [1, 2] : true));
expectType<number[] | [number]>(arrify(Boolean() ? [1, 2] : 3));
expectType<number[] | [{}]>(arrify(Boolean() ? [1, 2] : {}));
expectType<number[] | [number, string]>(
arrify(Boolean() ? [1, 2] : [1, 'foo'])
expectType<[number, number] | []>(arrify(false ? [1, 2] : null));
expectType<[number, number] | []>(arrify(false ? [1, 2] : undefined));
expectType<[number, number] | [string]>(arrify(false ? [1, 2] : '🦄'));
expectType<[number, number] | [string]>(arrify(false ? [1, 2] : ['🦄']));
expectAssignable<number[] | [boolean]>(arrify(false ? [1, 2] : true));
expectAssignable<number[] | [number]>(arrify(false ? [1, 2] : 3));
expectAssignable<number[] | [Record<string, unknown>]>(arrify(false ? [1, 2] : {}));
expectAssignable<number[] | [number, string]>(
arrify(false ? [1, 2] : [1, 'foo'])
);
expectType<number[] | (string | boolean)[]>(
arrify(Boolean() ? [1, 2] : new Set<string | boolean>(['🦄', true]))
expectAssignable<number[] | Array<string | boolean>>(
arrify(false ? [1, 2] : new Set<string | boolean>(['🦄', true]))
);
expectType<number[] | [boolean] | [string]>(
arrify(Boolean() ? [1, 2] : Boolean() ? true : '🦄')
expectAssignable<number[] | [boolean] | [string]>(
arrify(false ? [1, 2] : (false ? true : '🦄'))
);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://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:

Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Convert a value to an array",
"license": "MIT",
"repository": "sindresorhus/arrify",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -28,8 +31,8 @@
"ensure"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.39.1"
}
}
5 changes: 1 addition & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

> Convert a value to an array

## Install

```
$ npm install arrify
```


## Usage

```js
const arrify = require('arrify');
import arrify from 'arrify';

arrify('🦄');
//=> ['🦄']
Expand All @@ -33,7 +31,6 @@ arrify(undefined);

*Specifying `null` or `undefined` results in an empty array.*


---

<div align="center">
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import arrify from '.';
import arrify from './index.js';

test('main', t => {
t.deepEqual(arrify('foo'), ['foo']);
Expand Down

0 comments on commit 2838a3e

Please sign in to comment.