expand-object
Advanced tools
Comparing version 0.1.4 to 0.2.0
98
index.js
@@ -1,8 +0,1 @@ | ||
/*! | ||
* expand-object <https://github.com/jonschlinkert/expand-object> | ||
* | ||
* Copyright (c) 2015, Jon Schlinkert. | ||
* Licensed under the MIT License. | ||
*/ | ||
'use strict'; | ||
@@ -12,3 +5,10 @@ | ||
module.exports = function expand(str) { | ||
/** | ||
* Expand the given string into an object. | ||
* | ||
* @param {String} `str` | ||
* @return {Object} | ||
*/ | ||
function expand(str) { | ||
if (typeof str !== 'string') { | ||
@@ -18,30 +18,72 @@ throw new TypeError('expand-object expects a string.'); | ||
if (!/[.|]/.test(str) && /,/.test(str)) { | ||
return expandArr(str); | ||
if (!/[.|:]/.test(str) && /,/.test(str)) { | ||
return str.split(','); | ||
} | ||
var segs = str.split('|'); | ||
var len = segs.length, i = 0; | ||
var o = {}; | ||
var arr = str.split('|'); | ||
var len = arr.length, i = -1; | ||
var res = {}; | ||
while (len--) { | ||
var key = segs[i++]; | ||
var val = ''; | ||
var tmp = key.split(':'); | ||
if (tmp.length > 1) { | ||
key = tmp[0]; | ||
val = tmp[1]; | ||
val = expandArr(val); | ||
if (isArrayLike(str) && arr.length === 1) { | ||
return expandArray(str); | ||
} | ||
while (++i < len) { | ||
var val = arr[i]; | ||
if (!/[.,|:]/.test(val)) { | ||
res[val] = ''; | ||
} else { | ||
expandObject(res, val); | ||
} | ||
set(o, key, val); | ||
} | ||
return o; | ||
}; | ||
return res; | ||
} | ||
function expandArr(val) { | ||
var arr = val.split(','); | ||
if (arr.length > 1) { | ||
val = arr; | ||
function setValue(obj, a, b) { | ||
var val = resolveValue(b || ''); | ||
if (~a.indexOf('.')) { | ||
return set(obj, a, val); | ||
} else { | ||
obj[a] = val; | ||
} | ||
return obj; | ||
} | ||
function resolveValue(val) { | ||
if (Array.isArray(val)) { | ||
return val.map(function (ele) { | ||
if (~ele.indexOf('.')) { | ||
return setValue({}, ele, ''); | ||
} | ||
return ele; | ||
}); | ||
} | ||
return val; | ||
} | ||
function expandArray(str) { | ||
return str.split(',').map(function (ele) { | ||
return expandObject({}, ele); | ||
}); | ||
} | ||
function expandObject(res, str) { | ||
var segs = str.split(':'); | ||
var parts = (segs[1] || '').split(','); | ||
if (parts.length > 1) { | ||
setValue(res, segs[0], parts); | ||
} else { | ||
setValue(res, segs[0], segs[1]); | ||
} | ||
return res; | ||
} | ||
function isArrayLike(str) { | ||
return /^(?:\w+[,:]\w+[,:])+/.test(str); | ||
} | ||
/** | ||
* Expose `expand` | ||
*/ | ||
module.exports = expand; |
{ | ||
"name": "expand-object", | ||
"description": "Expand a string into a JavaScript object using a simple notation. Use the CLI or as a node.js lib.", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/jonschlinkert/expand-object", | ||
@@ -6,0 +6,0 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", |
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
8750
112