UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

69 lines (68 loc) 2.62 kB
export declare const enum PATCH_OP_TYPE { DEL = -1, EQL = 0, INS = 1 } export type Patch = PatchOperation[]; export type PatchOperation = PatchOperationDelete | PatchOperationEqual | PatchOperationInsert; export type PatchOperationDelete = [type: PATCH_OP_TYPE.DEL, txt: string]; export type PatchOperationEqual = [type: PATCH_OP_TYPE.EQL, txt: string]; export type PatchOperationInsert = [type: PATCH_OP_TYPE.INS, txt: string]; /** * Determine the common prefix of two strings. * * @param txt1 First string. * @param txt2 Second string. * @return The number of characters common to the start of each string. */ export declare const pfx: (txt1: string, txt2: string) => number; /** * Determine the common suffix of two strings. * * @param txt1 First string. * @param txt2 Second string. * @return The number of characters common to the end of each string. */ export declare const sfx: (txt1: string, txt2: string) => number; /** * Find the differences between two texts. * * @param src Old string to be diffed. * @param dst New string to be diffed. * @return A {@link Patch} - an array of patch operations. */ export declare const diff: (src: string, dst: string) => Patch; /** * Considers simple insertion and deletion cases around the caret position in * the destination string. If the fast patch cannot be constructed, it falls * back to the default full implementation. * * Cases considered: * * 1. Insertion of a single or multiple characters right before the caret. * 2. Deletion of one or more characters right before the caret. * * @param src Old string to be diffed. * @param dst New string to be diffed. * @param caret The position of the caret in the new string. Set to -1 to * ignore the caret position. * @return A {@link Patch} - an array of patch operations. */ export declare const diffEdit: (src: string, dst: string, caret: number) => Patch; export declare const src: (patch: Patch) => string; export declare const dst: (patch: Patch) => string; /** * Inverts patch such that it can be applied to `dst` to get `src` (instead of * `src` to get `dst`). * * @param patch The patch to invert. * @returns Inverted patch. */ export declare const invert: (patch: Patch) => Patch; /** * @param patch The patch to apply. * @param srcLen The length of the source string. * @param onInsert Callback for insert operations. * @param onDelete Callback for delete operations. */ export declare const apply: (patch: Patch, srcLen: number, onInsert: (pos: number, str: string) => void, onDelete: (pos: number, len: number, str: string) => void) => void;