Skip to content

Commit

Permalink
feature: clone deep
Browse files Browse the repository at this point in the history
  • Loading branch information
vigan-abd committed Aug 18, 2023
1 parent d3527b8 commit 88b8882
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.1.0
- feat: cloneDeep

# 1.0.0
- feat: getArrayHasIntersect
- feat: getArrayUniq
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export function cloneDeep (obj: Object): Object
export function getArrayHasIntersect (arr1: Array<any>, arr2: Array<any>): boolean
export function getArrayUniq (arr: Array<any>): Array<any>
export function isNil(val: any): boolean
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -8,6 +9,7 @@ const pick = require('./src/pick')
const pickBy = require('./src/pickBy')

module.exports = {
cloneDeep,
getArrayHasIntersect,
getArrayUniq,
isNil,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
5 changes: 5 additions & 0 deletions src/cloneDeep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

const cloneDeep = (obj) => JSON.parse(JSON.stringify(obj))

module.exports = cloneDeep
22 changes: 22 additions & 0 deletions test/cloneDeep.js
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit 88b8882

Please sign in to comment.