UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

52 lines (51 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatType = exports.validateType = void 0; const constants_1 = require("./constants"); const validateType = (type) => { switch (typeof type) { case 'string': case 'number': return; case 'object': { if (!(type instanceof Array)) throw new Error('INVALID_TYPE'); if (type.length > 32) throw new Error('INVALID_TYPE'); const length = type.length; LOOP: for (let i = 0; i < length; i++) { const step = type[i]; switch (typeof step) { case 'string': case 'number': continue LOOP; case 'object': if (!Array.isArray(step) || step.length !== 2) throw new Error('INVALID_TYPE'); continue LOOP; default: throw new Error('INVALID_TYPE'); } } return; } default: throw new Error('INVALID_TYPE'); } }; exports.validateType = validateType; const formatType = (step) => { let tag = ''; let discriminant = -1; if (Array.isArray(step)) { tag = step[0]; discriminant = step[1]; } else { tag = step; } if (typeof tag === 'number' && Math.abs(tag) <= 64 && constants_1.SliceTypeName[tag]) tag = constants_1.SliceTypeName[tag] ?? tag; return '<' + tag + (discriminant >= 0 ? ':' + discriminant : '') + '>'; }; exports.formatType = formatType;