From 173009d9cfe9abbf6630151594b28e41072fe307 Mon Sep 17 00:00:00 2001 From: David Lane Date: Mon, 24 Mar 2014 14:11:49 -0400 Subject: [PATCH] Fix AMD support --- bower.json | 3 ++- example/browser.amd.html | 11 +++++++++ example/example.amd.js | 38 ++++++++++++++++++++++++++++++++ example/example.js | 4 ++-- lib/jsonpath-object-transform.js | 12 +++++----- 5 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 example/browser.amd.html create mode 100644 example/example.amd.js diff --git a/bower.json b/bower.json index 8629db3..a45e772 100644 --- a/bower.json +++ b/bower.json @@ -25,6 +25,7 @@ ], "license": "MIT", "dependencies": { - "jsonpath": "*" + "jsonpath": "*", + "requirejs": "~2.1.11" } } diff --git a/example/browser.amd.html b/example/browser.amd.html new file mode 100644 index 0000000..71ada53 --- /dev/null +++ b/example/browser.amd.html @@ -0,0 +1,11 @@ + + + + jsonpath-object-transform + + + + + diff --git a/example/example.amd.js b/example/example.amd.js new file mode 100644 index 0000000..efa1637 --- /dev/null +++ b/example/example.amd.js @@ -0,0 +1,38 @@ +/*global window, require*/ + +require.config({ + paths: { + 'jsonpathObjectTransform': '../lib/jsonpath-object-transform', + 'JSONPath': '../bower_components/jsonpath/lib/jsonpath' + }, + shim: { + 'JSONPath': { + exports: 'jsonPath' + } + } +}); + +require(['jsonpathObjectTransform'], function(transform) { + var path = { + foo: ['$.some.crazy', { + bar: '$.example' + }] + }; + + var data = { + some: { + crazy: [ + { + example: 'A' + }, + { + example: 'B' + } + ] + } + }; + + var result = transform(data, path); + + window.console.log(JSON.stringify(result, null, 2)); +}); diff --git a/example/example.js b/example/example.js index 2cf55ee..394693b 100644 --- a/example/example.js +++ b/example/example.js @@ -1,7 +1,7 @@ /*jshint laxbreak:true*/ /*global window, require, console*/ -var tranform = (typeof exports === 'object') +var transform = (typeof exports === 'object') ? require('../lib/jsonpath-object-transform') : window.jsonpathObjectTransform; @@ -24,6 +24,6 @@ var data = { } }; -var result = tranform(data, path); +var result = transform(data, path); console.log(JSON.stringify(result, null, 2)); diff --git a/lib/jsonpath-object-transform.js b/lib/jsonpath-object-transform.js index d0a18f3..f7cc524 100644 --- a/lib/jsonpath-object-transform.js +++ b/lib/jsonpath-object-transform.js @@ -6,17 +6,19 @@ // AMD if (typeof define === 'function' && define.amd) { - define('jsonpathObjectTransform', ['JSONPath'], factory); + define('jsonpathObjectTransform', ['JSONPath'], function(jsonPath) { + return (root.jsonpathObjectTransform = factory(jsonPath)); + }); } // Node else if (typeof exports === 'object') { - module.exports = factory(require('JSONPath').eval); + module.exports = factory(require('JSONPath')); } // Browser global else { - root.jsonpathObjectTransform = factory(root.jsonPath.eval); + root.jsonpathObjectTransform = factory(root.jsonPath); } }(this, function(jsonPath) { 'use strict'; @@ -70,7 +72,7 @@ * @param {string} key */ function seekSingle(data, pathStr, result, key) { - var seek = jsonPath(data, pathStr) || []; + var seek = jsonPath.eval(data, pathStr) || []; result[key] = seek.length ? seek[0] : undefined; } @@ -86,7 +88,7 @@ function seekArray(data, pathArr, result, key) { var subpath = pathArr[1]; var path = pathArr[0]; - var seek = jsonPath(data, path) || []; + var seek = jsonPath.eval(data, path) || []; if (seek.length && subpath) { result = result[key] = [];