diff --git a/src/clone.ts b/src/clone.ts new file mode 100644 index 0000000..39dd9e7 --- /dev/null +++ b/src/clone.ts @@ -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; +} diff --git a/src/index.ts b/src/index.ts index aab923f..9224643 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/test/unit/clone.test.ts b/test/unit/clone.test.ts new file mode 100644 index 0000000..2be9fab --- /dev/null +++ b/test/unit/clone.test.ts @@ -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); + }); +});