collapse-lib
Advanced tools
Comparing version 1.0.1 to 1.0.2
195
collapse.js
@@ -6,124 +6,129 @@ /* eslint no-empty: 0 */ | ||
var CollapseLib = { | ||
/** | ||
* | ||
* @warning allowed types for complexDict : {}, [], JS primitives (int, float, or string) | ||
* | ||
* @param complexDict : a JS object that might have inner JS objects and arrays OR | ||
* a JS array that might have inner JS objects and arrays OR | ||
* @param plainDict : a one-level collapse JS object | ||
*/ | ||
/** | ||
* | ||
* @warning allowed types for complexObj : {}, [], JS primitives (int, float, or string) | ||
* | ||
* @param complexObj : a JS object that might have inner JS objects and arrays OR | ||
* a JS array that might have inner JS objects and arrays OR | ||
* @param plainObj : a one-level collapse JS object | ||
*/ | ||
// if you have an empty object, just return an empty object | ||
collapseDict: function(complexDict) { | ||
// make plain object to return | ||
var plainDict = {}, | ||
sawComplex = false, | ||
subDict; | ||
// if you have an empty object, just return an empty object | ||
function collapseObj(complexObj) { | ||
// make plain object to return | ||
var plainObj = {}, | ||
sawComplex = false, | ||
subObj; | ||
if (CollapseLib.isPlainObject(complexDict)) { | ||
// if complexDict is a JS object | ||
sawComplex = false; | ||
for (var complexKey in complexDict) { | ||
// if complexDict[complexKey] is an inner dict | ||
if (CollapseLib.isPlainObject(complexDict[complexKey])) { | ||
if (CollapseLib.isEmptyObject(complexDict[complexKey])) { | ||
return complexDict; | ||
} | ||
if (isPlainObject(complexObj)) { | ||
// if complexObj is a JS object | ||
sawComplex = false; | ||
for (var complexKey in complexObj) { | ||
// if complexObj[complexKey] is an inner obj | ||
if (isPlainObject(complexObj[complexKey])) { | ||
if (isEmptyObject(complexObj[complexKey])) { | ||
return complexObj; | ||
} | ||
subDict = complexDict[complexKey]; | ||
subObj = complexObj[complexKey]; | ||
sawComplex = true; | ||
for (var subKey in subObj) { | ||
plainObj[complexKey + '.' + subKey] = collapseObj(subObj[subKey]); | ||
} | ||
} else if (Array.isArray && Array.isArray(complexObj[complexKey])) { | ||
if (!isComplexArray(complexObj[complexKey])) { | ||
plainObj[complexKey] = getStrFromArray(complexObj[complexKey]); | ||
} else { | ||
sawComplex = true; | ||
for (var subKey in subDict) { | ||
plainDict[complexKey + '.' + subKey] = CollapseLib.collapseDict(subDict[subKey]); | ||
for (var i = 0; i < complexObj[complexKey].length; i++) { | ||
plainObj[complexKey + '[' + i + ']'] = collapseObj(complexObj[complexKey][i]); | ||
} | ||
} else if (Array.isArray && Array.isArray(complexDict[complexKey])) { | ||
if (!CollapseLib.isComplexArray(complexDict[complexKey])) { | ||
plainDict[complexKey] = CollapseLib.getStrFromArray(complexDict[complexKey]); | ||
} else { | ||
sawComplex = true; | ||
for (var i = 0; i < complexDict[complexKey].length; i++) { | ||
plainDict[complexKey + '[' + i + ']'] = CollapseLib.collapseDict(complexDict[complexKey][i]); | ||
} | ||
} | ||
} else { | ||
plainDict[complexKey] = CollapseLib.collapseDict(complexDict[complexKey]); | ||
} | ||
} | ||
if (sawComplex) { | ||
return CollapseLib.collapseDict(plainDict); | ||
} else { | ||
return plainDict; | ||
plainObj[complexKey] = collapseObj(complexObj[complexKey]); | ||
} | ||
} | ||
if (sawComplex) { | ||
return collapseObj(plainObj); | ||
} else { | ||
if (Array.isArray && Array.isArray(complexDict)) { | ||
plainDict = {}; | ||
return plainObj; | ||
} | ||
} else { | ||
if (Array.isArray && Array.isArray(complexObj)) { | ||
plainObj = {}; | ||
// if complexDict is an array | ||
// that contains an inner array or inner plain object | ||
if (CollapseLib.isComplexArray(complexDict)) { | ||
for (var i = 0; i < complexDict.length; i++) { | ||
plainDict['[' + i + ']'] = CollapseLib.collapseDict(complexDict[i]); | ||
} | ||
// if complexObj is an array | ||
// that contains an inner array or inner plain object | ||
if (isComplexArray(complexObj)) { | ||
for (var j = 0; j < complexObj.length; j++) { | ||
plainObj['[' + j + ']'] = collapseObj(complexObj[j]); | ||
} | ||
return CollapseLib.collapseDict(plainDict); | ||
} else { | ||
return complexDict.toString(); | ||
} | ||
return collapseObj(plainObj); | ||
} else { | ||
return complexObj.toString(); | ||
} | ||
} | ||
} | ||
, | ||
isComplexArray: function(arr) { | ||
for (var i = 0; i < arr.length; i++) { | ||
if ((Array.isArray && Array.isArray(arr[i])) || CollapseLib.isPlainObject(arr[i])) { | ||
return true; | ||
} | ||
function isComplexArray(arr) { | ||
for (var i = 0; i < arr.length; i++) { | ||
if ((Array.isArray && Array.isArray(arr[i])) || isPlainObject(arr[i])) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
return false; | ||
} | ||
, | ||
getStrFromArray: function(arr) { | ||
return arr.toString(); | ||
function getStrFromArray(arr) { | ||
return arr.toString(); | ||
} | ||
function isPlainObject(obj) { | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
// Must be an Object. | ||
// Because of IE, we also have to check the presence of the constructor property. | ||
// Make sure that DOM nodes and window objects don't pass through, as well | ||
if (!obj || typeof obj !== 'object' || obj.nodeType) { | ||
return false; | ||
} | ||
, | ||
isPlainObject: function(obj) { | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
// Must be an Object. | ||
// Because of IE, we also have to check the presence of the constructor property. | ||
// Make sure that DOM nodes and window objects don't pass through, as well | ||
if (!obj || typeof obj !== 'object' || obj.nodeType) { | ||
try { | ||
// Not own constructor property must be Object | ||
if (obj.constructor && | ||
!hasOwn.call(obj, 'constructor') && | ||
!hasOwn.call(obj.constructor.prototype, 'isPrototypeOf')) { | ||
return false; | ||
} | ||
} catch (e) { | ||
// IE8,9 Will throw exceptions on certain host objects #9897 | ||
return false; | ||
} | ||
try { | ||
// Not own constructor property must be Object | ||
if (obj.constructor && | ||
!hasOwn.call(obj, 'constructor') && | ||
!hasOwn.call(obj.constructor.prototype, 'isPrototypeOf')) { | ||
return false; | ||
} | ||
} catch (e) { | ||
// IE8,9 Will throw exceptions on certain host objects #9897 | ||
// Own properties are enumerated firstly, so to speed up, | ||
// if last one is own, then all properties are own. | ||
var key; | ||
for (key in obj) {} | ||
return key === undefined || hasOwn.call(obj, key); | ||
} | ||
function isEmptyObject(obj) { | ||
for (var i in obj) { | ||
if (obj.hasOwnProperty(i)) { | ||
return false; | ||
} | ||
// Own properties are enumerated firstly, so to speed up, | ||
// if last one is own, then all properties are own. | ||
var key; | ||
for (key in obj) {} | ||
return key === undefined || hasOwn.call(obj, key); | ||
} | ||
return true; | ||
} | ||
, | ||
isEmptyObject: function(obj) { | ||
for (var i in obj) { | ||
if (obj.hasOwnProperty(i)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* Public methods exported | ||
*/ | ||
module.exports = { | ||
collapseObj: collapseObj, | ||
isComplexArray: isComplexArray, | ||
getStrFromArray: getStrFromArray, | ||
isPlainObject: isPlainObject, | ||
isEmptyObject: isEmptyObject | ||
}; |
{ | ||
"name": "collapse-lib", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Library to collapse an multi-level object into one level object, using dot notation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
129
6979