All files / json-text stringify.ts

89.47% Statements 17/19
90% Branches 9/10
100% Functions 3/3
93.75% Lines 15/16

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      59x 1750x   3x   317x   3x   3x   1424x 347x 1077x 1077x   12x   531x   534x 534x 330x            
/**
 * @todo Rename to `toLine`?
 */
export const stringify = (value: unknown, spacing: string = ' '): string => {
  switch (value) {
    case null:
      return '!n';
    case undefined:
      return '!u';
    case true:
      return '!t';
    case false:
      return '!f';
  }
  if (Array.isArray(value))
    return `[${spacing}${value.map((v) => stringify(v, spacing)).join(',' + spacing)}${spacing}]`;
  Iif (value instanceof Uint8Array) return `${value}`;
  switch (typeof value) {
    case 'number':
      return `${value}`;
    case 'string':
      return JSON.stringify(value);
    case 'object': {
      const keys = Object.keys(value as object);
      return `{${spacing}${keys
        .map((k) => `${k}${spacing}=${spacing}${stringify((value as any)[k], spacing)}`)
        .join(',' + spacing)}${spacing}}`;
    }
  }
  return '?';
};