All files / json-expression/src util.ts

93.22% Statements 358/384
78.26% Branches 108/138
93.44% Functions 57/61
93.65% Lines 236/252

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 3983x 3x           3x   3x 886x 16x 12x         3x 91x 83x 75x 73x     1196x 227x   3x 401x 6x         12x 22x 34x 30x 40x       3x 46x 46x 42x 42x     3x 61x 61x 57x 57x         3x 32x   6x   20x 14x 8x 6x     6x       3x 51x   8x 8x 6x     43x 43x 30x 30x 26x   13x     13x                   3x 12x           3x 16x       3x 46x     3x 36x     3x 36x     3x 18x     3x   3x 3x   3x 3x 3x   3x 3x 3x 3x 3x   14x 18x 16x 16x 18x 3x 20x 16x   3x 3x   3x 18x 10x 10x 4x 4x 4x 4x     3x 18x 10x 10x 4x 4x 4x 4x 4x 4x 4x 4x 4x           3x 16x 8x 8x         3x 12x 12x 12x 8x         3x 231x       3x 18x 18x 18x     3x 15x 15x 38x 45x   15x     3x 66x 66x 120x 32x     3x 6x 6x 14x 2x     3x 8x 8x 8x 8x 8x 8x 8x   8x     3x 7x 7x 29x       3x 1x 1x 5x       3x 4x 4x 4x 4x 8x 4x     3x 4x 26x 26x   4x 4x     3x 4x 20x 20x   4x 4x     3x               4x 4x 20x 20x 20x   4x 4x 4x 4x         3x 27x       12x   3x 4x 4x 4x 4x 4x     3x 4x 4x 4x 4x 2x 2x   4x     3x 5x 5x 4x 4x     3x 5x 5x         3x         3x 16x     16x     3x   3x 2462x     3x 1002x     3x 4516x 4516x 1052x 1052x 1052x 3464x 1002x     3x 3x 3x 273x 273x 273x   3x     3x 1084x 328x 328x      
import {deepEqual} from '@jsonjoy.com/util/lib/json-equal/deepEqual';
import {toPath, get as get_} from '@jsonjoy.com/json-pointer';
import type {Vars} from './Vars';
import type {Expression, Literal, OperatorDefinition, OperatorMap} from './types';
 
// ----------------------------------------------------- Input operator helpers
 
export const get = (path: string, data: unknown) => get_(data, toPath(path));
 
export const throwOnUndef = (value: unknown, def?: unknown) => {
  if (value !== undefined) return value;
  if (def === undefined) throw new Error('NOT_FOUND');
  return def;
};
 
// ------------------------------------------------------ Type operator helpers
 
export const type = (value: unknown): string => {
  if (value === null) return 'null';
  if (value instanceof Array) return 'array';
  if (value instanceof Uint8Array) return 'binary';
  return typeof value;
};
 
export const num = (value: unknown): number => +(value as number) || 0;
export const int = (value: unknown): number => ~~(value as number);
 
export const str = (value: unknown): string => {
  if (typeof value !== 'object') return '' + value;
  return JSON.stringify(value);
};
 
// ------------------------------------------------ Comparison operator helpers
 
export const cmp = (a: any, b: any): 1 | -1 | 0 => (a > b ? 1 : a < b ? -1 : 0);
export const betweenNeNe = (val: any, min: any, max: any): boolean => val > min && val < max;
export const betweenNeEq = (val: any, min: any, max: any): boolean => val > min && val <= max;
export const betweenEqNe = (val: any, min: any, max: any): boolean => val >= min && val < max;
export const betweenEqEq = (val: any, min: any, max: any): boolean => val >= min && val <= max;
 
// ------------------------------------------------ Arithmetic operator helpers
 
export const slash = (a: unknown, b: unknown) => {
  const divisor = num(b);
  if (divisor === 0) throw new Error('DIVISION_BY_ZERO');
  const res = num(a) / divisor;
  return Number.isFinite(res) ? res : 0;
};
 
