UNPKG

mingo

Version:

MongoDB query language for in-memory objects

63 lines (62 loc) 2.03 kB
import { computeValue } from "../../../core/_internal"; import { assert, isArray, isInteger, isNil, isString } from "../../../util"; import { errExpectNumber, errExpectString, errInvalidArgs, INT_OPTS } from "../_internal"; const OP = "$substrBytes"; const $substrBytes = (obj, expr, options) => { assert(isArray(expr) && expr.length === 3, `${OP} expects array(3)`); const [s, index, count] = computeValue(obj, expr, null, options); const foe = options.failOnError; const nil = isNil(s); if (!nil && !isString(s)) return errExpectString(foe, `${OP} arg1 <string>`); if (!isInteger(index) || index < 0) return errExpectNumber(foe, `${OP} arg2 <index>`, INT_OPTS.index); if (!isInteger(count) || count < 0) return errExpectNumber(foe, `${OP} arg3 <count>`, INT_OPTS.index); if (nil) return ""; let utf8Pos = 0; let start16 = null; let end16 = null; const err = `${OP} UTF-8 boundary falls inside a continuation byte`; for (let i = 0; i < s.length; ) { const cp = s.codePointAt(i); const utf8Len = cp < 128 ? 1 : cp < 2048 ? 2 : cp < 65536 ? 3 : 4; const utf16Len = cp > 65535 ? 2 : 1; if (start16 === null) { if (index > utf8Pos && index < utf8Pos + utf8Len) return errInvalidArgs(foe, err); if (utf8Pos === index) { start16 = i; } } const endByte = index + count; if (start16 !== null && end16 === null) { if (endByte > utf8Pos && endByte < utf8Pos + utf8Len) return errInvalidArgs(foe, err); if (utf8Pos === endByte) { end16 = i; break; } } utf8Pos += utf8Len; i += utf16Len; } if (start16 === null) { if (index === utf8Pos) return ""; return errInvalidArgs(foe, `${OP} byte index out of range`); } if (end16 === null) { const endByte = index + count; if (endByte !== utf8Pos) return errInvalidArgs(foe, `${OP} count extends beyond UTF-8 length`); end16 = s.length; } return s.slice(start16, end16); }; export { $substrBytes };