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 | 5x 27724x 27722x 5520x 5520x 22202x 22202x 22202x 15498x 9053x 7947x 4596x 6445x 6704x 6704x 1948x 1948x 1948x 1948x 4756x 5x 13486x 5x 5598x 5x 3420x 3365x 3365x 3365x 5x 2204x 2204x 8263x 2204x 2204x 5x 6203x 4286x 1917x 1917x 1917x | import type {BinaryOp, BinaryOpComponent} from './types';
export const append = (op: BinaryOp, component: BinaryOpComponent): void => {
if (!component) return;
if (!op.length) {
op.push(component);
return;
}
const lastIndex = op.length - 1;
const last = op[lastIndex];
if (typeof component === 'number') {
if (typeof last === 'number') {
if (component > 0 && last > 0) op[lastIndex] = last + component;
else if (component < 0 && last < 0) op[lastIndex] = last + component;
else op.push(component);
} else op.push(component);
} else if (component instanceof Uint8Array) {
if (last instanceof Uint8Array) {
const combined = new Uint8Array(last.length + component.length);
combined.set(last, 0);
combined.set(component, last.length);
op[lastIndex] = combined;
} else op.push(component);
}
};
export const componentLength = (component: BinaryOpComponent): number => {
return typeof component === 'number' ? Math.abs(component) : component.length;
};
export const isDeleteComponent = (component: BinaryOpComponent): boolean => {
return typeof component === 'number' && component < 0;
};
export const trim = (op: BinaryOp): void => {
if (!op.length) return;
const last = op[op.length - 1];
const isLastRetain = typeof last === 'number' && last > 0;
if (isLastRetain) op.pop();
};
export const normalize = (op: BinaryOp): BinaryOp => {
const op2: BinaryOp = [];
const length = op.length;
for (let i = 0; i < length; i++) append(op2, op[i]);
trim(op2);
return op2;
};
/**
* Extracts a full or a part of a component from an operation.
*
* @param component Component from which to extract a chunk.
* @param offset Position within the component to start from.
* @param maxLength Maximum length of the component to extract.
* @returns Full or partial component at index `index` of operation `op`.
*/
export const chunk = (component: BinaryOpComponent, offset: number, maxLength: number): BinaryOpComponent => {
if (typeof component === 'number') {
return component > 0 ? Math.min(component - offset, maxLength) : -Math.min(-component - offset, maxLength);
} else if (component instanceof Uint8Array) {
const end = Math.min(offset + maxLength, component.length);
return component.subarray(offset, end);
}
return component;
};
|