Refactored JsonPatchOperationPathCombiner extending URLCombiner

This commit is contained in:
Giuseppe Digilio
2019-02-28 15:52:43 +01:00
parent b59bcb52a5
commit ce06b1af2a

View File

@@ -1,4 +1,5 @@
import { isNotUndefined } from '../../../shared/empty.util'; import { isNotUndefined } from '../../../shared/empty.util';
import { URLCombiner } from '../../url-combiner/url-combiner';
/** /**
* Interface used to represent a JSON-PATCH path member * Interface used to represent a JSON-PATCH path member
@@ -14,11 +15,12 @@ export interface JsonPatchOperationPathObject {
* Combines a variable number of strings representing parts * Combines a variable number of strings representing parts
* of a JSON-PATCH path * of a JSON-PATCH path
*/ */
export class JsonPatchOperationPathCombiner { export class JsonPatchOperationPathCombiner extends URLCombiner {
private _rootElement: string; private _rootElement: string;
private _subRootElement: string; private _subRootElement: string;
constructor(rootElement, ...subRootElements: string[]) { constructor(rootElement, ...subRootElements: string[]) {
super(rootElement, ...subRootElements);
this._rootElement = rootElement; this._rootElement = rootElement;
this._subRootElement = subRootElements.join('/'); this._subRootElement = subRootElements.join('/');
} }
@@ -35,21 +37,19 @@ export class JsonPatchOperationPathCombiner {
* Combines the parts of this JsonPatchOperationPathCombiner in to a JSON-PATCH path member * Combines the parts of this JsonPatchOperationPathCombiner in to a JSON-PATCH path member
* *
* e.g. new JsonPatchOperationPathCombiner('sections', 'basic').getPath(['dc.title', '0']) * e.g. new JsonPatchOperationPathCombiner('sections', 'basic').getPath(['dc.title', '0'])
* returns: sections/basic/dc.title/0 * returns: {rootElement: 'sections', subRootElement: 'basic', path: '/sections/basic/dc.title/0'}
* *
* @return {string} * @return {JsonPatchOperationPathObject}
* The combined path * The combined path object
*/ */
public getPath(fragment?: string|string[]): JsonPatchOperationPathObject { public getPath(fragment?: string|string[]): JsonPatchOperationPathObject {
if (isNotUndefined(fragment) && Array.isArray(fragment)) { if (isNotUndefined(fragment) && Array.isArray(fragment)) {
fragment = fragment.join('/'); fragment = fragment.join('/');
} }
let path; let path = '/' + this.toString();
if (isNotUndefined(fragment)) { if (isNotUndefined(fragment)) {
path = '/' + this._rootElement + '/' + this._subRootElement + '/' + fragment; path += '/' + fragment;
} else {
path = '/' + this._rootElement + '/' + this._subRootElement;
} }
return {rootElement: this._rootElement, subRootElement: this._subRootElement, path: path}; return {rootElement: this._rootElement, subRootElement: this._subRootElement, path: path};