export const mod = (a: unknown, b: unknown) => {
  const divisor = num(b);
  if (divisor === 0) throw new Error('DIVISION_BY_ZERO');
  const res = num(a) % divisor;
  return Number.isFinite(res) ? res : 0;
};
 
// ----------------------------------------- Generic container operator helpers
 
export const len = (value: unknown): number => {
  switch (typeof value) {
    case 'string':
      return value.length;
    case 'object': {
      if (value instanceof Array) return value.length;
      if (value instanceof Uint8Array) return value.length;
      if (!value) return 0;
      return Object.keys(value).length;
    }
    default:
      return 0;
  }
};
 
export const member = (container: unknown, index: unknown): unknown => {
  switch (typeof container) {
    case 'string': {
      const i = int(index);
      if (i < 0 || i >= container.length) return undefined;
      return container[i];
    }
    case 'object': {
      Iif (!container) throw new Error('NOT_CONTAINER');
      if (container instanceof Array || container instanceof Uint8Array) {
        const i = int(index);
        if (i < 0 || i >= container.length) return undefined;
        return container[i];
      }
      switch (typeof index) {
        case 'string':
        case 'number':
          return (container as any)[index];
        default:
          throw new Error('NOT_STRING_INDEX');
      }
    }
    default:
      throw new Error('NOT_CONTAINER');
  }
};
 
export const asBin = (value: unknown): Uint8Array => {
  if (value instanceof Uint8Array) return value;
  throw new Error('NOT_BINARY');
};
 
// ---------------------------------------------------- String operator helpers
 
export const asStr = (value: unknown): string => {
  if (typeof value === 'string') return value;
  throw new Error('NOT_STRING');
};
 
export const starts = (outer: unknown, inner: unknown): boolean => {
  return str(outer).startsWith(str(inner));
};
 
export const contains = (outer: unknown, inner: unknown): boolean => {
  return str(outer).indexOf(str(inner)) > -1;
};
 
export const ends = (outer: unknown, inner: unknown): boolean => {
  return str(outer).endsWith(str(inner));
};
 
export const substr = (probablyString: string | unknown, from: number | unknown, to: number | unknown) =>
  str(probablyString).slice(int(from), int(to));
 
const EMAIL_REG =
  /^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i;
