expand-object
Advanced tools
Comparing version 0.3.7 to 0.3.8
77
index.js
@@ -13,4 +13,4 @@ 'use strict'; | ||
function expand(str, options) { | ||
options = options || {}; | ||
function expand(str, opts) { | ||
opts = opts || {}; | ||
@@ -21,6 +21,12 @@ if (typeof str !== 'string') { | ||
if (!/[.|:]/.test(str) && /,/.test(str)) { | ||
if (!/[.|:=]/.test(str) && /,/.test(str)) { | ||
return toArray(str); | ||
} | ||
var m; | ||
if ((m = /(\w+[:=]\w+\.)+/.exec(str)) && !/[|,+]/.test(str)) { | ||
var val = m[0].split(':').join('.'); | ||
str = val + str.slice(m[0].length); | ||
} | ||
var arr = splitString(str, '|'); | ||
@@ -31,3 +37,3 @@ var len = arr.length, i = -1; | ||
if (isArrayLike(str) && arr.length === 1) { | ||
return expandArrayObj(str); | ||
return expandArrayObj(str, opts); | ||
} | ||
@@ -45,6 +51,6 @@ | ||
res = set(res, m[1], prop); | ||
} else if (!/[.,\|:]/.test(val)) { | ||
res[val] = options.toBoolean ? true : ''; | ||
} else if (!/[.,\|:=]/.test(val)) { | ||
res[val] = opts.toBoolean ? true : ''; | ||
} else { | ||
res = expandObject(res, val); | ||
res = expandObject(res, val, opts); | ||
} | ||
@@ -55,8 +61,8 @@ } | ||
function setValue(obj, a, b) { | ||
var val = resolveValue(b); | ||
function setValue(obj, a, b, opts) { | ||
var val = resolveValue(b, opts); | ||
if (~String(a).indexOf('.')) { | ||
return set(obj, a, val); | ||
return set(obj, a, typeCast(val)); | ||
} else { | ||
obj[a] = val; | ||
obj[a] = typeCast(val); | ||
} | ||
@@ -66,4 +72,7 @@ return obj; | ||
function resolveValue(val) { | ||
if (typeof val === 'undefined') return ''; | ||
function resolveValue(val, opts) { | ||
opts = opts || {}; | ||
if (typeof val === 'undefined') { | ||
return opts.toBoolean ? true : ''; | ||
} | ||
if (typeof val === 'string' && ~val.indexOf(',')) { | ||
@@ -76,3 +85,3 @@ val = toArray(val); | ||
if (~String(ele).indexOf('.')) { | ||
return setValue({}, ele, ''); | ||
return setValue({}, ele, opts.toBoolean ? true : ''); | ||
} | ||
@@ -85,4 +94,4 @@ return ele; | ||
function expandArray(str) { | ||
var segs = String(str).split(':'); | ||
function expandArray(str, opts) { | ||
var segs = String(str).split(/[:=]/); | ||
var key = segs.shift(); | ||
@@ -92,3 +101,3 @@ var res = {}, val = []; | ||
segs.forEach(function (seg) { | ||
val = val.concat(resolveValue(toArray(seg))); | ||
val = val.concat(resolveValue(toArray(seg), opts)); | ||
}); | ||
@@ -109,3 +118,3 @@ | ||
function expandSiblings(segs) { | ||
function expandSiblings(segs, opts) { | ||
var first = segs.shift(); | ||
@@ -118,3 +127,3 @@ var parts = first.split('.'); | ||
var val = arr.reduce(function (acc, val) { | ||
expandObject(acc, val); | ||
expandObject(acc, val, opts); | ||
return acc; | ||
@@ -128,20 +137,16 @@ }, {}); | ||
function expandObject(res, str) { | ||
function expandObject(res, str, opts) { | ||
var segs = splitString(str, '+'); | ||
if (segs.length > 1) { | ||
return expandSiblings(segs); | ||
return expandSiblings(segs, opts); | ||
} | ||
segs = str.split(':'); | ||
var parts = toArray(segs[1]); | ||
if (parts.length > 1) { | ||
setValue(res, segs[0], parts); | ||
} else { | ||
setValue(res, segs[0], typeCast(segs[1])); | ||
} | ||
segs = str.split(/[:=]/); | ||
setValue(res, segs[0], segs[1]); | ||
return res; | ||
} | ||
function expandArrayObj(str) { | ||
function expandArrayObj(str, opts) { | ||
var m = /\w+:.*:/.exec(str); | ||
if (!m) return expandArray(str); | ||
if (!m) return expandArray(str, opts); | ||
@@ -156,3 +161,3 @@ var i = str.indexOf(':'); | ||
return ~ele.indexOf(':') | ||
? expandObject({}, ele) | ||
? expandObject({}, ele, opts) | ||
: ele; | ||
@@ -164,3 +169,3 @@ }); | ||
return toArray(str).map(function (ele) { | ||
return expandObject({}, ele); | ||
return expandObject({}, ele, opts); | ||
}); | ||
@@ -193,7 +198,9 @@ } | ||
function isRegexString(str) { | ||
return str.charAt(0) === '/' && /\/([gmi]+)?$/.test(str); | ||
return typeof str === 'string' | ||
&& /\/([gmi]+)?$/.test(str) | ||
&& str.charAt(0) === '/'; | ||
} | ||
function isArrayLike(str) { | ||
return /^(?:(\w+:\w+[,:])+)+/.exec(str); | ||
return typeof str === 'string' && /^(?:(\w+[:=]\w+[,:])+)+/.exec(str); | ||
} | ||
@@ -203,3 +210,3 @@ | ||
var obj = {}; | ||
var segs = String(val).split(':'); | ||
var segs = String(val).split(/[:=]/); | ||
obj[segs[0]] = typeCast(segs[1]); | ||
@@ -206,0 +213,0 @@ return obj; |
{ | ||
"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.3.7", | ||
"version": "0.3.8", | ||
"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
14141
256