@hn3000/json-ref
Version:
simple resolver for json reference and json pointer
367 lines • 18.1 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonPointerTest = void 0;
var fs = require("fs");
var index_1 = require("../src/index");
var tsunit_async_1 = require("@hn3000/tsunit-async");
var JsonPointerTest = /** @class */ (function (_super) {
__extends(JsonPointerTest, _super);
function JsonPointerTest() {
var _this = _super.call(this) || this;
var tmp = fs.readFileSync('./test/resource/json-ptr.test.json', 'utf-8');
_this.json = JSON.parse(tmp);
var pointerTestCases = [
["/a~1b", 1],
["/c%d", 2],
["/e^f", 3],
["/g|h", 4],
["/i\\j", 5],
["/k\"l", 6],
["/ ", 7],
["/m~0n", 8],
["", _this.json],
["/x/y", undefined]
];
_this.parameterizeUnitTest(_this.testPointer, pointerTestCases);
_this.parameterizeUnitTest(_this.testPointerGet, pointerTestCases);
_this.parameterizeUnitTest(_this.testDeref, pointerTestCases);
_this.parameterizeUnitTest(_this.testToStringIsIdenticalToInput, [
[""],
["/a"],
["/a/b"]
]);
_this.parameterizeUnitTest(_this.testHasParent, [
["", false],
["/a", true],
["/a/b", true]
]);
_this.parameterizeUnitTest(_this.testParent, [
["", null],
["/a", ""],
["/a/b", "/a"]
]);
return _this;
}
JsonPointerTest.prototype.testHasJson = function () {
this.areNotIdentical(null, this.json);
this.areNotIdentical(undefined, this.json);
};
JsonPointerTest.prototype.testEmptyPointerReturnsOriginalObject = function () {
var ptr = new index_1.JsonPointer("");
this.areIdentical(this.json, ptr.getValue(this.json));
};
JsonPointerTest.prototype.testSimplePointerReturnsReferencedProperty = function () {
var ptr = new index_1.JsonPointer("/foo");
this.areIdentical(this.json.foo, ptr.getValue(this.json));
};
JsonPointerTest.prototype.testArrayIndexReturnsArrayElement = function () {
var ptr = new index_1.JsonPointer("/foo/0");
this.areIdentical(this.json.foo[0], ptr.getValue(this.json));
this.areIdentical("bar", ptr.getValue(this.json));
};
JsonPointerTest.prototype.testArrayIndexDashReturnsArrayElementAfterLast = function () {
var ptr = new index_1.JsonPointer("/-");
var arr = [1];
arr['-'] = 'property called minus';
this.areIdentical(arr[1], ptr.getValue(arr));
};
JsonPointerTest.prototype.testObjectPropDashReturnsProperty = function () {
var ptr = new index_1.JsonPointer("/-");
var obj = { a: 1 };
obj['-'] = 'property called minus';
this.areIdentical(obj['-'], ptr.getValue(obj));
};
JsonPointerTest.prototype.testEmptyPropertyNameWorks = function () {
var ptr = new index_1.JsonPointer("/");
this.areIdentical(this.json[""], ptr.getValue(this.json));
this.areIdentical(0, ptr.getValue(this.json));
};
JsonPointerTest.prototype.testPointer = function (p, v) {
var ptr = new index_1.JsonPointer(p);
this.areIdentical(v, ptr.getValue(this.json));
};
JsonPointerTest.prototype.testToStringIsIdenticalToInput = function (p) {
var ptr = new index_1.JsonPointer(p);
this.areIdentical(p, ptr.toString());
};
JsonPointerTest.prototype.testPointerGet = function (p, v) {
var ptr = index_1.JsonPointer.get(p);
this.areIdentical(v, ptr.getValue(this.json));
};
JsonPointerTest.prototype.testDeref = function (p, v) {
var val = index_1.JsonPointer.deref(p, this.json);
this.areIdentical(v, val);
};
JsonPointerTest.prototype.testHasParent = function (p, shouldHaveParent) {
var ptr = index_1.JsonPointer.get(p);
var hasParent = ptr.hasParent();
this.areIdentical(shouldHaveParent, hasParent);
};
JsonPointerTest.prototype.testParent = function (p, expectedParent) {
var ptr = index_1.JsonPointer.get(p);
if (null == expectedParent) {
this.areIdentical(null, ptr.parent);
}
else {
this.isTrue(null != ptr.parent);
this.areIdentical(expectedParent, ptr.parent.toString());
}
};
JsonPointerTest.prototype.testPointerGetFromJsonPointer = function () {
var ptrA = new index_1.JsonPointer('/a/b');
var ptrB = index_1.JsonPointer.get(ptrA);
this.areIdentical(ptrA, ptrB);
};
JsonPointerTest.prototype.testJsonPointerWalkObject = function () {
var t = { a: { b: 11, c: 12, d: null } };
var r = [];
index_1.JsonPointer.walkObject(t, function (v, p) { return (r.push({ v: v, p: p }), false); });
this.areIdentical(4, r.length);
this.areIdentical(t.a, r[0].v);
this.areIdentical('/a', r[0].p.toString());
this.areIdentical(11, r[1].v);
this.areIdentical('/a/b', r[1].p.toString());
this.areIdentical(12, r[2].v);
this.areIdentical('/a/c', r[2].p.toString());
this.areIdentical(null, r[3].v);
this.areIdentical('/a/d', r[3].p.toString());
};
JsonPointerTest.prototype.testJsonPointerPaths = function () {
var t = { a: { b: 11, c: 12, d: null } };
var r = index_1.JsonPointer.paths(t);
this.areIdentical(4, r.length);
this.areIdentical('/a', r[0]);
this.areIdentical('/a/b', r[1]);
this.areIdentical('/a/c', r[2]);
this.areIdentical('/a/d', r[3]);
};
JsonPointerTest.prototype.testJsonPointerPointers = function () {
var t = { a: { b: 11, c: 12, d: null } };
var r = index_1.JsonPointer.pointers(t);
this.areIdentical(4, r.length);
this.areIdentical('/a', r[0].toString());
this.areIdentical('/a/b', r[1].toString());
this.areIdentical('/a/c', r[2].toString());
this.areIdentical('/a/d', r[3].toString());
};
JsonPointerTest.prototype.testJsonPointerPointersWithPredicate = function () {
var t = { a: { b: 11, c: 12, d: null } };
var r = index_1.JsonPointer.pointers(t, function (v, p) { return (p.keys.length > 1); });
this.areIdentical(3, r.length);
this.areIdentical('/a/b', r[0].toString());
this.areIdentical('/a/c', r[1].toString());
this.areIdentical('/a/d', r[2].toString());
};
JsonPointerTest.prototype.testPointerSetValue = function () {
var ptr = new index_1.JsonPointer('/lala');
var obj = {};
this.areCollectionsIdentical([], Object.keys(obj));
ptr.setValue(obj, '#');
this.areCollectionsIdentical(['lala'], Object.keys(obj));
};
JsonPointerTest.prototype.testPointerSetValueWithMissingIntermediates = function () {
var path = '/a/b/c/0/foo';
var ptr = new index_1.JsonPointer(path);
var obj = {};
this.areCollectionsIdentical([], index_1.JsonPointer.paths(obj, function (x) { return (typeof x !== 'object'); }));
ptr.setValue(obj, '#', true);
this.areCollectionsIdentical([path], index_1.JsonPointer.paths(obj, function (x) { return (typeof x !== 'object'); }));
this.isTrue(Array.isArray(obj.a.b.c), "expected array at /a/b/c, got ".concat(typeof obj.a.b.c));
};
JsonPointerTest.prototype.testPointerSetValueWithMissingIntermediatesButNoCreatePath = function () {
var path = '/a/b/c/0/foo';
var ptr = new index_1.JsonPointer(path);
var obj = {};
this.areCollectionsIdentical([], index_1.JsonPointer.paths(obj, function (x) { return (typeof x !== 'object'); }));
ptr.setValue(obj, '#', false);
this.areCollectionsIdentical([], index_1.JsonPointer.paths(obj, function (x) { return (typeof x !== 'object'); }));
this.isTrue(null == obj.a);
};
JsonPointerTest.prototype.testPointerSetValueWithMissingRoot = function () {
var path = '/a/b/c/0/foo';
var ptr = new index_1.JsonPointer(path);
var obj = {};
this.areCollectionsIdentical([], index_1.JsonPointer.paths(obj, function (x) { return (typeof x !== 'object'); }));
var newObj = ptr.setValue(null, '#', true);
this.areCollectionsIdentical([path], index_1.JsonPointer.paths(newObj, function (x) { return (typeof x !== 'object'); }));
this.isTrue(Array.isArray(newObj.a.b.c), "expected array at /a/b/c, got ".concat(typeof newObj.a.b.c));
};
JsonPointerTest.prototype.testPointerSetValueWithMissingRootStartingWithArray = function () {
var path = '/-/a/b';
var ptr = new index_1.JsonPointer(path);
var obj = {};
this.areCollectionsIdentical([], index_1.JsonPointer.paths(obj, function (x) { return (typeof x !== 'object'); }));
var newObj = ptr.setValue(null, '#', true);
ptr.setValue(newObj, '#', true);
this.areCollectionsIdentical(['/0/a/b', '/1/a/b'], index_1.JsonPointer.paths(newObj, function (x) { return (typeof x !== 'object'); }));
this.isTrue(Array.isArray(newObj), "expected array, got ".concat(typeof newObj));
};
JsonPointerTest.prototype.testPointerSetValueWithEmptyPath = function () {
var path = '';
var ptr = new index_1.JsonPointer(path);
var obj = {};
var val = '#';
var newObj = ptr.setValue(obj, val, true);
this.areNotIdentical(obj, newObj);
this.areIdentical(newObj, val);
};
JsonPointerTest.prototype.testPointerSetValueAppending = function () {
var path = '/a/-';
var ptr = new index_1.JsonPointer(path);
var obj = {};
this.areCollectionsIdentical([], index_1.JsonPointer.paths(obj));
ptr.setValue(obj, '1', true);
ptr.setValue(obj, '2', true);
ptr.setValue(obj, '3', true);
this.isTrue(3 == obj.a.length, "found ".concat(JSON.stringify(obj)));
var paths = index_1.JsonPointer.paths(obj, function (x) { return (typeof x !== 'object'); });
this.isTrue(3 == paths.length, "found paths ".concat(JSON.stringify(paths), " in ").concat(JSON.stringify(obj)));
this.areCollectionsIdentical(['/a/0', '/a/1', '/a/2'], paths);
this.isTrue(Array.isArray(obj.a), "expected array at /a, got ".concat(typeof obj.a));
this.areCollectionsIdentical(['1', '2', '3'], obj.a);
};
JsonPointerTest.prototype.testPointerWithValue = function () {
var ptr = new index_1.JsonPointer('/lala');
var obj = {};
this.areCollectionsIdentical([], Object.keys(obj));
var newObj = ptr.withValue(obj, '#');
this.areNotIdentical(obj, newObj);
this.areCollectionsIdentical([], Object.keys(obj));
this.areCollectionsIdentical(['lala'], Object.keys(newObj));
};
JsonPointerTest.prototype.testPointerWithValueDeepCreatePath = function () {
var ptr = new index_1.JsonPointer('/foo/bar');
var obj = {};
this.areCollectionsIdentical([], Object.keys(obj));
var newObj = ptr.withValue(obj, '#', true);
this.areNotIdentical(obj, newObj);
this.areCollectionsIdentical([], Object.keys(obj));
this.areCollectionsIdentical(['foo'], Object.keys(newObj));
this.areCollectionsIdentical(['bar'], Object.keys(newObj.foo));
};
JsonPointerTest.prototype.testPointerWithValueDeep = function () {
var ptr = new index_1.JsonPointer('/foo/bar');
var obj = { foo: { lala: 12 } };
this.areCollectionsIdentical(['foo'], Object.keys(obj));
this.areCollectionsIdentical(['lala'], Object.keys(obj.foo));
var newObj = ptr.withValue(obj, '#', true);
this.areNotIdentical(obj, newObj, 'root objects must be different');
this.areNotIdentical(obj.foo, newObj.foo, 'inner objects must be different');
this.areCollectionsIdentical(['foo'], Object.keys(obj));
this.areCollectionsIdentical(['lala'], Object.keys(obj.foo));
this.areCollectionsIdentical(['foo'], Object.keys(newObj));
this.areCollectionsIdentical(['lala', 'bar'], Object.keys(newObj.foo));
};
JsonPointerTest.prototype.testPointerWithValueDeepInArray = function () {
var ptr = new index_1.JsonPointer('/foo/0/bar');
var obj = { foo: [{ lala: 12 }] };
this.areCollectionsIdentical(['foo'], Object.keys(obj));
this.areCollectionsIdentical(['0'], Object.keys(obj.foo));
this.areCollectionsIdentical(['lala'], Object.keys(obj.foo[0]));
var newObj = ptr.withValue(obj, '#', false);
this.areNotIdentical(obj, newObj, 'root objects must be different');
this.areNotIdentical(obj.foo, newObj.foo, 'array must be different');
this.areNotIdentical(obj.foo[0], newObj.foo[0], 'inner objects must be different');
this.areCollectionsIdentical(['foo'], Object.keys(obj));
this.areCollectionsIdentical(['0'], Object.keys(obj.foo));
this.areCollectionsIdentical(['lala'], Object.keys(obj.foo[0]));
this.areCollectionsIdentical(['foo'], Object.keys(newObj));
this.areCollectionsIdentical(['0'], Object.keys(newObj.foo));
this.areCollectionsIdentical(['lala', 'bar'], Object.keys(newObj.foo[0]));
};
JsonPointerTest.prototype.testPointerWithValueDeepInNewArrayMember = function () {
var ptr = new index_1.JsonPointer('/foo/-/bar');
var obj = { foo: [{ lala: 12 }] };
this.areCollectionsIdentical(['foo'], Object.keys(obj));
this.areCollectionsIdentical(['0'], Object.keys(obj.foo));
this.areCollectionsIdentical(['lala'], Object.keys(obj.foo[0]));
var newObj = ptr.withValue(obj, '#', true);
this.areNotIdentical(obj, newObj, 'root objects must be different');
this.areNotIdentical(obj.foo, newObj.foo, 'array must be different');
this.areIdentical(obj.foo[0], newObj.foo[0], 'inner objects not must be different');
this.areCollectionsIdentical(['foo'], Object.keys(obj));
this.areCollectionsIdentical(['0'], Object.keys(obj.foo));
this.areCollectionsIdentical(['foo'], Object.keys(newObj));
this.areCollectionsIdentical(['0', '1'], Object.keys(newObj.foo));
this.areCollectionsIdentical(['lala'], Object.keys(newObj.foo[0]));
this.areCollectionsIdentical(['bar'], Object.keys(newObj.foo[1]));
};
JsonPointerTest.prototype.testPointerDeleteValue = function () {
var ptr = new index_1.JsonPointer('/lala');
var obj = { 'lala': '#' };
this.areCollectionsIdentical(['lala'], Object.keys(obj));
ptr.setValue(obj, null);
this.areCollectionsIdentical(['lala'], Object.keys(obj));
ptr.setValue(obj, undefined);
this.areCollectionsIdentical(['lala'], Object.keys(obj));
ptr.deleteValue(obj);
this.areCollectionsIdentical([], Object.keys(obj));
};
JsonPointerTest.prototype.testPointerDeleteMissingValue = function () {
var ptr = new index_1.JsonPointer('/lala/lala');
var obj = {};
this.areCollectionsIdentical([], Object.keys(obj));
ptr.setValue(obj, null, true);
this.areCollectionsIdentical(['lala'], Object.keys(obj));
delete obj['lala'];
this.areCollectionsIdentical([], Object.keys(obj));
ptr.deleteValue(obj);
this.areCollectionsIdentical([], Object.keys(obj));
};
JsonPointerTest.prototype.testPointerSegmentGet = function () {
var path = '/a/b/c/0/foo';
var ptr = new index_1.JsonPointer(path);
this.areIdentical('a', ptr.get(0));
this.areIdentical('a', ptr.get(-5));
this.areIdentical('b', ptr.get(1));
this.areIdentical('b', ptr.get(-4));
this.areIdentical('c', ptr.get(2));
this.areIdentical('c', ptr.get(-3));
this.areIdentical('0', ptr.get(3));
this.areIdentical('0', ptr.get(-2));
this.areIdentical('foo', ptr.get(4));
this.areIdentical('foo', ptr.get(-1));
};
JsonPointerTest.prototype.testJsonPointerStringArray = function () {
var ptr = index_1.JsonPointer.get(['a', 'b']);
this.areIdentical('/a/b', ptr.asString());
};
JsonPointerTest.prototype.testNewJsonPointerEmptyArray = function () {
var ptr = new index_1.JsonPointer([]);
var obj = { a: 1 };
this.areIdentical(obj, ptr.getValue(obj));
};
JsonPointerTest.prototype.testNewJsonPointerEmptyString = function () {
var ptr = new index_1.JsonPointer("");
var obj = { a: 1 };
this.areIdentical(obj, ptr.getValue(obj));
};
JsonPointerTest.prototype.testJsonPointerGetEmptyString = function () {
var ptr = index_1.JsonPointer.get("");
var obj = { a: 1 };
this.areIdentical(obj, ptr.getValue(obj));
};
JsonPointerTest.prototype.testJsonPointerGetEmptyStringArray = function () {
var ptr = index_1.JsonPointer.get([]);
var obj = { a: 1 };
this.areIdentical(obj, ptr.getValue(obj));
};
return JsonPointerTest;
}(tsunit_async_1.TestClass));
exports.JsonPointerTest = JsonPointerTest;
//# sourceMappingURL=json-ptr.test.js.map