Skip to content

Commit

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

* 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

* bumped package version

* updated the repo to reflect new location
  • Loading branch information
earobinson committed Apr 9, 2019
1 parent 3e921a3 commit 87be856
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 26 deletions.
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jyson.buildTemplateFunction({
'd': new jyson.Value({ path: 'd' })
});
```
- Closes [Issue #10](https://github.com/hubba/jyson/issues/10)
- Closes [Issue #10](https://github.com/earobinson/jyson/issues/10)

## v3.0.1
- Bug fixes
Expand All @@ -108,19 +108,19 @@ jyson.buildTemplateFunction({
}]
});
```
- Closes [Issue #22](https://github.com/hubba/jyson/issues/22)
- Closes [Issue #22](https://github.com/earobinson/jyson/issues/22)

## v2.0.0
- Jyson no longer throws an error to handle arrays it reads ahead to determine array length
- A Jyson template can now access two different arrays
- Closes [Issue #15](https://github.com/hubba/jyson/issues/15)
- Closes [Issue #15](https://github.com/earobinson/jyson/issues/15)

## v1.3.0
- Made jyson less dependent on lodash
- Closes [Issue #13](https://github.com/hubba/jyson/issues/13)
- Closes [Issue #13](https://github.com/earobinson/jyson/issues/13)

## v1.2.1
- Fixed a bug when if an array was not provide when it was in the template jyson would crash

## v1.2.0
- jyson now supports an array of objects Issue [Issue #9](https://github.com/hubba/jyson/issues/9)
- jyson now supports an array of objects Issue [Issue #9](https://github.com/earobinson/jyson/issues/9)
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behaviour may be
reported by contacting the project team at [oss@hubba.com]. All
reported by contacting the project team at [os@earobinson.net]. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ process easy and effective for everyone involved.

## Using the issue tracker

[GitHub Issues](https://github.com/hubba/jyson/issues) is the preferred channel
[GitHub Issues](https://github.com/earobinson/jyson/issues) is the preferred channel
for [bug reports](#bug-reports), [features requests](#feature-requests)
and [submitting pull requests](#pull-requests).

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Hubba
Copyright (c) 2017 Edward Andrew Robinson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
This package lets you generate fast json templates for your apis. It lets you quickly build powerful api templates.

[![NPM Version](https://img.shields.io/npm/v/jyson.svg)](https://www.npmjs.com/package/jyson)
[![Travis Build Status](https://img.shields.io/travis/hubba/jyson.svg)](https://travis-ci.org/hubba/jyson)
[![Coveralls](https://img.shields.io/coveralls/github/hubba/jyson.svg)](https://coveralls.io/github/hubba/jyson)
[![Travis Build Status](https://img.shields.io/travis/earobinson/jyson.svg)](https://travis-ci.org/earobinson/jyson)
[![Coveralls](https://img.shields.io/coveralls/github/earobinson/jyson.svg)](https://coveralls.io/github/earobinson/jyson)
[![NPM Downloads](https://img.shields.io/npm/dm/jyson.svg)](https://www.npmjs.com/package/jyson)

## Install
Expand All @@ -14,7 +14,7 @@ npm install jyson --save

## Usage

jyson can create many different types of templates, for a full list of examples check out the [example tests](https://github.com/hubba/jyson/blob/master/spec/lib/jyson/jyson.example.spec.js).
jyson can create many different types of templates, for a full list of examples check out the [example tests](https://github.com/earobinson/jyson/blob/master/spec/lib/jyson/jyson.example.spec.js).

```js
const jyson = require('jyson');
Expand Down Expand Up @@ -73,4 +73,4 @@ for opening issues, coding standards, and notes on development.

***

Built with ❤️ at [Hubba](https://www.hubba.com?utm_campaign=hubba_oss).
Built with ❤️ at [earobinson](https://www.earobinson.com?utm_campaign=earobinson_oss).
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.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jyson",
"version": "4.0.1",
"version": "4.1.0",
"description": "A template engine for json.",
"main": "index.js",
"scripts": {
Expand All @@ -12,18 +12,18 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/hubba/jyson.git"
"url": "git+https://github.com/earobinson/jyson.git"
},
"keywords": [
"json",
"template"
],
"author": "Edward Andrew Robinson <earobinson@hubba.com>",
"author": "Edward Andrew Robinson <os@earobinson.net>",
"license": "MIT",
"bugs": {
"url": "https://github.com/hubba/jyson/issues"
"url": "https://github.com/earobinson/jyson/issues"
},
"homepage": "https://github.com/hubba/jyson#readme",
"homepage": "https://github.com/earobinson/jyson#readme",
"devDependencies": {
"@ear/eslint-config": "^1.2.0",
"coveralls": "^3.0.2",
Expand Down
Loading

0 comments on commit 87be856

Please sign in to comment.