UNPKG

@jsonjoy.com/json-type

Version:

High-performance JSON Pointer implementation

83 lines 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Walker = void 0; class Walker { constructor(opts = {}) { this.opts = opts; } walk(type) { const onType = this.opts.onType ?? ((type) => { }); switch (type.kind) { case 'key': { onType(type); this.walk(type.value); break; } case 'any': case 'con': case 'bool': case 'num': case 'str': case 'bin': { onType(type); break; } case 'arr': { onType(type); if (type.head) for (const t of type.head) this.walk(t); if (type.type) this.walk(type.type); if (type.tail) for (const t of type.tail) this.walk(t); break; } case 'obj': { onType(type); for (const key of type.keys) this.walk(key.value); break; } case 'map': { onType(type); this.walk(type.value); if (type.key) this.walk(type.key); break; } case 'or': { onType(type); for (const t of type.types) this.walk(t); break; } case 'ref': { onType(type); break; } case 'fn': case 'fn$': { onType(type); this.walk(type.req); this.walk(type.res); break; } case 'module': { onType(type); for (const alias of type.keys) this.walk(alias.value); break; } default: throw new Error('UNK_KIND'); } } } exports.Walker = Walker; Walker.walk = (type, opts = {}) => { const walker = new Walker(opts); walker.walk(type); }; //# sourceMappingURL=Walker.js.map