json-joy
Version:
Collection of libraries for building collaborative editing apps.
16 lines (15 loc) • 755 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceIndices = replaceIndices;
const json_pointer_1 = require("@jsonjoy.com/json-pointer");
function replaceIndices(path, arrayPath, index, incUp) {
const remainder = path.substr(arrayPath.length);
let slashIndex = remainder.indexOf('/');
if (slashIndex === -1)
slashIndex = remainder.length;
const oldIndex = remainder.substr(0, slashIndex);
const rest = remainder.substr(slashIndex);
const isOldBigger = incUp ? oldIndex >= index : oldIndex > index;
const shouldChangeIndex = (0, json_pointer_1.isValidIndex)(oldIndex) && isOldBigger;
return shouldChangeIndex ? `${arrayPath}${~~oldIndex + (incUp ? 1 : -1)}${rest}` : path;
}
;