@everymundo/simple-clone
Advanced tools
Comparing version 1.1.0 to 1.1.1
24
index.js
@@ -1,24 +0,24 @@ | ||
const clone = o => JSON.parse(JSON.stringify(o)); | ||
const clone = o => JSON.parse(JSON.stringify(o)) | ||
const realClone = (obj, history = []) => { | ||
if (history.includes(obj)) return '[Circular]'; | ||
if (history.includes(obj)) return '[Circular]' | ||
if (Array.isArray(obj)) { | ||
history.push(obj); | ||
return obj.map(_ => realClone(_, history)); | ||
history.push(obj) | ||
return obj.map(_ => realClone(_, history)) | ||
} | ||
if (typeof obj === 'object') { | ||
history.push(obj); | ||
const ret = {}; | ||
history.push(obj) | ||
const ret = {} | ||
Object.keys(obj).forEach((key) => { | ||
ret[key] = realClone(obj[key], history); | ||
}); | ||
ret[key] = realClone(obj[key], history) | ||
}) | ||
return ret; | ||
return ret | ||
} | ||
return obj; | ||
}; | ||
return obj | ||
} | ||
module.exports = { clone, realClone }; | ||
module.exports = { clone, realClone } |
{ | ||
"name": "@everymundo/simple-clone", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Very simplistic cloning tool for nodejs.", | ||
"main": "index.js", | ||
"scripts": { | ||
"cover": "istanbul cover -x '*.test.js' _mocha", | ||
"test": "mocha" | ||
"cover": "LOG_LEVEL=trace nyc -x test --reporter=lcov --reporter=text mocha test --recursive", | ||
"check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100", | ||
"check-lint": "standard --env=mocha *.js lib/*.js spec/*.js spec/lib/*.js", | ||
"fix-lint": "standard --env=mocha --fix *.js lib/*.js test/*.js", | ||
"test": "NODE_ENV=test LOG_LEVEL=test mocha --recursive $@" | ||
}, | ||
"config": { | ||
"ghooks": { | ||
"pre-commit": "npm run check-lint && npm test", | ||
"pre-push": "npm run cover && npm run check-coverage" | ||
} | ||
}, | ||
"repository": { | ||
@@ -22,6 +31,8 @@ "type": "git", | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"istanbul": "^1.1.0-alpha.1", | ||
"mocha": "^4.1.0" | ||
"chai": "^4.2.0", | ||
"ghooks": "^2.0.4", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.1.1", | ||
"standard": "^12.0.1" | ||
} | ||
} |
const { expect } = require('chai'); | ||
const { clone, realClone } = require('../'); | ||
const { expect } = require('chai') | ||
const { clone, realClone } = require('../') | ||
describe('clone', () => { | ||
it('should return a different obj with the same structure', () => { | ||
const original = {a: 1, b: 'two', c:[true], d:{e:'f'}}; | ||
const result = clone(original); | ||
const original = { a: 1, b: 'two', c: [true], d: { e: 'f' } } | ||
const result = clone(original) | ||
expect(result).to.not.equal(original); | ||
expect(result).to.deep.equal(original); | ||
}); | ||
}); | ||
expect(result).to.not.equal(original) | ||
expect(result).to.deep.equal(original) | ||
}) | ||
}) | ||
describe('realClone', () => { | ||
it('should return a different obj with the same structure', () => { | ||
const original = {a: 1, b: 'two', c:[true], d:{e:'f'}}; | ||
const result = realClone(original); | ||
const original = { a: 1, b: 'two', c: [true], d: { e: 'f' } } | ||
const result = realClone(original) | ||
expect(result).to.not.equal(original); | ||
expect(result).to.deep.equal(original); | ||
}); | ||
expect(result).to.not.equal(original) | ||
expect(result).to.deep.equal(original) | ||
}) | ||
it('should return a different Array with the copy of the values', () => { | ||
const original = [{a: 1}, {b: 'two'}, [true], {d:{e:'f'}}]; | ||
const result = realClone(original); | ||
const original = [{ a: 1 }, { b: 'two' }, [true], { d: { e: 'f' } }] | ||
const result = realClone(original) | ||
expect(result).to.not.equal(original); | ||
expect(result).to.deep.equal(original); | ||
}); | ||
expect(result).to.not.equal(original) | ||
expect(result).to.deep.equal(original) | ||
}) | ||
it('should return a clone of the input signalizing the Circular ref', () => { | ||
const someObject = { a: 1, original: '[Circular]' }; | ||
const original = [someObject, {b: 'two'}, [true], {d:{e:'f'}}]; | ||
const someObject = { a: 1, original: '[Circular]' } | ||
const original = [someObject, { b: 'two' }, [true], { d: { e: 'f' } }] | ||
// cloning original before adding Circular ref | ||
const expected = clone(original); | ||
const expected = clone(original) | ||
// adds circular ref | ||
someObject.original = original; | ||
someObject.original = original | ||
const result = realClone(original); | ||
const result = realClone(original) | ||
expect(result).to.not.equal(original); | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
expect(result).to.not.equal(original) | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4241
53
5