deep-clone-map
Advanced tools
Comparing version 1.0.1 to 1.1.0
const Benchmark = require('benchmark') | ||
const deepCloneMap = require('deep-clone-map').default | ||
const deepMap = require('deep-map') | ||
const mapObj = require('map-obj') | ||
const testData = require('./test-data') | ||
const { isNode, addHtmlRow } = require('./util') | ||
const { addHtmlRow } = require('./util') | ||
@@ -18,8 +16,2 @@ if (global && !global.Benchmark) { | ||
}) | ||
.add('deep-map#Array', function () { | ||
deepMap(testData.arr, val => val + 1) | ||
}) | ||
.add('map-obj#Array', function () { | ||
mapObj(testData.arr, (key, value) => [value + 1, key]) | ||
}) | ||
.on('cycle', function (event) { | ||
@@ -26,0 +18,0 @@ console.log(String(event.target)) |
const Benchmark = require('benchmark') | ||
const deepCloneMap = require('deep-clone-map').default | ||
const deepCloneMap = require('../dist/main').default | ||
const deepMap = require('deep-map') | ||
const mapObj = require('map-obj') | ||
const testData = require('./test-data') | ||
const { isNode, addHtmlRow } = require('./util') | ||
const { mapObj2 } = require('./ver2') | ||
const { addHtmlRow } = require('./util') | ||
@@ -16,14 +15,11 @@ if (global && !global.Benchmark) { | ||
suite | ||
// .add('deep-map#Object', function () { | ||
// deepMap(testData.obj, val => val + 1) | ||
// }) | ||
// .add('map-obj#Object', function () { | ||
// mapObj(testData.obj, (key, value) => [value + 1, key], { deep: true }) | ||
// }) | ||
// .add('deep-clone-map#Object', function () { | ||
// deepCloneMap(testData.obj, val => val + 1) | ||
// }) | ||
.add('map-objsasasa#Object', function () { | ||
mapObj2(testData.obj, val => val + 1) | ||
.add('deep-clone-map#Object', function () { | ||
deepCloneMap(testData.obj, val => val + 1) | ||
}) | ||
.add('deep-map#Object', function () { | ||
deepMap(testData.obj, val => val + 1) | ||
}) | ||
.add('map-obj#Object', function () { | ||
mapObj(testData.obj, (key, value) => [value + 1, key], { deep: true }) | ||
}) | ||
.on('cycle', function (event) { | ||
@@ -30,0 +26,0 @@ console.log(String(event.target)) |
@@ -16,3 +16,2 @@ { | ||
"benchmark": "^2.1.4", | ||
"deep-clone-map": "file:../", | ||
"deep-map": "^2.0.0", | ||
@@ -19,0 +18,0 @@ "es6-weak-map": "^2.0.3", |
@@ -77,26 +77,15 @@ module.exports.arr = [ | ||
b: 2, | ||
c: [1, 2, 3, 4], | ||
d: { | ||
c: { | ||
a: 1, | ||
b: 2, | ||
c: [1, 2, 3, 4], | ||
d: { | ||
c: { | ||
a: 1, | ||
b: 2, | ||
c: [1, 2, 3, 4], | ||
d: { | ||
c: { | ||
a: 1, | ||
b: 2, | ||
c: [ | ||
{ | ||
a: 1, | ||
b: 2, | ||
c: [1, 2, 3, 4], | ||
}, | ||
{ | ||
a: 1, | ||
b: 2, | ||
c: [1, 2, 3, 4], | ||
}, | ||
], | ||
c: { | ||
a: 1, | ||
b: 2, | ||
}, | ||
}, | ||
@@ -103,0 +92,0 @@ }, |
declare type Param = object | any[] | string | number | null; | ||
declare type Callback = (arg0: any, arg1?: string) => any; | ||
declare const _default: <T = Param>(param: T, callback?: Callback) => T; | ||
export default _default; | ||
declare function deepCloneMap<T = Param>(o: T, cb?: Callback): any[] | T; | ||
export default deepCloneMap; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var traverse = function (obj, callback, prevKey) { | ||
if (callback === void 0) { callback = function (val) { return val; }; } | ||
if (prevKey === void 0) { prevKey = ''; } | ||
if (!obj || typeof obj === 'number' || typeof obj === 'string') { | ||
return callback(obj, prevKey); | ||
} | ||
for (var key in obj) { | ||
var previousKey = prevKey ? prevKey + '.' + key : key; | ||
if (obj[key].constructor === Array) { | ||
var len = obj[key].length; | ||
var i = 0; | ||
var newArr = []; | ||
while (i < len) { | ||
// for (let i = len - 1; i >= 0; i--) { | ||
// for (let i = 0; i < len; i++) { | ||
var previousArrKey = previousKey ? previousKey + '.' + i : previousKey; | ||
var mapedValue = traverse(obj[key][i], callback, previousArrKey); | ||
if (mapedValue) { | ||
newArr.push(mapedValue); | ||
} | ||
else { | ||
newArr[i] = obj[key][i]; | ||
} | ||
i++; | ||
function deepCloneMap(o, cb = val => val) { | ||
// @ts-ignore | ||
const nObj = o.constructor === Array ? [...o] : { ...o }; | ||
(function t(obj, prevKey = '') { | ||
// @ts-ignore | ||
for (const key in obj) { | ||
const previousKey = prevKey ? prevKey + '.' + key : key; | ||
if (obj[key] && typeof obj[key] === 'object') { | ||
obj[key] = | ||
obj[key].constructor === Array ? [...obj[key]] : { ...obj[key] }; | ||
t(obj[key], previousKey); | ||
} | ||
obj[key] = newArr; | ||
} | ||
else if (typeof obj[key] === 'object') { | ||
if (obj[key]) { | ||
obj[key] = __assign({}, obj[key]); | ||
traverse(obj[key], callback, previousKey); | ||
else { | ||
obj[key] = cb(obj[key], previousKey); | ||
} | ||
} | ||
else { | ||
obj[key] = callback(obj[key], previousKey); | ||
} | ||
} | ||
}; | ||
exports.default = (function (param, callback) { | ||
if (!param || typeof param === 'number' || typeof param === 'string') { | ||
return callback(param); | ||
} | ||
var paramToParse = param.constructor === Array ? __spread(param) : __assign({}, param); | ||
traverse(paramToParse, callback); | ||
return paramToParse; | ||
}); | ||
})(nObj); | ||
return nObj; | ||
} | ||
exports.default = deepCloneMap; | ||
//# sourceMappingURL=main.js.map |
@@ -1,2 +0,2 @@ | ||
// import deepCloneMap from '.' | ||
import deepCloneMap from '.' | ||
import { | ||
@@ -14,36 +14,3 @@ testObj1, | ||
} from './test-data' | ||
import deepCloneMap from './benchmark/node_modules/deep-map' | ||
// const deepCloneMap = (obj, cb) => | ||
// deepMap(obj, (k, v) => [cb(v), k], { deep: true }) | ||
// function deepCloneMap(obj, cb = val => val, nObj = new Object()) { | ||
// for (const key in obj) { | ||
// if (obj[key] && typeof obj[key] === 'object') { | ||
// nObj[key] = deepCloneMap(obj[key], cb, nObj[key]) | ||
// } else { | ||
// nObj[key] = cb(obj[key]) | ||
// } | ||
// } | ||
// return nObj | ||
// } | ||
// function deepCloneMap(o, cb = val => val) { | ||
// const nObj = {} | ||
// ;(function t(obj) { | ||
// for (const key in obj) { | ||
// if (obj[key] && typeof obj[key] === 'object') { | ||
// nObj[key] = obj[key] | ||
// t(nObj[key]) | ||
// } else { | ||
// nObj[key] = cb(obj[key]) | ||
// } | ||
// } | ||
// })(o) | ||
// return nObj | ||
// } | ||
describe('objects', () => { | ||
@@ -50,0 +17,0 @@ it('should parse and map a deeply nested object', () => { |
65
main.ts
@@ -5,55 +5,24 @@ type Param = object | any[] | string | number | null | ||
const traverse = ( | ||
obj: Param, | ||
callback: Callback = val => val, | ||
prevKey = '', | ||
) => { | ||
if (!obj || typeof obj === 'number' || typeof obj === 'string') { | ||
return callback(obj, prevKey) | ||
} | ||
function deepCloneMap<T = Param>(o: T, cb: Callback = val => val) { | ||
// @ts-ignore | ||
const nObj = o.constructor === Array ? [...o] : { ...o } | ||
for (const key in obj) { | ||
const previousKey = prevKey ? prevKey + '.' + key : key | ||
;(function t(obj, prevKey = '') { | ||
// @ts-ignore | ||
for (const key in obj) { | ||
const previousKey = prevKey ? prevKey + '.' + key : key | ||
if (obj[key].constructor === Array) { | ||
const len = obj[key].length | ||
let i = 0 | ||
const newArr = [] | ||
while (i < len) { | ||
const previousArrKey = previousKey ? previousKey + '.' + i : previousKey | ||
const mapedValue = traverse(obj[key][i], callback, previousArrKey) | ||
if (mapedValue) { | ||
newArr.push(mapedValue) | ||
} else { | ||
newArr[i] = obj[key][i] | ||
} | ||
i++ | ||
if (obj[key] && typeof obj[key] === 'object') { | ||
obj[key] = | ||
obj[key].constructor === Array ? [...obj[key]] : { ...obj[key] } | ||
t(obj[key], previousKey) | ||
} else { | ||
obj[key] = cb(obj[key], previousKey) | ||
} | ||
obj[key] = newArr | ||
} else if (typeof obj[key] === 'object') { | ||
if (obj[key]) { | ||
obj[key] = { ...obj[key] } | ||
traverse(obj[key], callback, previousKey) | ||
} | ||
} else { | ||
obj[key] = callback(obj[key], previousKey) | ||
} | ||
} | ||
})(nObj) | ||
return nObj | ||
} | ||
export default <T = Param>(param: T, callback?: Callback): T => { | ||
if (!param || typeof param === 'number' || typeof param === 'string') { | ||
return callback(param) | ||
} | ||
const paramToParse = | ||
param.constructor === Array ? [...(param as any)] : { ...param } | ||
traverse(paramToParse as Param, callback) | ||
return paramToParse as T | ||
} | ||
export default deepCloneMap |
{ | ||
"name": "deep-clone-map", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Deep clone and map complex nested objects", | ||
@@ -5,0 +5,0 @@ "main": "dist/main.js", |
@@ -5,3 +5,3 @@ { | ||
"esModuleInterop": true, | ||
"target": "es5", | ||
"target": "es2020", | ||
"moduleResolution": "node", | ||
@@ -15,3 +15,3 @@ "sourceMap": true, | ||
}, | ||
"lib": ["es2015", "es5"], | ||
"lib": ["es2020", "es2015", "es5"], | ||
"include": [ | ||
@@ -18,0 +18,0 @@ "main.ts" |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
8
1429101
22
42029