UNPKG

dtsgeneratorws

Version:

TypeScript d.ts file generator for JSON Schema file

65 lines (60 loc) 1.64 kB
export function get(obj: any, path: string[], isCreateOnNotExists: boolean = false): any { if (path.length === 0) { return obj; } let o = obj; const lastKey = path[path.length - 1]; for (let i = 0; i < path.length - 1; i++) { const key = path[i]; let next = o[key]; if (next == null) { if (isCreateOnNotExists) { next = {}; o[key] = next; } else { return undefined; } } o = next; } return o[lastKey]; } export function set(obj: any, path: string[], value: any): void { if (path.length === 0) { return; } let o = obj; const lastKey = path[path.length - 1]; for (let i = 0; i < path.length - 1; i++) { const key = path[i]; let next = o[key]; if (next == null) { next = {}; o[key] = next; } o = next; } o[lastKey] = value; } export function parse(s: string): string[] { if (/^#/.test(s)) { s = s.substring(1); } const path = s.split('/'); if (path.shift() !== '') { throw new Error('Invalid JSON-Pointer format: ' + s); } return path.map((key) => untilde(key)); } function untilde(key: string): string { return key.replace(/~(0|1)/g, (match) => { switch (match) { case '~0': return '~'; case '~1': return '/'; default: throw new Error('Unsupported tilded number.'); } }); } export function tilde(key: string): string { return key.replace(/~/, '~0').replace(/\//g, '~1'); }