Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export JSONPatcherProxy as ES Module, #57

Merged
merged 5 commits into from Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -4,7 +4,7 @@ dist: trusty
before_script:
- npm install
- 'export PATH=$PWD/node_modules/.bin:$PATH'
node_js: lts/*
node_js: node
script:
- npm run test
- npm run bench
21 changes: 12 additions & 9 deletions README.md
Expand Up @@ -44,23 +44,26 @@ $ npm install jsonpatcherproxy --save

### In a web browser

* Include `dist/jsonpatcherproxy.min.js`, as in:
Load the bundled distribution script:

```html
<script src="dist/jsonpatcherproxy.min.js"></script>
```
<script src="dist/jsonpatcherproxy.js"></script>

In [browsers that support ECMAScript modules](https://caniuse.com/#feat=es6-module), the below code uses this library as a module:

```html
<script type="module">
import { JSONPatcherProxy } from 'jsonpatcherproxy/src/jsonpatcherproxy.mjs';
</script>
```

**You can use rawgit.com as a CDN**.

### In Node.js

Call require to get the instance:

```js
var JSONPatcherProxy = require('jsonpatcherproxy');
```
Or in ES6 and TS:
```js
import JSONPatcherProxy from 'jsonpatcherproxy';
import { JSONPatcherProxy } from 'jsonpatcherproxy';
```
## Usage

Expand Down
408 changes: 228 additions & 180 deletions dist/jsonpatcherproxy.js

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions dist/jsonpatcherproxy.min.js

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

26 changes: 26 additions & 0 deletions jasmine-run.js
@@ -0,0 +1,26 @@
import glob from 'glob';
import Jasmine from 'jasmine';

const jasmine = new Jasmine();

const pattern = process.argv[2] || 'test/spec/*Spec.js';

jasmine.loadConfig({
random: false,
stopSpecOnExpectationFailure: false
});
// Load your specs
glob(pattern, function (er, files) {
Promise.all(
files
// Use relative paths
.map(f => f.replace(/^([^\/])/, './$1'))
.map(f => import(f)
.catch(e => {
console.error('** Error loading ' + f + ': ');
console.error(e);
process.exit(1);
}))
)
.then(() => jasmine.execute());
});
14 changes: 3 additions & 11 deletions package-lock.json

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

11 changes: 6 additions & 5 deletions package.json
Expand Up @@ -4,13 +4,14 @@
"description": "Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.",
"main": "src/jsonpatcherproxy.js",
"typings": "index.d.ts",
"type": "module",
"scripts": {
"build": "webpack",
"build-watch": "webpack --watch",
"bench": "node test/spec/proxyBenchmark.js",
"bench-compare": "node test/spec/proxyBenchmark.js --compare",
"test": "jasmine DUPLEX=proxy JASMINE_CONFIG_PATH=test/jasmine.json",
"test-debug": "node --inspect-brk node_modules/jasmine/bin/jasmine.js DUPLEX=proxy JASMINE_CONFIG_PATH=test/jasmine.json",
"bench": "node --experimental-modules test/spec/proxyBenchmark.js",
"bench-compare": "node --experimental-modules test/spec/proxyBenchmark.js --compare",
"test": "node --experimental-modules jasmine-run.js",
"test-debug": "node --inspect-brk --experimental-modules jasmine-run.js",
"version": "webpack && git add -A"
},
"keywords": [
Expand All @@ -32,7 +33,7 @@
"webpack-cli": "^3.2.3"
},
"dependencies": {
"fast-json-patch": "^2.1.0"
"fast-json-patch": "^3.0.0-0"
},
"//comments": "fast-json-patch is a dependency, though it is only used for type definitions and performance benchmark"
}
5 changes: 1 addition & 4 deletions src/jsonpatcherproxy.js
Expand Up @@ -437,7 +437,4 @@ const JSONPatcherProxy = (function() {
return JSONPatcherProxy;
})();

if (typeof module !== 'undefined') {
module.exports = JSONPatcherProxy;
module.exports.default = JSONPatcherProxy;
}
export { JSONPatcherProxy };
2 changes: 2 additions & 0 deletions test/helpers/fake-module.js
@@ -0,0 +1,2 @@
/** Fake module, to allow importing (without using) in browser */
export function createRequire(){};
15 changes: 10 additions & 5 deletions test/index.html
Expand Up @@ -22,12 +22,17 @@
<link rel="stylesheet" type="text/css" href="lib/benchmark_reporter.css">


<!-- include source files here... -->
<script src="../src/jsonpatcherproxy.js"></script>

<!-- Import map for running (`require`'ing) Benchmark in a browser -->
<script type="importmap">
{
"imports": {
"module": ["./helpers/fake-module.js"]
}
}
</script>
<!-- include spec files here... -->
<script src="spec/proxyBenchmark.js"></script>
<script src="spec/proxySpec.js"></script>
<script type="module" src="./spec/proxyBenchmark.js"></script>
<script type="module" src="./spec/proxySpec.js"></script>

</head>

Expand Down
11 changes: 0 additions & 11 deletions test/jasmine.json

This file was deleted.

14 changes: 6 additions & 8 deletions test/spec/proxyBenchmark.js
Expand Up @@ -6,6 +6,12 @@
$ npm run bench
*/

import { JSONPatcherProxy } from '../../src/jsonpatcherproxy.js';
import * as jsonpatch from '../../node_modules/fast-json-patch/index.mjs';

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

let includeComparisons = true;
const isNode = (typeof window === 'undefined');

Expand All @@ -21,14 +27,6 @@ if (isNode) {
}
}

if (typeof jsonpatch === 'undefined') {
global.jsonpatch = require('fast-json-patch');
}

if (typeof JSONPatcherProxy === 'undefined') {
global.JSONPatcherProxy = require('../../src/jsonpatcherproxy.js');
}

if (typeof Benchmark === 'undefined') {
global.Benchmark = require('benchmark');
global.benchmarkResultsToConsole = require('./../helpers/benchmarkReporter.js').benchmarkResultsToConsole;
Expand Down
10 changes: 3 additions & 7 deletions test/spec/proxySpec.js
@@ -1,9 +1,5 @@
if (typeof jsonpatch === 'undefined') {
global.jsonpatch = require('fast-json-patch');
}
if (typeof JSONPatcherProxy === 'undefined') {
global.JSONPatcherProxy = require('../../src/jsonpatcherproxy');
}
import { JSONPatcherProxy } from '../../src/jsonpatcherproxy.js';
import * as jsonpatch from '../../node_modules/fast-json-patch/index.mjs';

function getPatchesUsingGenerate(objFactory, objChanger) {
const obj = objFactory();
Expand Down Expand Up @@ -697,7 +693,7 @@ describe('proxy', function() {
foo: undefined
};
};

const objChanger = function(obj) {
delete obj.foo;
};
Expand Down