Skip to content

Commit

Permalink
Merge pull request #236 from /issues/233-export-star-as-default
Browse files Browse the repository at this point in the history
Export star as default
  • Loading branch information
warpech committed Aug 9, 2019
2 parents 39f187e + 18a14f0 commit 6f4d6d6
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -10,6 +10,7 @@ npm-debug.log

# Typescript
*.d.ts
test/spec/typings/typingsSpec.js

# compiled files
src/*.js
Expand Down
39 changes: 39 additions & 0 deletions dist/fast-json-patch.js
Expand Up @@ -660,12 +660,36 @@ function validate(sequence, document, externalValidator) {
}
}
exports.validate = validate;
/**
* Default export for backwards compat
*/
exports.default = {
JsonPatchError: exports.JsonPatchError,
deepClone: exports.deepClone,
getValueByPointer: getValueByPointer,
applyOperation: applyOperation,
applyPatch: applyPatch,
applyReducer: applyReducer,
validator: validator,
validate: validate
};


/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {

var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
/*!
* https://github.com/Starcounter-Jack/JSON-Patch
Expand Down Expand Up @@ -861,6 +885,21 @@ function compare(tree1, tree2, invertible) {
return patches;
}
exports.compare = compare;
/**
* Default export for backwards compat
*/
// import just to re-export as default
var core = __webpack_require__(1);
var helpers_3 = __webpack_require__(0);
exports.default = __assign({}, core, {
// duplex
unobserve: unobserve,
observe: observe,
generate: generate,
compare: compare,
// helpers
JsonPatchError: helpers_3.PatchError, deepClone: helpers_1._deepClone, escapePathComponent: helpers_1.escapePathComponent,
unescapePathComponent: helpers_3.unescapePathComponent });


/***/ }),
Expand Down
4 changes: 2 additions & 2 deletions dist/fast-json-patch.min.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -52,8 +52,11 @@
"build": "tsc && webpack",
"serve": "http-server -p 5000 --silent",
"tsc-watch": "tsc -w",
"test": "npm run tsc && npm run test-core && npm run test-duplex",
"test": "npm run tsc && npm run test-core && npm run test-duplex && npm run test-commonjs && npm run test-webpack-import && npm run test-typings",
"test-sauce": "npm run build && node test/Sauce/Runner.js",
"test-commonjs": "jasmine test/spec/commonjs/requireSpec.js",
"test-webpack-import": "webpack --env.NODE_ENV=test && jasmine test/spec/webpack/importSpec.build.js",
"test-typings": "tsc test/spec/typings/typingsSpec.ts",
"test-duplex": "jasmine DUPLEX=yes JASMINE_CONFIG_PATH=test/jasmine.json",
"test-core": "jasmine DUPLEX=no JASMINE_CONFIG_PATH=test/jasmine.json test/spec/jsonPatchTestsSpec.js test/spec/coreSpec.js test/spec/validateSpec.js",
"bench": "npm run bench-core && npm run bench-duplex",
Expand Down
15 changes: 15 additions & 0 deletions src/core.ts
Expand Up @@ -457,3 +457,18 @@ export function validate<T>(sequence: Operation[], document?: T, externalValidat
}
}
}

/**
* Default export for backwards compat
*/

export default {
JsonPatchError,
deepClone,
getValueByPointer,
applyOperation,
applyPatch,
applyReducer,
validator,
validate
}
21 changes: 21 additions & 0 deletions src/duplex.ts
Expand Up @@ -216,3 +216,24 @@ export function compare(tree1: Object | Array<any>, tree2: Object | Array<any>,
_generate(tree1, tree2, patches, '', invertible);
return patches;
}

/**
* Default export for backwards compat
*/
// import just to re-export as default
import * as core from './core';
import { PatchError as JsonPatchError, unescapePathComponent } from './helpers';

export default {
...core,
// duplex
unobserve,
observe,
generate,
compare,
// helpers
JsonPatchError,
deepClone:_deepClone,
escapePathComponent,
unescapePathComponent
}
2 changes: 1 addition & 1 deletion test/jasmine.json
@@ -1,7 +1,7 @@
{
"spec_dir": "test",
"spec_files": [
"**/*[sS]pec.js"
"spec/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
Expand Down
24 changes: 24 additions & 0 deletions test/spec/commonjs/requireSpec.js
@@ -0,0 +1,24 @@
const jsonpatch = require('../../..');

describe('CommonJS', function () {
describe('require', function () {
it('should have the expected structure', function () {
expect(typeof jsonpatch).withContext("result from require() should be an object").toEqual("object");
expect(typeof jsonpatch).withContext("result from require() should not be a function").not.toEqual("function");
expect(jsonpatch.applyOperation).withContext("applyOperation should be a method within the object").toBeDefined();
expect(jsonpatch.applyPatch).withContext("applyPatch should be a method within the object").toBeDefined();
expect(jsonpatch.applyReducer).withContext("applyReducer should be a method within the object").toBeDefined();
expect(jsonpatch.getValueByPointer).withContext("getValueByPointer should be a method within the object").toBeDefined();
expect(jsonpatch.validate).withContext("validate should be a method within the object").toBeDefined();
expect(jsonpatch.validator).withContext("validator should be a method within the object").toBeDefined();
expect(jsonpatch.JsonPatchError).withContext("JsonPatchError should be a method within the object").toBeDefined();
expect(jsonpatch.deepClone).withContext("deepClone should be a method within the object").toBeDefined();
expect(jsonpatch.escapePathComponent).withContext("escapePathComponent should be a method within the object").toBeDefined();
expect(jsonpatch.unescapePathComponent).withContext("unescapePathComponent should be a method within the object").toBeDefined();
expect(jsonpatch.unobserve).withContext("unobserve should be a method within the object").toBeDefined();
expect(jsonpatch.observe).withContext("observe should be a method within the object").toBeDefined();
expect(jsonpatch.generate).withContext("generate should be a method within the object").toBeDefined();
expect(jsonpatch.compare).withContext("compare should be a method within the object").toBeDefined();
});
});
});
22 changes: 22 additions & 0 deletions test/spec/typings/typingsSpec.ts
@@ -0,0 +1,22 @@
/**
* Run using `npm run test-typings`
* The sole fact that this file compiles means that typings work
* This follows how DefinitelyTyped tests work
* @see https://stackoverflow.com/questions/49296151/how-to-write-tests-for-typescript-typing-definition
*/

import jsonpatch from '../../..';
import * as jsonpatchStar from '../../..';
import { applyPatch, Operation } from '../../..';

const document = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };

const typedPatch = new Array<Operation>({ op: "replace", path: "/firstName", value: "Joachim" });
const untypedPatch = [{ op: "replace", path: "/firstName", value: "Joachim" }];

const test_jsonpatch = jsonpatch.applyPatch(document, typedPatch).newDocument;
const test_jsonpatchStar = jsonpatchStar.applyPatch(document, typedPatch).newDocument;
const test_applyPatch = applyPatch(document, typedPatch).newDocument;

// the below line would NOT compile with TSC
// const test_applyPatch = applyPatch(document, untypedPatch).newDocument;

0 comments on commit 6f4d6d6

Please sign in to comment.