const HOSTNAME_REG =
  /^(?=.{1,253}\.?$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*\.?$/i;
const IP4_REG = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
const IP6_REG =
  /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(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d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i;
const DURATION_REG = /^P(?!$)((\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?|(\d+W)?)$/;
const DATE_REG = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
const TIME_REG = /^(\d\d):(\d\d):(\d\d(?:\.\d+)?)(z|([+-])(\d\d)(?::?(\d\d))?)?$/i;
const DATE_TIME_SEPARATOR_REG = /t|\s/i;
 
export const isEmail = (value: unknown): boolean => typeof value === 'string' && EMAIL_REG.test(value);
export const isHostname = (value: unknown): boolean => typeof value === 'string' && HOSTNAME_REG.test(value);
export const isIp4 = (value: unknown): boolean => typeof value === 'string' && IP4_REG.test(value);
export const isIp6 = (value: unknown): boolean => typeof value === 'string' && IP6_REG.test(value);
export const isUuid = (value: unknown): boolean => typeof value === 'string' && UUID_REG.test(value);
export const isUri = (value: unknown): boolean =>
  typeof value === 'string' && NOT_URI_FRAGMENT_REG.test(value) && URI_REG.test(value);
export const isDuration = (value: unknown): boolean => typeof value === 'string' && DURATION_REG.test(value);
 
const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const isLeapYear = (year: number): boolean => year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
 
export const isDate = (value: unknown): boolean => {
  if (typeof value !== 'string') return false;
  const matches: string[] | null = DATE_REG.exec(value);
  if (!matches) return false;
  const year: number = +matches[1];
  const month: number = +matches[2];
  const day: number = +matches[3];
  return month >= 1 && month <= 12 && day >= 1 && day <= (month === 2 && isLeapYear(year) ? 29 : DAYS[month]);
};
 
export const isTime = (value: unknown): boolean => {
  if (typeof value !== 'string') return false;
  const matches: string[] | null = TIME_REG.exec(value);
  if (!matches) return false;
  const hr: number = +matches[1];
  const min: number = +matches[2];
  const sec: number = +matches[3];
  const tz: string | undefined = matches[4];
  const tzSign: number = matches[5] === '-' ? -1 : 1;
  const tzH: number = +(matches[6] || 0);
  const tzM: number = +(matches[7] || 0);
  Iif (tzH > 23 || tzM > 59 || !tz) return false;
  if (hr <= 23 && min <= 59 && sec < 60) return true;
  const utcMin = min - tzM * tzSign;
  const utcHr = hr - tzH * tzSign - (utcMin < 0 ? 1 : 0);
  return (utcHr === 23 || utcHr === -1) && (utcMin === 59 || utcMin === -1) && sec < 61;
};
 
export const isDateTime = (str: unknown): boolean => {
  if (typeof str !== 'string') return false;
  const dateTime = str.split(DATE_TIME_SEPARATOR_REG) as [string, string];
  return dateTime.length === 2 && isDate(dateTime[0]) && isTime(dateTime[1]);
};
 
// ---------------------------------------------------- Binary operator helpers
 
export const u8 = (bin: unknown, pos: unknown) => {
  const buf = asBin(bin);
  const index = int(pos);
  if (index < 0 || index >= buf.length) throw new Error('OUT_OF_BOUNDS');
  return buf[index];
};
 
// ----------------------------------------------------- Array operator helpers
 
export const asArr = (value: unknown): unknown[] => {
  if (value instanceof Array) return value as unknown[];
  throw new Error('NOT_ARRAY');
};
 
export const head = (operand1: unknown, operand2: unknown): unknown => {
  const arr = asArr(operand1);
  const count = int(operand2);
  return count >= 0 ? arr.slice(0, count) : arr.slice(count);
};
 
export const concat = (arrays: unknown[]): unknown[] => {
  const result: unknown[] = [];
  for (const array of arrays) {
    asArr(array);
    for (const item of array as unknown[]) result.push(item);
  }
  return result;
};
 
export const isInArr = (arr: unknown, what: unknown): boolean => {
  const arr2 = asArr(arr);
  const length = arr2.length;
  for (let i = 0; i < length; i++) if (deepEqual(arr2[i], what)) return true;
  return false;
};
 
export const isInArr2 = (arr: unknown, check: (item: unknown) => boolean): boolean => {
  const arr2 = asArr(arr);
  const length = arr2.length;
  for (let i = 0; i < length; i++) if (check(arr2[i])) return true;
  return false;
};
 
export const fromEntries = (maybeEntries: unknown): Record<string, unknown> => {
  const entries = asArr(maybeEntries);
  const result: Record<string, unknown> = {};
  for (const maybeEntry of entries) {
    const entry = asArr(maybeEntry);
    const [key, value] = asArr(entry);
    Iif (entry.length !== 2) throw new Error('NOT_PAIR');
    result[str(key)] = value;
  }
  return result;
};
 
export const indexOf = (container: unknown, item: unknown): -1 | number => {
  const arr = asArr(container);
  const length = arr.length;
  for (let i = 0; i < length; i++) if (deepEqual(arr[i], item)) return i;
  return -1;
};
 
export const indexOf2 = (container: unknown, check: (item: unknown) => boolean): -1 | number => {
  const arr = asArr(container);
  const length = arr.length;
  for (let i = 0; i < length; i++) if (check(arr[i])) return i;
  return -1;
};
 
export const zip = (maybeArr1: unknown, maybeArr2: unknown): [unknown, unknown][] => {
  const arr1 = asArr(maybeArr1);
  const arr2 = asArr(maybeArr2);
  const length = Math.min(arr1.length, arr2.length);
  const result: [unknown, unknown][] = [];
  for (let i = 0; i < length; i++) result.push([arr1[i], arr2[i]]);
  return result;
};
 
export const filter = (arr: unknown[], varname: string, vars: Vars, run: () => unknown): unknown => {
  const result = arr.filter((item) => {
    vars.set(varname, item);
    return run();
  });
  vars.del(varname);
  return result;
};
 
export const map = (arr: unknown[], varname: string, vars: Vars, run: () => unknown): unknown => {
  const result = arr.map((item) => {
    vars.set(varname, item);
    return run();
  });
  vars.del(varname);
  return result;
};
 
export const reduce = (
  arr: unknown[],
  initialValue: unknown,
  accname: string,
  varname: string,
  vars: Vars,
  run: () => unknown,
): unknown => {
  vars.set(accname, initialValue);
  for (const item of arr) {
    vars.set(varname, item);
    const res = run();
    vars.set(accname, res);
  }
  const result = vars.get(accname);
  vars.del(accname);
  vars.del(varname);
  return result;
};
 
// ---------------------------------------------------- Object operator helpers
 
export const asObj = (value: unknown): object => {
  if (type(value) === 'object') return value as object;
  throw new Error('NOT_OBJECT');
};
 
export const keys = (value: unknown): string[] => Object.keys(asObj(value));
 
export const values = (value: unknown): unknown[] => {
  const values: unknown[] = [];
  const theKeys = keys(value);
  const length = theKeys.length;
  for (let i = 0; i < length; i++) values.push((value as any)[theKeys[i]]);
  return values;
};
 
export const entries = (value: unknown): [key: string, value: unknown][] => {
  const entries: [key: string, value: unknown][] = [];
  const theKeys = keys(value);
  const length = theKeys.length;
  for (let i = 0; i < length; i++) {
    const key = theKeys[i];
    entries.push([key, (value as any)[key]]);
  }
  return entries;
};
 
export const objSetRaw = (obj: Record<string, unknown>, key: string, value: unknown): Record<string, unknown> => {
  const prop = str(key);
  if (prop === '__proto__') throw new Error('PROTO_KEY');
  obj[prop] = value;
  return obj;
};
 
export const objDelRaw = (obj: Record<string, unknown>, key: string): Record<string, unknown> => {
  delete obj[key];
  return obj;
};
 
// -------------------------------------------------------------------- Various
 
export const isLiteral = (value: unknown): boolean => {
  if (value instanceof Array) return value.length === 1;
  else return true;
};
 
export const asLiteral = <T>(value: Literal<T>): T => {
  Iif (value instanceof Array) {
    Iif (value.length !== 1) throw new Error('Invalid literal.');
    return value[0];
  } else return value;
};
 
export const literal = <T = unknown>(value: T): T | [T] => (value instanceof Array ? [value] : value);
 
export const assertFixedArity = (operator: string, arity: number, expr: Expression): void => {
  if (expr.length !== arity + 1) throw new Error(`"${operator}" operator expects ${arity} operands.`);
};
 
export const assertVariadicArity = (operator: string, expr: Expression): void => {
  if (expr.length < 3) throw new Error(`"${operator}" operator expects at least two operands.`);
};
 
export const assertArity = (operator: string, arity: number | [min: number, max: number], expr: Expression): void => {
  Iif (!arity) return;
  if (arity instanceof Array) {
    const [min, max] = arity;
    Iif (expr.length < min + 1) throw new Error(`"${operator}" operator expects at least ${min} operands.`);
    if (max !== -1 && expr.length > max + 1) throw new Error(`"${operator}" operator expects at most ${max} operands.`);
  } else if (arity !== -1) assertFixedArity(operator, arity, expr);
  else assertVariadicArity(operator, expr);
};
 
export const operatorsToMap = (operators: OperatorDefinition<Expression>[]): OperatorMap => {
  const map: OperatorMap = new Map();
  for (const operator of operators) {
    const [name, aliases] = operator;
    map.set(name, operator);
    for (const alias of aliases) map.set(alias, operator);
  }
  return map;
};
 
export const parseVar = (name: string): [name: string, pointer: string] => {
  if (name[0] === '/') return ['', name];
  const slashIndex = name.indexOf('/');
  if (slashIndex === -1) return [name, ''];
  return [name.slice(0, slashIndex), name.slice(slashIndex)];
};