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

Commit 5dc0c8d

Browse files
mancristianaKent C. Dodds
authored andcommitted
feat(copyArrayByValue): Add copyArrayByValue function (#213) (#214)
Closes #213
1 parent 9192fdd commit 5dc0c8d

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/copyArrayByValue.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default copyArrayByValue
2+
3+
/**
4+
* Original Source: https://stackoverflow.com/a/7486130/5679427
5+
*
6+
* This method will clone the array and return a reference to the new array.
7+
*
8+
* @param {Array} array - The array to be copied
9+
* @return {Array} - Reference to the copied array
10+
*/
11+
function copyArrayByValue(array) {
12+
return array.slice()
13+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import last from './last'
7979
import descendingOrder from './descending-order'
8080
import reduceCount from './reduceCount'
8181
import BitwiseAnd from './BitwiseAnd'
82+
import copyArrayByValue from './copyArrayByValue'
8283

8384
export {
8485
reverseArrayInPlace,
@@ -162,4 +163,5 @@ export {
162163
descendingOrder,
163164
reduceCount,
164165
BitwiseAnd,
166+
copyArrayByValue,
165167
}

test/copyArrayByValue.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import test from 'ava'
2+
import {copyArrayByValue} from '../src'
3+
4+
test('copies the given array', t => {
5+
const original = ['a', 'b', 'c', 1, 2, {d: 'e'}]
6+
const expected = ['a', 'b', 'c', 1, 2, {d: 'e'}]
7+
const actual = copyArrayByValue(original)
8+
t.deepEqual(actual, expected)
9+
})

0 commit comments

Comments
 (0)