diff --git a/CHANGELOG b/CHANGELOG index 0ddbd33..b4280f4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +# 1.1.0 +- feat: cloneDeep + # 1.0.0 - feat: getArrayHasIntersect - feat: getArrayUniq diff --git a/index.d.ts b/index.d.ts index f53ebef..29cd6ea 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,3 +1,4 @@ +export function cloneDeep (obj: Object): Object export function getArrayHasIntersect (arr1: Array, arr2: Array): boolean export function getArrayUniq (arr: Array): Array export function isNil(val: any): boolean diff --git a/index.js b/index.js index 79d831f..b6434f8 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ 'use strict' +const cloneDeep = require('./src/cloneDeep') const getArrayHasIntersect = require('./src/getArrayHasIntersect') const getArrayUniq = require('./src/getArrayUniq') const isNil = require('./src/isNil') @@ -8,6 +9,7 @@ const pick = require('./src/pick') const pickBy = require('./src/pickBy') module.exports = { + cloneDeep, getArrayHasIntersect, getArrayUniq, isNil, diff --git a/package.json b/package.json index da7a8a6..7b1bce1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bitfinex/lib-js-util-base", - "version": "1.0.0", + "version": "1.1.0", "description": "general utils", "main": "index.js", "scripts": { diff --git a/src/cloneDeep.js b/src/cloneDeep.js new file mode 100644 index 0000000..08b6c68 --- /dev/null +++ b/src/cloneDeep.js @@ -0,0 +1,5 @@ +'use strict' + +const cloneDeep = (obj) => JSON.parse(JSON.stringify(obj)) + +module.exports = cloneDeep diff --git a/test/cloneDeep.js b/test/cloneDeep.js new file mode 100644 index 0000000..dc7db0a --- /dev/null +++ b/test/cloneDeep.js @@ -0,0 +1,22 @@ +'use strict' + +/* eslint-env mocha */ + +const assert = require('assert') +const { cloneDeep } = require('../index') + +describe('cloneDeep', () => { + it('should deep clone objects', () => { + const obj = { foo: 1, bar: { baz: 'a' } } + const clone = cloneDeep(obj) + assert.deepStrictEqual(obj, clone) + assert.notStrictEqual(obj, clone) + }) + + it('should deep clone arrays', () => { + const obj = [1, [2, 3]] + const clone = cloneDeep(obj) + assert.deepStrictEqual(obj, clone) + assert.notStrictEqual(obj, clone) + }) +})