Skip to content

Commit

Permalink
Adds templateOpts access via strings (#2)
Browse files Browse the repository at this point in the history
* Allow basic templateOpts access

* Added a test for arrays

* Added in better support for templateOpts and Arrays

* removed the skip

* Added in more tests for objects

* handle
  • Loading branch information
earobinson committed Apr 4, 2019
1 parent 3e921a3 commit b6598a3
Show file tree
Hide file tree
Showing 3 changed files with 343 additions and 9 deletions.
29 changes: 21 additions & 8 deletions lib/jyson.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@ const getKeyFromArrayIndexes = (key, arrayIndexes) => {
return keyFromArrayIndexes;
};

const getKey = (object, key, undefinedValue, arrayIndexes) => {
const jysonValuePath = JysonValue.getPath(key);
const getKey = (object, key, templateOpts, undefinedValue, arrayIndexes) => {
let jysonValuePath = JysonValue.getPath(key);
const jysonValueUndefinedValue = JysonValue.getUndefinedValue(key, undefinedValue);

if(jysonValuePath[0] === '#') {
const indexRegex = /\[([^)]+)\]/;

jysonValuePath = jysonValuePath.substring(1);
const match = indexRegex.exec(jysonValuePath);
if (match) {
const indexValue = getKey(object, match[1], templateOpts, undefinedValue, arrayIndexes);
jysonValuePath = jysonValuePath.replace(indexRegex, `.${indexValue}`);
}

return get(templateOpts, getKeyFromArrayIndexes(jysonValuePath, arrayIndexes), jysonValueUndefinedValue);
}
return get(object, getKeyFromArrayIndexes(jysonValuePath, arrayIndexes), jysonValueUndefinedValue);
};

Expand All @@ -30,26 +43,26 @@ const setValue = (json, key, value) => {
return json[key] = value;
};

const getArrayValueLength = (object, json, arrayIndexes, arrayKey) => {
const getArrayValueLength = (object, json, arrayIndexes, arrayKey, templateOpts) => {
if(JysonValue.isAPath(json)) {
const jysonValuePath = JysonValue.getPath(json);

const arrayLocation = getKeyFromArrayIndexes(jysonValuePath, arrayIndexes).split('.$').shift();

return get(object, `${arrayLocation}.length`, -1);
return getKey(object, `${arrayLocation}.length`, templateOpts, -1, arrayIndexes);
}

if(typeof json === 'object') {
return Object.keys(json).reduce((maxLength, key) => {
return Math.max(maxLength, getArrayValueLength(object, json[key], arrayIndexes, arrayKey));
return Math.max(maxLength, getArrayValueLength(object, json[key], arrayIndexes, arrayKey, templateOpts));
}, -1);
}

throw new Error(`jyson encountered an invalid array at: ${arrayKey}`);
};

const getKeyAndSetValue = (jsonResult, key, path, object, templateOpts, opts) => {
const value = getKey(object, path, opts.undefinedValue, opts.arrayIndexes);
const value = getKey(object, path, templateOpts, opts.undefinedValue, opts.arrayIndexes);

// If we encounter an array without a $ in jyson, we consider that an error
assert.ok(!Array.isArray(value) || path.indexOf('$') !== -1, `jyson encountered an array when it was not expecting one: ${path}`);
Expand All @@ -72,7 +85,7 @@ const fillKeys = (json, object, templateOpts, opts) => {
assert.ok(json[key].length === 1, `jyson template arrays must be of length one at key: ${key}`);

const jysonArrayValue = JysonArray.getValue(json[key]);
const arrayValueLength = getArrayValueLength(object, jysonArrayValue, opts.arrayIndexes, key);
const arrayValueLength = getArrayValueLength(object, jysonArrayValue, opts.arrayIndexes, key, templateOpts);
const arrayIndexesIndex = opts.arrayIndexes.length;
const arrayValue = [];

Expand All @@ -89,7 +102,7 @@ const fillKeys = (json, object, templateOpts, opts) => {

let result;
if(JysonValue.isAPath(jysonArrayValue)) {
result = setValue(jsonResult, key, getKey(object, jysonArrayValue, opts.undefinedValue, opts.arrayIndexes));
result = setValue(jsonResult, key, getKey(object, jysonArrayValue, templateOpts, opts.undefinedValue, opts.arrayIndexes));
} else {
result = fillKeys(jysonArrayValue, object, templateOpts, opts);
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b6598a3

Please sign in to comment.