UNPKG

@darkgl/slow-json-stringify

Version:
38 lines 1.3 kB
const _find = (path) => { const length = path.length; let str = 'obj'; for (let i = 0; i < length; ++i) { str += `?.['${path[i]}']`; } return eval(`(obj=>${str})`); }; const _makeArraySerializer = (serializer) => { if (serializer instanceof Function) { return (array) => { let acc = ''; const { length } = array; for (let i = 0; i < length - 1; i++) { acc += `${serializer(array[i])},`; } acc += serializer(array[length - 1]); return `[${acc}]`; }; } return (array) => JSON.stringify(array); }; const TYPES = ['number', 'string', 'boolean', 'array', 'null']; const attr = (type, serializer) => { if (!TYPES.includes(type)) { throw new Error(`Expected one of: "number", "string", "boolean", "null". received "${type}" instead`); } const usedSerializer = serializer || ((value) => value); return { isSJS: true, type, serializer: type === 'array' ? _makeArraySerializer(serializer) : usedSerializer, }; }; const defaultRegex = /\n|\r|\t|\"|\\/gm; const escape = (regex = defaultRegex) => (str) => str.replace(regex, (char) => `\\${char}`); export { _find, escape, attr }; //# sourceMappingURL=_utils.js.map