Comparing version 0.1.2 to 0.1.3
44
main.js
@@ -308,2 +308,46 @@ /* global Blob File FormData $ */ | ||
}) | ||
}, | ||
/** | ||
* flat keys in object | ||
* @param {object} obj | ||
* @returns {object} | ||
* @example { a: { a1: 1, a2: 2 }, b: 3 } >> { 'a.a1': 1, 'a.a2': 2, 'b': 3 } | ||
*/ | ||
flat: function (obj) { | ||
const _flat = {} | ||
const _f = function (obj, base) { | ||
for (const key in obj) { | ||
try { | ||
if (obj[key].constructor == Object) { | ||
_f(obj[key], base + key + '.') | ||
} else { | ||
_flat[base + key] = obj[key] | ||
} | ||
} catch (e) { | ||
_flat[base + key] = obj[key] | ||
} | ||
} | ||
} | ||
_f(obj, '') | ||
return _flat | ||
}, | ||
/** | ||
* get value in object using a flat key | ||
* @param {object} obj | ||
* @param {string} fkey | ||
* @returns {object} | ||
* @example tools.object.getByFlatKey({ a: { b: {c: 1} } }, 'a.b.c') >> 1 | ||
*/ | ||
getByFlatKey: function (obj, fkey) { | ||
let _path = fkey.split('.') | ||
let _walk = obj | ||
for (let i = 0; i < _path.length; i++) { | ||
if (!_walk[_path[i]]) { | ||
return undefined | ||
} | ||
_walk = _walk[_path[i]] | ||
} | ||
return _walk | ||
} | ||
@@ -310,0 +354,0 @@ }, |
{ | ||
"name": "a-toolbox", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "basic lightweight tools", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
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
22450
590