Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 038dc6d

Browse files
author
Kent C. Dodds
committed
Merge pull request #19 from kentcdodds/pr/array-fill
feat: Add array-fill method 👍
2 parents 2e98050 + edfa757 commit 038dc6d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/array-fill.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default fill
2+
3+
/**
4+
* Original Source: http://stackoverflow.com/a/13735425/971592
5+
*
6+
* This method will return an array with the given value prefilled
7+
*
8+
* @param {Array} array - the array to fill
9+
* @param {*} value - The value to prefill
10+
* @return {Array} - The prefilled array
11+
*/
12+
function fill(array, value) {
13+
return Array.apply(null, array).map(value.constructor.prototype.valueOf, value)
14+
}
15+

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import getQueryStringParam from './get-query-string-param'
33
import snakeToCamel from './snake-to-camel'
44
import padLeft from './pad-left'
55
import randomInteger from './random-integer'
6-
6+
import arrayFill from './array-fill'
77

88
export {
99
flatten,
1010
snakeToCamel,
1111
getQueryStringParam,
1212
padLeft,
1313
randomInteger,
14+
arrayFill,
1415
}

test/array-fill.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import test from 'ava'
2+
import {arrayFill} from '../src'
3+
4+
test('fills an array with a number', t => {
5+
const original = [1, 2, 3, 4]
6+
const expected = [7, 7, 7, 7]
7+
const actual = arrayFill(original, 7)
8+
t.same(actual, expected)
9+
})
10+
11+
test('fills an array with a string', t => {
12+
const original = Array(4)
13+
const expected = ['wookie', 'wookie', 'wookie', 'wookie']
14+
const actual = arrayFill(original, 'wookie')
15+
t.same(actual, expected)
16+
})
17+
18+
test('fills an array with a boolean', t => {
19+
const original = Array(4)
20+
const expected = [false, false, false, false]
21+
const actual = arrayFill(original, false)
22+
t.same(actual, expected)
23+
})
24+
25+
test.todo('allow for non-primitive values like objects, arrays, and dates')
26+

0 commit comments

Comments
 (0)