json-joy
Version:
Collection of libraries for building collaborative editing apps.
24 lines (23 loc) • 787 B
JavaScript
import * as str from './str';
export const toStr = (buf) => {
let hex = '';
const length = buf.length;
for (let i = 0; i < length; i++)
hex += String.fromCharCode(buf[i]);
return hex;
};
export const toBin = (hex) => {
const length = hex.length;
const buf = new Uint8Array(length);
for (let i = 0; i < length; i++)
buf[i] = hex.charCodeAt(i);
return buf;
};
export const diff = (src, dst) => {
const txtSrc = toStr(src);
const txtDst = toStr(dst);
return str.diff(txtSrc, txtDst);
};
export const apply = (patch, srcLen, onInsert, onDelete) => str.apply(patch, srcLen, (pos, str) => onInsert(pos, toBin(str)), onDelete);
export const src = (patch) => toBin(str.src(patch));
export const dst = (patch) => toBin(str.dst(patch));