Comparing version 0.1.0 to 0.1.1
/* | ||
* json-sharp - v0.1.0 | ||
* json-sharp - v0.1.1 | ||
* Process operations on pure JSON objects | ||
@@ -68,3 +68,3 @@ * | ||
// AMD. Register as an anonymous module. | ||
define(['deepmerge'], factory); | ||
define(['deepmerge', 'JSONPath'], factory); | ||
} else if (typeof module === 'object' && module.exports) { | ||
@@ -74,8 +74,8 @@ // Node. Does not work with strict CommonJS, but | ||
// like Node. | ||
module.exports = factory(require('deepmerge')); | ||
module.exports = factory(require('deepmerge'), require('JSONPath')); | ||
} else { | ||
// Browser globals (root is window) | ||
root.returnExports = factory(root.deepmerge); | ||
root.returnExports = factory(root.deepmerge, root.JSONPath); | ||
} | ||
}(this, function (deepmerge) { /* jshint ignore:line */ | ||
}(this, function (deepmerge, JSONPath) { /* jshint ignore:line */ | ||
var JSONSharp = { | ||
@@ -132,2 +132,8 @@ process: function (obj, context) { | ||
}, | ||
resolveProperty: function (property, context) { | ||
if (property.indexOf('$.') !== 0) { | ||
return context[property]; | ||
} | ||
return JSONPath.eval(context, property); | ||
}, | ||
operations: { | ||
@@ -139,3 +145,3 @@ '#merge': function (obj) { | ||
var result; | ||
var value = context[obj['#property']]; | ||
var value = JSONSharp.resolveProperty(obj['#property'], context); | ||
var options = obj['#case']; | ||
@@ -142,0 +148,0 @@ |
/* | ||
* json-sharp - v0.1.0 | ||
* json-sharp - v0.1.1 | ||
* Process operations on pure JSON objects | ||
@@ -8,2 +8,2 @@ * | ||
*/ | ||
!function(a,b){"function"==typeof define&&define.amd?define(b):"object"==typeof exports?module.exports=b():a.deepmerge=b()}(this,function(){return function a(b,c){var d=Array.isArray(c),e=d&&[]||{};return d?(b=b||[],e=e.concat(b),c.forEach(function(c,d){"undefined"==typeof e[d]?e[d]=c:"object"==typeof c?e[d]=a(b[d],c):-1===b.indexOf(c)&&e.push(c)})):(b&&"object"==typeof b&&Object.keys(b).forEach(function(a){e[a]=b[a]}),Object.keys(c).forEach(function(d){"object"==typeof c[d]&&c[d]&&b[d]?e[d]=a(b[d],c[d]):e[d]=c[d]})),e}}),function(a,b){"function"==typeof define&&define.amd?define(["deepmerge"],b):"object"==typeof module&&module.exports?module.exports=b(require("deepmerge")):a.returnExports=b(a.deepmerge)}(this,function(a){var b={process:function(a,b){var c=this._clone(a);return this.processNode(c,b)},processNode:function(a,b){var c=Object.prototype.toString.call(a),d="[object Array]"===c,e="[object Object]"===c;return e?this.processObject(a,b):d?this.processArray(a,b):a},processObject:function(a,b){var c,d;if(d=this.getOperation(a),void 0!==d)return d.func(this.processNode(a[d.name],b),b);for(c in a)a.hasOwnProperty(c)&&(a[c]=this.processNode(a[c],b));return a},processArray:function(a,b){for(var c=0;c<a.length;c++)a[c]=this.processNode(a[c],b);return a},getOperation:function(a){for(var b in this.operations)if(this.operations.hasOwnProperty(b)&&a.hasOwnProperty(b))return{name:b,func:this.operations[b]}},operations:{"#merge":function(b){return a.apply(a,b)},"#switch":function(a,b){var c,d=b[a["#property"]],e=a["#case"];return c=e[d],void 0!==c?c:(c=e["#default"],void 0!==c?c:void 0)}},_clone:function(a){return JSON.parse(JSON.stringify(a))}};return{JSONSharp:b}}); | ||
!function(a,b){"function"==typeof define&&define.amd?define(b):"object"==typeof exports?module.exports=b():a.deepmerge=b()}(this,function(){return function a(b,c){var d=Array.isArray(c),e=d&&[]||{};return d?(b=b||[],e=e.concat(b),c.forEach(function(c,d){"undefined"==typeof e[d]?e[d]=c:"object"==typeof c?e[d]=a(b[d],c):-1===b.indexOf(c)&&e.push(c)})):(b&&"object"==typeof b&&Object.keys(b).forEach(function(a){e[a]=b[a]}),Object.keys(c).forEach(function(d){"object"==typeof c[d]&&c[d]&&b[d]?e[d]=a(b[d],c[d]):e[d]=c[d]})),e}}),function(a,b){"function"==typeof define&&define.amd?define(["deepmerge","JSONPath"],b):"object"==typeof module&&module.exports?module.exports=b(require("deepmerge"),require("JSONPath")):a.returnExports=b(a.deepmerge,a.JSONPath)}(this,function(a,b){var c={process:function(a,b){var c=this._clone(a);return this.processNode(c,b)},processNode:function(a,b){var c=Object.prototype.toString.call(a),d="[object Array]"===c,e="[object Object]"===c;return e?this.processObject(a,b):d?this.processArray(a,b):a},processObject:function(a,b){var c,d;if(d=this.getOperation(a),void 0!==d)return d.func(this.processNode(a[d.name],b),b);for(c in a)a.hasOwnProperty(c)&&(a[c]=this.processNode(a[c],b));return a},processArray:function(a,b){for(var c=0;c<a.length;c++)a[c]=this.processNode(a[c],b);return a},getOperation:function(a){for(var b in this.operations)if(this.operations.hasOwnProperty(b)&&a.hasOwnProperty(b))return{name:b,func:this.operations[b]}},resolveProperty:function(a,c){return 0!==a.indexOf("$.")?c[a]:b.eval(c,a)},operations:{"#merge":function(b){return a.apply(a,b)},"#switch":function(a,b){var d,e=c.resolveProperty(a["#property"],b),f=a["#case"];return d=f[e],void 0!==d?d:(d=f["#default"],void 0!==d?d:void 0)}},_clone:function(a){return JSON.parse(JSON.stringify(a))}};return{JSONSharp:c}}); |
@@ -73,3 +73,3 @@ module.exports = function(grunt) { | ||
createTag: true, | ||
tagName: '<%= pkg.name %>-%VERSION%', | ||
tagName: '%VERSION%', | ||
tagMessage: 'New version %VERSION%', | ||
@@ -76,0 +76,0 @@ push: false, |
{ | ||
"name": "json-sharp", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Process operations on pure JSON objects", | ||
@@ -34,4 +34,5 @@ "author": { | ||
"dependencies": { | ||
"JSONPath": "^0.10.0", | ||
"deepmerge": "^0.2.10" | ||
} | ||
} |
@@ -69,1 +69,55 @@ [](https://travis-ci.org/globocom/json-sharp) | ||
``` | ||
## Operations | ||
### `#merge` | ||
The `#merge` operation takes a list of objects and deeply merges its properties | ||
using the [deepmerge](https://www.npmjs.com/package/deepmerge) library. | ||
Examples: | ||
```js | ||
JSONSharp.process({'#merge': [{a: 'a'}, {b: 'b']}, {}); | ||
// ==> {a: 'a', b: 'b'} | ||
``` | ||
### `#switch` | ||
The `#switch` operation works much like the [switch](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/switch) Javascript statement, with the exception that it doesn't | ||
use a `break` statement. | ||
It takes an object with the following properties: | ||
* `#property`: the property name or `JSONPath` to be matched for results | ||
* `#case`: an object mapping `#property` values to desired results | ||
* `#case.#default`: the value will be used if no matching value is found | ||
Examples: | ||
```js | ||
var switchObj = { | ||
'#switch': { | ||
'#property': 'name', | ||
'#case': {a: 'Prop A', '#default': 'not found'} | ||
} | ||
}; | ||
JSONSharp.process(switchObj, {}); | ||
// ==> "not found" | ||
JSONSharp.process(switchObj, {name: 'a'}); | ||
// ==> "Prop A" | ||
JSONSharp.process(switchObj, {name: '$.a'}); | ||
// ==> "Prop A" | ||
``` | ||
## Property resolution | ||
A `#property` starting with `$.` will be resolved using the | ||
[JSONPath](https://www.npmjs.com/package/JSONPath) library, otherwise simple | ||
property access will be used. |
@@ -8,3 +8,3 @@ /* global define */ | ||
// AMD. Register as an anonymous module. | ||
define(['deepmerge'], factory); | ||
define(['deepmerge', 'JSONPath'], factory); | ||
} else if (typeof module === 'object' && module.exports) { | ||
@@ -14,8 +14,8 @@ // Node. Does not work with strict CommonJS, but | ||
// like Node. | ||
module.exports = factory(require('deepmerge')); | ||
module.exports = factory(require('deepmerge'), require('JSONPath')); | ||
} else { | ||
// Browser globals (root is window) | ||
root.returnExports = factory(root.deepmerge); | ||
root.returnExports = factory(root.deepmerge, root.JSONPath); | ||
} | ||
}(this, function (deepmerge) { /* jshint ignore:line */ | ||
}(this, function (deepmerge, JSONPath) { /* jshint ignore:line */ | ||
var JSONSharp = { | ||
@@ -72,2 +72,8 @@ process: function (obj, context) { | ||
}, | ||
resolveProperty: function (property, context) { | ||
if (property.indexOf('$.') !== 0) { | ||
return context[property]; | ||
} | ||
return JSONPath.eval(context, property); | ||
}, | ||
operations: { | ||
@@ -79,3 +85,3 @@ '#merge': function (obj) { | ||
var result; | ||
var value = context[obj['#property']]; | ||
var value = JSONSharp.resolveProperty(obj['#property'], context); | ||
var options = obj['#case']; | ||
@@ -82,0 +88,0 @@ |
@@ -102,2 +102,9 @@ /* global before, after */ | ||
}); | ||
it('should access deep context property using JSONPath', function () { | ||
this.obj['#switch']['#property'] = '$.deep.option[0]'; | ||
var context = {deep: {option: ['a']}}; | ||
var result = JSONSharp.process(this.obj, context); | ||
expect(result).to.be.equal('AAA'); | ||
}); | ||
}); | ||
@@ -104,0 +111,0 @@ |
Sorry, the diff of this file is not supported yet
24724
474
123
2
+ AddedJSONPath@^0.10.0
+ AddedJSONPath@0.10.0(transitive)
+ Addedunderscore@1.13.7(transitive)