2015-10-08 20:16:45 +03:00
|
|
|
'use strict';
|
|
|
|
import JsonPointerLib from 'json-pointer';
|
|
|
|
|
2016-05-25 18:34:31 +03:00
|
|
|
const origParse = JsonPointerLib.parse;
|
2015-10-08 20:16:45 +03:00
|
|
|
/**
|
|
|
|
* Wrapper for JsonPointer. Provides common operations
|
|
|
|
*/
|
2016-05-25 18:34:31 +03:00
|
|
|
|
2016-06-12 20:44:34 +03:00
|
|
|
export class JsonPointer {
|
2015-10-08 20:16:45 +03:00
|
|
|
/**
|
|
|
|
* returns last JsonPointer token
|
2015-10-09 08:31:09 +03:00
|
|
|
* if level > 1 returns levels last (second last/third last)
|
2015-10-08 20:16:45 +03:00
|
|
|
* @example
|
|
|
|
* // returns subpath
|
2015-10-09 08:31:09 +03:00
|
|
|
* JsonPointerHelper.baseName('/path/0/subpath')
|
|
|
|
* // returns foo
|
|
|
|
* JsonPointerHelper.baseName('/path/foo/subpath', 2)
|
2015-10-08 20:16:45 +03:00
|
|
|
*/
|
2015-10-09 08:31:09 +03:00
|
|
|
static baseName(pointer, level=1) {
|
2015-10-09 23:19:35 +03:00
|
|
|
let tokens = JsonPointer.parse(pointer);
|
2015-10-08 23:21:51 +03:00
|
|
|
return tokens[tokens.length - (level)];
|
2015-10-08 20:16:45 +03:00
|
|
|
}
|
2015-10-08 23:21:51 +03:00
|
|
|
|
2015-10-10 16:01:41 +03:00
|
|
|
/**
|
|
|
|
* returns dirname of pointer
|
|
|
|
* if level > 1 returns corresponding dirname in the hierarchy
|
|
|
|
* @example
|
|
|
|
* // returns /path/0
|
|
|
|
* JsonPointerHelper.dirName('/path/0/subpath')
|
|
|
|
* // returns /path
|
|
|
|
* JsonPointerHelper.dirName('/path/foo/subpath', 2)
|
|
|
|
*/
|
|
|
|
static dirName(pointer, level=1) {
|
|
|
|
let tokens = JsonPointer.parse(pointer);
|
2016-05-25 18:34:31 +03:00
|
|
|
return JsonPointerLib.compile(tokens.slice(0, tokens.length - level));
|
2015-10-10 16:01:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* overridden JsonPointer original parse to take care of prefixing '#' symbol
|
|
|
|
* that is not valid JsonPointer
|
|
|
|
*/
|
|
|
|
static parse(pointer) {
|
|
|
|
let ptr = pointer;
|
|
|
|
if (ptr.charAt(0) === '#') {
|
|
|
|
ptr = ptr.substring(1);
|
|
|
|
}
|
2016-05-25 18:34:31 +03:00
|
|
|
return origParse(ptr);
|
2015-10-10 16:01:41 +03:00
|
|
|
}
|
2015-10-17 21:12:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a JSON pointer path, by joining one or more tokens to a base path.
|
|
|
|
*
|
|
|
|
* @param {string} base - The base path
|
|
|
|
* @param {string|string[]} tokens - The token(s) to append (e.g. ["name", "first"])
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
static join(base, tokens) {
|
|
|
|
// TODO: optimize
|
|
|
|
let baseTokens = JsonPointer.parse(base);
|
|
|
|
let resTokens = baseTokens.concat(tokens);
|
2016-05-25 18:34:31 +03:00
|
|
|
return JsonPointerLib.compile(resTokens);
|
|
|
|
}
|
2016-06-12 20:44:34 +03:00
|
|
|
|
2016-05-25 18:34:31 +03:00
|
|
|
static get(object: Object, pointer:string) {
|
|
|
|
return JsonPointerLib.get(object, pointer);
|
|
|
|
}
|
2016-06-12 20:44:34 +03:00
|
|
|
|
2016-05-25 18:34:31 +03:00
|
|
|
static compile(tokens: string[]) {
|
|
|
|
return JsonPointerLib.compile(tokens);
|
|
|
|
}
|
2016-06-12 20:44:34 +03:00
|
|
|
|
2016-05-25 18:34:31 +03:00
|
|
|
static escape(pointer: string) {
|
|
|
|
return JsonPointerLib.escape(pointer);
|
2015-10-17 21:12:46 +03:00
|
|
|
}
|
2015-10-10 16:01:41 +03:00
|
|
|
}
|
2015-10-10 16:48:17 +03:00
|
|
|
JsonPointerLib.parse = JsonPointer.parse;
|
2016-02-10 14:19:50 +03:00
|
|
|
Object.assign(JsonPointer, JsonPointerLib);
|
2015-10-08 23:21:51 +03:00
|
|
|
export default JsonPointer;
|