This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from kentcdodds/pr/array-fill
feat: Add array-fill method 👍
- Loading branch information
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|