@bookbox/core
Version:
Bookbox — e-book format
93 lines (92 loc) • 3.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateCounters = void 0;
function getInitial(type = 'number', custom) {
switch (type) {
case 'number': {
if (custom === undefined)
return 1;
const n = typeof custom === 'number' ? custom : parseFloat(custom);
if (Number.isNaN(n))
return 1;
return n;
}
case 'char': {
if (custom === undefined || typeof custom !== 'string' || custom.length !== 1)
return 'a'.charCodeAt(0);
return custom.charCodeAt(0);
}
case 'latin': {
return 'a'.charCodeAt(0);
}
case 'big-latin': {
return 'A'.charCodeAt(0);
}
case 'roman': {
return 'ⅰ'.charCodeAt(0);
}
case 'big-roman': {
return 'Ⅰ'.charCodeAt(0);
}
case 'cyrillic': {
return 'а'.charCodeAt(0);
}
case 'big-cyrillic': {
return 'А'.charCodeAt(0);
}
default: {
return 1;
}
}
}
function calculateCounters(schema, counters = new Map(), steps = new Map(), bumped = new Set()) {
var _a;
for (const item of schema) {
if (typeof item === 'string') {
continue;
}
if (item.name !== 'counter') {
calculateCounters(item.children, counters, steps, bumped);
continue;
}
const props = item.props;
const fillCounter = (name, bump = false) => {
var _a, _b;
const [value, isUnicodeChar] = (_a = counters.get(name)) !== null && _a !== void 0 ? _a : [1, false];
let targetValue = value;
if (bump && bumped.has(name)) {
// use > 1
const step = (_b = steps.get(name)) !== null && _b !== void 0 ? _b : 1;
const newValue = value + step;
counters.set(name, [newValue, isUnicodeChar]);
targetValue = newValue;
}
item.children = [isUnicodeChar ? String.fromCharCode(targetValue) : `${targetValue}`];
};
if (props.start) {
const type = (_a = props.type) !== null && _a !== void 0 ? _a : 'number';
const isUnicodeChar = type !== 'number';
const initial = getInitial(type, props.initial);
counters.set(props.start, [initial, isUnicodeChar]);
if (props.step) {
steps.set(props.start, props.step);
}
continue;
}
if (props.end) {
counters.delete(props.end);
steps.delete(props.end);
bumped.delete(props.end);
continue;
}
if (props.use) {
fillCounter(props.use, true);
bumped.add(props.use);
continue;
}
if (props.last) {
fillCounter(props.last);
}
}
}
exports.calculateCounters = calculateCounters;