Skip to content

Commit

Permalink
Fix AMD support
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdln committed Mar 24, 2014
1 parent 0cbbbb9 commit 173009d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"license": "MIT",
"dependencies": {
"jsonpath": "*"
"jsonpath": "*",
"requirejs": "~2.1.11"
}
}
11 changes: 11 additions & 0 deletions example/browser.amd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype HTML>
<html>
<head>
<title>jsonpath-object-transform</title>
</head>
<body>
<script data-main="./example.amd.js"
src="../bower_components/requirejs/require.js">
</script>
</body>
</html>
38 changes: 38 additions & 0 deletions example/example.amd.js
Original file line number Diff line number Diff line change
@@ -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));
});
4 changes: 2 additions & 2 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -24,6 +24,6 @@ var data = {
}
};

var result = tranform(data, path);
var result = transform(data, path);

console.log(JSON.stringify(result, null, 2));
12 changes: 7 additions & 5 deletions lib/jsonpath-object-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand All @@ -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] = [];
Expand Down

0 comments on commit 173009d

Please sign in to comment.