UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

13 lines (12 loc) 621 B
import { isValidIndex } from '@jsonjoy.com/json-pointer'; export 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 = isValidIndex(oldIndex) && isOldBigger; return shouldChangeIndex ? `${arrayPath}${~~oldIndex + (incUp ? 1 : -1)}${rest}` : path; }