Comparing version 0.2.3 to 0.2.5
@@ -22,3 +22,3 @@ <a name="module_JGL"></a> | ||
* [JGL.relative(from, to)](#module_JGL.relative) | ||
* [JGL.resolve(graph, query)](#module_JGL.resolve) | ||
* [JGL.resolve(pathValues)](#module_JGL.resolve) | ||
* [JGL.segmentContains(segment, key)](#module_JGL.segmentContains) | ||
@@ -147,7 +147,6 @@ * [JGL.segmentKeys(segment)](#module_JGL.segmentKeys) | ||
<a name="module_JGL.resolve"></a> | ||
####JGL.resolve(graph, query) | ||
####JGL.resolve(pathValues) | ||
**Params** | ||
- graph `Object` - The object to traverse | ||
- query <code>[Query](#module_JGL..Query)</code> - The query paths to follow | ||
- pathValues <code>[Array.<PathValue>](#module_JGL..PathValue)</code> | ||
@@ -154,0 +153,0 @@ **Returns**: [Array.<PathValue>](#module_JGL..PathValue) |
@@ -0,1 +1,2 @@ | ||
var _ = require('underscore'); | ||
/** | ||
@@ -63,3 +64,3 @@ * An object that can be expanded into a series of indices | ||
*/ | ||
module.exports = /** @lends module:JGL. */{ | ||
_.extend(exports,/** @lends module:JGL. */{ | ||
get: require('./get'), | ||
@@ -85,3 +86,3 @@ set: require('./set'), | ||
ERROR_KEY: require('./error-key') | ||
}; | ||
}); | ||
var relative = require('./relative'), | ||
JGL = require('./index'); | ||
JGL = require('./'); | ||
/** | ||
@@ -11,3 +11,12 @@ * @member module:JGL.isRelative | ||
module.exports = function isRelative (from, to) { | ||
var relpath = relative(from, to); | ||
var relpath; | ||
if (from.length === 0) { | ||
return true; | ||
} | ||
if (to.length < from.length) { | ||
return false; | ||
} | ||
/** | ||
@@ -20,4 +29,4 @@ * The last path that was generated by {@link module:JGL.isRelative}. | ||
*/ | ||
JGL.LAST_RELATIVE_PATH = relpath; | ||
return relpath.length < from.length - to.length; | ||
JGL.LAST_RELATIVE_PATH = relative(from, to); | ||
return JGL.LAST_RELATIVE_PATH.length === to.length - from.length; | ||
}; |
@@ -11,2 +11,3 @@ var segmentContains = require('./segment-contains'); | ||
var len = from.length, | ||
flag = true, | ||
i = 0; | ||
@@ -16,2 +17,3 @@ | ||
if (!segmentContains(from[i], to[i])) { | ||
flag = false; | ||
break; | ||
@@ -21,4 +23,4 @@ } | ||
return to.slice(i !== from.length ? 0 : i); | ||
return to.slice(flag ? from.length : 0); | ||
}; | ||
@@ -1,29 +0,47 @@ | ||
var traverse = require('./traverse'), | ||
explode = require('./explode'); | ||
var JGL = require('./'), | ||
pathValueIsRef = require('./path-value-is-ref'), | ||
pathValueIsError = require('./path-value-is-error'), | ||
isRelative = require('./is-relative'), | ||
relative = require('./relative'), | ||
REF_KEY = require('./ref-key'); | ||
/** | ||
* @member module:JGL.resolve | ||
* @function | ||
* @param {Object} graph The object to traverse | ||
* @param {module:JGL~Query} query The query paths to follow | ||
* @param {Array<module:JGL~PathValue>} pathValues | ||
* @returns {Array<module:JGL~PathValue>} | ||
*/ | ||
module.exports = function resolve (graph, query) { | ||
return explode(query). | ||
reduce(function (acc, path) { | ||
// Add null to punch-through references | ||
var pvs = traverse(graph, path.concat(null)), | ||
len = pvs.length, | ||
lastIdx = len - 1, | ||
lastPv = pvs[lastIdx], | ||
pv; | ||
module.exports = function resolve (pathValues) { | ||
var path, ref; | ||
acc = acc.concat(pvs.slice(0, lastIdx)); | ||
if (len > 1 && lastPv.value !== undefined) { | ||
pv = { path: path, value: lastPv.value }; | ||
pv.value['@path'] = lastPv.path; | ||
return pathValues. | ||
reduce(function (acc, pv) { | ||
if (pathValueIsError(pv)) { | ||
if (ref) { | ||
if (isRelative(ref, pv.path)) { | ||
path = path.concat(JGL.LAST_RELATIVE_PATH); | ||
} | ||
acc.push({ path: path, value: pv.value }); | ||
ref = null; | ||
} | ||
else { | ||
acc.push(pv); | ||
} | ||
} | ||
else if (pathValueIsRef(pv)) { | ||
path = ref && isRelative(ref, pv.path) ? | ||
path.concat(JGL.LAST_RELATIVE_PATH) : | ||
pv.path.slice(); | ||
ref = pv.value[REF_KEY]; | ||
} | ||
else { | ||
path = path ? | ||
path.concat(ref ? relative(ref, pv.path) : pv.path) : | ||
pv.path; | ||
acc.push({ path: path, value: pv.value }); | ||
path = null; | ||
ref = null; | ||
} | ||
return acc.concat(pv ? pv : lastPv); | ||
return acc; | ||
}, []); | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "A library for traversing cyclic JSON Graphs", | ||
"version": "0.2.3", | ||
"version": "0.2.5", | ||
"keywords": ["json", "graph", "path", "query"], | ||
@@ -8,0 +8,0 @@ "main": "./lib/index.js", |
30022
543