json-joy
Version:
Collection of libraries for building collaborative editing apps.
28 lines (27 loc) • 817 B
JavaScript
import { compileClosure } from '@jsonjoy.com/util/lib/codegen';
import { $findRef } from '@jsonjoy.com/json-pointer/lib/codegen/findRef';
export const $$add = (op) => {
const find = $findRef(op.path);
const js = /* js */ `
(function(find, path){
return function(doc){
var value = ${JSON.stringify(op.value)};
var f = find(doc);
var obj = f.obj, key = f.key, val = f.val;
if (!obj) doc = value;
else if (typeof key === 'string') obj[key] = value;
else {
var length = obj.length;
if (key < length) obj.splice(key, 0, value);
else if (key > length) throw new Error('INVALID_INDEX');
else obj.push(value);
}
return doc;
};
})`;
return {
deps: [find, op.path],
js: js,
};
};
export const $add = (op) => compileClosure($$add(op));