Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "corridor", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "JSON/DOM data corridor for data-binding", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -59,10 +59,9 @@ /** | ||
// create type-specific value string | ||
value = JSON.stringify(coerce(value, opts.type, opts)); | ||
upwalk(elem, root, function(elem, field, opts) { | ||
if (field !== undefined) { | ||
value = field.replace('$$$', value); | ||
} | ||
}); | ||
// build out full contribution | ||
value = buildup(value, elem, root); | ||
// merge contribution into the result data | ||
merge(data, JSON.parse(value)); | ||
@@ -83,61 +82,58 @@ | ||
// data structure for existing fields | ||
// used to figure out true contribution paths for inserting data into elements | ||
var workspace = {}; | ||
// for each value'd, enabled data-field | ||
slice.call(root.querySelectorAll('[data-field]')) | ||
.filter(hasVal) | ||
.forEach(function(elem) { | ||
var | ||
opts = options(elem, defaults), | ||
target = JSON.stringify("\ufff0"), | ||
queue, | ||
path, | ||
value, | ||
node; | ||
.filter(enabled) | ||
.map(function(elem) { | ||
// build up nested representation and parse it out | ||
upwalk(elem, root, function(elem, field, opts) { | ||
if (field !== undefined) { | ||
target = field.replace('$$$', target); | ||
var target, path; | ||
// build up the target contribution | ||
// starting with "\ufff0" value tag | ||
target = JSON.parse(buildup(JSON.stringify("\ufff0"), elem, root)); | ||
// insert into workspace | ||
merge(workspace, target); | ||
// find path to target in workspace | ||
path = locate(workspace, "\ufff0"); | ||
// set actual val into workspace to prevent false hits for future fields | ||
(function(pathCopy){ | ||
var | ||
opts = options(elem, defaults), | ||
node = workspace; | ||
while (pathCopy.length > 1) { | ||
node = node[pathCopy.shift()]; | ||
} | ||
}); | ||
target = JSON.parse(target); | ||
node[pathCopy[0]] = coerce(val(elem), opts.type, opts) | ||
})(path.slice(0)); | ||
// find path to target value | ||
queue = [[target, []]]; | ||
while (!path && queue.length) { | ||
path = (function(node, stubPath){ | ||
var k, nextPath; | ||
for (k in node) { | ||
nextPath = stubPath.concat([k]); | ||
if (node[k] === "\ufff0") { | ||
return nextPath; | ||
} else { | ||
queue.push([node[k], nextPath]); | ||
} | ||
} | ||
}).apply(null, queue.shift()); | ||
} | ||
// emit path/elem tuple | ||
return {path:path, elem:elem}; | ||
// walk down data object, following path to final node | ||
node = data; | ||
while (node && path.length) { | ||
node = node[path.shift()]; | ||
} | ||
}) | ||
.forEach(function(tuple){ | ||
// last chance for value coercion, then set the val | ||
value = node; | ||
if (value === undefined) { | ||
return; | ||
} else if (opts.type === 'json') { | ||
value = JSON.stringify(value); | ||
} else if (opts.type === 'list' && toString.call(value) === '[object Array]') { | ||
value = (function(cat) { | ||
return ( | ||
!(/[\s,]/).test(cat) ? value.join(' ') : | ||
cat.indexOf(',') === -1 ? value.join(', ') : | ||
value.join("\n") | ||
); | ||
})(value.join('')); | ||
var | ||
// for each path/elem pair | ||
path = tuple.path, | ||
elem = tuple.elem, | ||
opts = options(elem), | ||
// walk down input data object, following path to final node | ||
value = follow(path, data); | ||
if (value !== undefined) { | ||
// last chance for value coercion, then assign val to elem | ||
val(elem, ( | ||
opts.type === 'json' ? JSON.stringify(value) : | ||
opts.type === 'list' ? listify(value) : | ||
value | ||
)); | ||
} | ||
val(elem, value); | ||
@@ -218,2 +214,72 @@ }); | ||
/** | ||
* Walk up the parent chain and perform replacements to build up full contribution. | ||
* @param {string} value Starting value, must be a string. | ||
* @param {HTMLElement} elem The element to start walking up from. | ||
* @param {HTMLElement} root The topmost/stop element (optional). | ||
* @return {string} The built up contribution string. | ||
*/ | ||
buildup = corridor.buildup = function(value, elem, root) { | ||
upwalk(elem, root || null, function(elem, field, opts) { | ||
if (field !== undefined) { | ||
value = field.replace('$$$', value); | ||
} | ||
}); | ||
return value; | ||
}, | ||
/** | ||
* Follow a given path down a data object to find the bottom node. | ||
* If the path can't be followed all the way down, this function returns undefined. | ||
* @param {array} path Array of keys to use to walk down node. | ||
* @param {mixed} node The starting node to traverse down. | ||
* @return {mixed} The final node at the bottom of the path if discoverable. | ||
*/ | ||
follow = corridor.follow = function(path, node) { | ||
while (node && path.length) { | ||
node = node[path.shift()]; | ||
} | ||
return node; | ||
}, | ||
/** | ||
* Starting from a given data node, locate the specified value and report its path. | ||
* @param {mixed} node The starting data node to traverse. | ||
* @param {mixed} value The value to locate (by strict equality check). | ||
* @return {array} An array of keys pointing to the value, or undefined if not found. | ||
*/ | ||
locate = corridor.locate = function(node, value) { | ||
var | ||
queue = [[node, []]], | ||
path; | ||
while (!path && queue.length) { | ||
path = (function(node, stubPath){ | ||
var i, ii, k, nextPath; | ||
if (toString.call(node) === '[object Array]') { | ||
for (i = 0, ii = node.length; i < ii; i++) { | ||
nextPath = stubPath.concat([i]); | ||
if (node[i] === value) { | ||
return nextPath; | ||
} else { | ||
queue.push([node[i], nextPath]); | ||
} | ||
} | ||
} else { | ||
for (k in node) { | ||
nextPath = stubPath.concat([k]); | ||
if (node[k] === value) { | ||
return nextPath; | ||
} else { | ||
queue.push([node[k], nextPath]); | ||
} | ||
} | ||
} | ||
}).apply(null, queue.shift()); | ||
} | ||
return path; | ||
}, | ||
/** | ||
* Figure out whether an element is eligible for inclusion by traversing the DOM. | ||
@@ -278,2 +344,19 @@ * @param {HTMLElement} elem The element to start from. | ||
/** | ||
* Serialize an array into a parsable list string. | ||
* @param {array} arry The array to serialize. | ||
* @return {string} a parsable list string. | ||
*/ | ||
listify = corridor.listify = function(arry) { | ||
if (toString.call(arry) !== '[abject Array]') { | ||
return arry; | ||
} | ||
var cat = arry.join('') | ||
return ( | ||
!(/[\s,]/).test(cat) ? arry.join(' ') : // use whitespace if none present | ||
cat.indexOf(',') === -1 ? arry.join(', ') : // or commas if there are none | ||
arry.join("\n") // last resort, newline delimited | ||
); | ||
}, | ||
/** | ||
* Given a toggleable element, find out if it's toggled off. | ||
@@ -280,0 +363,0 @@ * A toggleable element is an element with "role" set to "toggleable" in its data-opts. |
Sorry, the diff of this file is not supported yet
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
50286
982