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

Commit

Permalink
Merge pull request #19 from kentcdodds/pr/array-fill
Browse files Browse the repository at this point in the history
feat: Add array-fill method 👍
  • Loading branch information
Kent C. Dodds committed Mar 17, 2016
2 parents 2e98050 + edfa757 commit 038dc6d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/array-fill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default fill

/**
* Original Source: http://stackoverflow.com/a/13735425/971592
*
* This method will return an array with the given value prefilled
*
* @param {Array} array - the array to fill
* @param {*} value - The value to prefill
* @return {Array} - The prefilled array
*/
function fill(array, value) {
return Array.apply(null, array).map(value.constructor.prototype.valueOf, value)
}

3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import getQueryStringParam from './get-query-string-param'
import snakeToCamel from './snake-to-camel'
import padLeft from './pad-left'
import randomInteger from './random-integer'

import arrayFill from './array-fill'

export {
flatten,
snakeToCamel,
getQueryStringParam,
padLeft,
randomInteger,
arrayFill,
}
26 changes: 26 additions & 0 deletions test/array-fill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from 'ava'
import {arrayFill} from '../src'

test('fills an array with a number', t => {
const original = [1, 2, 3, 4]
const expected = [7, 7, 7, 7]
const actual = arrayFill(original, 7)
t.same(actual, expected)
})

test('fills an array with a string', t => {
const original = Array(4)
const expected = ['wookie', 'wookie', 'wookie', 'wookie']
const actual = arrayFill(original, 'wookie')
t.same(actual, expected)
})

test('fills an array with a boolean', t => {
const original = Array(4)
const expected = [false, false, false, false]
const actual = arrayFill(original, false)
t.same(actual, expected)
})

test.todo('allow for non-primitive values like objects, arrays, and dates')

0 comments on commit 038dc6d

Please sign in to comment.