All files / diff/src bin.ts

100% Statements 33/33
100% Branches 0/0
100% Functions 8/8
100% Lines 21/21

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 384x   4x 1052x 1052x 624607x 1052x     4x 634x 634x 578777x 634x     4x 272x 272x 272x     4x           4x     5x 4x     268x 268x  
import * as str from './str';
 
export const toStr = (buf: Uint8Array): string => {
  let hex = '';
  const length = buf.length;
  for (let i = 0; i < length; i++) hex += String.fromCharCode(buf[i]);
  return hex;
};
 
export const toBin = (str: string): Uint8Array => {
  const length = str.length;
  const buf = new Uint8Array(length);
  for (let i = 0; i < length; i++) buf[i] = str.charCodeAt(i);
  return buf;
};
 
export const diff = (src: Uint8Array, dst: Uint8Array): str.Patch => {
  const txtSrc = toStr(src);
  const txtDst = toStr(dst);
  return str.diff(txtSrc, txtDst);
};
 
export const apply = (
  patch: str.Patch,
  srcLen: number,
  onInsert: (pos: number, bytes: Uint8Array) => void,
  onDelete: (pos: number, len: number, bytes: Uint8Array) => void,
) =>
  str.apply(
    patch,
    srcLen,
    (pos, str) => onInsert(pos, toBin(str)),
    (pos, len, str) => onDelete(pos, len, toBin(str)),
  );
 
export const src = (patch: str.Patch): Uint8Array => toBin(str.src(patch));
export const dst = (patch: str.Patch): Uint8Array => toBin(str.dst(patch));