Skip to content

Commit

Permalink
feat(Method): Added a clone method
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswlane committed Dec 16, 2017
1 parent 05d987d commit 7669c02
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/clone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function clone(items: any) {
if (Array.isArray(items)) {
return [...items];
}

const cloned: any = {};

Object.keys(items).forEach((prop) => {
cloned[prop] = items[prop];
});

return cloned;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { checkAllItems } from './check-all-items';
export { clone } from './clone';
export { checkItemByItemNumber } from './check-item-by-item-number';
export { checkItemsInGroup } from './check-items-in-group';
export { filterList } from './filter-list';
Expand Down
19 changes: 19 additions & 0 deletions test/unit/clone.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { clone } from '../../src';

describe('Clone', () => {
test('should clone items in a array', () => {
const existingArray = [1, 2, 3, 4, 5];

const newArray = clone(existingArray);

expect(existingArray).toEqual(newArray);
});

test('should clone items in a object', () => {
const existingObject = { a: 1, b: 2, c: 3, d: 4, e: 5 };

const newObject = clone(existingObject);

expect(existingObject).toEqual(newObject);
});
});

0 comments on commit 7669c02

Please sign in to comment.