UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

58 lines (57 loc) 1.88 kB
import { SliceTypeName } from './constants'; export 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'); } }; export const getTag = (type) => { if (!Array.isArray(type)) return type; const length = type.length; if (!length) return ''; const tagWithMaybeDiscriminant = type[length - 1]; const hasDiscriminant = Array.isArray(tagWithMaybeDiscriminant); const tag = hasDiscriminant ? tagWithMaybeDiscriminant[0] : tagWithMaybeDiscriminant; return tag; }; export 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 && SliceTypeName[tag]) tag = SliceTypeName[tag] ?? tag; return '<' + tag + (discriminant >= 0 ? ':' + discriminant : '') + '>'; };