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 15, 2021
1 parent 54f3404 commit d7476d4
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 101 deletions.
5 changes: 1 addition & 4 deletions .editorconfig
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
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
8 changes: 1 addition & 7 deletions .github/workflows/main.yml
Expand Up @@ -12,15 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
- 6
- 4
- 0.12
- 0.1
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
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
19 changes: 9 additions & 10 deletions index.js
@@ -1,15 +1,14 @@
'use strict';
module.exports = function (item, n) {
var ret = new Array(n);
var isFn = typeof item === 'function';
export default function filledArray(fillValue, count) {
const returnValue = Array.from({length: count});
const isFunction = typeof fillValue === 'function';

if (!isFn && typeof ret.fill === 'function') {
return ret.fill(item);
if (!isFunction) {
return returnValue.fill(fillValue);
}

for (var i = 0; i < n; i++) {
ret[i] = isFn ? item(i, n, ret) : item;
for (let index = 0; index < count; index++) {
returnValue[index] = isFunction ? fillValue(index, count, returnValue) : fillValue;
}

return ret;
};
return returnValue;
}
22 changes: 5 additions & 17 deletions license
@@ -1,21 +1,9 @@
The MIT License (MIT)
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:
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.
71 changes: 36 additions & 35 deletions package.json
@@ -1,37 +1,38 @@
{
"name": "filled-array",
"version": "1.1.0",
"description": "Returns an array filled with the specified input",
"license": "MIT",
"repository": "sindresorhus/filled-array",
"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",
"elements",
"el",
"filled",
"repeat",
"repeating",
"string",
"str",
"text",
"fill"
],
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "filled-array",
"version": "1.1.0",
"description": "Returns an array filled with the specified input",
"license": "MIT",
"repository": "sindresorhus/filled-array",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"array",
"elements",
"filled",
"repeat",
"repeating",
"string",
"text",
"fill"
],
"devDependencies": {
"ava": "^3.15.0",
"xo": "^0.38.2"
}
}
22 changes: 7 additions & 15 deletions readme.md
Expand Up @@ -2,39 +2,36 @@

> Returns an array filled with the specified input

## Install

```
$ npm install --save filled-array
$ npm install filled-array
```


## Usage

```js
const filledArray = require('filled-array');
import filledArray from 'filled-array';

filledArray('x', 3);
//=> ['x', 'x', 'x']

filledArray(0, 3);
//=> [0, 0, 0]

filledArray(i => {
return (++i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i;
filledArray(index => {
return (++index % 3 ? '' : 'Fizz') + (index % 5 ? '' : 'Buzz') || index;
}, 15);
//=> [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz']
```


## API

### filledArray(filler, count)
### filledArray(fillValue, count)

#### filler
#### fillValue

Type: Any
Type: `unknown`

Value to fill the array with.

Expand All @@ -45,8 +42,3 @@ You can pass a function to generate the array items dynamically. The function is
Type: `number`

Number of items to fill the array with.


## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
24 changes: 12 additions & 12 deletions test.js
@@ -1,5 +1,5 @@
import test from 'ava';
import fn from './';
import filledArray from './index.js';

function indexPlus(index) {
return index + 1;
Expand All @@ -10,20 +10,20 @@ function fizzBuzz(index) {
}

function comprehensive(index, length, partial) {
return partial.indexOf(index) === -1 ? index + 1 : length;
return partial.includes(index) ? length : index + 1;
}

test(t => {
t.deepEqual(fn('a', 0), []);
t.deepEqual(fn('a', 1), ['a']);
t.deepEqual(fn('a', 2), ['a', 'a']);
t.deepEqual(fn('a', 5), ['a', 'a', 'a', 'a', 'a']);
t.deepEqual(fn('foo', 2), ['foo', 'foo']);
t.deepEqual(fn(0, 2), [0, 0]);
t.deepEqual(fn(indexPlus, 5), [1, 2, 3, 4, 5]);
test('main', t => {
t.deepEqual(filledArray('a', 0), []);
t.deepEqual(filledArray('a', 1), ['a']);
t.deepEqual(filledArray('a', 2), ['a', 'a']);
t.deepEqual(filledArray('a', 5), ['a', 'a', 'a', 'a', 'a']);
t.deepEqual(filledArray('foo', 2), ['foo', 'foo']);
t.deepEqual(filledArray(0, 2), [0, 0]);
t.deepEqual(filledArray(indexPlus, 5), [1, 2, 3, 4, 5]);
t.deepEqual(
fn(fizzBuzz, 15),
filledArray(fizzBuzz, 15),
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz']
);
t.deepEqual(fn(comprehensive, 5), [1, 5, 3, 5, 5]);
t.deepEqual(filledArray(comprehensive, 5), [1, 5, 3, 5, 5]);
});

0 comments on commit d7476d4

Please sign in to comment.