UNPKG

@platform/state

Version:

A small, simple, strongly typed, [rx/observable] state-machine.

35 lines (34 loc) 1.2 kB
export class Patch { } Patch.toPatchSet = (forward, backward) => { return { prev: backward ? toPatches(backward) : [], next: forward ? toPatches(forward) : [], }; }; Patch.isEmpty = (input) => { if (input === null || typeof input !== 'object') { return true; } else { const isEmptyArray = (input) => (Array.isArray(input) ? input.length === 0 : true); return isEmptyArray(input.prev) && isEmptyArray(input.next); } }; const toPatch = (input) => { const hasForwardSlash = input.path.some((part) => { return typeof part === 'string' ? part.includes('/') : false; }); if (hasForwardSlash) { const path = input.path .map((part) => (typeof part === 'string' ? `'${part}'` : part)) .join(','); const err = `Property names cannot contain the "/" character. op: '${input.op}' path: [${path}]`; throw new Error(err); } return Object.assign(Object.assign({}, input), { path: input.path.join('/') }); }; const toPatches = (input) => { const patches = Array.isArray(input) ? input : [input]; return patches.filter((p) => Boolean(p)).map((p) => toPatch(p)); };