@sanity/mutate
Advanced tools
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"encode.cjs","sources":["../../src/encoders/sanity/decode.ts","../../src/encoders/sanity/encode.ts"],"sourcesContent":["import {type SetIfMissingOp, type SetOp} from '../../mutations/operations/types'\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {parse as parsePath} from '../../path/parser/parse'\n\nexport type {Mutation, SanityDocumentBase}\n\nexport type SanityDiffMatchPatch = {\n id: string\n diffMatchPatch: {[path: string]: string}\n}\n\nexport type SanitySetPatch = {\n id: string\n set: {[path: string]: any}\n}\n\nexport type Insert = {\n before?: string\n after?: string\n replace?: string\n items: any[]\n}\n\nexport type SanityInsertPatch = {\n id: string\n insert: Insert\n}\n\nexport type SanityUnsetPatch = {\n id: string\n unset: string[]\n}\n\nexport type SanityIncPatch = {\n id: string\n inc: {[path: string]: number}\n}\n\nexport type SanityDecPatch = {\n id: string\n dec: {[path: string]: number}\n}\n\nexport type SanitySetIfMissingPatch = {\n id: string\n setIfMissing: {[path: string]: any}\n}\n\nexport type SanityPatch =\n | SanitySetPatch\n | SanityUnsetPatch\n | SanityInsertPatch\n | SanitySetIfMissingPatch\n | SanityDiffMatchPatch\n | SanityIncPatch\n | SanityDecPatch\n\nexport type SanityCreateIfNotExistsMutation<Doc extends SanityDocumentBase> = {\n createIfNotExists: Doc\n}\n\nexport type SanityCreateOrReplaceMutation<Doc extends SanityDocumentBase> = {\n createOrReplace: Doc\n}\n\nexport type SanityCreateMutation<Doc extends SanityDocumentBase> = {\n create: Doc\n}\n\nexport type SanityDeleteMutation = {\n delete: {id: string}\n}\n\nexport type SanityPatchMutation = {\n patch:\n | SanitySetPatch\n | SanitySetIfMissingPatch\n | SanityDiffMatchPatch\n | SanityInsertPatch\n | SanityUnsetPatch\n}\n\nexport type SanityMutation<\n Doc extends SanityDocumentBase = SanityDocumentBase,\n> =\n | SanityCreateMutation<Doc>\n | SanityCreateIfNotExistsMutation<Doc>\n | SanityCreateOrReplaceMutation<Doc>\n | SanityDeleteMutation\n | SanityPatchMutation\n\nfunction isCreateIfNotExistsMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateIfNotExistsMutation<Doc> {\n return 'createIfNotExists' in sanityMutation\n}\n\nfunction isCreateOrReplaceMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateOrReplaceMutation<Doc> {\n return 'createOrReplace' in sanityMutation\n}\n\nfunction isCreateMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateMutation<Doc> {\n return 'create' in sanityMutation\n}\n\nfunction isDeleteMutation(\n sanityMutation: SanityMutation<any>,\n): sanityMutation is SanityDeleteMutation {\n return 'delete' in sanityMutation\n}\n\nfunction isPatchMutation(\n sanityMutation: SanityMutation<any>,\n): sanityMutation is SanityPatchMutation {\n return 'patch' in sanityMutation\n}\n\nfunction isSetPatch(sanityPatch: SanityPatch): sanityPatch is SanitySetPatch {\n return 'set' in sanityPatch\n}\n\nfunction isSetIfMissingPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanitySetIfMissingPatch {\n return 'setIfMissing' in sanityPatch\n}\n\nfunction isDiffMatchPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityDiffMatchPatch {\n return 'diffMatchPatch' in sanityPatch\n}\n\nfunction isUnsetPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityUnsetPatch {\n return 'unset' in sanityPatch\n}\n\nfunction isIncPatch(sanityPatch: SanityPatch): sanityPatch is SanityIncPatch {\n return 'inc' in sanityPatch\n}\n\nfunction isDecPatch(sanityPatch: SanityPatch): sanityPatch is SanityDecPatch {\n return 'inc' in sanityPatch\n}\n\nfunction isInsertPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityInsertPatch {\n return 'insert' in sanityPatch\n}\n\nexport function decodeAll<Doc extends SanityDocumentBase>(\n sanityMutations: SanityMutation<Doc>[],\n) {\n return sanityMutations.map(decodeMutation)\n}\n\nexport function decode<Doc extends SanityDocumentBase>(\n encodedMutation: SanityMutation<Doc>,\n) {\n return decodeMutation(encodedMutation)\n}\n\nfunction decodeMutation<Doc extends SanityDocumentBase>(\n encodedMutation: SanityMutation<Doc>,\n): Mutation {\n if (isCreateIfNotExistsMutation(encodedMutation)) {\n return {\n type: 'createIfNotExists',\n document: encodedMutation.createIfNotExists,\n }\n }\n if (isCreateOrReplaceMutation(encodedMutation)) {\n return {\n type: 'createOrReplace',\n document: encodedMutation.createOrReplace,\n }\n }\n if (isCreateMutation(encodedMutation)) {\n return {type: 'create', document: encodedMutation.create}\n }\n if (isDeleteMutation(encodedMutation)) {\n return {id: encodedMutation.delete.id, type: 'delete'}\n }\n if (isPatchMutation(encodedMutation)) {\n return {\n type: 'patch',\n id: encodedMutation.patch.id,\n patches: decodeNodePatches(encodedMutation.patch),\n }\n }\n throw new Error(`Unknown mutation: ${JSON.stringify(encodedMutation)}`)\n}\n\nconst POSITION_KEYS = ['before', 'replace', 'after'] as const\n\nfunction getInsertPosition(insert: Insert) {\n const positions = POSITION_KEYS.filter(k => k in insert)\n if (positions.length > 1) {\n throw new Error(\n `Insert patch is ambiguous. Should only contain one of: ${POSITION_KEYS.join(\n ', ',\n )}, instead found ${positions.join(', ')}`,\n )\n }\n return positions[0]\n}\n\nfunction decodeNodePatches<T>(patch: SanityPatch): NodePatch<any, any>[] {\n // If multiple patches are included, then the order of execution is as follows\n // set, setIfMissing, unset, inc, dec, insert.\n // order is defined here: https://www.sanity.io/docs/http-mutations#2f480b2baca5\n return [\n ...getSetPatches(patch),\n ...getSetIfMissingPatches(patch),\n ...getUnsetPatches(patch),\n ...getIncPatches(patch),\n ...getDecPatches(patch),\n ...getInsertPatches(patch),\n ...getDiffMatchPatchPatches(patch),\n ]\n\n throw new Error(`Unknown patch: ${JSON.stringify(patch)}`)\n}\n\nfunction getSetPatches(patch: SanityPatch): NodePatch<any[], SetOp<any>>[] {\n return isSetPatch(patch)\n ? Object.keys(patch.set).map(path => ({\n path: parsePath(path),\n op: {type: 'set', value: patch.set[path]},\n }))\n : []\n}\n\nfunction getSetIfMissingPatches(\n patch: SanityPatch,\n): NodePatch<any[], SetIfMissingOp<any>>[] {\n return isSetIfMissingPatch(patch)\n ? Object.keys(patch.setIfMissing).map(path => ({\n path: parsePath(path),\n op: {type: 'setIfMissing', value: patch.setIfMissing[path]},\n }))\n : []\n}\n\nfunction getDiffMatchPatchPatches(patch: SanityPatch) {\n return isDiffMatchPatch(patch)\n ? Object.keys(patch.diffMatchPatch).map(path => ({\n path: parsePath(path),\n op: {type: 'diffMatchPatch', value: patch.diffMatchPatch[path]},\n }))\n : []\n}\n\nfunction getUnsetPatches(patch: SanityPatch) {\n return isUnsetPatch(patch)\n ? patch.unset.map(path => ({\n path: parsePath(path),\n op: {type: 'unset'},\n }))\n : []\n}\n\nfunction getIncPatches(patch: SanityPatch) {\n return isIncPatch(patch)\n ? Object.keys(patch.inc).map(path => ({\n path: parsePath(path),\n op: {type: 'inc', amount: patch.inc[path]},\n }))\n : []\n}\n\nfunction getDecPatches(patch: SanityPatch) {\n return isDecPatch(patch)\n ? Object.keys(patch.dec).map(path => ({\n path: parsePath(path),\n op: {type: 'dec', amount: patch.dec[path]},\n }))\n : []\n}\n\nfunction getInsertPatches(patch: SanityPatch) {\n if (!isInsertPatch(patch)) {\n return []\n }\n const position = getInsertPosition(patch.insert)\n if (!position) {\n throw new Error('Insert patch missing position')\n }\n\n const path = parsePath(patch.insert[position]!)\n const referenceItem = path.pop()\n\n const op =\n position === 'replace'\n ? {\n type: 'insert',\n position: position,\n referenceItem,\n items: patch.insert.items,\n }\n : {\n type: 'insert',\n position: position,\n referenceItem,\n items: patch.insert.items,\n }\n\n return [{path, op}]\n}\n","import {\n type Mutation,\n type NodePatch,\n type Transaction,\n} from '../../mutations/types'\nimport {stringify as stringifyPath} from '../../path/parser/stringify'\n\nexport function encode(mutation: Mutation) {\n return encodeMutation(mutation)\n}\n\nexport function encodeAll(mutations: Mutation[]) {\n return mutations.flatMap(encode)\n}\n\nexport function encodeTransaction(transaction: Transaction) {\n return {\n transactionId: transaction.id,\n mutations: encodeAll(transaction.mutations),\n }\n}\n\nexport function encodeMutation(mutation: Mutation) {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return {[mutation.type]: mutation.document}\n }\n if (mutation.type === 'delete') {\n return {\n delete: {id: mutation.id},\n }\n }\n const ifRevisionID = mutation.options?.ifRevision\n return mutation.patches.map(patch => {\n return {\n patch: {\n id: mutation.id,\n ...(ifRevisionID && {ifRevisionID}),\n ...patchToSanity(patch),\n },\n }\n })\n}\n\nfunction patchToSanity(patch: NodePatch) {\n const {path, op} = patch\n if (op.type === 'unset') {\n return {unset: [stringifyPath(path)]}\n }\n if (op.type === 'insert') {\n return {\n insert: {\n [op.position]: stringifyPath([...path, op.referenceItem]),\n items: op.items,\n },\n }\n }\n if (op.type === 'diffMatchPatch') {\n return {diffMatchPatch: {[stringifyPath(path)]: op.value}}\n }\n if (op.type === 'inc') {\n return {inc: {[stringifyPath(path)]: op.amount}}\n }\n if (op.type === 'dec') {\n return {dec: {[stringifyPath(path)]: op.amount}}\n }\n if (op.type === 'set' || op.type === 'setIfMissing') {\n return {[op.type]: {[stringifyPath(path)]: op.value}}\n }\n if (op.type === 'truncate') {\n const range = [\n op.startIndex,\n typeof op.endIndex === 'number' ? op.endIndex : '',\n ].join(':')\n\n return {unset: [`${stringifyPath(path)}[${range}]`]}\n }\n if (op.type === 'upsert') {\n // note: upsert currently not supported by sanity, so will always insert at reference position\n return {\n unset: op.items.map(item =>\n stringifyPath([...path, {_key: (item as any)._key}]),\n ),\n insert: {\n [op.position]: stringifyPath([...path, op.referenceItem]),\n items: op.items,\n },\n }\n }\n if (op.type === 'assign') {\n return {\n set: Object.fromEntries(\n Object.keys(op.value).map(key => [\n stringifyPath(path.concat(key)),\n op.value[key as keyof typeof op.value],\n ]),\n ),\n }\n }\n if (op.type === 'unassign') {\n return {\n unset: op.keys.map(key => stringifyPath(path.concat(key))),\n }\n }\n if (op.type === 'replace') {\n return {\n insert: {\n replace: stringifyPath(path.concat(op.referenceItem)),\n items: op.items,\n },\n }\n }\n if (op.type === 'remove') {\n return {\n unset: [stringifyPath(path.concat(op.referenceItem))],\n }\n }\n //@ts-expect-error all cases should be covered\n throw new Error(`Unknown operation type ${op.type}`)\n}\n"],"names":["parsePath","stringifyPath"],"mappings":";;AA+FA,SAAS,4BACP,gBACwD;AACxD,SAAO,uBAAuB;AAChC;AAEA,SAAS,0BACP,gBACsD;AACtD,SAAO,qBAAqB;AAC9B;AAEA,SAAS,iBACP,gBAC6C;AAC7C,SAAO,YAAY;AACrB;AAEA,SAAS,iBACP,gBACwC;AACxC,SAAO,YAAY;AACrB;AAEA,SAAS,gBACP,gBACuC;AACvC,SAAO,WAAW;AACpB;AAEA,SAAS,WAAW,aAAyD;AAC3E,SAAO,SAAS;AAClB;AAEA,SAAS,oBACP,aACwC;AACxC,SAAO,kBAAkB;AAC3B;AAEA,SAAS,iBACP,aACqC;AACrC,SAAO,oBAAoB;AAC7B;AAEA,SAAS,aACP,aACiC;AACjC,SAAO,WAAW;AACpB;AAEA,SAAS,WAAW,aAAyD;AAC3E,SAAO,SAAS;AAClB;AAEA,SAAS,WAAW,aAAyD;AAC3E,SAAO,SAAS;AAClB;AAEA,SAAS,cACP,aACkC;AAClC,SAAO,YAAY;AACrB;AAEO,SAAS,UACd,iBACA;AACO,SAAA,gBAAgB,IAAI,cAAc;AAC3C;AAEO,SAAS,OACd,iBACA;AACA,SAAO,eAAe,eAAe;AACvC;AAEA,SAAS,eACP,iBACU;AACV,MAAI,4BAA4B,eAAe;AACtC,WAAA;AAAA,MACL,MAAM;AAAA,MACN,UAAU,gBAAgB;AAAA,IAC5B;AAEF,MAAI,0BAA0B,eAAe;AACpC,WAAA;AAAA,MACL,M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L,KAAK,OAAO;AAAA,QACV,OAAO,KAAK,GAAG,KAAK,EAAE,IAAI,CAAO,QAAA;AAAA,UAC/BA,UAAAA,UAAc,KAAK,OAAO,GAAG,CAAC;AAAA,UAC9B,GAAG,MAAM,GAA4B;AAAA,QACtC,CAAA;AAAA,MAAA;AAAA,IAEL;AAEF,MAAI,GAAG,SAAS;AACP,WAAA;AAAA,MACL,OAAO,GAAG,KAAK,IAAI,CAAA,QAAOA,UAAAA,UAAc,KAAK,OAAO,GAAG,CAAC,CAAC;AAAA,IAC3D;AAEF,MAAI,GAAG,SAAS;AACP,WAAA;AAAA,MACL,QAAQ;AAAA,QACN,SAASA,UAAc,UAAA,KAAK,OAAO,GAAG,aAAa,CAAC;AAAA,QACpD,OAAO,GAAG;AAAA,MAAA;AAAA,IAEd;AAEF,MAAI,GAAG,SAAS;AACP,WAAA;AAAA,MACL,OAAO,CAACA,UAAAA,UAAc,KAAK,OAAO,GAAG,aAAa,CAAC,CAAC;AAAA,IACtD;AAGF,QAAM,IAAI,MAAM,0BAA0B,GAAG,IAAI,EAAE;AACrD;;;;;;;"} | ||
| {"version":3,"file":"encode.cjs","sources":["../../src/encoders/sanity/decode.ts","../../src/encoders/sanity/encode.ts"],"sourcesContent":["import {type SetIfMissingOp, type SetOp} from '../../mutations/operations/types'\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {parse as parsePath} from '../../path/parser/parse'\n\nexport type {Mutation, SanityDocumentBase}\n\nexport type SanityDiffMatchPatch = {\n id: string\n diffMatchPatch: {[path: string]: string}\n}\n\nexport type SanitySetPatch = {\n id: string\n set: {[path: string]: any}\n}\n\nexport type Insert = {\n before?: string\n after?: string\n replace?: string\n items: any[]\n}\n\nexport type SanityInsertPatch = {\n id: string\n insert: Insert\n}\n\nexport type SanityUnsetPatch = {\n id: string\n unset: string[]\n}\n\nexport type SanityIncPatch = {\n id: string\n inc: {[path: string]: number}\n}\n\nexport type SanityDecPatch = {\n id: string\n dec: {[path: string]: number}\n}\n\nexport type SanitySetIfMissingPatch = {\n id: string\n setIfMissing: {[path: string]: any}\n}\n\nexport type SanityPatch =\n | SanitySetPatch\n | SanityUnsetPatch\n | SanityInsertPatch\n | SanitySetIfMissingPatch\n | SanityDiffMatchPatch\n | SanityIncPatch\n | SanityDecPatch\n\nexport type SanityCreateIfNotExistsMutation<Doc extends SanityDocumentBase> = {\n createIfNotExists: Doc\n}\n\nexport type SanityCreateOrReplaceMutation<Doc extends SanityDocumentBase> = {\n createOrReplace: Doc\n}\n\nexport type SanityCreateMutation<Doc extends SanityDocumentBase> = {\n create: Doc\n}\n\nexport type SanityDeleteMutation = {\n delete: {id: string}\n}\n\nexport type SanityPatchMutation = {\n patch:\n | SanitySetPatch\n | SanitySetIfMissingPatch\n | SanityDiffMatchPatch\n | SanityInsertPatch\n | SanityUnsetPatch\n}\n\nexport type SanityMutation<\n Doc extends SanityDocumentBase = SanityDocumentBase,\n> =\n | SanityCreateMutation<Doc>\n | SanityCreateIfNotExistsMutation<Doc>\n | SanityCreateOrReplaceMutation<Doc>\n | SanityDeleteMutation\n | SanityPatchMutation\n\nfunction isCreateIfNotExistsMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateIfNotExistsMutation<Doc> {\n return 'createIfNotExists' in sanityMutation\n}\n\nfunction isCreateOrReplaceMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateOrReplaceMutation<Doc> {\n return 'createOrReplace' in sanityMutation\n}\n\nfunction isCreateMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateMutation<Doc> {\n return 'create' in sanityMutation\n}\n\nfunction isDeleteMutation(\n sanityMutation: SanityMutation<any>,\n): sanityMutation is SanityDeleteMutation {\n return 'delete' in sanityMutation\n}\n\nfunction isPatchMutation(\n sanityMutation: SanityMutation<any>,\n): sanityMutation is SanityPatchMutation {\n return 'patch' in sanityMutation\n}\n\nfunction isSetPatch(sanityPatch: SanityPatch): sanityPatch is SanitySetPatch {\n return 'set' in sanityPatch\n}\n\nfunction isSetIfMissingPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanitySetIfMissingPatch {\n return 'setIfMissing' in sanityPatch\n}\n\nfunction isDiffMatchPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityDiffMatchPatch {\n return 'diffMatchPatch' in sanityPatch\n}\n\nfunction isUnsetPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityUnsetPatch {\n return 'unset' in sanityPatch\n}\n\nfunction isIncPatch(sanityPatch: SanityPatch): sanityPatch is SanityIncPatch {\n return 'inc' in sanityPatch\n}\n\nfunction isDecPatch(sanityPatch: SanityPatch): sanityPatch is SanityDecPatch {\n return 'inc' in sanityPatch\n}\n\nfunction isInsertPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityInsertPatch {\n return 'insert' in sanityPatch\n}\n\nexport function decodeAll<Doc extends SanityDocumentBase>(\n sanityMutations: SanityMutation<Doc>[],\n) {\n return sanityMutations.map(decodeMutation)\n}\n\nexport function decode<Doc extends SanityDocumentBase>(\n encodedMutation: SanityMutation<Doc>,\n) {\n return decodeMutation(encodedMutation)\n}\n\nfunction decodeMutation<Doc extends SanityDocumentBase>(\n encodedMutation: SanityMutation<Doc>,\n): Mutation {\n if (isCreateIfNotExistsMutation(encodedMutation)) {\n return {\n type: 'createIfNotExists',\n document: encodedMutation.createIfNotExists,\n }\n }\n if (isCreateOrReplaceMutation(encodedMutation)) {\n return {\n type: 'createOrReplace',\n document: encodedMutation.createOrReplace,\n }\n }\n if (isCreateMutation(encodedMutation)) {\n return {type: 'create', document: encodedMutation.create}\n }\n if (isDeleteMutation(encodedMutation)) {\n return {id: encodedMutation.delete.id, type: 'delete'}\n }\n if (isPatchMutation(encodedMutation)) {\n return {\n type: 'patch',\n id: encodedMutation.patch.id,\n patches: decodeNodePatches(encodedMutation.patch),\n }\n }\n throw new Error(`Unknown mutation: ${JSON.stringify(encodedMutation)}`)\n}\n\nconst POSITION_KEYS = ['before', 'replace', 'after'] as const\n\nfunction getInsertPosition(insert: Insert) {\n const positions = POSITION_KEYS.filter(k => k in insert)\n if (positions.length > 1) {\n throw new Error(\n `Insert patch is ambiguous. Should only contain one of: ${POSITION_KEYS.join(\n ', ',\n )}, instead found ${positions.join(', ')}`,\n )\n }\n return positions[0]\n}\n\nfunction decodeNodePatches<T>(patch: SanityPatch): NodePatch<any, any>[] {\n // If multiple patches are included, then the order of execution is as follows\n // set, setIfMissing, unset, inc, dec, insert.\n // order is defined here: https://www.sanity.io/docs/http-mutations#2f480b2baca5\n return [\n ...getSetPatches(patch),\n ...getSetIfMissingPatches(patch),\n ...getUnsetPatches(patch),\n ...getIncPatches(patch),\n ...getDecPatches(patch),\n ...getInsertPatches(patch),\n ...getDiffMatchPatchPatches(patch),\n ]\n\n throw new Error(`Unknown patch: ${JSON.stringify(patch)}`)\n}\n\nfunction getSetPatches(patch: SanityPatch): NodePatch<any[], SetOp<any>>[] {\n return isSetPatch(patch)\n ? Object.keys(patch.set).map(path => ({\n path: parsePath(path),\n op: {type: 'set', value: patch.set[path]},\n }))\n : []\n}\n\nfunction getSetIfMissingPatches(\n patch: SanityPatch,\n): NodePatch<any[], SetIfMissingOp<any>>[] {\n return isSetIfMissingPatch(patch)\n ? Object.keys(patch.setIfMissing).map(path => ({\n path: parsePath(path),\n op: {type: 'setIfMissing', value: patch.setIfMissing[path]},\n }))\n : []\n}\n\nfunction getDiffMatchPatchPatches(patch: SanityPatch) {\n return isDiffMatchPatch(patch)\n ? Object.keys(patch.diffMatchPatch).map(path => ({\n path: parsePath(path),\n op: {type: 'diffMatchPatch', value: patch.diffMatchPatch[path]},\n }))\n : []\n}\n\nfunction getUnsetPatches(patch: SanityPatch) {\n return isUnsetPatch(patch)\n ? patch.unset.map(path => ({\n path: parsePath(path),\n op: {type: 'unset'},\n }))\n : []\n}\n\nfunction getIncPatches(patch: SanityPatch) {\n return isIncPatch(patch)\n ? Object.keys(patch.inc).map(path => ({\n path: parsePath(path),\n op: {type: 'inc', amount: patch.inc[path]},\n }))\n : []\n}\n\nfunction getDecPatches(patch: SanityPatch) {\n return isDecPatch(patch)\n ? Object.keys(patch.dec).map(path => ({\n path: parsePath(path),\n op: {type: 'dec', amount: patch.dec[path]},\n }))\n : []\n}\n\nfunction getInsertPatches(patch: SanityPatch) {\n if (!isInsertPatch(patch)) {\n return []\n }\n const position = getInsertPosition(patch.insert)\n if (!position) {\n throw new Error('Insert patch missing position')\n }\n\n const path = parsePath(patch.insert[position]!)\n const referenceItem = path.pop()\n\n const op =\n position === 'replace'\n ? {\n type: 'insert',\n position: position,\n referenceItem,\n items: patch.insert.items,\n }\n : {\n type: 'insert',\n position: position,\n referenceItem,\n items: patch.insert.items,\n }\n\n return [{path, op}]\n}\n","import {\n type Mutation,\n type NodePatch,\n type Transaction,\n} from '../../mutations/types'\nimport {stringify as stringifyPath} from '../../path/parser/stringify'\n\nexport function encode(mutation: Mutation) {\n return encodeMutation(mutation)\n}\n\nexport function encodeAll(mutations: Mutation[]) {\n return mutations.flatMap(encode)\n}\n\nexport function encodeTransaction(transaction: Transaction) {\n return {\n transactionId: transaction.id,\n mutations: encodeAll(transaction.mutations),\n }\n}\n\nexport function encodeMutation(mutation: Mutation) {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return {[mutation.type]: mutation.document}\n }\n if (mutation.type === 'delete') {\n return {\n delete: {id: mutation.id},\n }\n }\n const ifRevisionID = mutation.options?.ifRevision\n return mutation.patches.map(patch => {\n return {\n patch: {\n id: mutation.id,\n ...(ifRevisionID && {ifRevisionID}),\n ...patchToSanity(patch),\n },\n }\n })\n}\n\nfunction patchToSanity(patch: NodePatch) {\n const {path, op} = patch\n if (op.type === 'unset') {\n return {unset: [stringifyPath(path)]}\n }\n if (op.type === 'insert') {\n return {\n insert: {\n [op.position]: stringifyPath([...path, op.referenceItem]),\n items: op.items,\n },\n }\n }\n if (op.type === 'diffMatchPatch') {\n return {diffMatchPatch: {[stringifyPath(path)]: op.value}}\n }\n if (op.type === 'inc') {\n return {inc: {[stringifyPath(path)]: op.amount}}\n }\n if (op.type === 'dec') {\n return {dec: {[stringifyPath(path)]: op.amount}}\n }\n if (op.type === 'set' || op.type === 'setIfMissing') {\n return {[op.type]: {[stringifyPath(path)]: op.value}}\n }\n if (op.type === 'truncate') {\n const range = [\n op.startIndex,\n typeof op.endIndex === 'number' ? op.endIndex : '',\n ].join(':')\n\n return {unset: [`${stringifyPath(path)}[${range}]`]}\n }\n if (op.type === 'upsert') {\n // note: upsert currently not supported by sanity, so will always insert at reference position\n return {\n unset: op.items.map(item =>\n stringifyPath([...path, {_key: (item as any)._key}]),\n ),\n insert: {\n [op.position]: stringifyPath([...path, op.referenceItem]),\n items: op.items,\n },\n }\n }\n if (op.type === 'assign') {\n return {\n set: Object.fromEntries(\n Object.keys(op.value).map(key => [\n stringifyPath(path.concat(key)),\n op.value[key as keyof typeof op.value],\n ]),\n ),\n }\n }\n if (op.type === 'unassign') {\n return {\n unset: op.keys.map(key => stringifyPath(path.concat(key))),\n }\n }\n if (op.type === 'replace') {\n return {\n insert: {\n replace: stringifyPath(path.concat(op.referenceItem)),\n items: op.items,\n },\n }\n }\n if (op.type === 'remove') {\n return {\n unset: [stringifyPath(path.concat(op.referenceItem))],\n }\n }\n //@ts-expect-error all cases should be covered\n throw new Error(`Unknown operation type ${op.type}`)\n}\n"],"names":["parsePath","stringifyPath"],"mappings":";;AA+FA,SAAS,4BACP,gBACwD;AACxD,SAAO,uBAAuB;AAChC;AAEA,SAAS,0BACP,gBACsD;AACtD,SAAO,qBAAqB;AAC9B;AAEA,SAAS,iBACP,gBAC6C;AAC7C,SAAO,YAAY;AACrB;AAEA,SAAS,iBACP,gBACwC;AACxC,SAAO,YAAY;AACrB;AAEA,SAAS,gBACP,gBACuC;AACvC,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AAO;AAAA,MACL,OAAO,CAACA,UAAAA,UAAc,KAAK,OAAO,GAAG,aAAa,CAAC,CAAC;AAAA,IAAA;AAIxD,QAAM,IAAI,MAAM,0BAA0B,GAAG,IAAI,EAAE;AACrD;;;;;;;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"getAtPath.cjs","sources":["../../src/path/get/getAtPath.ts"],"sourcesContent":["import {type AnyArray} from '../../utils/typeUtils'\nimport {type KeyedPathElement, type Path, type PathElement} from '../types'\nimport {isArrayElement, isKeyedElement} from '../utils/predicates'\nimport {type FindInArray} from './types'\n\nexport type {AnyArray} from '../../utils/typeUtils'\n\nexport type Get<\n P extends number | KeyedPathElement | Readonly<KeyedPathElement> | string,\n T,\n> = T extends AnyArray\n ? P extends KeyedPathElement | Readonly<KeyedPathElement> | number\n ? FindInArray<P, T>\n : undefined\n : P extends keyof T\n ? T[P]\n : never\n\nexport type GetAtPath<P extends readonly PathElement[], T> = P extends []\n ? T\n : P extends [infer Head, ...infer Tail]\n ? Head extends PathElement\n ? Tail extends PathElement[]\n ? GetAtPath<Tail, Get<Head, T>>\n : undefined\n : undefined\n : undefined\n\nexport function getAtPath<const Head extends PathElement, const T>(\n path: [head: Head],\n value: T,\n): Get<Head, T>\nexport function getAtPath<\n const Head extends PathElement,\n const Tail extends PathElement[],\n T,\n>(path: [head: Head, ...tail: Tail], value: T): GetAtPath<[Head, ...Tail], T>\nexport function getAtPath<T>(path: [], value: T): T\nexport function getAtPath(path: Path, value: unknown): unknown\nexport function getAtPath(path: Path, value: unknown): unknown {\n if (path.length === 0) {\n return value\n }\n\n let current = value\n for (const head of path) {\n if (isArrayElement(head)) {\n if (!Array.isArray(current)) {\n return undefined\n }\n\n if (isKeyedElement(head)) {\n current = current.find(item => item._key === head._key)\n continue\n }\n current = current[head]\n continue\n }\n current = (current as any)[head]\n }\n return current\n}\n"],"names":["isArrayElement","isKeyedElement"],"mappings":";;AAuCgB,SAAA,UAAU,MAAY,OAAyB;AAC7D,MAAI,KAAK,WAAW;AACX,WAAA;AAGT,MAAI,UAAU;AACd,aAAW,QAAQ,MAAM;AACnB,QAAAA,UAAAA,eAAe,IAAI,GAAG;AACpB,UAAA,CAAC,MAAM,QAAQ,OAAO;AACxB;AAGE,UAAAC,UAAAA,eAAe,IAAI,GAAG;AACxB,kBAAU,QAAQ,KAAK,CAAA,SAAQ,KAAK,SAAS,KAAK,IAAI;AACtD;AAAA,MAAA;AAEF,gBAAU,QAAQ,IAAI;AACtB;AAAA,IAAA;AAEF,cAAW,QAAgB,IAAI;AAAA,EAAA;AAE1B,SAAA;AACT;;"} | ||
| {"version":3,"file":"getAtPath.cjs","sources":["../../src/path/get/getAtPath.ts"],"sourcesContent":["import {type AnyArray} from '../../utils/typeUtils'\nimport {type KeyedPathElement, type Path, type PathElement} from '../types'\nimport {isArrayElement, isKeyedElement} from '../utils/predicates'\nimport {type FindInArray} from './types'\n\nexport type {AnyArray} from '../../utils/typeUtils'\n\nexport type Get<\n P extends number | KeyedPathElement | Readonly<KeyedPathElement> | string,\n T,\n> = T extends AnyArray\n ? P extends KeyedPathElement | Readonly<KeyedPathElement> | number\n ? FindInArray<P, T>\n : undefined\n : P extends keyof T\n ? T[P]\n : never\n\nexport type GetAtPath<P extends readonly PathElement[], T> = P extends []\n ? T\n : P extends [infer Head, ...infer Tail]\n ? Head extends PathElement\n ? Tail extends PathElement[]\n ? GetAtPath<Tail, Get<Head, T>>\n : undefined\n : undefined\n : undefined\n\nexport function getAtPath<const Head extends PathElement, const T>(\n path: [head: Head],\n value: T,\n): Get<Head, T>\nexport function getAtPath<\n const Head extends PathElement,\n const Tail extends PathElement[],\n T,\n>(path: [head: Head, ...tail: Tail], value: T): GetAtPath<[Head, ...Tail], T>\nexport function getAtPath<T>(path: [], value: T): T\nexport function getAtPath(path: Path, value: unknown): unknown\nexport function getAtPath(path: Path, value: unknown): unknown {\n if (path.length === 0) {\n return value\n }\n\n let current = value\n for (const head of path) {\n if (isArrayElement(head)) {\n if (!Array.isArray(current)) {\n return undefined\n }\n\n if (isKeyedElement(head)) {\n current = current.find(item => item._key === head._key)\n continue\n }\n current = current[head]\n continue\n }\n current = (current as any)[head]\n }\n return current\n}\n"],"names":["isArrayElement","isKeyedElement"],"mappings":";;AAuCO,SAAS,UAAU,MAAY,OAAyB;AAC7D,MAAI,KAAK,WAAW;AAClB,WAAO;AAGT,MAAI,UAAU;AACd,aAAW,QAAQ,MAAM;AACvB,QAAIA,UAAAA,eAAe,IAAI,GAAG;AACxB,UAAI,CAAC,MAAM,QAAQ,OAAO;AACxB;AAGF,UAAIC,UAAAA,eAAe,IAAI,GAAG;AACxB,kBAAU,QAAQ,KAAK,CAAA,SAAQ,KAAK,SAAS,KAAK,IAAI;AACtD;AAAA,MACF;AACA,gBAAU,QAAQ,IAAI;AACtB;AAAA,IACF;AACA,cAAW,QAAgB,IAAI;AAAA,EACjC;AACA,SAAO;AACT;;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"isObject.cjs","sources":["../../src/utils/isObject.ts"],"sourcesContent":["export function isObject(val: unknown): val is {\n [K in string]: unknown\n} {\n return val !== null && typeof val === 'object' && !Array.isArray(val)\n}\n"],"names":[],"mappings":";AAAO,SAAS,SAAS,KAEvB;AACO,SAAA,QAAQ,QAAQ,OAAO,OAAQ,YAAY,CAAC,MAAM,QAAQ,GAAG;AACtE;;"} | ||
| {"version":3,"file":"isObject.cjs","sources":["../../src/utils/isObject.ts"],"sourcesContent":["export function isObject(val: unknown): val is {\n [K in string]: unknown\n} {\n return val !== null && typeof val === 'object' && !Array.isArray(val)\n}\n"],"names":[],"mappings":";AAAO,SAAS,SAAS,KAEvB;AACA,SAAO,QAAQ,QAAQ,OAAO,OAAQ,YAAY,CAAC,MAAM,QAAQ,GAAG;AACtE;;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"parse.cjs","sources":["../../src/path/parser/parse.ts"],"sourcesContent":["import {type PathElement} from '../types'\nimport {type StringToPath} from './types'\n\nexport function parse<const T extends string>(path: T): StringToPath<T> {\n return path\n .split(/[[.\\]]/g)\n .filter(Boolean)\n .map(seg => (seg.includes('==') ? parseSegment(seg) : coerce(seg))) as any\n}\n\nconst IS_NUMERIC = /^-?\\d+$/\n\nfunction unquote(str: string) {\n return str.replace(/^['\"]/, '').replace(/['\"]$/, '')\n}\n\nfunction parseSegment(segment: string): PathElement {\n const [key, value] = segment.split('==')\n if (key !== '_key') {\n throw new Error(\n `Currently only \"_key\" is supported as path segment. Found ${key}`,\n )\n }\n if (typeof value === 'undefined') {\n throw new Error('Invalid path segment, expected `key==\"value\"`')\n }\n return {_key: unquote(value)}\n}\n\nfunction coerce(segment: string): PathElement {\n return IS_NUMERIC.test(segment) ? Number(segment) : segment\n}\n"],"names":[],"mappings":";AAGO,SAAS,MAA8B,MAA0B;AACtE,SAAO,KACJ,MAAM,SAAS,EACf,OAAO,OAAO,EACd,IAAI,CAAA,QAAQ,IAAI,SAAS,IAAI,IAAI,aAAa,GAAG,IAAI,OAAO,GAAG,CAAE;AACtE;AAEA,MAAM,aAAa;AAEnB,SAAS,QAAQ,KAAa;AAC5B,SAAO,IAAI,QAAQ,SAAS,EAAE,EAAE,QAAQ,SAAS,EAAE;AACrD;AAEA,SAAS,aAAa,SAA8B;AAClD,QAAM,CAAC,KAAK,KAAK,IAAI,QAAQ,MAAM,IAAI;AACvC,MAAI,QAAQ;AACV,UAAM,IAAI;AAAA,MACR,6DAA6D,GAAG;AAAA,IAClE;AAEF,MAAI,OAAO,QAAU;AACb,UAAA,IAAI,MAAM,+CAA+C;AAEjE,SAAO,EAAC,MAAM,QAAQ,KAAK,EAAC;AAC9B;AAEA,SAAS,OAAO,SAA8B;AAC5C,SAAO,WAAW,KAAK,OAAO,IAAI,OAAO,OAAO,IAAI;AACtD;;"} | ||
| {"version":3,"file":"parse.cjs","sources":["../../src/path/parser/parse.ts"],"sourcesContent":["import {type PathElement} from '../types'\nimport {type StringToPath} from './types'\n\nexport function parse<const T extends string>(path: T): StringToPath<T> {\n return path\n .split(/[[.\\]]/g)\n .filter(Boolean)\n .map(seg => (seg.includes('==') ? parseSegment(seg) : coerce(seg))) as any\n}\n\nconst IS_NUMERIC = /^-?\\d+$/\n\nfunction unquote(str: string) {\n return str.replace(/^['\"]/, '').replace(/['\"]$/, '')\n}\n\nfunction parseSegment(segment: string): PathElement {\n const [key, value] = segment.split('==')\n if (key !== '_key') {\n throw new Error(\n `Currently only \"_key\" is supported as path segment. Found ${key}`,\n )\n }\n if (typeof value === 'undefined') {\n throw new Error('Invalid path segment, expected `key==\"value\"`')\n }\n return {_key: unquote(value)}\n}\n\nfunction coerce(segment: string): PathElement {\n return IS_NUMERIC.test(segment) ? Number(segment) : segment\n}\n"],"names":[],"mappings":";AAGO,SAAS,MAA8B,MAA0B;AACtE,SAAO,KACJ,MAAM,SAAS,EACf,OAAO,OAAO,EACd,IAAI,CAAA,QAAQ,IAAI,SAAS,IAAI,IAAI,aAAa,GAAG,IAAI,OAAO,GAAG,CAAE;AACtE;AAEA,MAAM,aAAa;AAEnB,SAAS,QAAQ,KAAa;AAC5B,SAAO,IAAI,QAAQ,SAAS,EAAE,EAAE,QAAQ,SAAS,EAAE;AACrD;AAEA,SAAS,aAAa,SAA8B;AAClD,QAAM,CAAC,KAAK,KAAK,IAAI,QAAQ,MAAM,IAAI;AACvC,MAAI,QAAQ;AACV,UAAM,IAAI;AAAA,MACR,6DAA6D,GAAG;AAAA,IAAA;AAGpE,MAAI,OAAO,QAAU;AACnB,UAAM,IAAI,MAAM,+CAA+C;AAEjE,SAAO,EAAC,MAAM,QAAQ,KAAK,EAAA;AAC7B;AAEA,SAAS,OAAO,SAA8B;AAC5C,SAAO,WAAW,KAAK,OAAO,IAAI,OAAO,OAAO,IAAI;AACtD;;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"stringify.cjs","sources":["../../src/path/utils/predicates.ts","../../src/path/parser/stringify.ts"],"sourcesContent":["import {type KeyedPathElement, type Path, type PathElement} from '../types'\n\nfunction safeGetElementAt<T>(array: T[] | readonly T[], index: number): T {\n if (index < 0 || index >= array.length) {\n throw new Error('Index out of bounds')\n }\n return array[index]!\n}\n\nexport function startsWith(parentPath: Path, path: Path): boolean {\n return (\n parentPath.length <= path.length &&\n parentPath.every((segment, i) =>\n isElementEqual(segment, safeGetElementAt(path, i)),\n )\n )\n}\n\nexport function isEqual(path: Path, otherPath: Path): boolean {\n return (\n path.length === otherPath.length &&\n path.every((segment, i) =>\n isElementEqual(segment, safeGetElementAt(path, i)),\n )\n )\n}\n\nexport function isElementEqual(\n segmentA: PathElement,\n segmentB: PathElement,\n): boolean {\n if (isKeyElement(segmentA) && isKeyElement(segmentB)) {\n return segmentA._key === segmentB._key\n }\n\n if (isIndexElement(segmentA)) {\n return Number(segmentA) === Number(segmentB)\n }\n\n return segmentA === segmentB\n}\n\nexport function isKeyElement(\n segment: PathElement,\n): segment is KeyedPathElement {\n return typeof (segment as any)?._key === 'string'\n}\nexport function isIndexElement(segment: PathElement): segment is number {\n return typeof segment === 'number'\n}\n\nexport function isKeyedElement(\n element: PathElement,\n): element is KeyedPathElement {\n return (\n typeof element === 'object' &&\n '_key' in element &&\n typeof element._key === 'string'\n )\n}\nexport function isArrayElement(\n element: PathElement,\n): element is KeyedPathElement | number {\n return typeof element === 'number' || isKeyedElement(element)\n}\n\nexport function isPropertyElement(element: PathElement): element is string {\n return typeof element === 'string'\n}\n","import {type Path, type PathElement} from '../types'\nimport {isKeyedElement} from '../utils/predicates'\n\nconst IS_DOTTABLE = /^[a-z_$]+/\n\nfunction stringifySegment(segment: PathElement, hasLeading: boolean): string {\n if (Array.isArray(segment)) {\n return `[${segment[0]}:${segment[1] || ''}]`\n }\n const type = typeof segment\n\n const isNumber = type === 'number'\n\n if (isNumber) {\n return `[${segment}]`\n }\n\n if (isKeyedElement(segment)) {\n return `[_key==${JSON.stringify(segment._key)}]`\n }\n\n if (typeof segment === 'string' && IS_DOTTABLE.test(segment)) {\n return hasLeading ? segment : `.${segment}`\n }\n\n return `['${segment}']`\n}\n\nexport function stringify(pathArray: Path): string {\n return pathArray\n .map((segment, i) => stringifySegment(segment, i === 0))\n .join('')\n}\n"],"names":[],"mappings":";AAEA,SAAS,iBAAoB,OAA2B,OAAkB;AACpE,MAAA,QAAQ,KAAK,SAAS,MAAM;AACxB,UAAA,IAAI,MAAM,qBAAqB;AAEvC,SAAO,MAAM,KAAK;AACpB;AAEgB,SAAA,WAAW,YAAkB,MAAqB;AAChE,SACE,WAAW,UAAU,KAAK,UAC1B,WAAW;AAAA,IAAM,CAAC,SAAS,MACzB,eAAe,SAAS,iBAAiB,MAAM,CAAC,CAAC;AAAA,EACnD;AAEJ;AAEgB,SAAA,QAAQ,MAAY,WAA0B;AAC5D,SACE,KAAK,WAAW,UAAU,UAC1B,KAAK;AAAA,IAAM,CAAC,SAAS,MACnB,eAAe,SAAS,iBAAiB,MAAM,CAAC,CAAC;AAAA,EACnD;AAEJ;AAEgB,SAAA,eACd,UACA,UACS;AACT,SAAI,aAAa,QAAQ,KAAK,aAAa,QAAQ,IAC1C,SAAS,SAAS,SAAS,OAGhC,eAAe,QAAQ,IAClB,OAAO,QAAQ,MAAM,OAAO,QAAQ,IAGtC,aAAa;AACtB;AAEO,SAAS,aACd,SAC6B;AACtB,SAAA,OAAQ,SAAiB,QAAS;AAC3C;AACO,SAAS,eAAe,SAAyC;AACtE,SAAO,OAAO,WAAY;AAC5B;AAEO,SAAS,eACd,SAC6B;AAC7B,SACE,OAAO,WAAY,YACnB,UAAU,WACV,OAAO,QAAQ,QAAS;AAE5B;AACO,SAAS,eACd,SACsC;AACtC,SAAO,OAAO,WAAY,YAAY,eAAe,OAAO;AAC9D;AAEO,SAAS,kBAAkB,SAAyC;AACzE,SAAO,OAAO,WAAY;AAC5B;ACjEA,MAAM,cAAc;AAEpB,SAAS,iBAAiB,SAAsB,YAA6B;AAC3E,SAAI,MAAM,QAAQ,OAAO,IAChB,IAAI,QAAQ,CAAC,CAAC,IAAI,QAAQ,CAAC,KAAK,EAAE,MAE9B,OAAO,WAEM,WAGjB,IAAI,OAAO,MAGhB,eAAe,OAAO,IACjB,UAAU,KAAK,UAAU,QAAQ,IAAI,CAAC,MAG3C,OAAO,WAAY,YAAY,YAAY,KAAK,OAAO,IAClD,aAAa,UAAU,IAAI,OAAO,KAGpC,KAAK,OAAO;AACrB;AAEO,SAAS,UAAU,WAAyB;AACjD,SAAO,UACJ,IAAI,CAAC,SAAS,MAAM,iBAAiB,SAAS,MAAM,CAAC,CAAC,EACtD,KAAK,EAAE;AACZ;;;;;;;;;;"} | ||
| {"version":3,"file":"stringify.cjs","sources":["../../src/path/utils/predicates.ts","../../src/path/parser/stringify.ts"],"sourcesContent":["import {type KeyedPathElement, type Path, type PathElement} from '../types'\n\nfunction safeGetElementAt<T>(array: T[] | readonly T[], index: number): T {\n if (index < 0 || index >= array.length) {\n throw new Error('Index out of bounds')\n }\n return array[index]!\n}\n\nexport function startsWith(parentPath: Path, path: Path): boolean {\n return (\n parentPath.length <= path.length &&\n parentPath.every((segment, i) =>\n isElementEqual(segment, safeGetElementAt(path, i)),\n )\n )\n}\n\nexport function isEqual(path: Path, otherPath: Path): boolean {\n return (\n path.length === otherPath.length &&\n path.every((segment, i) =>\n isElementEqual(segment, safeGetElementAt(path, i)),\n )\n )\n}\n\nexport function isElementEqual(\n segmentA: PathElement,\n segmentB: PathElement,\n): boolean {\n if (isKeyElement(segmentA) && isKeyElement(segmentB)) {\n return segmentA._key === segmentB._key\n }\n\n if (isIndexElement(segmentA)) {\n return Number(segmentA) === Number(segmentB)\n }\n\n return segmentA === segmentB\n}\n\nexport function isKeyElement(\n segment: PathElement,\n): segment is KeyedPathElement {\n return typeof (segment as any)?._key === 'string'\n}\nexport function isIndexElement(segment: PathElement): segment is number {\n return typeof segment === 'number'\n}\n\nexport function isKeyedElement(\n element: PathElement,\n): element is KeyedPathElement {\n return (\n typeof element === 'object' &&\n '_key' in element &&\n typeof element._key === 'string'\n )\n}\nexport function isArrayElement(\n element: PathElement,\n): element is KeyedPathElement | number {\n return typeof element === 'number' || isKeyedElement(element)\n}\n\nexport function isPropertyElement(element: PathElement): element is string {\n return typeof element === 'string'\n}\n","import {type Path, type PathElement} from '../types'\nimport {isKeyedElement} from '../utils/predicates'\n\nconst IS_DOTTABLE = /^[a-z_$]+/\n\nfunction stringifySegment(segment: PathElement, hasLeading: boolean): string {\n if (Array.isArray(segment)) {\n return `[${segment[0]}:${segment[1] || ''}]`\n }\n const type = typeof segment\n\n const isNumber = type === 'number'\n\n if (isNumber) {\n return `[${segment}]`\n }\n\n if (isKeyedElement(segment)) {\n return `[_key==${JSON.stringify(segment._key)}]`\n }\n\n if (typeof segment === 'string' && IS_DOTTABLE.test(segment)) {\n return hasLeading ? segment : `.${segment}`\n }\n\n return `['${segment}']`\n}\n\nexport function stringify(pathArray: Path): string {\n return pathArray\n .map((segment, i) => stringifySegment(segment, i === 0))\n .join('')\n}\n"],"names":[],"mappings":";AAEA,SAAS,iBAAoB,OAA2B,OAAkB;AACxE,MAAI,QAAQ,KAAK,SAAS,MAAM;AAC9B,UAAM,IAAI,MAAM,qBAAqB;AAEvC,SAAO,MAAM,KAAK;AACpB;AAEO,SAAS,WAAW,YAAkB,MAAqB;AAChE,SACE,WAAW,UAAU,KAAK,UAC1B,WAAW;AAAA,IAAM,CAAC,SAAS,MACzB,eAAe,SAAS,iBAAiB,MAAM,CAAC,CAAC;AAAA,EAAA;AAGvD;AAEO,SAAS,QAAQ,MAAY,WAA0B;AAC5D,SACE,KAAK,WAAW,UAAU,UAC1B,KAAK;AAAA,IAAM,CAAC,SAAS,MACnB,eAAe,SAAS,iBAAiB,MAAM,CAAC,CAAC;AAAA,EAAA;AAGvD;AAEO,SAAS,eACd,UACA,UACS;AACT,SAAI,aAAa,QAAQ,KAAK,aAAa,QAAQ,IAC1C,SAAS,SAAS,SAAS,OAGhC,eAAe,QAAQ,IAClB,OAAO,QAAQ,MAAM,OAAO,QAAQ,IAGtC,aAAa;AACtB;AAEO,SAAS,aACd,SAC6B;AAC7B,SAAO,OAAQ,SAAiB,QAAS;AAC3C;AACO,SAAS,eAAe,SAAyC;AACtE,SAAO,OAAO,WAAY;AAC5B;AAEO,SAAS,eACd,SAC6B;AAC7B,SACE,OAAO,WAAY,YACnB,UAAU,WACV,OAAO,QAAQ,QAAS;AAE5B;AACO,SAAS,eACd,SACsC;AACtC,SAAO,OAAO,WAAY,YAAY,eAAe,OAAO;AAC9D;AAEO,SAAS,kBAAkB,SAAyC;AACzE,SAAO,OAAO,WAAY;AAC5B;ACjEA,MAAM,cAAc;AAEpB,SAAS,iBAAiB,SAAsB,YAA6B;AAC3E,SAAI,MAAM,QAAQ,OAAO,IAChB,IAAI,QAAQ,CAAC,CAAC,IAAI,QAAQ,CAAC,KAAK,EAAE,MAE9B,OAAO,WAEM,WAGjB,IAAI,OAAO,MAGhB,eAAe,OAAO,IACjB,UAAU,KAAK,UAAU,QAAQ,IAAI,CAAC,MAG3C,OAAO,WAAY,YAAY,YAAY,KAAK,OAAO,IAClD,aAAa,UAAU,IAAI,OAAO,KAGpC,KAAK,OAAO;AACrB;AAEO,SAAS,UAAU,WAAyB;AACjD,SAAO,UACJ,IAAI,CAAC,SAAS,MAAM,iBAAiB,SAAS,MAAM,CAAC,CAAC,EACtD,KAAK,EAAE;AACZ;;;;;;;;;;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"utils.cjs","sources":["../../src/apply/utils/getKeyOf.ts","../../src/apply/utils/array.ts","../../src/apply/patch/operations/array.ts","../../src/apply/patch/operations/common.ts","../../src/apply/patch/operations/number.ts","../../src/apply/utils/hasOwn.ts","../../src/apply/utils/isEmpty.ts","../../src/apply/utils/omit.ts","../../src/apply/patch/operations/object.ts","../../src/apply/patch/operations/string.ts","../../src/apply/patch/applyOp.ts","../../src/apply/patch/applyNodePatch.ts","../../src/apply/applyPatchMutation.ts","../../src/apply/store/utils.ts"],"sourcesContent":["export function keyOf(value: any): string | null {\n return (\n (value !== null &&\n typeof value === 'object' &&\n typeof value._key === 'string' &&\n value._key) ||\n null\n )\n}\n","import {isKeyedElement, type PathElement} from '../../path'\nimport {keyOf} from './getKeyOf'\n\nexport function findTargetIndex<T>(array: T[], pathSegment: PathElement) {\n if (typeof pathSegment === 'number') {\n return normalizeIndex(array.length, pathSegment)\n }\n if (isKeyedElement(pathSegment)) {\n const idx = array.findIndex(value => keyOf(value) === pathSegment._key)\n return idx === -1 ? null : idx\n }\n throw new Error(\n `Expected path segment to be addressing a single array item either by numeric index or by '_key'. Instead saw ${JSON.stringify(\n pathSegment,\n )}`,\n )\n}\n\nexport function getTargetIdx(position: 'before' | 'after', index: number) {\n return position === 'before' ? index : index + 1\n}\n\n// normalizes the index according to the array length\n// returns null if the normalized index is out of bounds\nexport function normalizeIndex(length: number, index: number) {\n if (length === 0 && (index === -1 || index === 0)) {\n return 0\n }\n const normalized = index < 0 ? length + index : index\n return normalized >= length || normalized < 0 ? null : normalized\n}\n\n// non-mutating splice\nexport function splice<T>(arr: T[], start: number, deleteCount: number): T[]\nexport function splice<T>(\n arr: T[],\n start: number,\n deleteCount: number,\n items: T[],\n): T[]\nexport function splice<T>(\n arr: T[],\n start: number,\n deleteCount: number,\n items?: T[],\n): T[] {\n const copy = arr.slice()\n copy.splice(start, deleteCount, ...(items || []))\n return copy\n}\n","import {\n type InsertOp,\n type KeyedPathElement,\n type RelativePosition,\n type RemoveOp,\n type ReplaceOp,\n type TruncateOp,\n type UpsertOp,\n} from '../../../mutations/operations/types'\nimport {findTargetIndex, getTargetIdx, splice} from '../../utils/array'\n\nexport function insert<\n O extends InsertOp<unknown[], RelativePosition, number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"insert()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to insert ${op.position}`)\n }\n // special case for empty arrays\n if (currentValue.length === 0) {\n return op.items\n }\n return splice(currentValue, getTargetIdx(op.position, index), 0, op.items)\n}\n\nexport function upsert<\n O extends UpsertOp<unknown[], RelativePosition, number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"upsert()\" on non-array value')\n }\n\n if (op.items.length === 0) {\n return currentValue\n }\n const replaceItemsMap: number[] = []\n const insertItems: unknown[] = []\n op.items.forEach((itemToBeUpserted: any, i) => {\n const existingIndex = currentValue.findIndex(\n existingItem => (existingItem as any)?._key === itemToBeUpserted._key,\n )\n if (existingIndex >= 0) {\n replaceItemsMap[existingIndex] = i\n } else {\n insertItems.push(itemToBeUpserted)\n }\n })\n\n if (replaceItemsMap.length === 0 && insertItems.length == 0) {\n return currentValue\n }\n\n const next = [...currentValue]\n // Replace existing items\n for (const i of replaceItemsMap) {\n next[i] = op.items[replaceItemsMap[i]!]!\n }\n\n // Insert the items that doesn't exist\n return insert(\n {\n type: 'insert',\n items: insertItems,\n referenceItem: op.referenceItem,\n position: op.position,\n },\n next,\n )\n}\n\nexport function replace<\n O extends ReplaceOp<unknown[], number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"replace()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to replace`)\n }\n return splice(currentValue, index, op.items.length, op.items)\n}\nexport function remove<\n O extends RemoveOp<number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"remove()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to replace`)\n }\n return splice(currentValue, index, 1, [])\n}\n\nexport function truncate<O extends TruncateOp, CurrentValue extends unknown[]>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"truncate()\" on non-array value')\n }\n\n return typeof op.endIndex === 'number'\n ? currentValue\n .slice(0, op.startIndex)\n .concat(currentValue.slice(op.endIndex))\n : currentValue.slice(0, op.startIndex)\n}\n","import {\n type SetIfMissingOp,\n type SetOp,\n type UnsetOp,\n} from '../../../mutations/operations/types'\n\nexport function set<O extends SetOp<any>, CurrentValue>(\n op: O,\n currentValue: CurrentValue,\n) {\n return op.value\n}\n\nexport function setIfMissing<O extends SetIfMissingOp<any>, CurrentValue>(\n op: O,\n currentValue: CurrentValue,\n) {\n return currentValue ?? op.value\n}\n\nexport function unset<O extends UnsetOp, CurrentValue>(op: O) {\n return undefined\n}\n","import {type DecOp, type IncOp} from '../../../mutations/operations/types'\n\nexport function inc<O extends IncOp<number>, CurrentValue extends number>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (typeof currentValue !== 'number') {\n throw new TypeError('Cannot apply \"inc()\" on non-numeric value')\n }\n\n return currentValue + op.amount\n}\n\nexport function dec<O extends DecOp<number>, CurrentValue extends number>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (typeof currentValue !== 'number') {\n throw new TypeError('Cannot apply \"dec()\" on non-numeric value')\n }\n\n return currentValue - op.amount\n}\n","export const hasOwn = Object.prototype.hasOwnProperty.call.bind(\n Object.prototype.hasOwnProperty,\n)\n","import {hasOwn} from './hasOwn'\n\nexport function isEmpty(v: object) {\n for (const key in v) {\n if (hasOwn(v, key)) {\n return false\n }\n }\n return true\n}\n","export function omit<T, K extends keyof T>(val: T, props: K[]): Omit<T, K> {\n const copy = {...val}\n for (const prop of props) {\n delete copy[prop]\n }\n return copy\n}\n","import {\n type AssignOp,\n type UnassignOp,\n} from '../../../mutations/operations/types'\nimport {isObject} from '../../../utils/isObject'\nimport {isEmpty} from '../../utils/isEmpty'\nimport {omit} from '../../utils/omit'\n\nexport function unassign<T extends object, K extends string[]>(\n op: UnassignOp<K>,\n currentValue: T,\n) {\n if (!isObject(currentValue)) {\n throw new TypeError('Cannot apply \"unassign()\" on non-object value')\n }\n\n return op.keys.length === 0\n ? currentValue\n : omit(currentValue, op.keys as any[])\n}\n\nexport function assign<T extends object>(op: AssignOp<T>, currentValue: T) {\n if (!isObject(currentValue)) {\n throw new TypeError('Cannot apply \"assign()\" on non-object value')\n }\n\n return isEmpty(op.value) ? currentValue : {...currentValue, ...op.value}\n}\n","import {applyPatches, parsePatch} from '@sanity/diff-match-patch'\n\nimport {type DiffMatchPatchOp} from '../../../mutations/operations/types'\n\nexport function diffMatchPatch<\n O extends DiffMatchPatchOp,\n CurrentValue extends string,\n>(op: O, currentValue: CurrentValue) {\n if (typeof currentValue !== 'string') {\n throw new TypeError('Cannot apply \"diffMatchPatch()\" on non-string value')\n }\n\n return applyPatches(parsePatch(op.value), currentValue)[0]\n}\n","import {\n type AnyOp,\n type ArrayOp,\n type NumberOp,\n type ObjectOp,\n type Operation,\n type StringOp,\n} from '../../mutations/operations/types'\nimport {type AnyArray} from '../../utils/typeUtils'\nimport * as operations from './operations'\nimport {type ApplyOp} from './typings/applyOp'\n\nexport function applyOp<const Op extends AnyOp, const CurrentValue>(\n op: Op,\n currentValue: CurrentValue,\n): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends NumberOp,\n const CurrentValue extends number,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends StringOp,\n const CurrentValue extends string,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends ObjectOp,\n const CurrentValue extends {[k in keyof any]: unknown},\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends ArrayOp,\n const CurrentValue extends AnyArray,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<const Op extends Operation, const CurrentValue>(\n op: Op,\n currentValue: CurrentValue,\n): ApplyOp<Op, CurrentValue> {\n if (!(op.type in operations)) {\n throw new Error(`Invalid operation type: \"${op.type}\"`)\n }\n\n // eslint-disable-next-line import/namespace\n return (operations[op.type] as CallableFunction)(op, currentValue)\n}\n","import {type Operation} from '../../mutations/operations/types'\nimport {type NodePatch, type NodePatchList} from '../../mutations/types'\nimport {isArrayElement, isPropertyElement, stringify} from '../../path'\nimport {isObject} from '../../utils/isObject'\nimport {type NormalizeReadOnlyArray} from '../../utils/typeUtils'\nimport {type KeyedPathElement, type Path} from '../'\nimport {findTargetIndex, splice} from '../utils/array'\nimport {applyOp} from './applyOp'\nimport {\n type ApplyAtPath,\n type ApplyNodePatch,\n type ApplyPatches,\n} from './typings/applyNodePatch'\n\nexport function applyPatches<Patches extends NodePatchList, const Doc>(\n patches: Patches,\n document: Doc,\n): ApplyPatches<NormalizeReadOnlyArray<Patches>, Doc> {\n return (patches as NodePatch[]).reduce(\n (prev, patch) => applyNodePatch(patch, prev) as any,\n document,\n ) as any\n}\n\nexport function applyNodePatch<const Patch extends NodePatch, const Doc>(\n patch: Patch,\n document: Doc,\n): ApplyNodePatch<Patch, Doc> {\n return applyAtPath(patch.path, patch.op, document) as any\n}\n\nfunction applyAtPath<P extends Path, O extends Operation, T>(\n path: P,\n op: O,\n value: T,\n): ApplyAtPath<P, O, T> {\n if (!isNonEmptyArray(path)) {\n return applyOp(op as any, value) as any\n }\n\n const [head, ...tail] = path\n\n if (isArrayElement(head) && Array.isArray(value)) {\n return applyInArray(head, tail, op, value) as any\n }\n\n if (isPropertyElement(head) && isObject(value)) {\n return applyInObject(head, tail, op, value) as any\n }\n\n throw new Error(\n `Cannot apply operation of type \"${op.type}\" to path ${stringify(\n path,\n )} on ${typeof value} value`,\n )\n}\n\nfunction applyInObject<Key extends keyof any, T extends {[key in Key]?: any}>(\n head: Key,\n tail: Path,\n op: Operation,\n object: T,\n) {\n const current = object[head]\n\n if (current === undefined && tail.length > 0) {\n return object\n }\n\n // The patch targets the item at the index specified by \"head\"\n // so forward it to the item\n const patchedValue = applyAtPath(tail, op, current)\n\n // If the result of applying it to the item yields the item back we assume it was\n // a noop and don't modify our value. If we get a new value back, we return a\n // new array with the modified item replaced\n return patchedValue === current ? object : {...object, [head]: patchedValue}\n}\n\nfunction applyInArray<T>(\n head: number | KeyedPathElement,\n tail: Path,\n op: Operation,\n value: T[],\n) {\n const index = findTargetIndex(value, head!)\n\n if (index === null) {\n // partial is default behavior for arrays\n // the patch targets an index that is out of bounds\n return value\n }\n\n // If the given selector could not be found, return as-is\n if (index === -1) {\n return value\n }\n\n const current = value[index]!\n\n // The patch targets the item at the index specified by \"head\"\n // so forward it to the item\n const patchedItem = applyAtPath(tail, op, current)\n\n // If the result of applying it to the item yields the item back we assume it was\n // a noop and don't modify our value. If we get a new value back, we return a\n // new array with the modified item replaced\n return patchedItem === current\n ? value\n : splice(value, index, 1, [patchedItem])\n}\n\nfunction isNonEmptyArray<T>(a: T[] | readonly T[]): a is [T, ...T[]] {\n return a.length > 0\n}\n","import {type PatchMutation, type SanityDocumentBase} from '../mutations/types'\nimport {type NormalizeReadOnlyArray} from '../utils/typeUtils'\nimport {applyPatches} from './patch/applyNodePatch'\nimport {type ApplyPatches} from './patch/typings/applyNodePatch'\n\nexport type ApplyPatchMutation<\n Mutation extends PatchMutation,\n Doc extends SanityDocumentBase,\n> =\n Mutation extends PatchMutation<infer Patches>\n ? ApplyPatches<NormalizeReadOnlyArray<Patches>, Doc>\n : Doc\n\nexport function applyPatchMutation<\n const Mutation extends PatchMutation,\n const Doc extends SanityDocumentBase,\n>(mutation: Mutation, document: Doc): ApplyPatchMutation<Mutation, Doc> {\n if (\n mutation.options?.ifRevision &&\n document._rev !== mutation.options.ifRevision\n ) {\n throw new Error('Revision mismatch')\n }\n if (mutation.id !== document._id) {\n throw new Error(\n `Document id mismatch. Refusing to apply mutation for document with id=\"${mutation.id}\" on the given document with id=\"${document._id}\"`,\n )\n }\n return applyPatches(mutation.patches, document) as any\n}\n","import {type SanityDocumentBase} from '../../mutations/types'\nimport {type StoredDocument} from '../applyInIndex'\n\nexport function hasId(doc: SanityDocumentBase): doc is StoredDocument {\n return '_id' in doc\n}\nexport function assignId<Doc extends SanityDocumentBase>(\n doc: Doc,\n generateId: () => string,\n): Doc & {_id: string} {\n return hasId(doc) ? doc : {...doc, _id: generateId()}\n}\n"],"names":["isKeyedElement","isObject","applyPatches","parsePatch","isArrayElement","isPropertyElement","stringify"],"mappings":";;AAAO,SAAS,MAAM,OAA2B;AAE5C,SAAA,UAAU,QACT,OAAO,SAAU,YACjB,OAAO,MAAM,QAAS,YACtB,MAAM,QACR;AAEJ;ACLgB,SAAA,gBAAmB,OAAY,aAA0B;AACvE,MAAI,OAAO,eAAgB;AAClB,WAAA,eAAe,MAAM,QAAQ,WAAW;AAE7C,MAAAA,UAAAA,eAAe,WAAW,GAAG;AACzB,UAAA,MAAM,MAAM,UAAU,CAAA,UAAS,MAAM,KAAK,MAAM,YAAY,IAAI;AAC/D,WAAA,QAAQ,KAAK,OAAO;AAAA,EAAA;AAE7B,QAAM,IAAI;AAAA,IACR,gHAAgH,KAAK;AAAA,MACnH;AAAA,IAAA,CACD;AAAA,EACH;AACF;AAEgB,SAAA,aAAa,UAA8B,OAAe;AACjE,SAAA,aAAa,WAAW,QAAQ,QAAQ;AACjD;AAIgB,SAAA,eAAe,QAAgB,OAAe;AAC5D,MAAI,WAAW,MAAM,UAAU,MAAM,UAAU;AACtC,WAAA;AAET,QAAM,aAAa,QAAQ,IAAI,SAAS,QAAQ;AAChD,SAAO,cAAc,UAAU,aAAa,IAAI,OAAO;AACzD;AAUO,SAAS,OACd,KACA,OACA,aACA,OACK;AACC,QAAA,OAAO,IAAI,MAAM;AACvB,SAAA,KAAK,OAAO,OAAO,aAAa,GAAI,SAAS,CAAG,CAAA,GACzC;AACT;ACtCgB,SAAA,OAGd,IAAO,cAA4B;AAC/B,MAAA,CAAC,MAAM,QAAQ,YAAY;AACvB,UAAA,IAAI,UAAU,4CAA4C;A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AmC,GAAG,IAAI,aAAaK,UAAA;AAAA,MACrD;AAAA,IAAA,CACD,OAAO,OAAO,KAAK;AAAA,EACtB;AACF;AAEA,SAAS,cACP,MACA,MACA,IACA,QACA;AACM,QAAA,UAAU,OAAO,IAAI;AAEvB,MAAA,YAAY,UAAa,KAAK,SAAS;AAClC,WAAA;AAKT,QAAM,eAAe,YAAY,MAAM,IAAI,OAAO;AAK3C,SAAA,iBAAiB,UAAU,SAAS,EAAC,GAAG,QAAQ,CAAC,IAAI,GAAG,aAAY;AAC7E;AAEA,SAAS,aACP,MACA,MACA,IACA,OACA;AACM,QAAA,QAAQ,gBAAgB,OAAO,IAAK;AAEtC,MAAA,UAAU,QAOV,UAAU;AACL,WAAA;AAGH,QAAA,UAAU,MAAM,KAAK,GAIrB,cAAc,YAAY,MAAM,IAAI,OAAO;AAK1C,SAAA,gBAAgB,UACnB,QACA,OAAO,OAAO,OAAO,GAAG,CAAC,WAAW,CAAC;AAC3C;AAEA,SAAS,gBAAmB,GAAyC;AACnE,SAAO,EAAE,SAAS;AACpB;ACrGgB,SAAA,mBAGd,UAAoB,UAAkD;AACtE,MACE,SAAS,SAAS,cAClB,SAAS,SAAS,SAAS,QAAQ;AAE7B,UAAA,IAAI,MAAM,mBAAmB;AAEjC,MAAA,SAAS,OAAO,SAAS;AAC3B,UAAM,IAAI;AAAA,MACR,0EAA0E,SAAS,EAAE,oCAAoC,SAAS,GAAG;AAAA,IACvI;AAEK,SAAA,aAAa,SAAS,SAAS,QAAQ;AAChD;AC1BO,SAAS,MAAM,KAAgD;AACpE,SAAO,SAAS;AAClB;AACgB,SAAA,SACd,KACA,YACqB;AACd,SAAA,MAAM,GAAG,IAAI,MAAM,EAAC,GAAG,KAAK,KAAK,aAAY;AACtD;;;;;;;;"} | ||
| {"version":3,"file":"utils.cjs","sources":["../../src/apply/utils/getKeyOf.ts","../../src/apply/utils/array.ts","../../src/apply/patch/operations/array.ts","../../src/apply/patch/operations/common.ts","../../src/apply/patch/operations/number.ts","../../src/apply/utils/hasOwn.ts","../../src/apply/utils/isEmpty.ts","../../src/apply/utils/omit.ts","../../src/apply/patch/operations/object.ts","../../src/apply/patch/operations/string.ts","../../src/apply/patch/applyOp.ts","../../src/apply/patch/applyNodePatch.ts","../../src/apply/applyPatchMutation.ts","../../src/apply/store/utils.ts"],"sourcesContent":["export function keyOf(value: any): string | null {\n return (\n (value !== null &&\n typeof value === 'object' &&\n typeof value._key === 'string' &&\n value._key) ||\n null\n )\n}\n","import {isKeyedElement, type PathElement} from '../../path'\nimport {keyOf} from './getKeyOf'\n\nexport function findTargetIndex<T>(array: T[], pathSegment: PathElement) {\n if (typeof pathSegment === 'number') {\n return normalizeIndex(array.length, pathSegment)\n }\n if (isKeyedElement(pathSegment)) {\n const idx = array.findIndex(value => keyOf(value) === pathSegment._key)\n return idx === -1 ? null : idx\n }\n throw new Error(\n `Expected path segment to be addressing a single array item either by numeric index or by '_key'. Instead saw ${JSON.stringify(\n pathSegment,\n )}`,\n )\n}\n\nexport function getTargetIdx(position: 'before' | 'after', index: number) {\n return position === 'before' ? index : index + 1\n}\n\n// normalizes the index according to the array length\n// returns null if the normalized index is out of bounds\nexport function normalizeIndex(length: number, index: number) {\n if (length === 0 && (index === -1 || index === 0)) {\n return 0\n }\n const normalized = index < 0 ? length + index : index\n return normalized >= length || normalized < 0 ? null : normalized\n}\n\n// non-mutating splice\nexport function splice<T>(arr: T[], start: number, deleteCount: number): T[]\nexport function splice<T>(\n arr: T[],\n start: number,\n deleteCount: number,\n items: T[],\n): T[]\nexport function splice<T>(\n arr: T[],\n start: number,\n deleteCount: number,\n items?: T[],\n): T[] {\n const copy = arr.slice()\n copy.splice(start, deleteCount, ...(items || []))\n return copy\n}\n","import {\n type InsertOp,\n type KeyedPathElement,\n type RelativePosition,\n type RemoveOp,\n type ReplaceOp,\n type TruncateOp,\n type UpsertOp,\n} from '../../../mutations/operations/types'\nimport {findTargetIndex, getTargetIdx, splice} from '../../utils/array'\n\nexport function insert<\n O extends InsertOp<unknown[], RelativePosition, number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"insert()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to insert ${op.position}`)\n }\n // special case for empty arrays\n if (currentValue.length === 0) {\n return op.items\n }\n return splice(currentValue, getTargetIdx(op.position, index), 0, op.items)\n}\n\nexport function upsert<\n O extends UpsertOp<unknown[], RelativePosition, number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"upsert()\" on non-array value')\n }\n\n if (op.items.length === 0) {\n return currentValue\n }\n const replaceItemsMap: number[] = []\n const insertItems: unknown[] = []\n op.items.forEach((itemToBeUpserted: any, i) => {\n const existingIndex = currentValue.findIndex(\n existingItem => (existingItem as any)?._key === itemToBeUpserted._key,\n )\n if (existingIndex >= 0) {\n replaceItemsMap[existingIndex] = i\n } else {\n insertItems.push(itemToBeUpserted)\n }\n })\n\n if (replaceItemsMap.length === 0 && insertItems.length == 0) {\n return currentValue\n }\n\n const next = [...currentValue]\n // Replace existing items\n for (const i of replaceItemsMap) {\n next[i] = op.items[replaceItemsMap[i]!]!\n }\n\n // Insert the items that doesn't exist\n return insert(\n {\n type: 'insert',\n items: insertItems,\n referenceItem: op.referenceItem,\n position: op.position,\n },\n next,\n )\n}\n\nexport function replace<\n O extends ReplaceOp<unknown[], number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"replace()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to replace`)\n }\n return splice(currentValue, index, op.items.length, op.items)\n}\nexport function remove<\n O extends RemoveOp<number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"remove()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to replace`)\n }\n return splice(currentValue, index, 1, [])\n}\n\nexport function truncate<O extends TruncateOp, CurrentValue extends unknown[]>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"truncate()\" on non-array value')\n }\n\n return typeof op.endIndex === 'number'\n ? currentValue\n .slice(0, op.startIndex)\n .concat(currentValue.slice(op.endIndex))\n : currentValue.slice(0, op.startIndex)\n}\n","import {\n type SetIfMissingOp,\n type SetOp,\n type UnsetOp,\n} from '../../../mutations/operations/types'\n\nexport function set<O extends SetOp<any>, CurrentValue>(\n op: O,\n currentValue: CurrentValue,\n) {\n return op.value\n}\n\nexport function setIfMissing<O extends SetIfMissingOp<any>, CurrentValue>(\n op: O,\n currentValue: CurrentValue,\n) {\n return currentValue ?? op.value\n}\n\nexport function unset<O extends UnsetOp, CurrentValue>(op: O) {\n return undefined\n}\n","import {type DecOp, type IncOp} from '../../../mutations/operations/types'\n\nexport function inc<O extends IncOp<number>, CurrentValue extends number>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (typeof currentValue !== 'number') {\n throw new TypeError('Cannot apply \"inc()\" on non-numeric value')\n }\n\n return currentValue + op.amount\n}\n\nexport function dec<O extends DecOp<number>, CurrentValue extends number>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (typeof currentValue !== 'number') {\n throw new TypeError('Cannot apply \"dec()\" on non-numeric value')\n }\n\n return currentValue - op.amount\n}\n","export const hasOwn = Object.prototype.hasOwnProperty.call.bind(\n Object.prototype.hasOwnProperty,\n)\n","import {hasOwn} from './hasOwn'\n\nexport function isEmpty(v: object) {\n for (const key in v) {\n if (hasOwn(v, key)) {\n return false\n }\n }\n return true\n}\n","export function omit<T, K extends keyof T>(val: T, props: K[]): Omit<T, K> {\n const copy = {...val}\n for (const prop of props) {\n delete copy[prop]\n }\n return copy\n}\n","import {\n type AssignOp,\n type UnassignOp,\n} from '../../../mutations/operations/types'\nimport {isObject} from '../../../utils/isObject'\nimport {isEmpty} from '../../utils/isEmpty'\nimport {omit} from '../../utils/omit'\n\nexport function unassign<T extends object, K extends string[]>(\n op: UnassignOp<K>,\n currentValue: T,\n) {\n if (!isObject(currentValue)) {\n throw new TypeError('Cannot apply \"unassign()\" on non-object value')\n }\n\n return op.keys.length === 0\n ? currentValue\n : omit(currentValue, op.keys as any[])\n}\n\nexport function assign<T extends object>(op: AssignOp<T>, currentValue: T) {\n if (!isObject(currentValue)) {\n throw new TypeError('Cannot apply \"assign()\" on non-object value')\n }\n\n return isEmpty(op.value) ? currentValue : {...currentValue, ...op.value}\n}\n","import {applyPatches, parsePatch} from '@sanity/diff-match-patch'\n\nimport {type DiffMatchPatchOp} from '../../../mutations/operations/types'\n\nexport function diffMatchPatch<\n O extends DiffMatchPatchOp,\n CurrentValue extends string,\n>(op: O, currentValue: CurrentValue) {\n if (typeof currentValue !== 'string') {\n throw new TypeError('Cannot apply \"diffMatchPatch()\" on non-string value')\n }\n\n return applyPatches(parsePatch(op.value), currentValue)[0]\n}\n","import {\n type AnyOp,\n type ArrayOp,\n type NumberOp,\n type ObjectOp,\n type Operation,\n type StringOp,\n} from '../../mutations/operations/types'\nimport {type AnyArray} from '../../utils/typeUtils'\nimport * as operations from './operations'\nimport {type ApplyOp} from './typings/applyOp'\n\nexport function applyOp<const Op extends AnyOp, const CurrentValue>(\n op: Op,\n currentValue: CurrentValue,\n): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends NumberOp,\n const CurrentValue extends number,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends StringOp,\n const CurrentValue extends string,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends ObjectOp,\n const CurrentValue extends {[k in keyof any]: unknown},\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends ArrayOp,\n const CurrentValue extends AnyArray,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<const Op extends Operation, const CurrentValue>(\n op: Op,\n currentValue: CurrentValue,\n): ApplyOp<Op, CurrentValue> {\n if (!(op.type in operations)) {\n throw new Error(`Invalid operation type: \"${op.type}\"`)\n }\n\n // eslint-disable-next-line import/namespace\n return (operations[op.type] as CallableFunction)(op, currentValue)\n}\n","import {type Operation} from '../../mutations/operations/types'\nimport {type NodePatch, type NodePatchList} from '../../mutations/types'\nimport {isArrayElement, isPropertyElement, stringify} from '../../path'\nimport {isObject} from '../../utils/isObject'\nimport {type NormalizeReadOnlyArray} from '../../utils/typeUtils'\nimport {type KeyedPathElement, type Path} from '../'\nimport {findTargetIndex, splice} from '../utils/array'\nimport {applyOp} from './applyOp'\nimport {\n type ApplyAtPath,\n type ApplyNodePatch,\n type ApplyPatches,\n} from './typings/applyNodePatch'\n\nexport function applyPatches<Patches extends NodePatchList, const Doc>(\n patches: Patches,\n document: Doc,\n): ApplyPatches<NormalizeReadOnlyArray<Patches>, Doc> {\n return (patches as NodePatch[]).reduce(\n (prev, patch) => applyNodePatch(patch, prev) as any,\n document,\n ) as any\n}\n\nexport function applyNodePatch<const Patch extends NodePatch, const Doc>(\n patch: Patch,\n document: Doc,\n): ApplyNodePatch<Patch, Doc> {\n return applyAtPath(patch.path, patch.op, document) as any\n}\n\nfunction applyAtPath<P extends Path, O extends Operation, T>(\n path: P,\n op: O,\n value: T,\n): ApplyAtPath<P, O, T> {\n if (!isNonEmptyArray(path)) {\n return applyOp(op as any, value) as any\n }\n\n const [head, ...tail] = path\n\n if (isArrayElement(head) && Array.isArray(value)) {\n return applyInArray(head, tail, op, value) as any\n }\n\n if (isPropertyElement(head) && isObject(value)) {\n return applyInObject(head, tail, op, value) as any\n }\n\n throw new Error(\n `Cannot apply operation of type \"${op.type}\" to path ${stringify(\n path,\n )} on ${typeof value} value`,\n )\n}\n\nfunction applyInObject<Key extends keyof any, T extends {[key in Key]?: any}>(\n head: Key,\n tail: Path,\n op: Operation,\n object: T,\n) {\n const current = object[head]\n\n if (current === undefined && tail.length > 0) {\n return object\n }\n\n // The patch targets the item at the index specified by \"head\"\n // so forward it to the item\n const patchedValue = applyAtPath(tail, op, current)\n\n // If the result of applying it to the item yields the item back we assume it was\n // a noop and don't modify our value. If we get a new value back, we return a\n // new array with the modified item replaced\n return patchedValue === current ? object : {...object, [head]: patchedValue}\n}\n\nfunction applyInArray<T>(\n head: number | KeyedPathElement,\n tail: Path,\n op: Operation,\n value: T[],\n) {\n const index = findTargetIndex(value, head!)\n\n if (index === null) {\n // partial is default behavior for arrays\n // the patch targets an index that is out of bounds\n return value\n }\n\n // If the given selector could not be found, return as-is\n if (index === -1) {\n return value\n }\n\n const current = value[index]!\n\n // The patch targets the item at the index specified by \"head\"\n // so forward it to the item\n const patchedItem = applyAtPath(tail, op, current)\n\n // If the result of applying it to the item yields the item back we assume it was\n // a noop and don't modify our value. If we get a new value back, we return a\n // new array with the modified item replaced\n return patchedItem === current\n ? value\n : splice(value, index, 1, [patchedItem])\n}\n\nfunction isNonEmptyArray<T>(a: T[] | readonly T[]): a is [T, ...T[]] {\n return a.length > 0\n}\n","import {type PatchMutation, type SanityDocumentBase} from '../mutations/types'\nimport {type NormalizeReadOnlyArray} from '../utils/typeUtils'\nimport {applyPatches} from './patch/applyNodePatch'\nimport {type ApplyPatches} from './patch/typings/applyNodePatch'\n\nexport type ApplyPatchMutation<\n Mutation extends PatchMutation,\n Doc extends SanityDocumentBase,\n> =\n Mutation extends PatchMutation<infer Patches>\n ? ApplyPatches<NormalizeReadOnlyArray<Patches>, Doc>\n : Doc\n\nexport function applyPatchMutation<\n const Mutation extends PatchMutation,\n const Doc extends SanityDocumentBase,\n>(mutation: Mutation, document: Doc): ApplyPatchMutation<Mutation, Doc> {\n if (\n mutation.options?.ifRevision &&\n document._rev !== mutation.options.ifRevision\n ) {\n throw new Error('Revision mismatch')\n }\n if (mutation.id !== document._id) {\n throw new Error(\n `Document id mismatch. Refusing to apply mutation for document with id=\"${mutation.id}\" on the given document with id=\"${document._id}\"`,\n )\n }\n return applyPatches(mutation.patches, document) as any\n}\n","import {type SanityDocumentBase} from '../../mutations/types'\nimport {type StoredDocument} from '../applyInIndex'\n\nexport function hasId(doc: SanityDocumentBase): doc is StoredDocument {\n return '_id' in doc\n}\nexport function assignId<Doc extends SanityDocumentBase>(\n doc: Doc,\n generateId: () => string,\n): Doc & {_id: string} {\n return hasId(doc) ? doc : {...doc, _id: generateId()}\n}\n"],"names":["isKeyedElement","isObject","applyPatches","parsePatch","isArrayElement","isPropertyElement","stringify"],"mappings":";;AAAO,SAAS,MAAM,OAA2B;AAC/C,SACG,UAAU,QACT,OAAO,SAAU,YACjB,OAAO,MAAM,QAAS,YACtB,MAAM,QACR;AAEJ;ACLO,SAAS,gBAAmB,OAAY,aAA0B;AACvE,MAAI,OAAO,eAAgB;AACzB,WAAO,eAAe,MAAM,QAAQ,WAAW;AAEjD,MAAIA,UAAAA,eAAe,WAAW,GAAG;AAC/B,UAAM,MAAM,MAAM,UAAU,CAAA,UAAS,MAAM,KAAK,MAAM,YAAY,IAAI;AACtE,WAAO,QAAQ,KAAK,OAAO;AAAA,EAC7B;AACA,QAAM,IAAI;AAAA,IACR,gHAAgH,KAAK;AAAA,MACnH;AAAA,IAAA,CACD;AAAA,EAAA;AAEL;AAEO,SAAS,aAAa,UAA8B,OAAe;AACxE,SAAO,aAAa,WAAW,QAAQ,QAAQ;AACjD;AAIO,SAAS,eAAe,QAAgB,OAAe;AAC5D,MAAI,WAAW,MAAM,UAAU,MAAM,UAAU;AAC7C,WAAO;AAET,QAAM,aAAa,QAAQ,IAAI,SAAS,QAAQ;AAChD,SAAO,cAAc,UAAU,aAAa,IAAI,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gD;AACpE,SAAO,SAAS;AAClB;AACO,SAAS,SACd,KACA,YACqB;AACrB,SAAO,MAAM,GAAG,IAAI,MAAM,EAAC,GAAG,KAAK,KAAK,aAAW;AACrD;;;;;;;;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"encode.js","sources":["../../src/encoders/sanity/decode.ts","../../src/encoders/sanity/encode.ts"],"sourcesContent":["import {type SetIfMissingOp, type SetOp} from '../../mutations/operations/types'\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {parse as parsePath} from '../../path/parser/parse'\n\nexport type {Mutation, SanityDocumentBase}\n\nexport type SanityDiffMatchPatch = {\n id: string\n diffMatchPatch: {[path: string]: string}\n}\n\nexport type SanitySetPatch = {\n id: string\n set: {[path: string]: any}\n}\n\nexport type Insert = {\n before?: string\n after?: string\n replace?: string\n items: any[]\n}\n\nexport type SanityInsertPatch = {\n id: string\n insert: Insert\n}\n\nexport type SanityUnsetPatch = {\n id: string\n unset: string[]\n}\n\nexport type SanityIncPatch = {\n id: string\n inc: {[path: string]: number}\n}\n\nexport type SanityDecPatch = {\n id: string\n dec: {[path: string]: number}\n}\n\nexport type SanitySetIfMissingPatch = {\n id: string\n setIfMissing: {[path: string]: any}\n}\n\nexport type SanityPatch =\n | SanitySetPatch\n | SanityUnsetPatch\n | SanityInsertPatch\n | SanitySetIfMissingPatch\n | SanityDiffMatchPatch\n | SanityIncPatch\n | SanityDecPatch\n\nexport type SanityCreateIfNotExistsMutation<Doc extends SanityDocumentBase> = {\n createIfNotExists: Doc\n}\n\nexport type SanityCreateOrReplaceMutation<Doc extends SanityDocumentBase> = {\n createOrReplace: Doc\n}\n\nexport type SanityCreateMutation<Doc extends SanityDocumentBase> = {\n create: Doc\n}\n\nexport type SanityDeleteMutation = {\n delete: {id: string}\n}\n\nexport type SanityPatchMutation = {\n patch:\n | SanitySetPatch\n | SanitySetIfMissingPatch\n | SanityDiffMatchPatch\n | SanityInsertPatch\n | SanityUnsetPatch\n}\n\nexport type SanityMutation<\n Doc extends SanityDocumentBase = SanityDocumentBase,\n> =\n | SanityCreateMutation<Doc>\n | SanityCreateIfNotExistsMutation<Doc>\n | SanityCreateOrReplaceMutation<Doc>\n | SanityDeleteMutation\n | SanityPatchMutation\n\nfunction isCreateIfNotExistsMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateIfNotExistsMutation<Doc> {\n return 'createIfNotExists' in sanityMutation\n}\n\nfunction isCreateOrReplaceMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateOrReplaceMutation<Doc> {\n return 'createOrReplace' in sanityMutation\n}\n\nfunction isCreateMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateMutation<Doc> {\n return 'create' in sanityMutation\n}\n\nfunction isDeleteMutation(\n sanityMutation: SanityMutation<any>,\n): sanityMutation is SanityDeleteMutation {\n return 'delete' in sanityMutation\n}\n\nfunction isPatchMutation(\n sanityMutation: SanityMutation<any>,\n): sanityMutation is SanityPatchMutation {\n return 'patch' in sanityMutation\n}\n\nfunction isSetPatch(sanityPatch: SanityPatch): sanityPatch is SanitySetPatch {\n return 'set' in sanityPatch\n}\n\nfunction isSetIfMissingPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanitySetIfMissingPatch {\n return 'setIfMissing' in sanityPatch\n}\n\nfunction isDiffMatchPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityDiffMatchPatch {\n return 'diffMatchPatch' in sanityPatch\n}\n\nfunction isUnsetPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityUnsetPatch {\n return 'unset' in sanityPatch\n}\n\nfunction isIncPatch(sanityPatch: SanityPatch): sanityPatch is SanityIncPatch {\n return 'inc' in sanityPatch\n}\n\nfunction isDecPatch(sanityPatch: SanityPatch): sanityPatch is SanityDecPatch {\n return 'inc' in sanityPatch\n}\n\nfunction isInsertPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityInsertPatch {\n return 'insert' in sanityPatch\n}\n\nexport function decodeAll<Doc extends SanityDocumentBase>(\n sanityMutations: SanityMutation<Doc>[],\n) {\n return sanityMutations.map(decodeMutation)\n}\n\nexport function decode<Doc extends SanityDocumentBase>(\n encodedMutation: SanityMutation<Doc>,\n) {\n return decodeMutation(encodedMutation)\n}\n\nfunction decodeMutation<Doc extends SanityDocumentBase>(\n encodedMutation: SanityMutation<Doc>,\n): Mutation {\n if (isCreateIfNotExistsMutation(encodedMutation)) {\n return {\n type: 'createIfNotExists',\n document: encodedMutation.createIfNotExists,\n }\n }\n if (isCreateOrReplaceMutation(encodedMutation)) {\n return {\n type: 'createOrReplace',\n document: encodedMutation.createOrReplace,\n }\n }\n if (isCreateMutation(encodedMutation)) {\n return {type: 'create', document: encodedMutation.create}\n }\n if (isDeleteMutation(encodedMutation)) {\n return {id: encodedMutation.delete.id, type: 'delete'}\n }\n if (isPatchMutation(encodedMutation)) {\n return {\n type: 'patch',\n id: encodedMutation.patch.id,\n patches: decodeNodePatches(encodedMutation.patch),\n }\n }\n throw new Error(`Unknown mutation: ${JSON.stringify(encodedMutation)}`)\n}\n\nconst POSITION_KEYS = ['before', 'replace', 'after'] as const\n\nfunction getInsertPosition(insert: Insert) {\n const positions = POSITION_KEYS.filter(k => k in insert)\n if (positions.length > 1) {\n throw new Error(\n `Insert patch is ambiguous. Should only contain one of: ${POSITION_KEYS.join(\n ', ',\n )}, instead found ${positions.join(', ')}`,\n )\n }\n return positions[0]\n}\n\nfunction decodeNodePatches<T>(patch: SanityPatch): NodePatch<any, any>[] {\n // If multiple patches are included, then the order of execution is as follows\n // set, setIfMissing, unset, inc, dec, insert.\n // order is defined here: https://www.sanity.io/docs/http-mutations#2f480b2baca5\n return [\n ...getSetPatches(patch),\n ...getSetIfMissingPatches(patch),\n ...getUnsetPatches(patch),\n ...getIncPatches(patch),\n ...getDecPatches(patch),\n ...getInsertPatches(patch),\n ...getDiffMatchPatchPatches(patch),\n ]\n\n throw new Error(`Unknown patch: ${JSON.stringify(patch)}`)\n}\n\nfunction getSetPatches(patch: SanityPatch): NodePatch<any[], SetOp<any>>[] {\n return isSetPatch(patch)\n ? Object.keys(patch.set).map(path => ({\n path: parsePath(path),\n op: {type: 'set', value: patch.set[path]},\n }))\n : []\n}\n\nfunction getSetIfMissingPatches(\n patch: SanityPatch,\n): NodePatch<any[], SetIfMissingOp<any>>[] {\n return isSetIfMissingPatch(patch)\n ? Object.keys(patch.setIfMissing).map(path => ({\n path: parsePath(path),\n op: {type: 'setIfMissing', value: patch.setIfMissing[path]},\n }))\n : []\n}\n\nfunction getDiffMatchPatchPatches(patch: SanityPatch) {\n return isDiffMatchPatch(patch)\n ? Object.keys(patch.diffMatchPatch).map(path => ({\n path: parsePath(path),\n op: {type: 'diffMatchPatch', value: patch.diffMatchPatch[path]},\n }))\n : []\n}\n\nfunction getUnsetPatches(patch: SanityPatch) {\n return isUnsetPatch(patch)\n ? patch.unset.map(path => ({\n path: parsePath(path),\n op: {type: 'unset'},\n }))\n : []\n}\n\nfunction getIncPatches(patch: SanityPatch) {\n return isIncPatch(patch)\n ? Object.keys(patch.inc).map(path => ({\n path: parsePath(path),\n op: {type: 'inc', amount: patch.inc[path]},\n }))\n : []\n}\n\nfunction getDecPatches(patch: SanityPatch) {\n return isDecPatch(patch)\n ? Object.keys(patch.dec).map(path => ({\n path: parsePath(path),\n op: {type: 'dec', amount: patch.dec[path]},\n }))\n : []\n}\n\nfunction getInsertPatches(patch: SanityPatch) {\n if (!isInsertPatch(patch)) {\n return []\n }\n const position = getInsertPosition(patch.insert)\n if (!position) {\n throw new Error('Insert patch missing position')\n }\n\n const path = parsePath(patch.insert[position]!)\n const referenceItem = path.pop()\n\n const op =\n position === 'replace'\n ? {\n type: 'insert',\n position: position,\n referenceItem,\n items: patch.insert.items,\n }\n : {\n type: 'insert',\n position: position,\n referenceItem,\n items: patch.insert.items,\n }\n\n return [{path, op}]\n}\n","import {\n type Mutation,\n type NodePatch,\n type Transaction,\n} from '../../mutations/types'\nimport {stringify as stringifyPath} from '../../path/parser/stringify'\n\nexport function encode(mutation: Mutation) {\n return encodeMutation(mutation)\n}\n\nexport function encodeAll(mutations: Mutation[]) {\n return mutations.flatMap(encode)\n}\n\nexport function encodeTransaction(transaction: Transaction) {\n return {\n transactionId: transaction.id,\n mutations: encodeAll(transaction.mutations),\n }\n}\n\nexport function encodeMutation(mutation: Mutation) {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return {[mutation.type]: mutation.document}\n }\n if (mutation.type === 'delete') {\n return {\n delete: {id: mutation.id},\n }\n }\n const ifRevisionID = mutation.options?.ifRevision\n return mutation.patches.map(patch => {\n return {\n patch: {\n id: mutation.id,\n ...(ifRevisionID && {ifRevisionID}),\n ...patchToSanity(patch),\n },\n }\n })\n}\n\nfunction patchToSanity(patch: NodePatch) {\n const {path, op} = patch\n if (op.type === 'unset') {\n return {unset: [stringifyPath(path)]}\n }\n if (op.type === 'insert') {\n return {\n insert: {\n [op.position]: stringifyPath([...path, op.referenceItem]),\n items: op.items,\n },\n }\n }\n if (op.type === 'diffMatchPatch') {\n return {diffMatchPatch: {[stringifyPath(path)]: op.value}}\n }\n if (op.type === 'inc') {\n return {inc: {[stringifyPath(path)]: op.amount}}\n }\n if (op.type === 'dec') {\n return {dec: {[stringifyPath(path)]: op.amount}}\n }\n if (op.type === 'set' || op.type === 'setIfMissing') {\n return {[op.type]: {[stringifyPath(path)]: op.value}}\n }\n if (op.type === 'truncate') {\n const range = [\n op.startIndex,\n typeof op.endIndex === 'number' ? op.endIndex : '',\n ].join(':')\n\n return {unset: [`${stringifyPath(path)}[${range}]`]}\n }\n if (op.type === 'upsert') {\n // note: upsert currently not supported by sanity, so will always insert at reference position\n return {\n unset: op.items.map(item =>\n stringifyPath([...path, {_key: (item as any)._key}]),\n ),\n insert: {\n [op.position]: stringifyPath([...path, op.referenceItem]),\n items: op.items,\n },\n }\n }\n if (op.type === 'assign') {\n return {\n set: Object.fromEntries(\n Object.keys(op.value).map(key => [\n stringifyPath(path.concat(key)),\n op.value[key as keyof typeof op.value],\n ]),\n ),\n }\n }\n if (op.type === 'unassign') {\n return {\n unset: op.keys.map(key => stringifyPath(path.concat(key))),\n }\n }\n if (op.type === 'replace') {\n return {\n insert: {\n replace: stringifyPath(path.concat(op.referenceItem)),\n items: op.items,\n },\n }\n }\n if (op.type === 'remove') {\n return {\n unset: [stringifyPath(path.concat(op.referenceItem))],\n }\n }\n //@ts-expect-error all cases should be covered\n throw new Error(`Unknown operation type ${op.type}`)\n}\n"],"names":["parsePath","stringifyPath"],"mappings":";;AA+FA,SAAS,4BACP,gBACwD;AACxD,SAAO,uBAAuB;AAChC;AAEA,SAAS,0BACP,gBACsD;AACtD,SAAO,qBAAqB;AAC9B;AAEA,SAAS,iBACP,gBAC6C;AAC7C,SAAO,YAAY;AACrB;AAEA,SAAS,iBACP,gBACwC;AACxC,SAAO,YAAY;AACrB;AAEA,SAAS,gBACP,gBACuC;AACvC,SAAO,WAAW;AACpB;AAEA,SAAS,WAAW,aAAyD;AAC3E,SAAO,SAAS;AAClB;AAEA,SAAS,oBACP,aACwC;AACxC,SAAO,kBAAkB;AAC3B;AAEA,SAAS,iBACP,aACqC;AACrC,SAAO,oBAAoB;AAC7B;AAEA,SAAS,aACP,aACiC;AACjC,SAAO,WAAW;AACpB;AAEA,SAAS,WAAW,aAAyD;AAC3E,SAAO,SAAS;AAClB;AAEA,SAAS,WAAW,aAAyD;AAC3E,SAAO,SAAS;AAClB;AAEA,SAAS,cACP,aACkC;AAClC,SAAO,YAAY;AACrB;AAEO,SAAS,UACd,iBACA;AACO,SAAA,gBAAgB,IAAI,cAAc;AAC3C;AAEO,SAAS,OACd,iBACA;AACA,SAAO,eAAe,eAAe;AACvC;AAEA,SAAS,eACP,iBACU;AACV,MAAI,4BAA4B,eAAe;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AA;AAAA,IAEd;AAEF,MAAI,GAAG,SAAS;AACP,WAAA;AAAA,MACL,KAAK,OAAO;AAAA,QACV,OAAO,KAAK,GAAG,KAAK,EAAE,IAAI,CAAO,QAAA;AAAA,UAC/BA,UAAc,KAAK,OAAO,GAAG,CAAC;AAAA,UAC9B,GAAG,MAAM,GAA4B;AAAA,QACtC,CAAA;AAAA,MAAA;AAAA,IAEL;AAEF,MAAI,GAAG,SAAS;AACP,WAAA;AAAA,MACL,OAAO,GAAG,KAAK,IAAI,CAAA,QAAOA,UAAc,KAAK,OAAO,GAAG,CAAC,CAAC;AAAA,IAC3D;AAEF,MAAI,GAAG,SAAS;AACP,WAAA;AAAA,MACL,QAAQ;AAAA,QACN,SAASA,UAAc,KAAK,OAAO,GAAG,aAAa,CAAC;AAAA,QACpD,OAAO,GAAG;AAAA,MAAA;AAAA,IAEd;AAEF,MAAI,GAAG,SAAS;AACP,WAAA;AAAA,MACL,OAAO,CAACA,UAAc,KAAK,OAAO,GAAG,aAAa,CAAC,CAAC;AAAA,IACtD;AAGF,QAAM,IAAI,MAAM,0BAA0B,GAAG,IAAI,EAAE;AACrD;"} | ||
| {"version":3,"file":"encode.js","sources":["../../src/encoders/sanity/decode.ts","../../src/encoders/sanity/encode.ts"],"sourcesContent":["import {type SetIfMissingOp, type SetOp} from '../../mutations/operations/types'\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {parse as parsePath} from '../../path/parser/parse'\n\nexport type {Mutation, SanityDocumentBase}\n\nexport type SanityDiffMatchPatch = {\n id: string\n diffMatchPatch: {[path: string]: string}\n}\n\nexport type SanitySetPatch = {\n id: string\n set: {[path: string]: any}\n}\n\nexport type Insert = {\n before?: string\n after?: string\n replace?: string\n items: any[]\n}\n\nexport type SanityInsertPatch = {\n id: string\n insert: Insert\n}\n\nexport type SanityUnsetPatch = {\n id: string\n unset: string[]\n}\n\nexport type SanityIncPatch = {\n id: string\n inc: {[path: string]: number}\n}\n\nexport type SanityDecPatch = {\n id: string\n dec: {[path: string]: number}\n}\n\nexport type SanitySetIfMissingPatch = {\n id: string\n setIfMissing: {[path: string]: any}\n}\n\nexport type SanityPatch =\n | SanitySetPatch\n | SanityUnsetPatch\n | SanityInsertPatch\n | SanitySetIfMissingPatch\n | SanityDiffMatchPatch\n | SanityIncPatch\n | SanityDecPatch\n\nexport type SanityCreateIfNotExistsMutation<Doc extends SanityDocumentBase> = {\n createIfNotExists: Doc\n}\n\nexport type SanityCreateOrReplaceMutation<Doc extends SanityDocumentBase> = {\n createOrReplace: Doc\n}\n\nexport type SanityCreateMutation<Doc extends SanityDocumentBase> = {\n create: Doc\n}\n\nexport type SanityDeleteMutation = {\n delete: {id: string}\n}\n\nexport type SanityPatchMutation = {\n patch:\n | SanitySetPatch\n | SanitySetIfMissingPatch\n | SanityDiffMatchPatch\n | SanityInsertPatch\n | SanityUnsetPatch\n}\n\nexport type SanityMutation<\n Doc extends SanityDocumentBase = SanityDocumentBase,\n> =\n | SanityCreateMutation<Doc>\n | SanityCreateIfNotExistsMutation<Doc>\n | SanityCreateOrReplaceMutation<Doc>\n | SanityDeleteMutation\n | SanityPatchMutation\n\nfunction isCreateIfNotExistsMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateIfNotExistsMutation<Doc> {\n return 'createIfNotExists' in sanityMutation\n}\n\nfunction isCreateOrReplaceMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateOrReplaceMutation<Doc> {\n return 'createOrReplace' in sanityMutation\n}\n\nfunction isCreateMutation<Doc extends SanityDocumentBase>(\n sanityMutation: SanityMutation<Doc>,\n): sanityMutation is SanityCreateMutation<Doc> {\n return 'create' in sanityMutation\n}\n\nfunction isDeleteMutation(\n sanityMutation: SanityMutation<any>,\n): sanityMutation is SanityDeleteMutation {\n return 'delete' in sanityMutation\n}\n\nfunction isPatchMutation(\n sanityMutation: SanityMutation<any>,\n): sanityMutation is SanityPatchMutation {\n return 'patch' in sanityMutation\n}\n\nfunction isSetPatch(sanityPatch: SanityPatch): sanityPatch is SanitySetPatch {\n return 'set' in sanityPatch\n}\n\nfunction isSetIfMissingPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanitySetIfMissingPatch {\n return 'setIfMissing' in sanityPatch\n}\n\nfunction isDiffMatchPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityDiffMatchPatch {\n return 'diffMatchPatch' in sanityPatch\n}\n\nfunction isUnsetPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityUnsetPatch {\n return 'unset' in sanityPatch\n}\n\nfunction isIncPatch(sanityPatch: SanityPatch): sanityPatch is SanityIncPatch {\n return 'inc' in sanityPatch\n}\n\nfunction isDecPatch(sanityPatch: SanityPatch): sanityPatch is SanityDecPatch {\n return 'inc' in sanityPatch\n}\n\nfunction isInsertPatch(\n sanityPatch: SanityPatch,\n): sanityPatch is SanityInsertPatch {\n return 'insert' in sanityPatch\n}\n\nexport function decodeAll<Doc extends SanityDocumentBase>(\n sanityMutations: SanityMutation<Doc>[],\n) {\n return sanityMutations.map(decodeMutation)\n}\n\nexport function decode<Doc extends SanityDocumentBase>(\n encodedMutation: SanityMutation<Doc>,\n) {\n return decodeMutation(encodedMutation)\n}\n\nfunction decodeMutation<Doc extends SanityDocumentBase>(\n encodedMutation: SanityMutation<Doc>,\n): Mutation {\n if (isCreateIfNotExistsMutation(encodedMutation)) {\n return {\n type: 'createIfNotExists',\n document: encodedMutation.createIfNotExists,\n }\n }\n if (isCreateOrReplaceMutation(encodedMutation)) {\n return {\n type: 'createOrReplace',\n document: encodedMutation.createOrReplace,\n }\n }\n if (isCreateMutation(encodedMutation)) {\n return {type: 'create', document: encodedMutation.create}\n }\n if (isDeleteMutation(encodedMutation)) {\n return {id: encodedMutation.delete.id, type: 'delete'}\n }\n if (isPatchMutation(encodedMutation)) {\n return {\n type: 'patch',\n id: encodedMutation.patch.id,\n patches: decodeNodePatches(encodedMutation.patch),\n }\n }\n throw new Error(`Unknown mutation: ${JSON.stringify(encodedMutation)}`)\n}\n\nconst POSITION_KEYS = ['before', 'replace', 'after'] as const\n\nfunction getInsertPosition(insert: Insert) {\n const positions = POSITION_KEYS.filter(k => k in insert)\n if (positions.length > 1) {\n throw new Error(\n `Insert patch is ambiguous. Should only contain one of: ${POSITION_KEYS.join(\n ', ',\n )}, instead found ${positions.join(', ')}`,\n )\n }\n return positions[0]\n}\n\nfunction decodeNodePatches<T>(patch: SanityPatch): NodePatch<any, any>[] {\n // If multiple patches are included, then the order of execution is as follows\n // set, setIfMissing, unset, inc, dec, insert.\n // order is defined here: https://www.sanity.io/docs/http-mutations#2f480b2baca5\n return [\n ...getSetPatches(patch),\n ...getSetIfMissingPatches(patch),\n ...getUnsetPatches(patch),\n ...getIncPatches(patch),\n ...getDecPatches(patch),\n ...getInsertPatches(patch),\n ...getDiffMatchPatchPatches(patch),\n ]\n\n throw new Error(`Unknown patch: ${JSON.stringify(patch)}`)\n}\n\nfunction getSetPatches(patch: SanityPatch): NodePatch<any[], SetOp<any>>[] {\n return isSetPatch(patch)\n ? Object.keys(patch.set).map(path => ({\n path: parsePath(path),\n op: {type: 'set', value: patch.set[path]},\n }))\n : []\n}\n\nfunction getSetIfMissingPatches(\n patch: SanityPatch,\n): NodePatch<any[], SetIfMissingOp<any>>[] {\n return isSetIfMissingPatch(patch)\n ? Object.keys(patch.setIfMissing).map(path => ({\n path: parsePath(path),\n op: {type: 'setIfMissing', value: patch.setIfMissing[path]},\n }))\n : []\n}\n\nfunction getDiffMatchPatchPatches(patch: SanityPatch) {\n return isDiffMatchPatch(patch)\n ? Object.keys(patch.diffMatchPatch).map(path => ({\n path: parsePath(path),\n op: {type: 'diffMatchPatch', value: patch.diffMatchPatch[path]},\n }))\n : []\n}\n\nfunction getUnsetPatches(patch: SanityPatch) {\n return isUnsetPatch(patch)\n ? patch.unset.map(path => ({\n path: parsePath(path),\n op: {type: 'unset'},\n }))\n : []\n}\n\nfunction getIncPatches(patch: SanityPatch) {\n return isIncPatch(patch)\n ? Object.keys(patch.inc).map(path => ({\n path: parsePath(path),\n op: {type: 'inc', amount: patch.inc[path]},\n }))\n : []\n}\n\nfunction getDecPatches(patch: SanityPatch) {\n return isDecPatch(patch)\n ? Object.keys(patch.dec).map(path => ({\n path: parsePath(path),\n op: {type: 'dec', amount: patch.dec[path]},\n }))\n : []\n}\n\nfunction getInsertPatches(patch: SanityPatch) {\n if (!isInsertPatch(patch)) {\n return []\n }\n const position = getInsertPosition(patch.insert)\n if (!position) {\n throw new Error('Insert patch missing position')\n }\n\n const path = parsePath(patch.insert[position]!)\n const referenceItem = path.pop()\n\n const op =\n position === 'replace'\n ? {\n type: 'insert',\n position: position,\n referenceItem,\n items: patch.insert.items,\n }\n : {\n type: 'insert',\n position: position,\n referenceItem,\n items: patch.insert.items,\n }\n\n return [{path, op}]\n}\n","import {\n type Mutation,\n type NodePatch,\n type Transaction,\n} from '../../mutations/types'\nimport {stringify as stringifyPath} from '../../path/parser/stringify'\n\nexport function encode(mutation: Mutation) {\n return encodeMutation(mutation)\n}\n\nexport function encodeAll(mutations: Mutation[]) {\n return mutations.flatMap(encode)\n}\n\nexport function encodeTransaction(transaction: Transaction) {\n return {\n transactionId: transaction.id,\n mutations: encodeAll(transaction.mutations),\n }\n}\n\nexport function encodeMutation(mutation: Mutation) {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return {[mutation.type]: mutation.document}\n }\n if (mutation.type === 'delete') {\n return {\n delete: {id: mutation.id},\n }\n }\n const ifRevisionID = mutation.options?.ifRevision\n return mutation.patches.map(patch => {\n return {\n patch: {\n id: mutation.id,\n ...(ifRevisionID && {ifRevisionID}),\n ...patchToSanity(patch),\n },\n }\n })\n}\n\nfunction patchToSanity(patch: NodePatch) {\n const {path, op} = patch\n if (op.type === 'unset') {\n return {unset: [stringifyPath(path)]}\n }\n if (op.type === 'insert') {\n return {\n insert: {\n [op.position]: stringifyPath([...path, op.referenceItem]),\n items: op.items,\n },\n }\n }\n if (op.type === 'diffMatchPatch') {\n return {diffMatchPatch: {[stringifyPath(path)]: op.value}}\n }\n if (op.type === 'inc') {\n return {inc: {[stringifyPath(path)]: op.amount}}\n }\n if (op.type === 'dec') {\n return {dec: {[stringifyPath(path)]: op.amount}}\n }\n if (op.type === 'set' || op.type === 'setIfMissing') {\n return {[op.type]: {[stringifyPath(path)]: op.value}}\n }\n if (op.type === 'truncate') {\n const range = [\n op.startIndex,\n typeof op.endIndex === 'number' ? op.endIndex : '',\n ].join(':')\n\n return {unset: [`${stringifyPath(path)}[${range}]`]}\n }\n if (op.type === 'upsert') {\n // note: upsert currently not supported by sanity, so will always insert at reference position\n return {\n unset: op.items.map(item =>\n stringifyPath([...path, {_key: (item as any)._key}]),\n ),\n insert: {\n [op.position]: stringifyPath([...path, op.referenceItem]),\n items: op.items,\n },\n }\n }\n if (op.type === 'assign') {\n return {\n set: Object.fromEntries(\n Object.keys(op.value).map(key => [\n stringifyPath(path.concat(key)),\n op.value[key as keyof typeof op.value],\n ]),\n ),\n }\n }\n if (op.type === 'unassign') {\n return {\n unset: op.keys.map(key => stringifyPath(path.concat(key))),\n }\n }\n if (op.type === 'replace') {\n return {\n insert: {\n replace: stringifyPath(path.concat(op.referenceItem)),\n items: op.items,\n },\n }\n }\n if (op.type === 'remove') {\n return {\n unset: [stringifyPath(path.concat(op.referenceItem))],\n }\n }\n //@ts-expect-error all cases should be covered\n throw new Error(`Unknown operation type ${op.type}`)\n}\n"],"names":["parsePath","stringifyPath"],"mappings":";;AA+FA,SAAS,4BACP,gBACwD;AACxD,SAAO,uBAAuB;AAChC;AAEA,SAAS,0BACP,gBACsD;AACtD,SAAO,qBAAqB;AAC9B;AAEA,SAAS,iBACP,gBAC6C;AAC7C,SAAO,YAAY;AACrB;AAEA,SAAS,iBACP,gBACwC;AACxC,SAAO,YAAY;AACrB;AAEA,SAA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AA0B,GAAG,IAAI,EAAE;AACrD;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"getAtPath.js","sources":["../../src/path/get/getAtPath.ts"],"sourcesContent":["import {type AnyArray} from '../../utils/typeUtils'\nimport {type KeyedPathElement, type Path, type PathElement} from '../types'\nimport {isArrayElement, isKeyedElement} from '../utils/predicates'\nimport {type FindInArray} from './types'\n\nexport type {AnyArray} from '../../utils/typeUtils'\n\nexport type Get<\n P extends number | KeyedPathElement | Readonly<KeyedPathElement> | string,\n T,\n> = T extends AnyArray\n ? P extends KeyedPathElement | Readonly<KeyedPathElement> | number\n ? FindInArray<P, T>\n : undefined\n : P extends keyof T\n ? T[P]\n : never\n\nexport type GetAtPath<P extends readonly PathElement[], T> = P extends []\n ? T\n : P extends [infer Head, ...infer Tail]\n ? Head extends PathElement\n ? Tail extends PathElement[]\n ? GetAtPath<Tail, Get<Head, T>>\n : undefined\n : undefined\n : undefined\n\nexport function getAtPath<const Head extends PathElement, const T>(\n path: [head: Head],\n value: T,\n): Get<Head, T>\nexport function getAtPath<\n const Head extends PathElement,\n const Tail extends PathElement[],\n T,\n>(path: [head: Head, ...tail: Tail], value: T): GetAtPath<[Head, ...Tail], T>\nexport function getAtPath<T>(path: [], value: T): T\nexport function getAtPath(path: Path, value: unknown): unknown\nexport function getAtPath(path: Path, value: unknown): unknown {\n if (path.length === 0) {\n return value\n }\n\n let current = value\n for (const head of path) {\n if (isArrayElement(head)) {\n if (!Array.isArray(current)) {\n return undefined\n }\n\n if (isKeyedElement(head)) {\n current = current.find(item => item._key === head._key)\n continue\n }\n current = current[head]\n continue\n }\n current = (current as any)[head]\n }\n return current\n}\n"],"names":[],"mappings":";AAuCgB,SAAA,UAAU,MAAY,OAAyB;AAC7D,MAAI,KAAK,WAAW;AACX,WAAA;AAGT,MAAI,UAAU;AACd,aAAW,QAAQ,MAAM;AACnB,QAAA,eAAe,IAAI,GAAG;AACpB,UAAA,CAAC,MAAM,QAAQ,OAAO;AACxB;AAGE,UAAA,eAAe,IAAI,GAAG;AACxB,kBAAU,QAAQ,KAAK,CAAA,SAAQ,KAAK,SAAS,KAAK,IAAI;AACtD;AAAA,MAAA;AAEF,gBAAU,QAAQ,IAAI;AACtB;AAAA,IAAA;AAEF,cAAW,QAAgB,IAAI;AAAA,EAAA;AAE1B,SAAA;AACT;"} | ||
| {"version":3,"file":"getAtPath.js","sources":["../../src/path/get/getAtPath.ts"],"sourcesContent":["import {type AnyArray} from '../../utils/typeUtils'\nimport {type KeyedPathElement, type Path, type PathElement} from '../types'\nimport {isArrayElement, isKeyedElement} from '../utils/predicates'\nimport {type FindInArray} from './types'\n\nexport type {AnyArray} from '../../utils/typeUtils'\n\nexport type Get<\n P extends number | KeyedPathElement | Readonly<KeyedPathElement> | string,\n T,\n> = T extends AnyArray\n ? P extends KeyedPathElement | Readonly<KeyedPathElement> | number\n ? FindInArray<P, T>\n : undefined\n : P extends keyof T\n ? T[P]\n : never\n\nexport type GetAtPath<P extends readonly PathElement[], T> = P extends []\n ? T\n : P extends [infer Head, ...infer Tail]\n ? Head extends PathElement\n ? Tail extends PathElement[]\n ? GetAtPath<Tail, Get<Head, T>>\n : undefined\n : undefined\n : undefined\n\nexport function getAtPath<const Head extends PathElement, const T>(\n path: [head: Head],\n value: T,\n): Get<Head, T>\nexport function getAtPath<\n const Head extends PathElement,\n const Tail extends PathElement[],\n T,\n>(path: [head: Head, ...tail: Tail], value: T): GetAtPath<[Head, ...Tail], T>\nexport function getAtPath<T>(path: [], value: T): T\nexport function getAtPath(path: Path, value: unknown): unknown\nexport function getAtPath(path: Path, value: unknown): unknown {\n if (path.length === 0) {\n return value\n }\n\n let current = value\n for (const head of path) {\n if (isArrayElement(head)) {\n if (!Array.isArray(current)) {\n return undefined\n }\n\n if (isKeyedElement(head)) {\n current = current.find(item => item._key === head._key)\n continue\n }\n current = current[head]\n continue\n }\n current = (current as any)[head]\n }\n return current\n}\n"],"names":[],"mappings":";AAuCO,SAAS,UAAU,MAAY,OAAyB;AAC7D,MAAI,KAAK,WAAW;AAClB,WAAO;AAGT,MAAI,UAAU;AACd,aAAW,QAAQ,MAAM;AACvB,QAAI,eAAe,IAAI,GAAG;AACxB,UAAI,CAAC,MAAM,QAAQ,OAAO;AACxB;AAGF,UAAI,eAAe,IAAI,GAAG;AACxB,kBAAU,QAAQ,KAAK,CAAA,SAAQ,KAAK,SAAS,KAAK,IAAI;AACtD;AAAA,MACF;AACA,gBAAU,QAAQ,IAAI;AACtB;AAAA,IACF;AACA,cAAW,QAAgB,IAAI;AAAA,EACjC;AACA,SAAO;AACT;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"isObject.js","sources":["../../src/utils/isObject.ts"],"sourcesContent":["export function isObject(val: unknown): val is {\n [K in string]: unknown\n} {\n return val !== null && typeof val === 'object' && !Array.isArray(val)\n}\n"],"names":[],"mappings":"AAAO,SAAS,SAAS,KAEvB;AACO,SAAA,QAAQ,QAAQ,OAAO,OAAQ,YAAY,CAAC,MAAM,QAAQ,GAAG;AACtE;"} | ||
| {"version":3,"file":"isObject.js","sources":["../../src/utils/isObject.ts"],"sourcesContent":["export function isObject(val: unknown): val is {\n [K in string]: unknown\n} {\n return val !== null && typeof val === 'object' && !Array.isArray(val)\n}\n"],"names":[],"mappings":"AAAO,SAAS,SAAS,KAEvB;AACA,SAAO,QAAQ,QAAQ,OAAO,OAAQ,YAAY,CAAC,MAAM,QAAQ,GAAG;AACtE;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"parse.js","sources":["../../src/path/parser/parse.ts"],"sourcesContent":["import {type PathElement} from '../types'\nimport {type StringToPath} from './types'\n\nexport function parse<const T extends string>(path: T): StringToPath<T> {\n return path\n .split(/[[.\\]]/g)\n .filter(Boolean)\n .map(seg => (seg.includes('==') ? parseSegment(seg) : coerce(seg))) as any\n}\n\nconst IS_NUMERIC = /^-?\\d+$/\n\nfunction unquote(str: string) {\n return str.replace(/^['\"]/, '').replace(/['\"]$/, '')\n}\n\nfunction parseSegment(segment: string): PathElement {\n const [key, value] = segment.split('==')\n if (key !== '_key') {\n throw new Error(\n `Currently only \"_key\" is supported as path segment. Found ${key}`,\n )\n }\n if (typeof value === 'undefined') {\n throw new Error('Invalid path segment, expected `key==\"value\"`')\n }\n return {_key: unquote(value)}\n}\n\nfunction coerce(segment: string): PathElement {\n return IS_NUMERIC.test(segment) ? Number(segment) : segment\n}\n"],"names":[],"mappings":"AAGO,SAAS,MAA8B,MAA0B;AACtE,SAAO,KACJ,MAAM,SAAS,EACf,OAAO,OAAO,EACd,IAAI,CAAA,QAAQ,IAAI,SAAS,IAAI,IAAI,aAAa,GAAG,IAAI,OAAO,GAAG,CAAE;AACtE;AAEA,MAAM,aAAa;AAEnB,SAAS,QAAQ,KAAa;AAC5B,SAAO,IAAI,QAAQ,SAAS,EAAE,EAAE,QAAQ,SAAS,EAAE;AACrD;AAEA,SAAS,aAAa,SAA8B;AAClD,QAAM,CAAC,KAAK,KAAK,IAAI,QAAQ,MAAM,IAAI;AACvC,MAAI,QAAQ;AACV,UAAM,IAAI;AAAA,MACR,6DAA6D,GAAG;AAAA,IAClE;AAEF,MAAI,OAAO,QAAU;AACb,UAAA,IAAI,MAAM,+CAA+C;AAEjE,SAAO,EAAC,MAAM,QAAQ,KAAK,EAAC;AAC9B;AAEA,SAAS,OAAO,SAA8B;AAC5C,SAAO,WAAW,KAAK,OAAO,IAAI,OAAO,OAAO,IAAI;AACtD;"} | ||
| {"version":3,"file":"parse.js","sources":["../../src/path/parser/parse.ts"],"sourcesContent":["import {type PathElement} from '../types'\nimport {type StringToPath} from './types'\n\nexport function parse<const T extends string>(path: T): StringToPath<T> {\n return path\n .split(/[[.\\]]/g)\n .filter(Boolean)\n .map(seg => (seg.includes('==') ? parseSegment(seg) : coerce(seg))) as any\n}\n\nconst IS_NUMERIC = /^-?\\d+$/\n\nfunction unquote(str: string) {\n return str.replace(/^['\"]/, '').replace(/['\"]$/, '')\n}\n\nfunction parseSegment(segment: string): PathElement {\n const [key, value] = segment.split('==')\n if (key !== '_key') {\n throw new Error(\n `Currently only \"_key\" is supported as path segment. Found ${key}`,\n )\n }\n if (typeof value === 'undefined') {\n throw new Error('Invalid path segment, expected `key==\"value\"`')\n }\n return {_key: unquote(value)}\n}\n\nfunction coerce(segment: string): PathElement {\n return IS_NUMERIC.test(segment) ? Number(segment) : segment\n}\n"],"names":[],"mappings":"AAGO,SAAS,MAA8B,MAA0B;AACtE,SAAO,KACJ,MAAM,SAAS,EACf,OAAO,OAAO,EACd,IAAI,CAAA,QAAQ,IAAI,SAAS,IAAI,IAAI,aAAa,GAAG,IAAI,OAAO,GAAG,CAAE;AACtE;AAEA,MAAM,aAAa;AAEnB,SAAS,QAAQ,KAAa;AAC5B,SAAO,IAAI,QAAQ,SAAS,EAAE,EAAE,QAAQ,SAAS,EAAE;AACrD;AAEA,SAAS,aAAa,SAA8B;AAClD,QAAM,CAAC,KAAK,KAAK,IAAI,QAAQ,MAAM,IAAI;AACvC,MAAI,QAAQ;AACV,UAAM,IAAI;AAAA,MACR,6DAA6D,GAAG;AAAA,IAAA;AAGpE,MAAI,OAAO,QAAU;AACnB,UAAM,IAAI,MAAM,+CAA+C;AAEjE,SAAO,EAAC,MAAM,QAAQ,KAAK,EAAA;AAC7B;AAEA,SAAS,OAAO,SAA8B;AAC5C,SAAO,WAAW,KAAK,OAAO,IAAI,OAAO,OAAO,IAAI;AACtD;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"stringify.js","sources":["../../src/path/utils/predicates.ts","../../src/path/parser/stringify.ts"],"sourcesContent":["import {type KeyedPathElement, type Path, type PathElement} from '../types'\n\nfunction safeGetElementAt<T>(array: T[] | readonly T[], index: number): T {\n if (index < 0 || index >= array.length) {\n throw new Error('Index out of bounds')\n }\n return array[index]!\n}\n\nexport function startsWith(parentPath: Path, path: Path): boolean {\n return (\n parentPath.length <= path.length &&\n parentPath.every((segment, i) =>\n isElementEqual(segment, safeGetElementAt(path, i)),\n )\n )\n}\n\nexport function isEqual(path: Path, otherPath: Path): boolean {\n return (\n path.length === otherPath.length &&\n path.every((segment, i) =>\n isElementEqual(segment, safeGetElementAt(path, i)),\n )\n )\n}\n\nexport function isElementEqual(\n segmentA: PathElement,\n segmentB: PathElement,\n): boolean {\n if (isKeyElement(segmentA) && isKeyElement(segmentB)) {\n return segmentA._key === segmentB._key\n }\n\n if (isIndexElement(segmentA)) {\n return Number(segmentA) === Number(segmentB)\n }\n\n return segmentA === segmentB\n}\n\nexport function isKeyElement(\n segment: PathElement,\n): segment is KeyedPathElement {\n return typeof (segment as any)?._key === 'string'\n}\nexport function isIndexElement(segment: PathElement): segment is number {\n return typeof segment === 'number'\n}\n\nexport function isKeyedElement(\n element: PathElement,\n): element is KeyedPathElement {\n return (\n typeof element === 'object' &&\n '_key' in element &&\n typeof element._key === 'string'\n )\n}\nexport function isArrayElement(\n element: PathElement,\n): element is KeyedPathElement | number {\n return typeof element === 'number' || isKeyedElement(element)\n}\n\nexport function isPropertyElement(element: PathElement): element is string {\n return typeof element === 'string'\n}\n","import {type Path, type PathElement} from '../types'\nimport {isKeyedElement} from '../utils/predicates'\n\nconst IS_DOTTABLE = /^[a-z_$]+/\n\nfunction stringifySegment(segment: PathElement, hasLeading: boolean): string {\n if (Array.isArray(segment)) {\n return `[${segment[0]}:${segment[1] || ''}]`\n }\n const type = typeof segment\n\n const isNumber = type === 'number'\n\n if (isNumber) {\n return `[${segment}]`\n }\n\n if (isKeyedElement(segment)) {\n return `[_key==${JSON.stringify(segment._key)}]`\n }\n\n if (typeof segment === 'string' && IS_DOTTABLE.test(segment)) {\n return hasLeading ? segment : `.${segment}`\n }\n\n return `['${segment}']`\n}\n\nexport function stringify(pathArray: Path): string {\n return pathArray\n .map((segment, i) => stringifySegment(segment, i === 0))\n .join('')\n}\n"],"names":[],"mappings":"AAEA,SAAS,iBAAoB,OAA2B,OAAkB;AACpE,MAAA,QAAQ,KAAK,SAAS,MAAM;AACxB,UAAA,IAAI,MAAM,qBAAqB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SAAS,UAAU,WAAyB;AACjD,SAAO,UACJ,IAAI,CAAC,SAAS,MAAM,iBAAiB,SAAS,MAAM,CAAC,CAAC,EACtD,KAAK,EAAE;AACZ;"} | ||
| {"version":3,"file":"stringify.js","sources":["../../src/path/utils/predicates.ts","../../src/path/parser/stringify.ts"],"sourcesContent":["import {type KeyedPathElement, type Path, type PathElement} from '../types'\n\nfunction safeGetElementAt<T>(array: T[] | readonly T[], index: number): T {\n if (index < 0 || index >= array.length) {\n throw new Error('Index out of bounds')\n }\n return array[index]!\n}\n\nexport function startsWith(parentPath: Path, path: Path): boolean {\n return (\n parentPath.length <= path.length &&\n parentPath.every((segment, i) =>\n isElementEqual(segment, safeGetElementAt(path, i)),\n )\n )\n}\n\nexport function isEqual(path: Path, otherPath: Path): boolean {\n return (\n path.length === otherPath.length &&\n path.every((segment, i) =>\n isElementEqual(segment, safeGetElementAt(path, i)),\n )\n )\n}\n\nexport function isElementEqual(\n segmentA: PathElement,\n segmentB: PathElement,\n): boolean {\n if (isKeyElement(segmentA) && isKeyElement(segmentB)) {\n return segmentA._key === segmentB._key\n }\n\n if (isIndexElement(segmentA)) {\n return Number(segmentA) === Number(segmentB)\n }\n\n return segmentA === segmentB\n}\n\nexport function isKeyElement(\n segment: PathElement,\n): segment is KeyedPathElement {\n return typeof (segment as any)?._key === 'string'\n}\nexport function isIndexElement(segment: PathElement): segment is number {\n return typeof segment === 'number'\n}\n\nexport function isKeyedElement(\n element: PathElement,\n): element is KeyedPathElement {\n return (\n typeof element === 'object' &&\n '_key' in element &&\n typeof element._key === 'string'\n )\n}\nexport function isArrayElement(\n element: PathElement,\n): element is KeyedPathElement | number {\n return typeof element === 'number' || isKeyedElement(element)\n}\n\nexport function isPropertyElement(element: PathElement): element is string {\n return typeof element === 'string'\n}\n","import {type Path, type PathElement} from '../types'\nimport {isKeyedElement} from '../utils/predicates'\n\nconst IS_DOTTABLE = /^[a-z_$]+/\n\nfunction stringifySegment(segment: PathElement, hasLeading: boolean): string {\n if (Array.isArray(segment)) {\n return `[${segment[0]}:${segment[1] || ''}]`\n }\n const type = typeof segment\n\n const isNumber = type === 'number'\n\n if (isNumber) {\n return `[${segment}]`\n }\n\n if (isKeyedElement(segment)) {\n return `[_key==${JSON.stringify(segment._key)}]`\n }\n\n if (typeof segment === 'string' && IS_DOTTABLE.test(segment)) {\n return hasLeading ? segment : `.${segment}`\n }\n\n return `['${segment}']`\n}\n\nexport function stringify(pathArray: Path): string {\n return pathArray\n .map((segment, i) => stringifySegment(segment, i === 0))\n .join('')\n}\n"],"names":[],"mappings":"AAEA,SAAS,iBAAoB,OAA2B,OAAkB;AACxE,MAAI,QAAQ,KAAK,SAAS,MAAM;AAC9B,UAAM,IAAI,MAAM,qBAAqB;AAEvC,SAAO,MAAM,KAAK;AACpB;AAEO,SAAS,WAAW,YAAkB,MAAqB;AAChE,SACE,WAAW,UAAU,KAAK,UAC1B,WAAW;AAAA,IAAM,CAAC,SAAS,MACzB,eAAe,SAAS,iBAAiB,MAAM,CAAC,CAAC;AAAA,EAAA;AAGvD;AAEO,SAAS,QAAQ,MAAY,WAA0B;AAC5D,SACE,KAAK,WAAW,UAAU,UAC1B,KAAK;AAAA,IAAM,CAAC,SAAS,MACnB,eAAe,SAAS,iBAAiB,MAAM,CAAC,CAAC;AAAA,EAAA;AAGvD;AAEO,SAAS,eACd,UACA,UACS;AACT,SAAI,aAAa,QAAQ,KAAK,aAAa,QAAQ,IAC1C,SAAS,SAAS,SAAS,OAGhC,eAAe,QAAQ,IAClB,OAAO,QAAQ,MAAM,OAAO,QAAQ,IAGtC,aAAa;AACtB;AAEO,SAAS,aACd,SAC6B;AAC7B,SAAO,OAAQ,SAAiB,QAAS;AAC3C;AACO,SAAS,eAAe,SAAyC;AACtE,SAAO,OAAO,WAAY;AAC5B;AAEO,SAAS,eACd,SAC6B;AAC7B,SACE,OAAO,WAAY,YACnB,UAAU,WACV,OAAO,QAAQ,QAAS;AAE5B;AACO,SAAS,eACd,SACsC;AACtC,SAAO,OAAO,WAAY,YAAY,eAAe,OAAO;AAC9D;AAEO,SAAS,kBAAkB,SAAyC;AACzE,SAAO,OAAO,WAAY;AAC5B;ACjEA,MAAM,cAAc;AAEpB,SAAS,iBAAiB,SAAsB,YAA6B;AAC3E,SAAI,MAAM,QAAQ,OAAO,IAChB,IAAI,QAAQ,CAAC,CAAC,IAAI,QAAQ,CAAC,KAAK,EAAE,MAE9B,OAAO,WAEM,WAGjB,IAAI,OAAO,MAGhB,eAAe,OAAO,IACjB,UAAU,KAAK,UAAU,QAAQ,IAAI,CAAC,MAG3C,OAAO,WAAY,YAAY,YAAY,KAAK,OAAO,IAClD,aAAa,UAAU,IAAI,OAAO,KAGpC,KAAK,OAAO;AACrB;AAEO,SAAS,UAAU,WAAyB;AACjD,SAAO,UACJ,IAAI,CAAC,SAAS,MAAM,iBAAiB,SAAS,MAAM,CAAC,CAAC,EACtD,KAAK,EAAE;AACZ;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"utils.js","sources":["../../src/apply/utils/getKeyOf.ts","../../src/apply/utils/array.ts","../../src/apply/patch/operations/array.ts","../../src/apply/patch/operations/common.ts","../../src/apply/patch/operations/number.ts","../../src/apply/utils/hasOwn.ts","../../src/apply/utils/isEmpty.ts","../../src/apply/utils/omit.ts","../../src/apply/patch/operations/object.ts","../../src/apply/patch/operations/string.ts","../../src/apply/patch/applyOp.ts","../../src/apply/patch/applyNodePatch.ts","../../src/apply/applyPatchMutation.ts","../../src/apply/store/utils.ts"],"sourcesContent":["export function keyOf(value: any): string | null {\n return (\n (value !== null &&\n typeof value === 'object' &&\n typeof value._key === 'string' &&\n value._key) ||\n null\n )\n}\n","import {isKeyedElement, type PathElement} from '../../path'\nimport {keyOf} from './getKeyOf'\n\nexport function findTargetIndex<T>(array: T[], pathSegment: PathElement) {\n if (typeof pathSegment === 'number') {\n return normalizeIndex(array.length, pathSegment)\n }\n if (isKeyedElement(pathSegment)) {\n const idx = array.findIndex(value => keyOf(value) === pathSegment._key)\n return idx === -1 ? null : idx\n }\n throw new Error(\n `Expected path segment to be addressing a single array item either by numeric index or by '_key'. Instead saw ${JSON.stringify(\n pathSegment,\n )}`,\n )\n}\n\nexport function getTargetIdx(position: 'before' | 'after', index: number) {\n return position === 'before' ? index : index + 1\n}\n\n// normalizes the index according to the array length\n// returns null if the normalized index is out of bounds\nexport function normalizeIndex(length: number, index: number) {\n if (length === 0 && (index === -1 || index === 0)) {\n return 0\n }\n const normalized = index < 0 ? length + index : index\n return normalized >= length || normalized < 0 ? null : normalized\n}\n\n// non-mutating splice\nexport function splice<T>(arr: T[], start: number, deleteCount: number): T[]\nexport function splice<T>(\n arr: T[],\n start: number,\n deleteCount: number,\n items: T[],\n): T[]\nexport function splice<T>(\n arr: T[],\n start: number,\n deleteCount: number,\n items?: T[],\n): T[] {\n const copy = arr.slice()\n copy.splice(start, deleteCount, ...(items || []))\n return copy\n}\n","import {\n type InsertOp,\n type KeyedPathElement,\n type RelativePosition,\n type RemoveOp,\n type ReplaceOp,\n type TruncateOp,\n type UpsertOp,\n} from '../../../mutations/operations/types'\nimport {findTargetIndex, getTargetIdx, splice} from '../../utils/array'\n\nexport function insert<\n O extends InsertOp<unknown[], RelativePosition, number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"insert()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to insert ${op.position}`)\n }\n // special case for empty arrays\n if (currentValue.length === 0) {\n return op.items\n }\n return splice(currentValue, getTargetIdx(op.position, index), 0, op.items)\n}\n\nexport function upsert<\n O extends UpsertOp<unknown[], RelativePosition, number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"upsert()\" on non-array value')\n }\n\n if (op.items.length === 0) {\n return currentValue\n }\n const replaceItemsMap: number[] = []\n const insertItems: unknown[] = []\n op.items.forEach((itemToBeUpserted: any, i) => {\n const existingIndex = currentValue.findIndex(\n existingItem => (existingItem as any)?._key === itemToBeUpserted._key,\n )\n if (existingIndex >= 0) {\n replaceItemsMap[existingIndex] = i\n } else {\n insertItems.push(itemToBeUpserted)\n }\n })\n\n if (replaceItemsMap.length === 0 && insertItems.length == 0) {\n return currentValue\n }\n\n const next = [...currentValue]\n // Replace existing items\n for (const i of replaceItemsMap) {\n next[i] = op.items[replaceItemsMap[i]!]!\n }\n\n // Insert the items that doesn't exist\n return insert(\n {\n type: 'insert',\n items: insertItems,\n referenceItem: op.referenceItem,\n position: op.position,\n },\n next,\n )\n}\n\nexport function replace<\n O extends ReplaceOp<unknown[], number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"replace()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to replace`)\n }\n return splice(currentValue, index, op.items.length, op.items)\n}\nexport function remove<\n O extends RemoveOp<number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"remove()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to replace`)\n }\n return splice(currentValue, index, 1, [])\n}\n\nexport function truncate<O extends TruncateOp, CurrentValue extends unknown[]>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"truncate()\" on non-array value')\n }\n\n return typeof op.endIndex === 'number'\n ? currentValue\n .slice(0, op.startIndex)\n .concat(currentValue.slice(op.endIndex))\n : currentValue.slice(0, op.startIndex)\n}\n","import {\n type SetIfMissingOp,\n type SetOp,\n type UnsetOp,\n} from '../../../mutations/operations/types'\n\nexport function set<O extends SetOp<any>, CurrentValue>(\n op: O,\n currentValue: CurrentValue,\n) {\n return op.value\n}\n\nexport function setIfMissing<O extends SetIfMissingOp<any>, CurrentValue>(\n op: O,\n currentValue: CurrentValue,\n) {\n return currentValue ?? op.value\n}\n\nexport function unset<O extends UnsetOp, CurrentValue>(op: O) {\n return undefined\n}\n","import {type DecOp, type IncOp} from '../../../mutations/operations/types'\n\nexport function inc<O extends IncOp<number>, CurrentValue extends number>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (typeof currentValue !== 'number') {\n throw new TypeError('Cannot apply \"inc()\" on non-numeric value')\n }\n\n return currentValue + op.amount\n}\n\nexport function dec<O extends DecOp<number>, CurrentValue extends number>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (typeof currentValue !== 'number') {\n throw new TypeError('Cannot apply \"dec()\" on non-numeric value')\n }\n\n return currentValue - op.amount\n}\n","export const hasOwn = Object.prototype.hasOwnProperty.call.bind(\n Object.prototype.hasOwnProperty,\n)\n","import {hasOwn} from './hasOwn'\n\nexport function isEmpty(v: object) {\n for (const key in v) {\n if (hasOwn(v, key)) {\n return false\n }\n }\n return true\n}\n","export function omit<T, K extends keyof T>(val: T, props: K[]): Omit<T, K> {\n const copy = {...val}\n for (const prop of props) {\n delete copy[prop]\n }\n return copy\n}\n","import {\n type AssignOp,\n type UnassignOp,\n} from '../../../mutations/operations/types'\nimport {isObject} from '../../../utils/isObject'\nimport {isEmpty} from '../../utils/isEmpty'\nimport {omit} from '../../utils/omit'\n\nexport function unassign<T extends object, K extends string[]>(\n op: UnassignOp<K>,\n currentValue: T,\n) {\n if (!isObject(currentValue)) {\n throw new TypeError('Cannot apply \"unassign()\" on non-object value')\n }\n\n return op.keys.length === 0\n ? currentValue\n : omit(currentValue, op.keys as any[])\n}\n\nexport function assign<T extends object>(op: AssignOp<T>, currentValue: T) {\n if (!isObject(currentValue)) {\n throw new TypeError('Cannot apply \"assign()\" on non-object value')\n }\n\n return isEmpty(op.value) ? currentValue : {...currentValue, ...op.value}\n}\n","import {applyPatches, parsePatch} from '@sanity/diff-match-patch'\n\nimport {type DiffMatchPatchOp} from '../../../mutations/operations/types'\n\nexport function diffMatchPatch<\n O extends DiffMatchPatchOp,\n CurrentValue extends string,\n>(op: O, currentValue: CurrentValue) {\n if (typeof currentValue !== 'string') {\n throw new TypeError('Cannot apply \"diffMatchPatch()\" on non-string value')\n }\n\n return applyPatches(parsePatch(op.value), currentValue)[0]\n}\n","import {\n type AnyOp,\n type ArrayOp,\n type NumberOp,\n type ObjectOp,\n type Operation,\n type StringOp,\n} from '../../mutations/operations/types'\nimport {type AnyArray} from '../../utils/typeUtils'\nimport * as operations from './operations'\nimport {type ApplyOp} from './typings/applyOp'\n\nexport function applyOp<const Op extends AnyOp, const CurrentValue>(\n op: Op,\n currentValue: CurrentValue,\n): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends NumberOp,\n const CurrentValue extends number,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends StringOp,\n const CurrentValue extends string,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends ObjectOp,\n const CurrentValue extends {[k in keyof any]: unknown},\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends ArrayOp,\n const CurrentValue extends AnyArray,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<const Op extends Operation, const CurrentValue>(\n op: Op,\n currentValue: CurrentValue,\n): ApplyOp<Op, CurrentValue> {\n if (!(op.type in operations)) {\n throw new Error(`Invalid operation type: \"${op.type}\"`)\n }\n\n // eslint-disable-next-line import/namespace\n return (operations[op.type] as CallableFunction)(op, currentValue)\n}\n","import {type Operation} from '../../mutations/operations/types'\nimport {type NodePatch, type NodePatchList} from '../../mutations/types'\nimport {isArrayElement, isPropertyElement, stringify} from '../../path'\nimport {isObject} from '../../utils/isObject'\nimport {type NormalizeReadOnlyArray} from '../../utils/typeUtils'\nimport {type KeyedPathElement, type Path} from '../'\nimport {findTargetIndex, splice} from '../utils/array'\nimport {applyOp} from './applyOp'\nimport {\n type ApplyAtPath,\n type ApplyNodePatch,\n type ApplyPatches,\n} from './typings/applyNodePatch'\n\nexport function applyPatches<Patches extends NodePatchList, const Doc>(\n patches: Patches,\n document: Doc,\n): ApplyPatches<NormalizeReadOnlyArray<Patches>, Doc> {\n return (patches as NodePatch[]).reduce(\n (prev, patch) => applyNodePatch(patch, prev) as any,\n document,\n ) as any\n}\n\nexport function applyNodePatch<const Patch extends NodePatch, const Doc>(\n patch: Patch,\n document: Doc,\n): ApplyNodePatch<Patch, Doc> {\n return applyAtPath(patch.path, patch.op, document) as any\n}\n\nfunction applyAtPath<P extends Path, O extends Operation, T>(\n path: P,\n op: O,\n value: T,\n): ApplyAtPath<P, O, T> {\n if (!isNonEmptyArray(path)) {\n return applyOp(op as any, value) as any\n }\n\n const [head, ...tail] = path\n\n if (isArrayElement(head) && Array.isArray(value)) {\n return applyInArray(head, tail, op, value) as any\n }\n\n if (isPropertyElement(head) && isObject(value)) {\n return applyInObject(head, tail, op, value) as any\n }\n\n throw new Error(\n `Cannot apply operation of type \"${op.type}\" to path ${stringify(\n path,\n )} on ${typeof value} value`,\n )\n}\n\nfunction applyInObject<Key extends keyof any, T extends {[key in Key]?: any}>(\n head: Key,\n tail: Path,\n op: Operation,\n object: T,\n) {\n const current = object[head]\n\n if (current === undefined && tail.length > 0) {\n return object\n }\n\n // The patch targets the item at the index specified by \"head\"\n // so forward it to the item\n const patchedValue = applyAtPath(tail, op, current)\n\n // If the result of applying it to the item yields the item back we assume it was\n // a noop and don't modify our value. If we get a new value back, we return a\n // new array with the modified item replaced\n return patchedValue === current ? object : {...object, [head]: patchedValue}\n}\n\nfunction applyInArray<T>(\n head: number | KeyedPathElement,\n tail: Path,\n op: Operation,\n value: T[],\n) {\n const index = findTargetIndex(value, head!)\n\n if (index === null) {\n // partial is default behavior for arrays\n // the patch targets an index that is out of bounds\n return value\n }\n\n // If the given selector could not be found, return as-is\n if (index === -1) {\n return value\n }\n\n const current = value[index]!\n\n // The patch targets the item at the index specified by \"head\"\n // so forward it to the item\n const patchedItem = applyAtPath(tail, op, current)\n\n // If the result of applying it to the item yields the item back we assume it was\n // a noop and don't modify our value. If we get a new value back, we return a\n // new array with the modified item replaced\n return patchedItem === current\n ? value\n : splice(value, index, 1, [patchedItem])\n}\n\nfunction isNonEmptyArray<T>(a: T[] | readonly T[]): a is [T, ...T[]] {\n return a.length > 0\n}\n","import {type PatchMutation, type SanityDocumentBase} from '../mutations/types'\nimport {type NormalizeReadOnlyArray} from '../utils/typeUtils'\nimport {applyPatches} from './patch/applyNodePatch'\nimport {type ApplyPatches} from './patch/typings/applyNodePatch'\n\nexport type ApplyPatchMutation<\n Mutation extends PatchMutation,\n Doc extends SanityDocumentBase,\n> =\n Mutation extends PatchMutation<infer Patches>\n ? ApplyPatches<NormalizeReadOnlyArray<Patches>, Doc>\n : Doc\n\nexport function applyPatchMutation<\n const Mutation extends PatchMutation,\n const Doc extends SanityDocumentBase,\n>(mutation: Mutation, document: Doc): ApplyPatchMutation<Mutation, Doc> {\n if (\n mutation.options?.ifRevision &&\n document._rev !== mutation.options.ifRevision\n ) {\n throw new Error('Revision mismatch')\n }\n if (mutation.id !== document._id) {\n throw new Error(\n `Document id mismatch. Refusing to apply mutation for document with id=\"${mutation.id}\" on the given document with id=\"${document._id}\"`,\n )\n }\n return applyPatches(mutation.patches, document) as any\n}\n","import {type SanityDocumentBase} from '../../mutations/types'\nimport {type StoredDocument} from '../applyInIndex'\n\nexport function hasId(doc: SanityDocumentBase): doc is StoredDocument {\n return '_id' in doc\n}\nexport function assignId<Doc extends SanityDocumentBase>(\n doc: Doc,\n generateId: () => string,\n): Doc & {_id: string} {\n return hasId(doc) ? doc : {...doc, _id: generateId()}\n}\n"],"names":["applyPatches"],"mappings":";;;AAAO,SA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S,aACP,MACA,MACA,IACA,OACA;AACM,QAAA,QAAQ,gBAAgB,OAAO,IAAK;AAEtC,MAAA,UAAU,QAOV,UAAU;AACL,WAAA;AAGH,QAAA,UAAU,MAAM,KAAK,GAIrB,cAAc,YAAY,MAAM,IAAI,OAAO;AAK1C,SAAA,gBAAgB,UACnB,QACA,OAAO,OAAO,OAAO,GAAG,CAAC,WAAW,CAAC;AAC3C;AAEA,SAAS,gBAAmB,GAAyC;AACnE,SAAO,EAAE,SAAS;AACpB;ACrGgB,SAAA,mBAGd,UAAoB,UAAkD;AACtE,MACE,SAAS,SAAS,cAClB,SAAS,SAAS,SAAS,QAAQ;AAE7B,UAAA,IAAI,MAAM,mBAAmB;AAEjC,MAAA,SAAS,OAAO,SAAS;AAC3B,UAAM,IAAI;AAAA,MACR,0EAA0E,SAAS,EAAE,oCAAoC,SAAS,GAAG;AAAA,IACvI;AAEK,SAAA,aAAa,SAAS,SAAS,QAAQ;AAChD;AC1BO,SAAS,MAAM,KAAgD;AACpE,SAAO,SAAS;AAClB;AACgB,SAAA,SACd,KACA,YACqB;AACd,SAAA,MAAM,GAAG,IAAI,MAAM,EAAC,GAAG,KAAK,KAAK,aAAY;AACtD;"} | ||
| {"version":3,"file":"utils.js","sources":["../../src/apply/utils/getKeyOf.ts","../../src/apply/utils/array.ts","../../src/apply/patch/operations/array.ts","../../src/apply/patch/operations/common.ts","../../src/apply/patch/operations/number.ts","../../src/apply/utils/hasOwn.ts","../../src/apply/utils/isEmpty.ts","../../src/apply/utils/omit.ts","../../src/apply/patch/operations/object.ts","../../src/apply/patch/operations/string.ts","../../src/apply/patch/applyOp.ts","../../src/apply/patch/applyNodePatch.ts","../../src/apply/applyPatchMutation.ts","../../src/apply/store/utils.ts"],"sourcesContent":["export function keyOf(value: any): string | null {\n return (\n (value !== null &&\n typeof value === 'object' &&\n typeof value._key === 'string' &&\n value._key) ||\n null\n )\n}\n","import {isKeyedElement, type PathElement} from '../../path'\nimport {keyOf} from './getKeyOf'\n\nexport function findTargetIndex<T>(array: T[], pathSegment: PathElement) {\n if (typeof pathSegment === 'number') {\n return normalizeIndex(array.length, pathSegment)\n }\n if (isKeyedElement(pathSegment)) {\n const idx = array.findIndex(value => keyOf(value) === pathSegment._key)\n return idx === -1 ? null : idx\n }\n throw new Error(\n `Expected path segment to be addressing a single array item either by numeric index or by '_key'. Instead saw ${JSON.stringify(\n pathSegment,\n )}`,\n )\n}\n\nexport function getTargetIdx(position: 'before' | 'after', index: number) {\n return position === 'before' ? index : index + 1\n}\n\n// normalizes the index according to the array length\n// returns null if the normalized index is out of bounds\nexport function normalizeIndex(length: number, index: number) {\n if (length === 0 && (index === -1 || index === 0)) {\n return 0\n }\n const normalized = index < 0 ? length + index : index\n return normalized >= length || normalized < 0 ? null : normalized\n}\n\n// non-mutating splice\nexport function splice<T>(arr: T[], start: number, deleteCount: number): T[]\nexport function splice<T>(\n arr: T[],\n start: number,\n deleteCount: number,\n items: T[],\n): T[]\nexport function splice<T>(\n arr: T[],\n start: number,\n deleteCount: number,\n items?: T[],\n): T[] {\n const copy = arr.slice()\n copy.splice(start, deleteCount, ...(items || []))\n return copy\n}\n","import {\n type InsertOp,\n type KeyedPathElement,\n type RelativePosition,\n type RemoveOp,\n type ReplaceOp,\n type TruncateOp,\n type UpsertOp,\n} from '../../../mutations/operations/types'\nimport {findTargetIndex, getTargetIdx, splice} from '../../utils/array'\n\nexport function insert<\n O extends InsertOp<unknown[], RelativePosition, number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"insert()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to insert ${op.position}`)\n }\n // special case for empty arrays\n if (currentValue.length === 0) {\n return op.items\n }\n return splice(currentValue, getTargetIdx(op.position, index), 0, op.items)\n}\n\nexport function upsert<\n O extends UpsertOp<unknown[], RelativePosition, number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"upsert()\" on non-array value')\n }\n\n if (op.items.length === 0) {\n return currentValue\n }\n const replaceItemsMap: number[] = []\n const insertItems: unknown[] = []\n op.items.forEach((itemToBeUpserted: any, i) => {\n const existingIndex = currentValue.findIndex(\n existingItem => (existingItem as any)?._key === itemToBeUpserted._key,\n )\n if (existingIndex >= 0) {\n replaceItemsMap[existingIndex] = i\n } else {\n insertItems.push(itemToBeUpserted)\n }\n })\n\n if (replaceItemsMap.length === 0 && insertItems.length == 0) {\n return currentValue\n }\n\n const next = [...currentValue]\n // Replace existing items\n for (const i of replaceItemsMap) {\n next[i] = op.items[replaceItemsMap[i]!]!\n }\n\n // Insert the items that doesn't exist\n return insert(\n {\n type: 'insert',\n items: insertItems,\n referenceItem: op.referenceItem,\n position: op.position,\n },\n next,\n )\n}\n\nexport function replace<\n O extends ReplaceOp<unknown[], number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"replace()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to replace`)\n }\n return splice(currentValue, index, op.items.length, op.items)\n}\nexport function remove<\n O extends RemoveOp<number | KeyedPathElement>,\n CurrentValue extends unknown[],\n>(op: O, currentValue: CurrentValue) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"remove()\" on non-array value')\n }\n\n const index = findTargetIndex(currentValue, op.referenceItem)\n if (index === null) {\n throw new Error(`Found no matching array element to replace`)\n }\n return splice(currentValue, index, 1, [])\n}\n\nexport function truncate<O extends TruncateOp, CurrentValue extends unknown[]>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (!Array.isArray(currentValue)) {\n throw new TypeError('Cannot apply \"truncate()\" on non-array value')\n }\n\n return typeof op.endIndex === 'number'\n ? currentValue\n .slice(0, op.startIndex)\n .concat(currentValue.slice(op.endIndex))\n : currentValue.slice(0, op.startIndex)\n}\n","import {\n type SetIfMissingOp,\n type SetOp,\n type UnsetOp,\n} from '../../../mutations/operations/types'\n\nexport function set<O extends SetOp<any>, CurrentValue>(\n op: O,\n currentValue: CurrentValue,\n) {\n return op.value\n}\n\nexport function setIfMissing<O extends SetIfMissingOp<any>, CurrentValue>(\n op: O,\n currentValue: CurrentValue,\n) {\n return currentValue ?? op.value\n}\n\nexport function unset<O extends UnsetOp, CurrentValue>(op: O) {\n return undefined\n}\n","import {type DecOp, type IncOp} from '../../../mutations/operations/types'\n\nexport function inc<O extends IncOp<number>, CurrentValue extends number>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (typeof currentValue !== 'number') {\n throw new TypeError('Cannot apply \"inc()\" on non-numeric value')\n }\n\n return currentValue + op.amount\n}\n\nexport function dec<O extends DecOp<number>, CurrentValue extends number>(\n op: O,\n currentValue: CurrentValue,\n) {\n if (typeof currentValue !== 'number') {\n throw new TypeError('Cannot apply \"dec()\" on non-numeric value')\n }\n\n return currentValue - op.amount\n}\n","export const hasOwn = Object.prototype.hasOwnProperty.call.bind(\n Object.prototype.hasOwnProperty,\n)\n","import {hasOwn} from './hasOwn'\n\nexport function isEmpty(v: object) {\n for (const key in v) {\n if (hasOwn(v, key)) {\n return false\n }\n }\n return true\n}\n","export function omit<T, K extends keyof T>(val: T, props: K[]): Omit<T, K> {\n const copy = {...val}\n for (const prop of props) {\n delete copy[prop]\n }\n return copy\n}\n","import {\n type AssignOp,\n type UnassignOp,\n} from '../../../mutations/operations/types'\nimport {isObject} from '../../../utils/isObject'\nimport {isEmpty} from '../../utils/isEmpty'\nimport {omit} from '../../utils/omit'\n\nexport function unassign<T extends object, K extends string[]>(\n op: UnassignOp<K>,\n currentValue: T,\n) {\n if (!isObject(currentValue)) {\n throw new TypeError('Cannot apply \"unassign()\" on non-object value')\n }\n\n return op.keys.length === 0\n ? currentValue\n : omit(currentValue, op.keys as any[])\n}\n\nexport function assign<T extends object>(op: AssignOp<T>, currentValue: T) {\n if (!isObject(currentValue)) {\n throw new TypeError('Cannot apply \"assign()\" on non-object value')\n }\n\n return isEmpty(op.value) ? currentValue : {...currentValue, ...op.value}\n}\n","import {applyPatches, parsePatch} from '@sanity/diff-match-patch'\n\nimport {type DiffMatchPatchOp} from '../../../mutations/operations/types'\n\nexport function diffMatchPatch<\n O extends DiffMatchPatchOp,\n CurrentValue extends string,\n>(op: O, currentValue: CurrentValue) {\n if (typeof currentValue !== 'string') {\n throw new TypeError('Cannot apply \"diffMatchPatch()\" on non-string value')\n }\n\n return applyPatches(parsePatch(op.value), currentValue)[0]\n}\n","import {\n type AnyOp,\n type ArrayOp,\n type NumberOp,\n type ObjectOp,\n type Operation,\n type StringOp,\n} from '../../mutations/operations/types'\nimport {type AnyArray} from '../../utils/typeUtils'\nimport * as operations from './operations'\nimport {type ApplyOp} from './typings/applyOp'\n\nexport function applyOp<const Op extends AnyOp, const CurrentValue>(\n op: Op,\n currentValue: CurrentValue,\n): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends NumberOp,\n const CurrentValue extends number,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends StringOp,\n const CurrentValue extends string,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends ObjectOp,\n const CurrentValue extends {[k in keyof any]: unknown},\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<\n const Op extends ArrayOp,\n const CurrentValue extends AnyArray,\n>(op: Op, currentValue: CurrentValue): ApplyOp<Op, CurrentValue>\nexport function applyOp<const Op extends Operation, const CurrentValue>(\n op: Op,\n currentValue: CurrentValue,\n): ApplyOp<Op, CurrentValue> {\n if (!(op.type in operations)) {\n throw new Error(`Invalid operation type: \"${op.type}\"`)\n }\n\n // eslint-disable-next-line import/namespace\n return (operations[op.type] as CallableFunction)(op, currentValue)\n}\n","import {type Operation} from '../../mutations/operations/types'\nimport {type NodePatch, type NodePatchList} from '../../mutations/types'\nimport {isArrayElement, isPropertyElement, stringify} from '../../path'\nimport {isObject} from '../../utils/isObject'\nimport {type NormalizeReadOnlyArray} from '../../utils/typeUtils'\nimport {type KeyedPathElement, type Path} from '../'\nimport {findTargetIndex, splice} from '../utils/array'\nimport {applyOp} from './applyOp'\nimport {\n type ApplyAtPath,\n type ApplyNodePatch,\n type ApplyPatches,\n} from './typings/applyNodePatch'\n\nexport function applyPatches<Patches extends NodePatchList, const Doc>(\n patches: Patches,\n document: Doc,\n): ApplyPatches<NormalizeReadOnlyArray<Patches>, Doc> {\n return (patches as NodePatch[]).reduce(\n (prev, patch) => applyNodePatch(patch, prev) as any,\n document,\n ) as any\n}\n\nexport function applyNodePatch<const Patch extends NodePatch, const Doc>(\n patch: Patch,\n document: Doc,\n): ApplyNodePatch<Patch, Doc> {\n return applyAtPath(patch.path, patch.op, document) as any\n}\n\nfunction applyAtPath<P extends Path, O extends Operation, T>(\n path: P,\n op: O,\n value: T,\n): ApplyAtPath<P, O, T> {\n if (!isNonEmptyArray(path)) {\n return applyOp(op as any, value) as any\n }\n\n const [head, ...tail] = path\n\n if (isArrayElement(head) && Array.isArray(value)) {\n return applyInArray(head, tail, op, value) as any\n }\n\n if (isPropertyElement(head) && isObject(value)) {\n return applyInObject(head, tail, op, value) as any\n }\n\n throw new Error(\n `Cannot apply operation of type \"${op.type}\" to path ${stringify(\n path,\n )} on ${typeof value} value`,\n )\n}\n\nfunction applyInObject<Key extends keyof any, T extends {[key in Key]?: any}>(\n head: Key,\n tail: Path,\n op: Operation,\n object: T,\n) {\n const current = object[head]\n\n if (current === undefined && tail.length > 0) {\n return object\n }\n\n // The patch targets the item at the index specified by \"head\"\n // so forward it to the item\n const patchedValue = applyAtPath(tail, op, current)\n\n // If the result of applying it to the item yields the item back we assume it was\n // a noop and don't modify our value. If we get a new value back, we return a\n // new array with the modified item replaced\n return patchedValue === current ? object : {...object, [head]: patchedValue}\n}\n\nfunction applyInArray<T>(\n head: number | KeyedPathElement,\n tail: Path,\n op: Operation,\n value: T[],\n) {\n const index = findTargetIndex(value, head!)\n\n if (index === null) {\n // partial is default behavior for arrays\n // the patch targets an index that is out of bounds\n return value\n }\n\n // If the given selector could not be found, return as-is\n if (index === -1) {\n return value\n }\n\n const current = value[index]!\n\n // The patch targets the item at the index specified by \"head\"\n // so forward it to the item\n const patchedItem = applyAtPath(tail, op, current)\n\n // If the result of applying it to the item yields the item back we assume it was\n // a noop and don't modify our value. If we get a new value back, we return a\n // new array with the modified item replaced\n return patchedItem === current\n ? value\n : splice(value, index, 1, [patchedItem])\n}\n\nfunction isNonEmptyArray<T>(a: T[] | readonly T[]): a is [T, ...T[]] {\n return a.length > 0\n}\n","import {type PatchMutation, type SanityDocumentBase} from '../mutations/types'\nimport {type NormalizeReadOnlyArray} from '../utils/typeUtils'\nimport {applyPatches} from './patch/applyNodePatch'\nimport {type ApplyPatches} from './patch/typings/applyNodePatch'\n\nexport type ApplyPatchMutation<\n Mutation extends PatchMutation,\n Doc extends SanityDocumentBase,\n> =\n Mutation extends PatchMutation<infer Patches>\n ? ApplyPatches<NormalizeReadOnlyArray<Patches>, Doc>\n : Doc\n\nexport function applyPatchMutation<\n const Mutation extends PatchMutation,\n const Doc extends SanityDocumentBase,\n>(mutation: Mutation, document: Doc): ApplyPatchMutation<Mutation, Doc> {\n if (\n mutation.options?.ifRevision &&\n document._rev !== mutation.options.ifRevision\n ) {\n throw new Error('Revision mismatch')\n }\n if (mutation.id !== document._id) {\n throw new Error(\n `Document id mismatch. Refusing to apply mutation for document with id=\"${mutation.id}\" on the given document with id=\"${document._id}\"`,\n )\n }\n return applyPatches(mutation.patches, document) as any\n}\n","import {type SanityDocumentBase} from '../../mutations/types'\nimport {type StoredDocument} from '../applyInIndex'\n\nexport function hasId(doc: SanityDocumentBase): doc is StoredDocument {\n return '_id' in doc\n}\nexport function assignId<Doc extends SanityDocumentBase>(\n doc: Doc,\n generateId: () => string,\n): Doc & {_id: string} {\n return hasId(doc) ? doc : {...doc, _id: generateId()}\n}\n"],"names":["applyPatches"],"mappings":";;;AAAO,SAAS,MAAM,OAA2B;AAC/C,SACG,UAAU,QACT,OAAO,SAAU,YACjB,OAAO,MAAM,QAAS,YACtB,MAAM,QACR;AAEJ;ACLO,SAAS,gBAAmB,OAAY,aAA0B;AACvE,MAAI,OAAO,eAAgB;AACzB,WAAO,eAAe,MAAM,QAAQ,WAAW;AAEjD,MAAI,eAAe,WAAW,GAAG;AAC/B,UAAM,MAAM,MAAM,UAAU,CAAA,UAAS,MAAM,KAAK,MAAM,YAAY,IAAI;AACtE,WAAO,QAAQ,KAAK,OAAO;AAAA,EAC7B;AACA,QAAM,IAAI;AAAA,IACR,gHAAgH,KAAK;AAAA,MACnH;AAAA,IAAA,CACD;AAAA,EAAA;AAEL;AAEO,SAAS,aAAa,UAA8B,OAAe;AACxE,SAAO,aAAa,WAAW,QAAQ,QAAQ;AACjD;AAIO,SAAS,eAAe,QAAgB,OAAe;AAC5D,MAAI,WAAW,MAAM,UAAU,MAAM,UAAU;AAC7C,WAAO;AAET,QAAM,aAAa,QAAQ,IAAI,SAAS,QAAQ;AAChD,SAAO,cAAc,UAAU,aAAa,IAAI,OAAO;AACzD;AAUO,SAAS,OACd,KACA,OACA,aACA,OACK;AACL,QAAM,OAAO,IAAI,MAAA;AACjB,SAAA,KAAK,OAAO,OAAO,aAAa,GAAI,SAAS,CAAA,CAAG,GACzC;AACT;ACtCO,SAAS,OAGd,IAAO,cAA4B;AACnC,MAAI,CAAC,MAAM,QAAQ,YAAY;AAC7B,UAAM,IAAI,UAAU,4CAA4C;AAGlE,QAAM,QAAQ,gBAAgB,cAAc,GAAG,aAAa;AAC5D,MAAI,UAAU;AACZ,UAAM,IAAI,MAAM,6CAA6C,GAAG,QAAQ,EAAE;AAG5E,SAAI,aAAa,WAAW,IACnB,GAAG,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ACzC,WAAO;AAKT,QAAM,eAAe,YAAY,MAAM,IAAI,OAAO;AAKlD,SAAO,iBAAiB,UAAU,SAAS,EAAC,GAAG,QAAQ,CAAC,IAAI,GAAG,aAAA;AACjE;AAEA,SAAS,aACP,MACA,MACA,IACA,OACA;AACA,QAAM,QAAQ,gBAAgB,OAAO,IAAK;AAS1C,MAPI,UAAU,QAOV,UAAU;AACZ,WAAO;AAGT,QAAM,UAAU,MAAM,KAAK,GAIrB,cAAc,YAAY,MAAM,IAAI,OAAO;AAKjD,SAAO,gBAAgB,UACnB,QACA,OAAO,OAAO,OAAO,GAAG,CAAC,WAAW,CAAC;AAC3C;AAEA,SAAS,gBAAmB,GAAyC;AACnE,SAAO,EAAE,SAAS;AACpB;ACrGO,SAAS,mBAGd,UAAoB,UAAkD;AACtE,MACE,SAAS,SAAS,cAClB,SAAS,SAAS,SAAS,QAAQ;AAEnC,UAAM,IAAI,MAAM,mBAAmB;AAErC,MAAI,SAAS,OAAO,SAAS;AAC3B,UAAM,IAAI;AAAA,MACR,0EAA0E,SAAS,EAAE,oCAAoC,SAAS,GAAG;AAAA,IAAA;AAGzI,SAAO,aAAa,SAAS,SAAS,QAAQ;AAChD;AC1BO,SAAS,MAAM,KAAgD;AACpE,SAAO,SAAS;AAClB;AACO,SAAS,SACd,KACA,YACqB;AACrB,SAAO,MAAM,GAAG,IAAI,MAAM,EAAC,GAAG,KAAK,KAAK,aAAW;AACrD;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"_unstable_apply.cjs","sources":["../src/apply/applyInCollection.ts","../src/apply/store/store.ts","../src/apply/applyInIndex.ts"],"sourcesContent":["import {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {arrify} from '../utils/arrify'\nimport {applyPatchMutation} from './applyPatchMutation'\nimport {splice} from './utils/array'\n\nexport function applyInCollection<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutations: Mutation | Mutation[],\n) {\n const a = arrify(mutations) as Mutation[]\n return a.reduce((prev, mutation) => {\n if (mutation.type === 'create') {\n return createIn(prev, mutation)\n }\n if (mutation.type === 'createIfNotExists') {\n return createIfNotExistsIn(prev, mutation)\n }\n if (mutation.type === 'delete') {\n return deleteIn(prev, mutation)\n }\n if (mutation.type === 'createOrReplace') {\n return createOrReplaceIn(prev, mutation)\n }\n if (mutation.type === 'patch') {\n return patchIn(prev, mutation)\n }\n // @ts-expect-error all cases should be covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n }, collection)\n}\n\nfunction createIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n if (currentIdx !== -1) {\n throw new Error('Document already exist')\n }\n return collection.concat(mutation.document)\n}\n\nfunction createIfNotExistsIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateIfNotExistsMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n return currentIdx === -1 ? collection.concat(mutation.document) : collection\n}\n\nfunction createOrReplaceIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateOrReplaceMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n return currentIdx === -1\n ? collection.concat(mutation.document)\n : splice(collection, currentIdx, 1, [mutation.document])\n}\n\nfunction deleteIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: DeleteMutation,\n) {\n const currentIdx = collection.findIndex(doc => doc._id === mutation.id)\n return currentIdx === -1 ? collection : splice(collection, currentIdx, 1)\n}\n\nfunction patchIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: PatchMutation,\n): Doc[] {\n const currentIdx = collection.findIndex(doc => doc._id === mutation.id)\n if (currentIdx === -1) {\n throw new Error('Cannot apply patch on nonexistent document')\n }\n const current = collection[currentIdx]!\n\n const next = applyPatchMutation(mutation, current)\n\n return next === current\n ? collection\n : splice(collection, currentIdx, 1, [next])\n}\n","import {nanoid} from 'nanoid'\n\nimport {type DocumentIndex, type Format} from '../../apply'\nimport {type Mutation, type SanityDocumentBase} from '../../mutations/types'\nimport {arrify} from '../../utils/arrify'\nimport {applyInIndex, type ToIdentified, type ToStored} from '../applyInIndex'\nimport {assignId} from './utils'\n\nexport type RequiredSelect<T, K extends keyof T> = Omit<T, K> & {\n [P in K]-?: T[P]\n}\n\nfunction update<Doc extends ToIdentified<SanityDocumentBase>>(\n doc: Doc,\n revision: string,\n): ToStored<Doc> {\n return {\n ...doc,\n _rev: revision,\n _createdAt: doc._createdAt || new Date().toISOString(),\n _updatedAt: new Date().toISOString(),\n }\n}\n\nconst empty: DocumentIndex<any> = {}\n\nexport const createStore = <Doc extends SanityDocumentBase>(\n initialEntries?: Doc[],\n) => {\n let version = 0\n\n let index: DocumentIndex<Format<ToStored<Doc & SanityDocumentBase>>> =\n initialEntries && initialEntries?.length > 0\n ? Object.fromEntries(\n initialEntries.map(entry => {\n const doc = update(assignId(entry, nanoid), nanoid())\n return [doc._id, doc]\n }),\n )\n : empty\n\n return {\n get version() {\n return version\n },\n // todo: support listening for changes\n entries: () => Object.entries(index),\n get: <Id extends string>(\n id: Id,\n ): Format<Omit<(typeof index)[keyof typeof index], '_id'> & {_id: Id}> =>\n index[id] as any,\n apply: (mutations: Mutation[] | Mutation) => {\n const nextIndex = applyInIndex(index, arrify(mutations))\n if (nextIndex !== index) {\n index = nextIndex\n version++\n }\n },\n }\n}\n","import {nanoid} from 'nanoid'\n\nimport {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {applyPatchMutation} from './applyPatchMutation'\nimport {assignId, hasId, type RequiredSelect} from './store'\n\nexport type DocumentIndex<Doc extends SanityDocumentBase> = {[id: string]: Doc}\n\nexport function applyInIndex<\n Doc extends SanityDocumentBase,\n Index extends DocumentIndex<ToStored<Doc>>,\n>(index: Index, mutations: Mutation<Doc>[]): Index {\n return mutations.reduce((prev, mutation) => {\n if (mutation.type === 'create') {\n return createIn(prev, mutation)\n }\n if (mutation.type === 'createIfNotExists') {\n return createIfNotExistsIn(prev, mutation)\n }\n if (mutation.type === 'delete') {\n return deleteIn(prev, mutation)\n }\n if (mutation.type === 'createOrReplace') {\n return createOrReplaceIn(prev, mutation)\n }\n if (mutation.type === 'patch') {\n return patchIn(prev, mutation)\n }\n // @ts-expect-error all cases should be covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n }, index)\n}\n\nexport type ToStored<Doc extends SanityDocumentBase> = Doc &\n Required<SanityDocumentBase>\n\nexport type ToIdentified<Doc extends SanityDocumentBase> = RequiredSelect<\n Doc,\n '_id'\n>\n\nexport type StoredDocument = ToStored<SanityDocumentBase>\n\nfunction createIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateMutation<Doc>): Index {\n const document = assignId(mutation.document, nanoid)\n\n if (document._id in index) {\n throw new Error('Document already exist')\n }\n return {...index, [document._id]: mutation.document}\n}\n\nfunction createIfNotExistsIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateIfNotExistsMutation<Doc>): Index {\n if (!hasId(mutation.document)) {\n throw new Error('Cannot createIfNotExists on document without _id')\n }\n return mutation.document._id in index\n ? index\n : {...index, [mutation.document._id]: mutation.document}\n}\n\nfunction createOrReplaceIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateOrReplaceMutation<Doc>): Index {\n if (!hasId(mutation.document)) {\n throw new Error('Cannot createIfNotExists on document without _id')\n }\n\n return {...index, [mutation.document._id]: mutation.document}\n}\n\nfunction deleteIn<Index extends DocumentIndex<SanityDocumentBase>>(\n index: Index,\n mutation: DeleteMutation,\n): Index {\n if (mutation.id in index) {\n const copy = {...index}\n delete copy[mutation.id]\n return copy\n } else {\n return index\n }\n}\n\nfunction patchIn<Index extends DocumentIndex<SanityDocumentBase>>(\n index: Index,\n mutation: PatchMutation,\n): Index {\n if (!(mutation.id in index)) {\n throw new Error('Cannot apply patch on nonexistent document')\n }\n const current = index[mutation.id]!\n const next = applyPatchMutation(mutation, current)\n\n return next === current ? index : {...index, [mutation.id]: next}\n}\n"],"names":["arrify","createIn","createIfNotExistsIn","deleteIn","createOrReplaceIn","patchIn","splice","applyPatchMutation","assignId","nanoid","hasId"],"mappings":";;;AAagB,SAAA,kBACd,YACA,WACA;AAEA,SADUA,OAAAA,OAAO,SAAS,EACjB,OAAO,CAAC,MAAM,aAAa;AAClC,QAAI,SAAS,SAAS;AACb,aAAAC,WAAS,MAAM,QAAQ;AAEhC,QAAI,SAAS,SAAS;AACb,aAAAC,sBAAoB,MAAM,QAAQ;AAE3C,QAAI,SAAS,SAAS;AACb,aAAAC,WAAS,MAAM,QAAQ;AAEhC,QAAI,SAAS,SAAS;AACb,aAAAC,oBAAkB,MAAM,QAAQ;AAEzC,QAAI,SAAS,SAAS;AACb,aAAAC,UAAQ,MAAM,QAAQ;AAG/B,UAAM,IAAI,MAAM,0BAA0B,SAAS,IAAI,EAAE;AAAA,KACxD,UAAU;AACf;AAEA,SAASJ,WACP,YACA,UACA;AAIA,MAHmB,WAAW;AAAA,IAC5B,CAAO,QAAA,IAAI,QAAQ,SAAS,SAAS;AAAA,EAAA,MAEpB;AACX,UAAA,IAAI,MAAM,wBAAwB;AAEnC,SAAA,WAAW,OAAO,SAAS,QAAQ;AAC5C;AAEA,SAASC,sBACP,YACA,UACA;AAIA,SAHmB,WAAW;AAAA,IAC5B,CAAO,QAAA,IAAI,QAAQ,SAAS,SAAS;AAAA,EAAA,MAEjB,KAAK,WAAW,OAAO,SAAS,QAAQ,IAAI;AACpE;AAEA,SAASE,oBACP,YACA,UACA;A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S,SAAS,GAAG,GAAG,SAAS,SAAQ;AAC9D;AAEA,SAAS,SACP,OACA,UACO;AACH,MAAA,SAAS,MAAM,OAAO;AAClB,UAAA,OAAO,EAAC,GAAG,MAAK;AACf,WAAA,OAAA,KAAK,SAAS,EAAE,GAChB;AAAA,EACT;AACS,WAAA;AAEX;AAEA,SAAS,QACP,OACA,UACO;AACH,MAAA,EAAE,SAAS,MAAM;AACb,UAAA,IAAI,MAAM,4CAA4C;AAExD,QAAA,UAAU,MAAM,SAAS,EAAE,GAC3B,OAAOH,MAAAA,mBAAmB,UAAU,OAAO;AAE1C,SAAA,SAAS,UAAU,QAAQ,EAAC,GAAG,OAAO,CAAC,SAAS,EAAE,GAAG,KAAI;AAClE;;;;;;;;;;"} | ||
| {"version":3,"file":"_unstable_apply.cjs","sources":["../src/apply/applyInCollection.ts","../src/apply/store/store.ts","../src/apply/applyInIndex.ts"],"sourcesContent":["import {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {arrify} from '../utils/arrify'\nimport {applyPatchMutation} from './applyPatchMutation'\nimport {splice} from './utils/array'\n\nexport function applyInCollection<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutations: Mutation | Mutation[],\n) {\n const a = arrify(mutations) as Mutation[]\n return a.reduce((prev, mutation) => {\n if (mutation.type === 'create') {\n return createIn(prev, mutation)\n }\n if (mutation.type === 'createIfNotExists') {\n return createIfNotExistsIn(prev, mutation)\n }\n if (mutation.type === 'delete') {\n return deleteIn(prev, mutation)\n }\n if (mutation.type === 'createOrReplace') {\n return createOrReplaceIn(prev, mutation)\n }\n if (mutation.type === 'patch') {\n return patchIn(prev, mutation)\n }\n // @ts-expect-error all cases should be covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n }, collection)\n}\n\nfunction createIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n if (currentIdx !== -1) {\n throw new Error('Document already exist')\n }\n return collection.concat(mutation.document)\n}\n\nfunction createIfNotExistsIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateIfNotExistsMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n return currentIdx === -1 ? collection.concat(mutation.document) : collection\n}\n\nfunction createOrReplaceIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateOrReplaceMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n return currentIdx === -1\n ? collection.concat(mutation.document)\n : splice(collection, currentIdx, 1, [mutation.document])\n}\n\nfunction deleteIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: DeleteMutation,\n) {\n const currentIdx = collection.findIndex(doc => doc._id === mutation.id)\n return currentIdx === -1 ? collection : splice(collection, currentIdx, 1)\n}\n\nfunction patchIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: PatchMutation,\n): Doc[] {\n const currentIdx = collection.findIndex(doc => doc._id === mutation.id)\n if (currentIdx === -1) {\n throw new Error('Cannot apply patch on nonexistent document')\n }\n const current = collection[currentIdx]!\n\n const next = applyPatchMutation(mutation, current)\n\n return next === current\n ? collection\n : splice(collection, currentIdx, 1, [next])\n}\n","import {nanoid} from 'nanoid'\n\nimport {type DocumentIndex, type Format} from '../../apply'\nimport {type Mutation, type SanityDocumentBase} from '../../mutations/types'\nimport {arrify} from '../../utils/arrify'\nimport {applyInIndex, type ToIdentified, type ToStored} from '../applyInIndex'\nimport {assignId} from './utils'\n\nexport type RequiredSelect<T, K extends keyof T> = Omit<T, K> & {\n [P in K]-?: T[P]\n}\n\nfunction update<Doc extends ToIdentified<SanityDocumentBase>>(\n doc: Doc,\n revision: string,\n): ToStored<Doc> {\n return {\n ...doc,\n _rev: revision,\n _createdAt: doc._createdAt || new Date().toISOString(),\n _updatedAt: new Date().toISOString(),\n }\n}\n\nconst empty: DocumentIndex<any> = {}\n\nexport const createStore = <Doc extends SanityDocumentBase>(\n initialEntries?: Doc[],\n) => {\n let version = 0\n\n let index: DocumentIndex<Format<ToStored<Doc & SanityDocumentBase>>> =\n initialEntries && initialEntries?.length > 0\n ? Object.fromEntries(\n initialEntries.map(entry => {\n const doc = update(assignId(entry, nanoid), nanoid())\n return [doc._id, doc]\n }),\n )\n : empty\n\n return {\n get version() {\n return version\n },\n // todo: support listening for changes\n entries: () => Object.entries(index),\n get: <Id extends string>(\n id: Id,\n ): Format<Omit<(typeof index)[keyof typeof index], '_id'> & {_id: Id}> =>\n index[id] as any,\n apply: (mutations: Mutation[] | Mutation) => {\n const nextIndex = applyInIndex(index, arrify(mutations))\n if (nextIndex !== index) {\n index = nextIndex\n version++\n }\n },\n }\n}\n","import {nanoid} from 'nanoid'\n\nimport {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {applyPatchMutation} from './applyPatchMutation'\nimport {assignId, hasId, type RequiredSelect} from './store'\n\nexport type DocumentIndex<Doc extends SanityDocumentBase> = {[id: string]: Doc}\n\nexport function applyInIndex<\n Doc extends SanityDocumentBase,\n Index extends DocumentIndex<ToStored<Doc>>,\n>(index: Index, mutations: Mutation<Doc>[]): Index {\n return mutations.reduce((prev, mutation) => {\n if (mutation.type === 'create') {\n return createIn(prev, mutation)\n }\n if (mutation.type === 'createIfNotExists') {\n return createIfNotExistsIn(prev, mutation)\n }\n if (mutation.type === 'delete') {\n return deleteIn(prev, mutation)\n }\n if (mutation.type === 'createOrReplace') {\n return createOrReplaceIn(prev, mutation)\n }\n if (mutation.type === 'patch') {\n return patchIn(prev, mutation)\n }\n // @ts-expect-error all cases should be covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n }, index)\n}\n\nexport type ToStored<Doc extends SanityDocumentBase> = Doc &\n Required<SanityDocumentBase>\n\nexport type ToIdentified<Doc extends SanityDocumentBase> = RequiredSelect<\n Doc,\n '_id'\n>\n\nexport type StoredDocument = ToStored<SanityDocumentBase>\n\nfunction createIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateMutation<Doc>): Index {\n const document = assignId(mutation.document, nanoid)\n\n if (document._id in index) {\n throw new Error('Document already exist')\n }\n return {...index, [document._id]: mutation.document}\n}\n\nfunction createIfNotExistsIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateIfNotExistsMutation<Doc>): Index {\n if (!hasId(mutation.document)) {\n throw new Error('Cannot createIfNotExists on document without _id')\n }\n return mutation.document._id in index\n ? index\n : {...index, [mutation.document._id]: mutation.document}\n}\n\nfunction createOrReplaceIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateOrReplaceMutation<Doc>): Index {\n if (!hasId(mutation.document)) {\n throw new Error('Cannot createIfNotExists on document without _id')\n }\n\n return {...index, [mutation.document._id]: mutation.document}\n}\n\nfunction deleteIn<Index extends DocumentIndex<SanityDocumentBase>>(\n index: Index,\n mutation: DeleteMutation,\n): Index {\n if (mutation.id in index) {\n const copy = {...index}\n delete copy[mutation.id]\n return copy\n } else {\n return index\n }\n}\n\nfunction patchIn<Index extends DocumentIndex<SanityDocumentBase>>(\n index: Index,\n mutation: PatchMutation,\n): Index {\n if (!(mutation.id in index)) {\n throw new Error('Cannot apply patch on nonexistent document')\n }\n const current = index[mutation.id]!\n const next = applyPatchMutation(mutation, current)\n\n return next === current ? index : {...index, [mutation.id]: next}\n}\n"],"names":["arrify","createIn","createIfNotExistsIn","deleteIn","createOrReplaceIn","patchIn","splice","applyPatchMutation","assignId","nanoid","hasId"],"mappings":";;;AAaO,SAAS,kBACd,YACA,WACA;AAEA,SADUA,OAAAA,OAA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GAAG,OAAO,CAAC,SAAS,EAAE,GAAG,KAAA;AAC9D;;;;;;;;;;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"_unstable_apply.js","sources":["../src/apply/applyInCollection.ts","../src/apply/store/store.ts","../src/apply/applyInIndex.ts"],"sourcesContent":["import {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {arrify} from '../utils/arrify'\nimport {applyPatchMutation} from './applyPatchMutation'\nimport {splice} from './utils/array'\n\nexport function applyInCollection<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutations: Mutation | Mutation[],\n) {\n const a = arrify(mutations) as Mutation[]\n return a.reduce((prev, mutation) => {\n if (mutation.type === 'create') {\n return createIn(prev, mutation)\n }\n if (mutation.type === 'createIfNotExists') {\n return createIfNotExistsIn(prev, mutation)\n }\n if (mutation.type === 'delete') {\n return deleteIn(prev, mutation)\n }\n if (mutation.type === 'createOrReplace') {\n return createOrReplaceIn(prev, mutation)\n }\n if (mutation.type === 'patch') {\n return patchIn(prev, mutation)\n }\n // @ts-expect-error all cases should be covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n }, collection)\n}\n\nfunction createIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n if (currentIdx !== -1) {\n throw new Error('Document already exist')\n }\n return collection.concat(mutation.document)\n}\n\nfunction createIfNotExistsIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateIfNotExistsMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n return currentIdx === -1 ? collection.concat(mutation.document) : collection\n}\n\nfunction createOrReplaceIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateOrReplaceMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n return currentIdx === -1\n ? collection.concat(mutation.document)\n : splice(collection, currentIdx, 1, [mutation.document])\n}\n\nfunction deleteIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: DeleteMutation,\n) {\n const currentIdx = collection.findIndex(doc => doc._id === mutation.id)\n return currentIdx === -1 ? collection : splice(collection, currentIdx, 1)\n}\n\nfunction patchIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: PatchMutation,\n): Doc[] {\n const currentIdx = collection.findIndex(doc => doc._id === mutation.id)\n if (currentIdx === -1) {\n throw new Error('Cannot apply patch on nonexistent document')\n }\n const current = collection[currentIdx]!\n\n const next = applyPatchMutation(mutation, current)\n\n return next === current\n ? collection\n : splice(collection, currentIdx, 1, [next])\n}\n","import {nanoid} from 'nanoid'\n\nimport {type DocumentIndex, type Format} from '../../apply'\nimport {type Mutation, type SanityDocumentBase} from '../../mutations/types'\nimport {arrify} from '../../utils/arrify'\nimport {applyInIndex, type ToIdentified, type ToStored} from '../applyInIndex'\nimport {assignId} from './utils'\n\nexport type RequiredSelect<T, K extends keyof T> = Omit<T, K> & {\n [P in K]-?: T[P]\n}\n\nfunction update<Doc extends ToIdentified<SanityDocumentBase>>(\n doc: Doc,\n revision: string,\n): ToStored<Doc> {\n return {\n ...doc,\n _rev: revision,\n _createdAt: doc._createdAt || new Date().toISOString(),\n _updatedAt: new Date().toISOString(),\n }\n}\n\nconst empty: DocumentIndex<any> = {}\n\nexport const createStore = <Doc extends SanityDocumentBase>(\n initialEntries?: Doc[],\n) => {\n let version = 0\n\n let index: DocumentIndex<Format<ToStored<Doc & SanityDocumentBase>>> =\n initialEntries && initialEntries?.length > 0\n ? Object.fromEntries(\n initialEntries.map(entry => {\n const doc = update(assignId(entry, nanoid), nanoid())\n return [doc._id, doc]\n }),\n )\n : empty\n\n return {\n get version() {\n return version\n },\n // todo: support listening for changes\n entries: () => Object.entries(index),\n get: <Id extends string>(\n id: Id,\n ): Format<Omit<(typeof index)[keyof typeof index], '_id'> & {_id: Id}> =>\n index[id] as any,\n apply: (mutations: Mutation[] | Mutation) => {\n const nextIndex = applyInIndex(index, arrify(mutations))\n if (nextIndex !== index) {\n index = nextIndex\n version++\n }\n },\n }\n}\n","import {nanoid} from 'nanoid'\n\nimport {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {applyPatchMutation} from './applyPatchMutation'\nimport {assignId, hasId, type RequiredSelect} from './store'\n\nexport type DocumentIndex<Doc extends SanityDocumentBase> = {[id: string]: Doc}\n\nexport function applyInIndex<\n Doc extends SanityDocumentBase,\n Index extends DocumentIndex<ToStored<Doc>>,\n>(index: Index, mutations: Mutation<Doc>[]): Index {\n return mutations.reduce((prev, mutation) => {\n if (mutation.type === 'create') {\n return createIn(prev, mutation)\n }\n if (mutation.type === 'createIfNotExists') {\n return createIfNotExistsIn(prev, mutation)\n }\n if (mutation.type === 'delete') {\n return deleteIn(prev, mutation)\n }\n if (mutation.type === 'createOrReplace') {\n return createOrReplaceIn(prev, mutation)\n }\n if (mutation.type === 'patch') {\n return patchIn(prev, mutation)\n }\n // @ts-expect-error all cases should be covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n }, index)\n}\n\nexport type ToStored<Doc extends SanityDocumentBase> = Doc &\n Required<SanityDocumentBase>\n\nexport type ToIdentified<Doc extends SanityDocumentBase> = RequiredSelect<\n Doc,\n '_id'\n>\n\nexport type StoredDocument = ToStored<SanityDocumentBase>\n\nfunction createIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateMutation<Doc>): Index {\n const document = assignId(mutation.document, nanoid)\n\n if (document._id in index) {\n throw new Error('Document already exist')\n }\n return {...index, [document._id]: mutation.document}\n}\n\nfunction createIfNotExistsIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateIfNotExistsMutation<Doc>): Index {\n if (!hasId(mutation.document)) {\n throw new Error('Cannot createIfNotExists on document without _id')\n }\n return mutation.document._id in index\n ? index\n : {...index, [mutation.document._id]: mutation.document}\n}\n\nfunction createOrReplaceIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateOrReplaceMutation<Doc>): Index {\n if (!hasId(mutation.document)) {\n throw new Error('Cannot createIfNotExists on document without _id')\n }\n\n return {...index, [mutation.document._id]: mutation.document}\n}\n\nfunction deleteIn<Index extends DocumentIndex<SanityDocumentBase>>(\n index: Index,\n mutation: DeleteMutation,\n): Index {\n if (mutation.id in index) {\n const copy = {...index}\n delete copy[mutation.id]\n return copy\n } else {\n return index\n }\n}\n\nfunction patchIn<Index extends DocumentIndex<SanityDocumentBase>>(\n index: Index,\n mutation: PatchMutation,\n): Index {\n if (!(mutation.id in index)) {\n throw new Error('Cannot apply patch on nonexistent document')\n }\n const current = index[mutation.id]!\n const next = applyPatchMutation(mutation, current)\n\n return next === current ? index : {...index, [mutation.id]: next}\n}\n"],"names":["createIn","createIfNotExistsIn","deleteIn","createOrReplaceIn","patchIn"],"mappings":";;;;AAagB,SAAA,kBACd,YACA,WACA;AAEA,SADU,OAAO,SAAS,EACjB,OAAO,CAAC,MAAM,aAAa;AAClC,QAAI,SAAS,SAAS;AACb,aAAAA,WAAS,MAAM,QAAQ;AAEhC,QAAI,SAAS,SAAS;AACb,aAAAC,sBAAoB,MAAM,QAAQ;AAE3C,QAAI,SAAS,SAAS;AACb,aAAAC,WAAS,MAAM,QAAQ;AAEhC,QAAI,SAAS,SAAS;AACb,aAAAC,oBAAkB,MAAM,QAAQ;AAEzC,QAAI,SAAS,SAAS;AACb,aAAAC,UAAQ,MAAM,QAAQ;AAG/B,UAAM,IAAI,MAAM,0BAA0B,SAAS,IAAI,EAAE;AAAA,KACxD,UAAU;AACf;AAEA,SAASJ,WACP,YACA,UAC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M,kDAAkD;AAEpE,SAAO,SAAS,SAAS,OAAO,QAC5B,QACA,EAAC,GAAG,OAAO,CAAC,SAAS,SAAS,GAAG,GAAG,SAAS,SAAQ;AAC3D;AAEA,SAAS,kBAGP,OAAc,UAA+C;AACzD,MAAA,CAAC,MAAM,SAAS,QAAQ;AACpB,UAAA,IAAI,MAAM,kDAAkD;AAG7D,SAAA,EAAC,GAAG,OAAO,CAAC,SAAS,SAAS,GAAG,GAAG,SAAS,SAAQ;AAC9D;AAEA,SAAS,SACP,OACA,UACO;AACH,MAAA,SAAS,MAAM,OAAO;AAClB,UAAA,OAAO,EAAC,GAAG,MAAK;AACf,WAAA,OAAA,KAAK,SAAS,EAAE,GAChB;AAAA,EACT;AACS,WAAA;AAEX;AAEA,SAAS,QACP,OACA,UACO;AACH,MAAA,EAAE,SAAS,MAAM;AACb,UAAA,IAAI,MAAM,4CAA4C;AAExD,QAAA,UAAU,MAAM,SAAS,EAAE,GAC3B,OAAO,mBAAmB,UAAU,OAAO;AAE1C,SAAA,SAAS,UAAU,QAAQ,EAAC,GAAG,OAAO,CAAC,SAAS,EAAE,GAAG,KAAI;AAClE;"} | ||
| {"version":3,"file":"_unstable_apply.js","sources":["../src/apply/applyInCollection.ts","../src/apply/store/store.ts","../src/apply/applyInIndex.ts"],"sourcesContent":["import {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {arrify} from '../utils/arrify'\nimport {applyPatchMutation} from './applyPatchMutation'\nimport {splice} from './utils/array'\n\nexport function applyInCollection<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutations: Mutation | Mutation[],\n) {\n const a = arrify(mutations) as Mutation[]\n return a.reduce((prev, mutation) => {\n if (mutation.type === 'create') {\n return createIn(prev, mutation)\n }\n if (mutation.type === 'createIfNotExists') {\n return createIfNotExistsIn(prev, mutation)\n }\n if (mutation.type === 'delete') {\n return deleteIn(prev, mutation)\n }\n if (mutation.type === 'createOrReplace') {\n return createOrReplaceIn(prev, mutation)\n }\n if (mutation.type === 'patch') {\n return patchIn(prev, mutation)\n }\n // @ts-expect-error all cases should be covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n }, collection)\n}\n\nfunction createIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n if (currentIdx !== -1) {\n throw new Error('Document already exist')\n }\n return collection.concat(mutation.document)\n}\n\nfunction createIfNotExistsIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateIfNotExistsMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n return currentIdx === -1 ? collection.concat(mutation.document) : collection\n}\n\nfunction createOrReplaceIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: CreateOrReplaceMutation<Doc>,\n) {\n const currentIdx = collection.findIndex(\n doc => doc._id === mutation.document._id,\n )\n return currentIdx === -1\n ? collection.concat(mutation.document)\n : splice(collection, currentIdx, 1, [mutation.document])\n}\n\nfunction deleteIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: DeleteMutation,\n) {\n const currentIdx = collection.findIndex(doc => doc._id === mutation.id)\n return currentIdx === -1 ? collection : splice(collection, currentIdx, 1)\n}\n\nfunction patchIn<Doc extends SanityDocumentBase>(\n collection: Doc[],\n mutation: PatchMutation,\n): Doc[] {\n const currentIdx = collection.findIndex(doc => doc._id === mutation.id)\n if (currentIdx === -1) {\n throw new Error('Cannot apply patch on nonexistent document')\n }\n const current = collection[currentIdx]!\n\n const next = applyPatchMutation(mutation, current)\n\n return next === current\n ? collection\n : splice(collection, currentIdx, 1, [next])\n}\n","import {nanoid} from 'nanoid'\n\nimport {type DocumentIndex, type Format} from '../../apply'\nimport {type Mutation, type SanityDocumentBase} from '../../mutations/types'\nimport {arrify} from '../../utils/arrify'\nimport {applyInIndex, type ToIdentified, type ToStored} from '../applyInIndex'\nimport {assignId} from './utils'\n\nexport type RequiredSelect<T, K extends keyof T> = Omit<T, K> & {\n [P in K]-?: T[P]\n}\n\nfunction update<Doc extends ToIdentified<SanityDocumentBase>>(\n doc: Doc,\n revision: string,\n): ToStored<Doc> {\n return {\n ...doc,\n _rev: revision,\n _createdAt: doc._createdAt || new Date().toISOString(),\n _updatedAt: new Date().toISOString(),\n }\n}\n\nconst empty: DocumentIndex<any> = {}\n\nexport const createStore = <Doc extends SanityDocumentBase>(\n initialEntries?: Doc[],\n) => {\n let version = 0\n\n let index: DocumentIndex<Format<ToStored<Doc & SanityDocumentBase>>> =\n initialEntries && initialEntries?.length > 0\n ? Object.fromEntries(\n initialEntries.map(entry => {\n const doc = update(assignId(entry, nanoid), nanoid())\n return [doc._id, doc]\n }),\n )\n : empty\n\n return {\n get version() {\n return version\n },\n // todo: support listening for changes\n entries: () => Object.entries(index),\n get: <Id extends string>(\n id: Id,\n ): Format<Omit<(typeof index)[keyof typeof index], '_id'> & {_id: Id}> =>\n index[id] as any,\n apply: (mutations: Mutation[] | Mutation) => {\n const nextIndex = applyInIndex(index, arrify(mutations))\n if (nextIndex !== index) {\n index = nextIndex\n version++\n }\n },\n }\n}\n","import {nanoid} from 'nanoid'\n\nimport {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {applyPatchMutation} from './applyPatchMutation'\nimport {assignId, hasId, type RequiredSelect} from './store'\n\nexport type DocumentIndex<Doc extends SanityDocumentBase> = {[id: string]: Doc}\n\nexport function applyInIndex<\n Doc extends SanityDocumentBase,\n Index extends DocumentIndex<ToStored<Doc>>,\n>(index: Index, mutations: Mutation<Doc>[]): Index {\n return mutations.reduce((prev, mutation) => {\n if (mutation.type === 'create') {\n return createIn(prev, mutation)\n }\n if (mutation.type === 'createIfNotExists') {\n return createIfNotExistsIn(prev, mutation)\n }\n if (mutation.type === 'delete') {\n return deleteIn(prev, mutation)\n }\n if (mutation.type === 'createOrReplace') {\n return createOrReplaceIn(prev, mutation)\n }\n if (mutation.type === 'patch') {\n return patchIn(prev, mutation)\n }\n // @ts-expect-error all cases should be covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n }, index)\n}\n\nexport type ToStored<Doc extends SanityDocumentBase> = Doc &\n Required<SanityDocumentBase>\n\nexport type ToIdentified<Doc extends SanityDocumentBase> = RequiredSelect<\n Doc,\n '_id'\n>\n\nexport type StoredDocument = ToStored<SanityDocumentBase>\n\nfunction createIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateMutation<Doc>): Index {\n const document = assignId(mutation.document, nanoid)\n\n if (document._id in index) {\n throw new Error('Document already exist')\n }\n return {...index, [document._id]: mutation.document}\n}\n\nfunction createIfNotExistsIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateIfNotExistsMutation<Doc>): Index {\n if (!hasId(mutation.document)) {\n throw new Error('Cannot createIfNotExists on document without _id')\n }\n return mutation.document._id in index\n ? index\n : {...index, [mutation.document._id]: mutation.document}\n}\n\nfunction createOrReplaceIn<\n Index extends DocumentIndex<Doc>,\n Doc extends SanityDocumentBase,\n>(index: Index, mutation: CreateOrReplaceMutation<Doc>): Index {\n if (!hasId(mutation.document)) {\n throw new Error('Cannot createIfNotExists on document without _id')\n }\n\n return {...index, [mutation.document._id]: mutation.document}\n}\n\nfunction deleteIn<Index extends DocumentIndex<SanityDocumentBase>>(\n index: Index,\n mutation: DeleteMutation,\n): Index {\n if (mutation.id in index) {\n const copy = {...index}\n delete copy[mutation.id]\n return copy\n } else {\n return index\n }\n}\n\nfunction patchIn<Index extends DocumentIndex<SanityDocumentBase>>(\n index: Index,\n mutation: PatchMutation,\n): Index {\n if (!(mutation.id in index)) {\n throw new Error('Cannot apply patch on nonexistent document')\n }\n const current = index[mutation.id]!\n const next = applyPatchMutation(mutation, current)\n\n return next === current ? index : {...index, [mutation.id]: next}\n}\n"],"names":["createIn","createIfNotExistsIn","deleteIn","createOrReplaceIn","patchIn"],"mappings":";;;;AAaO,SAAS,kBACd,YACA,WACA;AAEA,SADU,OAAO,SAAS,EACjB,OAAO,CAAC,MAAM,aAAa;AAClC,QAAI,SAAS,SAAS;AACpB,aAAOA,WAAS,MAAM,QAAQ;AAEhC,QAAI,SAAS,SAAS;AACpB,aAAOC,sBAAoB,MAAM,QAAQ;AAE3C,QAAI,SAAS,SAAS;AACpB,aAAOC,WAAS,MAAM,QAAQ;AAEhC,QAAI,SAAS,SAAS;AACpB,aAAOC,oBAAkB,MAAM,QAAQ;AAEzC,QAAI,SAAS,SAAS;AACpB,aAAOC,UAAQ,MAAM,QAAQ;AAG/B,UAAM,IAAI,MAAM,0BAA0B,SAAS,IAAI,EAAE;AAAA,EAC3D,GAAG,UAAU;AACf;AAEA,SAASJ,WACP,YACA,UACA;AAIA,MAHmB,WAAW;AAAA,IAC5B,CAAA,QAAO,IAAI,QAAQ,SAAS,SAAS;AAAA,EAAA,MAEpB;AACjB,UAAM,IAAI,MAAM,wBAAwB;AAE1C,SAAO,WAAW,OAAO,SAAS,QAAQ;AAC5C;AAEA,SAASC,sBACP,YACA,UACA;AAIA,SAHmB,WAAW;AAAA,IAC5B,CAAA,QAAO,IAAI,QAAQ,SAAS,SAAS;AAAA,EAAA,MAEjB,KAAK,WAAW,OAAO,SAAS,QAAQ,IAAI;AACpE;AAEA,SAASE,oBACP,YACA,UACA;AACA,QAAM,aAAa,WAAW;AAAA,IAC5B,CAAA,QAAO,IAAI,QAAQ,SAAS,SAAS;AAAA,EAAA;AAEvC,SAAO,eAAe,KAClB,WAAW,OAAO,SAAS,QAAQ,IACnC,OAAO,YAAY,YAAY,GAAG,CAAC,SAAS,QAAQ,CAAC;AAC3D;AAEA,SAASD,WACP,YACA,UACA;AACA,QAA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AU,MAAM,SAAS,EAAE,GAC3B,OAAO,mBAAmB,UAAU,OAAO;AAEjD,SAAO,SAAS,UAAU,QAAQ,EAAC,GAAG,OAAO,CAAC,SAAS,EAAE,GAAG,KAAA;AAC9D;"} |
@@ -0,1 +1,2 @@ | ||
| import {ListenerEvent as ListenerEvent_2} from '..' | ||
| import {Observable} from 'rxjs' | ||
@@ -65,3 +66,3 @@ import {RawPatch} from 'mendoza' | ||
| documentId: string, | ||
| ) => Observable<ListenerEvent> | ||
| ) => Observable<ListenerEvent_2> | ||
@@ -68,0 +69,0 @@ /** |
@@ -0,1 +1,2 @@ | ||
| import {ListenerEvent as ListenerEvent_2} from '..' | ||
| import {Observable} from 'rxjs' | ||
@@ -65,3 +66,3 @@ import {RawPatch} from 'mendoza' | ||
| documentId: string, | ||
| ) => Observable<ListenerEvent> | ||
| ) => Observable<ListenerEvent_2> | ||
@@ -68,0 +69,0 @@ /** |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.cjs","sources":["../src/encoders/compact/decode.ts","../src/encoders/compact/encode.ts","../src/mutations/creators.ts","../src/mutations/operations/creators.ts","../src/encoders/form-compat/encode.ts","../src/formatters/compact.ts","../src/mutations/autoKeys.ts"],"sourcesContent":["import {\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {type Index, type KeyedPathElement} from '../../path'\nimport {parse as parsePath} from '../../path/parser/parse'\nimport {\n type CompactMutation,\n type CompactPatchMutation,\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type ItemRef,\n} from './types'\n\nexport {Mutation, SanityDocumentBase}\n\nexport function decode<Doc extends SanityDocumentBase>(\n mutations: CompactMutation<Doc>[],\n): Mutation[] {\n return mutations.map(decodeMutation)\n}\n\nexport function decodeMutation<Doc extends SanityDocumentBase>(\n mutation: CompactMutation<Doc>,\n): Mutation {\n const [type] = mutation\n if (type === 'delete') {\n const [, id] = mutation as DeleteMutation\n return {id, type}\n } else if (type === 'create') {\n const [, document] = mutation as CreateMutation<Doc>\n return {type, document}\n } else if (type === 'createIfNotExists') {\n const [, document] = mutation as CreateIfNotExistsMutation<Doc>\n return {type, document}\n } else if (type === 'createOrReplace') {\n const [, document] = mutation as CreateOrReplaceMutation<Doc>\n return {type, document}\n } else if (type === 'patch') {\n return decodePatchMutation(mutation)\n }\n throw new Error(`Unrecognized mutation: ${JSON.stringify(mutation)}`)\n}\n\nfunction decodePatchMutation(mutation: CompactPatchMutation): PatchMutation {\n const [, type, id, serializedPath, , revisionId] = mutation\n\n const path = parsePath(serializedPath)\n if (type === 'dec' || type === 'inc') {\n const [, , , , [amount]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'inc', amount}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'unset') {\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'unset'}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'insert') {\n const [, , , , [position, ref, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {\n path,\n op: {\n type: 'insert',\n position,\n items,\n referenceItem: typeof ref === 'string' ? {_key: ref} : ref,\n },\n },\n ],\n ...createOpts(revisionId),\n }\n }\n if (type === 'set') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'set', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'setIfMissing') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'setIfMissing', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'diffMatchPatch') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'diffMatchPatch', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'truncate') {\n const [, , , , [startIndex, endIndex]] = mutation\n\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'truncate', startIndex, endIndex}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'assign') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'assign', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'replace') {\n const [, , , , [ref, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {path, op: {type: 'replace', items, referenceItem: decodeItemRef(ref)}},\n ],\n ...createOpts(revisionId),\n }\n }\n if (type === 'upsert') {\n const [, , , , [position, referenceItem, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {\n path,\n op: {\n type: 'upsert',\n items,\n referenceItem: decodeItemRef(referenceItem),\n position,\n },\n },\n ],\n ...createOpts(revisionId),\n }\n }\n throw new Error(`Invalid mutation type: ${type}`)\n}\n\nfunction decodeItemRef(ref: ItemRef): Index | KeyedPathElement {\n return typeof ref === 'string' ? {_key: ref} : ref\n}\n\nfunction createOpts(revisionId: undefined | string) {\n return revisionId ? {options: {ifRevision: revisionId}} : null\n}\n","// An example of a compact transport/serialization format\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {type Index, type KeyedPathElement} from '../../path'\nimport {stringify as stringifyPath} from '../../path/parser/stringify'\nimport {\n type CompactMutation,\n type CompactPatchMutation,\n type ItemRef,\n} from './types'\n\nexport function encode<Doc extends SanityDocumentBase>(\n mutations: Mutation[],\n): CompactMutation<Doc>[] {\n return mutations.flatMap(m => encodeMutation<Doc>(m))\n}\n\nfunction encodeItemRef(ref: Index | KeyedPathElement): ItemRef {\n return typeof ref === 'number' ? ref : ref._key\n}\n\nfunction encodeMutation<Doc extends SanityDocumentBase>(\n mutation: Mutation,\n): CompactMutation<Doc>[] {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return [[mutation.type, mutation.document]]\n }\n if (mutation.type === 'delete') {\n return [['delete', mutation.id]]\n }\n if (mutation.type === 'patch') {\n return mutation.patches.map(patch =>\n maybeAddRevision(\n mutation.options?.ifRevision,\n encodePatchMutation(mutation.id, patch),\n ),\n )\n }\n\n //@ts-expect-error - all cases are covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n}\n\nfunction encodePatchMutation(\n id: string,\n patch: NodePatch<any>,\n): CompactPatchMutation {\n const {op} = patch\n const path = stringifyPath(patch.path)\n if (op.type === 'unset') {\n return ['patch', 'unset', id, path, []]\n }\n if (op.type === 'diffMatchPatch') {\n return ['patch', 'diffMatchPatch', id, path, [op.value]]\n }\n if (op.type === 'inc' || op.type === 'dec') {\n return ['patch', op.type, id, path, [op.amount]]\n }\n if (op.type === 'set') {\n return ['patch', op.type, id, path, [op.value]]\n }\n if (op.type === 'setIfMissing') {\n return ['patch', op.type, id, path, [op.value]]\n }\n if (op.type === 'insert') {\n return [\n 'patch',\n 'insert',\n id,\n path,\n [op.position, encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'upsert') {\n return [\n 'patch',\n 'upsert',\n id,\n path,\n [op.position, encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'assign') {\n return ['patch', 'assign', id, path, [op.value]]\n }\n if (op.type === 'unassign') {\n return ['patch', 'assign', id, path, [op.keys]]\n }\n if (op.type === 'replace') {\n return [\n 'patch',\n 'replace',\n id,\n path,\n [encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'truncate') {\n return ['patch', 'truncate', id, path, [op.startIndex, op.endIndex]]\n }\n if (op.type === 'remove') {\n return ['patch', 'remove', id, path, [encodeItemRef(op.referenceItem)]]\n }\n // @ts-expect-error all cases are covered\n throw new Error(`Invalid operation type: ${op.type}`)\n}\n\nfunction maybeAddRevision<T extends CompactPatchMutation>(\n revision: string | undefined,\n mut: T,\n): T {\n const [mutType, patchType, id, path, args] = mut\n return (revision ? [mutType, patchType, id, path, args, revision] : mut) as T\n}\n","import {parse, type Path, type SafePath} from '../path'\nimport {arrify} from '../utils/arrify'\nimport {\n type NormalizeReadOnlyArray,\n type Optional,\n type Tuplify,\n} from '../utils/typeUtils'\nimport {type Operation} from './operations/types'\nimport {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type NodePatch,\n type NodePatchList,\n type PatchMutation,\n type PatchOptions,\n type SanityDocumentBase,\n} from './types'\n\nexport function create<Doc extends Optional<SanityDocumentBase, '_id'>>(\n document: Doc,\n): CreateMutation<Doc> {\n return {type: 'create', document}\n}\n\nexport function patch<P extends NodePatchList | NodePatch>(\n id: string,\n patches: P,\n options?: PatchOptions,\n): PatchMutation<NormalizeReadOnlyArray<Tuplify<P>>> {\n return {\n type: 'patch',\n id,\n patches: arrify(patches) as any,\n ...(options ? {options} : {}),\n }\n}\n\nexport function at<const P extends Path, O extends Operation>(\n path: P,\n operation: O,\n): NodePatch<NormalizeReadOnlyArray<P>, O>\n\nexport function at<const P extends string, O extends Operation>(\n path: P,\n operation: O,\n): NodePatch<SafePath<P>, O>\n\nexport function at<O extends Operation>(\n path: Path | string,\n operation: O,\n): NodePatch<Path, O> {\n return {\n path: typeof path === 'string' ? parse(path) : path,\n op: operation,\n }\n}\n\nexport function createIfNotExists<Doc extends SanityDocumentBase>(\n document: Doc,\n): CreateIfNotExistsMutation<Doc> {\n return {type: 'createIfNotExists', document}\n}\n\nexport function createOrReplace<Doc extends SanityDocumentBase>(\n document: Doc,\n): CreateOrReplaceMutation<Doc> {\n return {type: 'createOrReplace', document}\n}\n\nexport function delete_(id: string): DeleteMutation {\n return {type: 'delete', id}\n}\n\nexport const del = delete_\nexport const destroy = delete_\n","import {arrify} from '../../utils/arrify'\nimport {\n type AnyArray,\n type ArrayElement,\n type NormalizeReadOnlyArray,\n} from '../../utils/typeUtils'\nimport {\n type AssignOp,\n type DecOp,\n type DiffMatchPatchOp,\n type IncOp,\n type Index,\n type InsertOp,\n type KeyedPathElement,\n type RelativePosition,\n type RemoveOp,\n type ReplaceOp,\n type SetIfMissingOp,\n type SetOp,\n type TruncateOp,\n type UnassignOp,\n type UnsetOp,\n type UpsertOp,\n} from './types'\n\nexport const set = <const T>(value: T): SetOp<T> => ({type: 'set', value})\n\nexport const assign = <const T extends {[K in string]: unknown}>(\n value: T,\n): AssignOp<T> => ({\n type: 'assign',\n value,\n})\n\nexport const unassign = <const K extends readonly string[]>(\n keys: K,\n): UnassignOp<K> => ({\n type: 'unassign',\n keys,\n})\n\nexport const setIfMissing = <const T>(value: T): SetIfMissingOp<T> => ({\n type: 'setIfMissing',\n value,\n})\n\nexport const unset = (): UnsetOp => ({type: 'unset'})\n\nexport const inc = <const N extends number = 1>(\n amount: N = 1 as N,\n): IncOp<N> => ({\n type: 'inc',\n amount,\n})\n\nexport const dec = <const N extends number = 1>(\n amount: N = 1 as N,\n): DecOp<N> => ({\n type: 'dec',\n amount,\n})\n\nexport const diffMatchPatch = (value: string): DiffMatchPatchOp => ({\n type: 'diffMatchPatch',\n value,\n})\n\nexport function insert<\n const Items extends AnyArray<unknown>,\n const Pos extends RelativePosition,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n position: Pos,\n indexOrReferenceItem: ReferenceItem,\n): InsertOp<NormalizeReadOnlyArray<Items>, Pos, ReferenceItem> {\n return {\n type: 'insert',\n referenceItem: indexOrReferenceItem,\n position,\n items: arrify(items) as any,\n }\n}\n\nexport function append<const Items extends AnyArray<unknown>>(\n items: Items | ArrayElement<Items>,\n) {\n return insert(items, 'after', -1)\n}\n\nexport function prepend<const Items extends AnyArray<unknown>>(\n items: Items | ArrayElement<Items>,\n) {\n return insert(items, 'before', 0)\n}\n\nexport function insertBefore<\n const Items extends AnyArray<unknown>,\n const ReferenceItem extends Index | KeyedPathElement,\n>(items: Items | ArrayElement<Items>, indexOrReferenceItem: ReferenceItem) {\n return insert(items, 'before', indexOrReferenceItem)\n}\n\nexport const insertAfter = <\n const Items extends AnyArray<unknown>,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n indexOrReferenceItem: ReferenceItem,\n) => {\n return insert(items, 'after', indexOrReferenceItem)\n}\n\nexport function truncate(startIndex: number, endIndex?: number): TruncateOp {\n return {\n type: 'truncate',\n startIndex,\n endIndex,\n }\n}\n\n/*\n Use this when you know the ref Items already exists\n */\nexport function replace<\n Items extends any[],\n ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n referenceItem: ReferenceItem,\n): ReplaceOp<Items, ReferenceItem> {\n return {\n type: 'replace',\n referenceItem,\n items: arrify(items) as Items,\n }\n}\n\n/*\n Remove an item from an array by either key or index\n */\nexport function remove<ReferenceItem extends Index | KeyedPathElement>(\n referenceItem: ReferenceItem,\n): RemoveOp<ReferenceItem> {\n return {\n type: 'remove',\n referenceItem,\n }\n}\n\n/*\nuse this when the reference Items may or may not exist\n */\nexport function upsert<\n const Items extends AnyArray<unknown>,\n const Pos extends RelativePosition,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n position: Pos,\n referenceItem: ReferenceItem,\n): UpsertOp<Items, Pos, ReferenceItem> {\n return {\n type: 'upsert',\n items: arrify(items) as Items,\n referenceItem,\n position,\n }\n}\n","import {at} from '../../mutations/creators'\nimport {\n diffMatchPatch,\n insert,\n set,\n setIfMissing,\n unset,\n} from '../../mutations/operations/creators'\nimport {type NodePatch} from '../../mutations/types'\nimport {type KeyedPathElement} from '../../path'\nimport {\n type CompatPath,\n type FormPatchLike,\n type FormPatchPath,\n} from './form-patch-types'\n\nfunction assertCompatible(formPatchPath: FormPatchPath): CompatPath {\n if (formPatchPath.length === 0) {\n return formPatchPath as never[]\n }\n for (const element of formPatchPath) {\n if (Array.isArray(element)) {\n throw new Error('Form patch paths cannot include arrays')\n }\n }\n return formPatchPath as CompatPath\n}\n\n/**\n * Convert a Sanity form patch (ie emitted from an input component) to a {@link NodePatch}\n * Note the lack of encodeMutation here. Sanity forms never emit *mutations*, only patches\n * @param patches - Array of {@link FormPatchLike}\n * @internal\n */\nexport function encodePatches(patches: FormPatchLike[]): NodePatch[] {\n return patches.map(formPatch => {\n const path = assertCompatible(formPatch.path)\n if (formPatch.type === 'unset') {\n return at(path, unset())\n }\n if (formPatch.type === 'set') {\n return at(path, set(formPatch.value))\n }\n if (formPatch.type === 'setIfMissing') {\n return at(path, setIfMissing(formPatch.value))\n }\n if (formPatch.type === 'insert') {\n const arrayPath = path.slice(0, -1)\n const itemRef = formPatch.path[formPatch.path.length - 1]\n return at(\n arrayPath,\n insert(\n formPatch.items,\n formPatch.position,\n itemRef as number | KeyedPathElement,\n ),\n )\n }\n if (formPatch.type === 'diffMatchPatch') {\n return at(path, diffMatchPatch(formPatch.value))\n }\n // @ts-expect-error - should be exhaustive\n throw new Error(`Unknown patch type ${formPatch.type}`)\n })\n}\n","// An example of a compact formatter\n\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {type Index, type KeyedPathElement, stringify} from '../path'\n\nexport type ItemRef = string | number\n\nexport function format<Doc extends SanityDocumentBase>(\n mutations: Mutation[],\n): string {\n return mutations.flatMap(m => encodeMutation<Doc>(m)).join('\\n')\n}\n\nfunction encodeItemRef(ref: Index | KeyedPathElement): ItemRef {\n return typeof ref === 'number' ? ref : ref._key\n}\n\nfunction encodeMutation<Doc extends SanityDocumentBase>(\n mutation: Mutation,\n): string {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return [mutation.type, ': ', JSON.stringify(mutation.document)].join('')\n }\n if (mutation.type === 'delete') {\n return ['delete ', mutation.id].join(': ')\n }\n if (mutation.type === 'patch') {\n const ifRevision = mutation.options?.ifRevision\n return [\n 'patch',\n ' ',\n `id=${mutation.id}`,\n ifRevision ? ` (if revision==${ifRevision})` : '',\n ':\\n',\n mutation.patches\n .map(nodePatch => ` ${formatPatchMutation(nodePatch)}`)\n .join('\\n'),\n ].join('')\n }\n\n //@ts-expect-error - all cases are covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n}\n\nfunction formatPatchMutation(patch: NodePatch<any>): string {\n const {op} = patch\n const path = stringify(patch.path)\n if (op.type === 'unset') {\n return [path, 'unset()'].join(': ')\n }\n if (op.type === 'diffMatchPatch') {\n return [path, `diffMatchPatch(${op.value})`].join(': ')\n }\n if (op.type === 'inc' || op.type === 'dec') {\n return [path, `${op.type}(${op.amount})`].join(': ')\n }\n if (op.type === 'set' || op.type === 'setIfMissing') {\n return [path, `${op.type}(${JSON.stringify(op.value)})`].join(': ')\n }\n if (op.type === 'assign') {\n return [path, `${op.type}(${JSON.stringify(op.value)})`].join(': ')\n }\n if (op.type === 'unassign') {\n return [path, `${op.type}(${JSON.stringify(op.keys)})`].join(': ')\n }\n if (op.type === 'insert' || op.type === 'upsert') {\n return [\n path,\n `${op.type}(${op.position}, ${encodeItemRef(\n op.referenceItem,\n )}, ${JSON.stringify(op.items)})`,\n ].join(': ')\n }\n if (op.type === 'replace') {\n return [\n path,\n `replace(${encodeItemRef(op.referenceItem)}, ${JSON.stringify(\n op.items,\n )})`,\n ].join(': ')\n }\n if (op.type === 'truncate') {\n return [path, `truncate(${op.startIndex}, ${op.endIndex}`].join(': ')\n }\n if (op.type === 'remove') {\n return [path, `remove(${encodeItemRef(op.referenceItem)})`].join(': ')\n }\n // @ts-expect-error all cases are covered\n throw new Error(`Invalid operation type: ${op.type}`)\n}\n","import {type Index, type KeyedPathElement} from '../path'\nimport {isObject} from '../utils/isObject'\nimport {\n insert as _insert,\n replace as _replace,\n upsert as _upsert,\n} from './operations/creators'\nimport {type RelativePosition} from './operations/types'\n\nexport function autoKeys<Item>(generateKey: (item: Item) => string) {\n const ensureKeys = createEnsureKeys(generateKey)\n\n const insert = <\n Pos extends RelativePosition,\n Ref extends Index | KeyedPathElement,\n >(\n position: Pos,\n referenceItem: Ref,\n items: Item[],\n ) => _insert(ensureKeys(items), position, referenceItem)\n const upsert = <\n Pos extends RelativePosition,\n ReferenceItem extends Index | KeyedPathElement,\n >(\n items: Item[],\n position: Pos,\n referenceItem: ReferenceItem,\n ) => _upsert(ensureKeys(items), position, referenceItem)\n\n const replace = <\n Pos extends RelativePosition,\n ReferenceItem extends Index | KeyedPathElement,\n >(\n items: Item[],\n position: Pos,\n referenceItem: ReferenceItem,\n ) => _replace(ensureKeys(items), referenceItem)\n\n const insertBefore = <Ref extends Index | KeyedPathElement>(\n ref: Ref,\n items: Item[],\n ) => insert('before', ref, items)\n\n const prepend = (items: Item[]) => insertBefore(0, items)\n\n const insertAfter = <Ref extends Index | KeyedPathElement>(\n ref: Ref,\n items: Item[],\n ) => insert('after', ref, items)\n\n const append = (items: Item[]) => insert('after', -1, items)\n\n return {insert, upsert, replace, insertBefore, prepend, insertAfter, append}\n}\n\nfunction hasKey<T extends object>(item: T): item is T & {_key: string} {\n return '_key' in item\n}\n\nfunction createEnsureKeys<T>(generateKey: (item: T) => string) {\n return (array: T[]): T[] => {\n let didModify = false\n const withKeys = array.map(item => {\n if (needsKey(item)) {\n didModify = true\n return {...item, _key: generateKey(item)}\n }\n return item\n })\n return didModify ? withKeys : array\n }\n}\n\nfunction needsKey(arrayItem: any): arrayItem is object {\n return isObject(arrayItem) && !hasKey(arrayItem)\n}\n"],"names":["parsePath","encodeMutation","encodeItemRef","patch","stringifyPath","arrify","parse","stringify","insert","_insert","upsert","_upsert","replace","_replace","insertBefore","isObject"],"mappings":";;;AAmBO,SAAS,OACd,WACY;AACL,SAAA,UAAU,IAAI,cAAc;AACrC;AAEO,SAAS,eACd,UACU;AACJ,QAAA,CAAC,IAAI,IAAI;AACf,MAAI,SAAS,UAAU;AACf,UAAA,CAAA,EAAG,EAAE,IAAI;AACR,WAAA,EAAC,IAAI,KAAI;AAAA,EAAA,WACP,SAAS,UAAU;AACtB,UAAA,CAAA,EAAG,QAAQ,IAAI;AACd,WAAA,EAAC,MAAM,SAAQ;AAAA,EAAA,WACb,SAAS,qBAAqB;AACjC,UAAA,CAAA,EAAG,QAAQ,IAAI;AACd,WAAA,EAAC,MAAM,SAAQ;AAAA,EA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,KAAK,EAEgB;AAC7E;AAEA,SAAS,OAAyB,MAAqC;AACrE,SAAO,UAAU;AACnB;AAEA,SAAS,iBAAoB,aAAkC;AAC7D,SAAO,CAAC,UAAoB;AAC1B,QAAI,YAAY;AAChB,UAAM,WAAW,MAAM,IAAI,CACrB,SAAA,SAAS,IAAI,KACf,YAAY,IACL,EAAC,GAAG,MAAM,MAAM,YAAY,IAAI,OAElC,IACR;AACD,WAAO,YAAY,WAAW;AAAA,EAChC;AACF;AAEA,SAAS,SAAS,WAAqC;AACrD,SAAOO,SAAS,SAAA,SAAS,KAAK,CAAC,OAAO,SAAS;AACjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"} | ||
| {"version":3,"file":"index.cjs","sources":["../src/encoders/compact/decode.ts","../src/encoders/compact/encode.ts","../src/mutations/creators.ts","../src/mutations/operations/creators.ts","../src/encoders/form-compat/encode.ts","../src/formatters/compact.ts","../src/mutations/autoKeys.ts"],"sourcesContent":["import {\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {type Index, type KeyedPathElement} from '../../path'\nimport {parse as parsePath} from '../../path/parser/parse'\nimport {\n type CompactMutation,\n type CompactPatchMutation,\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type ItemRef,\n} from './types'\n\nexport {Mutation, SanityDocumentBase}\n\nexport function decode<Doc extends SanityDocumentBase>(\n mutations: CompactMutation<Doc>[],\n): Mutation[] {\n return mutations.map(decodeMutation)\n}\n\nexport function decodeMutation<Doc extends SanityDocumentBase>(\n mutation: CompactMutation<Doc>,\n): Mutation {\n const [type] = mutation\n if (type === 'delete') {\n const [, id] = mutation as DeleteMutation\n return {id, type}\n } else if (type === 'create') {\n const [, document] = mutation as CreateMutation<Doc>\n return {type, document}\n } else if (type === 'createIfNotExists') {\n const [, document] = mutation as CreateIfNotExistsMutation<Doc>\n return {type, document}\n } else if (type === 'createOrReplace') {\n const [, document] = mutation as CreateOrReplaceMutation<Doc>\n return {type, document}\n } else if (type === 'patch') {\n return decodePatchMutation(mutation)\n }\n throw new Error(`Unrecognized mutation: ${JSON.stringify(mutation)}`)\n}\n\nfunction decodePatchMutation(mutation: CompactPatchMutation): PatchMutation {\n const [, type, id, serializedPath, , revisionId] = mutation\n\n const path = parsePath(serializedPath)\n if (type === 'dec' || type === 'inc') {\n const [, , , , [amount]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'inc', amount}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'unset') {\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'unset'}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'insert') {\n const [, , , , [position, ref, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {\n path,\n op: {\n type: 'insert',\n position,\n items,\n referenceItem: typeof ref === 'string' ? {_key: ref} : ref,\n },\n },\n ],\n ...createOpts(revisionId),\n }\n }\n if (type === 'set') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'set', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'setIfMissing') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'setIfMissing', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'diffMatchPatch') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'diffMatchPatch', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'truncate') {\n const [, , , , [startIndex, endIndex]] = mutation\n\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'truncate', startIndex, endIndex}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'assign') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'assign', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'replace') {\n const [, , , , [ref, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {path, op: {type: 'replace', items, referenceItem: decodeItemRef(ref)}},\n ],\n ...createOpts(revisionId),\n }\n }\n if (type === 'upsert') {\n const [, , , , [position, referenceItem, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {\n path,\n op: {\n type: 'upsert',\n items,\n referenceItem: decodeItemRef(referenceItem),\n position,\n },\n },\n ],\n ...createOpts(revisionId),\n }\n }\n throw new Error(`Invalid mutation type: ${type}`)\n}\n\nfunction decodeItemRef(ref: ItemRef): Index | KeyedPathElement {\n return typeof ref === 'string' ? {_key: ref} : ref\n}\n\nfunction createOpts(revisionId: undefined | string) {\n return revisionId ? {options: {ifRevision: revisionId}} : null\n}\n","// An example of a compact transport/serialization format\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {type Index, type KeyedPathElement} from '../../path'\nimport {stringify as stringifyPath} from '../../path/parser/stringify'\nimport {\n type CompactMutation,\n type CompactPatchMutation,\n type ItemRef,\n} from './types'\n\nexport function encode<Doc extends SanityDocumentBase>(\n mutations: Mutation[],\n): CompactMutation<Doc>[] {\n return mutations.flatMap(m => encodeMutation<Doc>(m))\n}\n\nfunction encodeItemRef(ref: Index | KeyedPathElement): ItemRef {\n return typeof ref === 'number' ? ref : ref._key\n}\n\nfunction encodeMutation<Doc extends SanityDocumentBase>(\n mutation: Mutation,\n): CompactMutation<Doc>[] {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return [[mutation.type, mutation.document]]\n }\n if (mutation.type === 'delete') {\n return [['delete', mutation.id]]\n }\n if (mutation.type === 'patch') {\n return mutation.patches.map(patch =>\n maybeAddRevision(\n mutation.options?.ifRevision,\n encodePatchMutation(mutation.id, patch),\n ),\n )\n }\n\n //@ts-expect-error - all cases are covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n}\n\nfunction encodePatchMutation(\n id: string,\n patch: NodePatch<any>,\n): CompactPatchMutation {\n const {op} = patch\n const path = stringifyPath(patch.path)\n if (op.type === 'unset') {\n return ['patch', 'unset', id, path, []]\n }\n if (op.type === 'diffMatchPatch') {\n return ['patch', 'diffMatchPatch', id, path, [op.value]]\n }\n if (op.type === 'inc' || op.type === 'dec') {\n return ['patch', op.type, id, path, [op.amount]]\n }\n if (op.type === 'set') {\n return ['patch', op.type, id, path, [op.value]]\n }\n if (op.type === 'setIfMissing') {\n return ['patch', op.type, id, path, [op.value]]\n }\n if (op.type === 'insert') {\n return [\n 'patch',\n 'insert',\n id,\n path,\n [op.position, encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'upsert') {\n return [\n 'patch',\n 'upsert',\n id,\n path,\n [op.position, encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'assign') {\n return ['patch', 'assign', id, path, [op.value]]\n }\n if (op.type === 'unassign') {\n return ['patch', 'assign', id, path, [op.keys]]\n }\n if (op.type === 'replace') {\n return [\n 'patch',\n 'replace',\n id,\n path,\n [encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'truncate') {\n return ['patch', 'truncate', id, path, [op.startIndex, op.endIndex]]\n }\n if (op.type === 'remove') {\n return ['patch', 'remove', id, path, [encodeItemRef(op.referenceItem)]]\n }\n // @ts-expect-error all cases are covered\n throw new Error(`Invalid operation type: ${op.type}`)\n}\n\nfunction maybeAddRevision<T extends CompactPatchMutation>(\n revision: string | undefined,\n mut: T,\n): T {\n const [mutType, patchType, id, path, args] = mut\n return (revision ? [mutType, patchType, id, path, args, revision] : mut) as T\n}\n","import {parse, type Path, type SafePath} from '../path'\nimport {arrify} from '../utils/arrify'\nimport {\n type NormalizeReadOnlyArray,\n type Optional,\n type Tuplify,\n} from '../utils/typeUtils'\nimport {type Operation} from './operations/types'\nimport {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type NodePatch,\n type NodePatchList,\n type PatchMutation,\n type PatchOptions,\n type SanityDocumentBase,\n} from './types'\n\nexport function create<Doc extends Optional<SanityDocumentBase, '_id'>>(\n document: Doc,\n): CreateMutation<Doc> {\n return {type: 'create', document}\n}\n\nexport function patch<P extends NodePatchList | NodePatch>(\n id: string,\n patches: P,\n options?: PatchOptions,\n): PatchMutation<NormalizeReadOnlyArray<Tuplify<P>>> {\n return {\n type: 'patch',\n id,\n patches: arrify(patches) as any,\n ...(options ? {options} : {}),\n }\n}\n\nexport function at<const P extends Path, O extends Operation>(\n path: P,\n operation: O,\n): NodePatch<NormalizeReadOnlyArray<P>, O>\n\nexport function at<const P extends string, O extends Operation>(\n path: P,\n operation: O,\n): NodePatch<SafePath<P>, O>\n\nexport function at<O extends Operation>(\n path: Path | string,\n operation: O,\n): NodePatch<Path, O> {\n return {\n path: typeof path === 'string' ? parse(path) : path,\n op: operation,\n }\n}\n\nexport function createIfNotExists<Doc extends SanityDocumentBase>(\n document: Doc,\n): CreateIfNotExistsMutation<Doc> {\n return {type: 'createIfNotExists', document}\n}\n\nexport function createOrReplace<Doc extends SanityDocumentBase>(\n document: Doc,\n): CreateOrReplaceMutation<Doc> {\n return {type: 'createOrReplace', document}\n}\n\nexport function delete_(id: string): DeleteMutation {\n return {type: 'delete', id}\n}\n\nexport const del = delete_\nexport const destroy = delete_\n","import {arrify} from '../../utils/arrify'\nimport {\n type AnyArray,\n type ArrayElement,\n type NormalizeReadOnlyArray,\n} from '../../utils/typeUtils'\nimport {\n type AssignOp,\n type DecOp,\n type DiffMatchPatchOp,\n type IncOp,\n type Index,\n type InsertOp,\n type KeyedPathElement,\n type RelativePosition,\n type RemoveOp,\n type ReplaceOp,\n type SetIfMissingOp,\n type SetOp,\n type TruncateOp,\n type UnassignOp,\n type UnsetOp,\n type UpsertOp,\n} from './types'\n\nexport const set = <const T>(value: T): SetOp<T> => ({type: 'set', value})\n\nexport const assign = <const T extends {[K in string]: unknown}>(\n value: T,\n): AssignOp<T> => ({\n type: 'assign',\n value,\n})\n\nexport const unassign = <const K extends readonly string[]>(\n keys: K,\n): UnassignOp<K> => ({\n type: 'unassign',\n keys,\n})\n\nexport const setIfMissing = <const T>(value: T): SetIfMissingOp<T> => ({\n type: 'setIfMissing',\n value,\n})\n\nexport const unset = (): UnsetOp => ({type: 'unset'})\n\nexport const inc = <const N extends number = 1>(\n amount: N = 1 as N,\n): IncOp<N> => ({\n type: 'inc',\n amount,\n})\n\nexport const dec = <const N extends number = 1>(\n amount: N = 1 as N,\n): DecOp<N> => ({\n type: 'dec',\n amount,\n})\n\nexport const diffMatchPatch = (value: string): DiffMatchPatchOp => ({\n type: 'diffMatchPatch',\n value,\n})\n\nexport function insert<\n const Items extends AnyArray<unknown>,\n const Pos extends RelativePosition,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n position: Pos,\n indexOrReferenceItem: ReferenceItem,\n): InsertOp<NormalizeReadOnlyArray<Items>, Pos, ReferenceItem> {\n return {\n type: 'insert',\n referenceItem: indexOrReferenceItem,\n position,\n items: arrify(items) as any,\n }\n}\n\nexport function append<const Items extends AnyArray<unknown>>(\n items: Items | ArrayElement<Items>,\n) {\n return insert(items, 'after', -1)\n}\n\nexport function prepend<const Items extends AnyArray<unknown>>(\n items: Items | ArrayElement<Items>,\n) {\n return insert(items, 'before', 0)\n}\n\nexport function insertBefore<\n const Items extends AnyArray<unknown>,\n const ReferenceItem extends Index | KeyedPathElement,\n>(items: Items | ArrayElement<Items>, indexOrReferenceItem: ReferenceItem) {\n return insert(items, 'before', indexOrReferenceItem)\n}\n\nexport const insertAfter = <\n const Items extends AnyArray<unknown>,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n indexOrReferenceItem: ReferenceItem,\n) => {\n return insert(items, 'after', indexOrReferenceItem)\n}\n\nexport function truncate(startIndex: number, endIndex?: number): TruncateOp {\n return {\n type: 'truncate',\n startIndex,\n endIndex,\n }\n}\n\n/*\n Use this when you know the ref Items already exists\n */\nexport function replace<\n Items extends any[],\n ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n referenceItem: ReferenceItem,\n): ReplaceOp<Items, ReferenceItem> {\n return {\n type: 'replace',\n referenceItem,\n items: arrify(items) as Items,\n }\n}\n\n/*\n Remove an item from an array by either key or index\n */\nexport function remove<ReferenceItem extends Index | KeyedPathElement>(\n referenceItem: ReferenceItem,\n): RemoveOp<ReferenceItem> {\n return {\n type: 'remove',\n referenceItem,\n }\n}\n\n/*\nuse this when the reference Items may or may not exist\n */\nexport function upsert<\n const Items extends AnyArray<unknown>,\n const Pos extends RelativePosition,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n position: Pos,\n referenceItem: ReferenceItem,\n): UpsertOp<Items, Pos, ReferenceItem> {\n return {\n type: 'upsert',\n items: arrify(items) as Items,\n referenceItem,\n position,\n }\n}\n","import {at} from '../../mutations/creators'\nimport {\n diffMatchPatch,\n insert,\n set,\n setIfMissing,\n unset,\n} from '../../mutations/operations/creators'\nimport {type NodePatch} from '../../mutations/types'\nimport {type KeyedPathElement} from '../../path'\nimport {\n type CompatPath,\n type FormPatchLike,\n type FormPatchPath,\n} from './form-patch-types'\n\nfunction assertCompatible(formPatchPath: FormPatchPath): CompatPath {\n if (formPatchPath.length === 0) {\n return formPatchPath as never[]\n }\n for (const element of formPatchPath) {\n if (Array.isArray(element)) {\n throw new Error('Form patch paths cannot include arrays')\n }\n }\n return formPatchPath as CompatPath\n}\n\n/**\n * Convert a Sanity form patch (ie emitted from an input component) to a {@link NodePatch}\n * Note the lack of encodeMutation here. Sanity forms never emit *mutations*, only patches\n * @param patches - Array of {@link FormPatchLike}\n * @internal\n */\nexport function encodePatches(patches: FormPatchLike[]): NodePatch[] {\n return patches.map(formPatch => {\n const path = assertCompatible(formPatch.path)\n if (formPatch.type === 'unset') {\n return at(path, unset())\n }\n if (formPatch.type === 'set') {\n return at(path, set(formPatch.value))\n }\n if (formPatch.type === 'setIfMissing') {\n return at(path, setIfMissing(formPatch.value))\n }\n if (formPatch.type === 'insert') {\n const arrayPath = path.slice(0, -1)\n const itemRef = formPatch.path[formPatch.path.length - 1]\n return at(\n arrayPath,\n insert(\n formPatch.items,\n formPatch.position,\n itemRef as number | KeyedPathElement,\n ),\n )\n }\n if (formPatch.type === 'diffMatchPatch') {\n return at(path, diffMatchPatch(formPatch.value))\n }\n // @ts-expect-error - should be exhaustive\n throw new Error(`Unknown patch type ${formPatch.type}`)\n })\n}\n","// An example of a compact formatter\n\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {type Index, type KeyedPathElement, stringify} from '../path'\n\nexport type ItemRef = string | number\n\nexport function format<Doc extends SanityDocumentBase>(\n mutations: Mutation[],\n): string {\n return mutations.flatMap(m => encodeMutation<Doc>(m)).join('\\n')\n}\n\nfunction encodeItemRef(ref: Index | KeyedPathElement): ItemRef {\n return typeof ref === 'number' ? ref : ref._key\n}\n\nfunction encodeMutation<Doc extends SanityDocumentBase>(\n mutation: Mutation,\n): string {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return [mutation.type, ': ', JSON.stringify(mutation.document)].join('')\n }\n if (mutation.type === 'delete') {\n return ['delete ', mutation.id].join(': ')\n }\n if (mutation.type === 'patch') {\n const ifRevision = mutation.options?.ifRevision\n return [\n 'patch',\n ' ',\n `id=${mutation.id}`,\n ifRevision ? ` (if revision==${ifRevision})` : '',\n ':\\n',\n mutation.patches\n .map(nodePatch => ` ${formatPatchMutation(nodePatch)}`)\n .join('\\n'),\n ].join('')\n }\n\n //@ts-expect-error - all cases are covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n}\n\nfunction formatPatchMutation(patch: NodePatch<any>): string {\n const {op} = patch\n const path = stringify(patch.path)\n if (op.type === 'unset') {\n return [path, 'unset()'].join(': ')\n }\n if (op.type === 'diffMatchPatch') {\n return [path, `diffMatchPatch(${op.value})`].join(': ')\n }\n if (op.type === 'inc' || op.type === 'dec') {\n return [path, `${op.type}(${op.amount})`].join(': ')\n }\n if (op.type === 'set' || op.type === 'setIfMissing') {\n return [path, `${op.type}(${JSON.stringify(op.value)})`].join(': ')\n }\n if (op.type === 'assign') {\n return [path, `${op.type}(${JSON.stringify(op.value)})`].join(': ')\n }\n if (op.type === 'unassign') {\n return [path, `${op.type}(${JSON.stringify(op.keys)})`].join(': ')\n }\n if (op.type === 'insert' || op.type === 'upsert') {\n return [\n path,\n `${op.type}(${op.position}, ${encodeItemRef(\n op.referenceItem,\n )}, ${JSON.stringify(op.items)})`,\n ].join(': ')\n }\n if (op.type === 'replace') {\n return [\n path,\n `replace(${encodeItemRef(op.referenceItem)}, ${JSON.stringify(\n op.items,\n )})`,\n ].join(': ')\n }\n if (op.type === 'truncate') {\n return [path, `truncate(${op.startIndex}, ${op.endIndex}`].join(': ')\n }\n if (op.type === 'remove') {\n return [path, `remove(${encodeItemRef(op.referenceItem)})`].join(': ')\n }\n // @ts-expect-error all cases are covered\n throw new Error(`Invalid operation type: ${op.type}`)\n}\n","import {type Index, type KeyedPathElement} from '../path'\nimport {isObject} from '../utils/isObject'\nimport {\n insert as _insert,\n replace as _replace,\n upsert as _upsert,\n} from './operations/creators'\nimport {type RelativePosition} from './operations/types'\n\nexport function autoKeys<Item>(generateKey: (item: Item) => string) {\n const ensureKeys = createEnsureKeys(generateKey)\n\n const insert = <\n Pos extends RelativePosition,\n Ref extends Index | KeyedPathElement,\n >(\n position: Pos,\n referenceItem: Ref,\n items: Item[],\n ) => _insert(ensureKeys(items), position, referenceItem)\n const upsert = <\n Pos extends RelativePosition,\n ReferenceItem extends Index | KeyedPathElement,\n >(\n items: Item[],\n position: Pos,\n referenceItem: ReferenceItem,\n ) => _upsert(ensureKeys(items), position, referenceItem)\n\n const replace = <\n Pos extends RelativePosition,\n ReferenceItem extends Index | KeyedPathElement,\n >(\n items: Item[],\n position: Pos,\n referenceItem: ReferenceItem,\n ) => _replace(ensureKeys(items), referenceItem)\n\n const insertBefore = <Ref extends Index | KeyedPathElement>(\n ref: Ref,\n items: Item[],\n ) => insert('before', ref, items)\n\n const prepend = (items: Item[]) => insertBefore(0, items)\n\n const insertAfter = <Ref extends Index | KeyedPathElement>(\n ref: Ref,\n items: Item[],\n ) => insert('after', ref, items)\n\n const append = (items: Item[]) => insert('after', -1, items)\n\n return {insert, upsert, replace, insertBefore, prepend, insertAfter, append}\n}\n\nfunction hasKey<T extends object>(item: T): item is T & {_key: string} {\n return '_key' in item\n}\n\nfunction createEnsureKeys<T>(generateKey: (item: T) => string) {\n return (array: T[]): T[] => {\n let didModify = false\n const withKeys = array.map(item => {\n if (needsKey(item)) {\n didModify = true\n return {...item, _key: generateKey(item)}\n }\n return item\n })\n return didModify ? withKeys : array\n }\n}\n\nfunction needsKey(arrayItem: any): arrayItem is object {\n return isObject(arrayItem) && !hasKey(arrayItem)\n}\n"],"names":["parsePath","encodeMutation","encodeItemRef","patch","stringifyPath","arrify","parse","stringify","insert","_insert","upsert","_upsert","replace","_replace","insertBefore","isObject"],"mappings":";;;AAmBO,SAAS,OACd,WACY;AACZ,SAAO,UAAU,IAAI,cAAc;AACrC;AAEO,SAAS,eACd,UACU;AACV,QAAM,CAAC,IAAI,IAAI;AACf,MAAI,SAAS,UAAU;AACrB,UAAM,CAAA,EAAG,EAAE,IAAI;AACf,WAAO,EAAC,IAAI,KAAA;AAAA,EACd,WAAW,SAAS,UAAU;AAC5B,UAAM,CAAA,EAAG,QAAQ,IAAI;AACrB,WAAO,EAAC,MAAM,SAAA;AAAA,EAChB,WAAW,SAAS,qBAAqB;AACvC,UAAM,CAAA,EAAG,QAAQ,IAAI;AACrB,WAAO,EAAC,MAAM,SAAA;AAAA,EAChB,WAAW,SAAS,mBAAmB;AACrC,UAAM,CAAA,EAAG,QAAQ,IAAI;AACrB,WAAO,EAAC,MAAM,SAAA;AAAA,EAChB,WAAW,SAAS;AAClB,WAAO,oBAAoB,QAAQ;AAErC,QAAM,IAAI,M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AAC1B,QAAI,YAAY;AAChB,UAAM,WAAW,MAAM,IAAI,CAAA,SACrB,SAAS,IAAI,KACf,YAAY,IACL,EAAC,GAAG,MAAM,MAAM,YAAY,IAAI,EAAA,KAElC,IACR;AACD,WAAO,YAAY,WAAW;AAAA,EAChC;AACF;AAEA,SAAS,SAAS,WAAqC;AACrD,SAAOO,SAAAA,SAAS,SAAS,KAAK,CAAC,OAAO,SAAS;AACjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"} |
+18
-13
@@ -0,1 +1,6 @@ | ||
| import {AnyArray as AnyArray_2} from '../..' | ||
| import {InsertOp as InsertOp_2} from '..' | ||
| import {ReplaceOp as ReplaceOp_2} from '..' | ||
| import {UpsertOp as UpsertOp_2} from '..' | ||
| export declare type AnyArray<T = any> = T[] | readonly T[] | ||
@@ -55,3 +60,3 @@ | ||
| items: Item[], | ||
| ) => InsertOp<Item[], Pos, Ref> | ||
| ) => InsertOp_2<Item[], Pos, Ref> | ||
| upsert: < | ||
@@ -64,3 +69,3 @@ Pos extends RelativePosition, | ||
| referenceItem: ReferenceItem, | ||
| ) => UpsertOp<Item[], Pos, ReferenceItem> | ||
| ) => UpsertOp_2<Item[], Pos, ReferenceItem> | ||
| replace: < | ||
@@ -73,13 +78,13 @@ Pos extends RelativePosition, | ||
| referenceItem: ReferenceItem, | ||
| ) => ReplaceOp<Item[], ReferenceItem> | ||
| ) => ReplaceOp_2<Item[], ReferenceItem> | ||
| insertBefore: <Ref extends Index | KeyedPathElement>( | ||
| ref: Ref, | ||
| items: Item[], | ||
| ) => InsertOp<Item[], 'before', Ref> | ||
| prepend: (items: Item[]) => InsertOp<Item[], 'before', 0> | ||
| ) => InsertOp_2<Item[], 'before', Ref> | ||
| prepend: (items: Item[]) => InsertOp_2<Item[], 'before', 0> | ||
| insertAfter: <Ref extends Index | KeyedPathElement>( | ||
| ref: Ref, | ||
| items: Item[], | ||
| ) => InsertOp<Item[], 'after', Ref> | ||
| append: (items: Item[]) => InsertOp<Item[], 'after', -1> | ||
| ) => InsertOp_2<Item[], 'after', Ref> | ||
| append: (items: Item[]) => InsertOp_2<Item[], 'after', -1> | ||
| } | ||
@@ -295,3 +300,3 @@ | ||
| [x: string]: string | readonly any[] | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| replace?: undefined | ||
@@ -357,3 +362,3 @@ } | ||
| [x: string]: string | readonly any[] | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| replace?: undefined | ||
@@ -383,3 +388,3 @@ } | ||
| replace: string | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| } | ||
@@ -423,3 +428,3 @@ unset?: undefined | ||
| [x: string]: string | readonly any[] | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| replace?: undefined | ||
@@ -485,3 +490,3 @@ } | ||
| [x: string]: string | readonly any[] | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| replace?: undefined | ||
@@ -511,3 +516,3 @@ } | ||
| replace: string | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| } | ||
@@ -514,0 +519,0 @@ unset?: undefined |
+18
-13
@@ -0,1 +1,6 @@ | ||
| import {AnyArray as AnyArray_2} from '../..' | ||
| import {InsertOp as InsertOp_2} from '..' | ||
| import {ReplaceOp as ReplaceOp_2} from '..' | ||
| import {UpsertOp as UpsertOp_2} from '..' | ||
| export declare type AnyArray<T = any> = T[] | readonly T[] | ||
@@ -55,3 +60,3 @@ | ||
| items: Item[], | ||
| ) => InsertOp<Item[], Pos, Ref> | ||
| ) => InsertOp_2<Item[], Pos, Ref> | ||
| upsert: < | ||
@@ -64,3 +69,3 @@ Pos extends RelativePosition, | ||
| referenceItem: ReferenceItem, | ||
| ) => UpsertOp<Item[], Pos, ReferenceItem> | ||
| ) => UpsertOp_2<Item[], Pos, ReferenceItem> | ||
| replace: < | ||
@@ -73,13 +78,13 @@ Pos extends RelativePosition, | ||
| referenceItem: ReferenceItem, | ||
| ) => ReplaceOp<Item[], ReferenceItem> | ||
| ) => ReplaceOp_2<Item[], ReferenceItem> | ||
| insertBefore: <Ref extends Index | KeyedPathElement>( | ||
| ref: Ref, | ||
| items: Item[], | ||
| ) => InsertOp<Item[], 'before', Ref> | ||
| prepend: (items: Item[]) => InsertOp<Item[], 'before', 0> | ||
| ) => InsertOp_2<Item[], 'before', Ref> | ||
| prepend: (items: Item[]) => InsertOp_2<Item[], 'before', 0> | ||
| insertAfter: <Ref extends Index | KeyedPathElement>( | ||
| ref: Ref, | ||
| items: Item[], | ||
| ) => InsertOp<Item[], 'after', Ref> | ||
| append: (items: Item[]) => InsertOp<Item[], 'after', -1> | ||
| ) => InsertOp_2<Item[], 'after', Ref> | ||
| append: (items: Item[]) => InsertOp_2<Item[], 'after', -1> | ||
| } | ||
@@ -295,3 +300,3 @@ | ||
| [x: string]: string | readonly any[] | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| replace?: undefined | ||
@@ -357,3 +362,3 @@ } | ||
| [x: string]: string | readonly any[] | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| replace?: undefined | ||
@@ -383,3 +388,3 @@ } | ||
| replace: string | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| } | ||
@@ -423,3 +428,3 @@ unset?: undefined | ||
| [x: string]: string | readonly any[] | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| replace?: undefined | ||
@@ -485,3 +490,3 @@ } | ||
| [x: string]: string | readonly any[] | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| replace?: undefined | ||
@@ -511,3 +516,3 @@ } | ||
| replace: string | ||
| items: AnyArray | ||
| items: AnyArray_2 | ||
| } | ||
@@ -514,0 +519,0 @@ unset?: undefined |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.js","sources":["../src/encoders/compact/decode.ts","../src/encoders/compact/encode.ts","../src/mutations/creators.ts","../src/mutations/operations/creators.ts","../src/encoders/form-compat/encode.ts","../src/formatters/compact.ts","../src/mutations/autoKeys.ts"],"sourcesContent":["import {\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {type Index, type KeyedPathElement} from '../../path'\nimport {parse as parsePath} from '../../path/parser/parse'\nimport {\n type CompactMutation,\n type CompactPatchMutation,\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type ItemRef,\n} from './types'\n\nexport {Mutation, SanityDocumentBase}\n\nexport function decode<Doc extends SanityDocumentBase>(\n mutations: CompactMutation<Doc>[],\n): Mutation[] {\n return mutations.map(decodeMutation)\n}\n\nexport function decodeMutation<Doc extends SanityDocumentBase>(\n mutation: CompactMutation<Doc>,\n): Mutation {\n const [type] = mutation\n if (type === 'delete') {\n const [, id] = mutation as DeleteMutation\n return {id, type}\n } else if (type === 'create') {\n const [, document] = mutation as CreateMutation<Doc>\n return {type, document}\n } else if (type === 'createIfNotExists') {\n const [, document] = mutation as CreateIfNotExistsMutation<Doc>\n return {type, document}\n } else if (type === 'createOrReplace') {\n const [, document] = mutation as CreateOrReplaceMutation<Doc>\n return {type, document}\n } else if (type === 'patch') {\n return decodePatchMutation(mutation)\n }\n throw new Error(`Unrecognized mutation: ${JSON.stringify(mutation)}`)\n}\n\nfunction decodePatchMutation(mutation: CompactPatchMutation): PatchMutation {\n const [, type, id, serializedPath, , revisionId] = mutation\n\n const path = parsePath(serializedPath)\n if (type === 'dec' || type === 'inc') {\n const [, , , , [amount]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'inc', amount}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'unset') {\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'unset'}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'insert') {\n const [, , , , [position, ref, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {\n path,\n op: {\n type: 'insert',\n position,\n items,\n referenceItem: typeof ref === 'string' ? {_key: ref} : ref,\n },\n },\n ],\n ...createOpts(revisionId),\n }\n }\n if (type === 'set') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'set', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'setIfMissing') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'setIfMissing', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'diffMatchPatch') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'diffMatchPatch', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'truncate') {\n const [, , , , [startIndex, endIndex]] = mutation\n\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'truncate', startIndex, endIndex}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'assign') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'assign', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'replace') {\n const [, , , , [ref, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {path, op: {type: 'replace', items, referenceItem: decodeItemRef(ref)}},\n ],\n ...createOpts(revisionId),\n }\n }\n if (type === 'upsert') {\n const [, , , , [position, referenceItem, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {\n path,\n op: {\n type: 'upsert',\n items,\n referenceItem: decodeItemRef(referenceItem),\n position,\n },\n },\n ],\n ...createOpts(revisionId),\n }\n }\n throw new Error(`Invalid mutation type: ${type}`)\n}\n\nfunction decodeItemRef(ref: ItemRef): Index | KeyedPathElement {\n return typeof ref === 'string' ? {_key: ref} : ref\n}\n\nfunction createOpts(revisionId: undefined | string) {\n return revisionId ? {options: {ifRevision: revisionId}} : null\n}\n","// An example of a compact transport/serialization format\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {type Index, type KeyedPathElement} from '../../path'\nimport {stringify as stringifyPath} from '../../path/parser/stringify'\nimport {\n type CompactMutation,\n type CompactPatchMutation,\n type ItemRef,\n} from './types'\n\nexport function encode<Doc extends SanityDocumentBase>(\n mutations: Mutation[],\n): CompactMutation<Doc>[] {\n return mutations.flatMap(m => encodeMutation<Doc>(m))\n}\n\nfunction encodeItemRef(ref: Index | KeyedPathElement): ItemRef {\n return typeof ref === 'number' ? ref : ref._key\n}\n\nfunction encodeMutation<Doc extends SanityDocumentBase>(\n mutation: Mutation,\n): CompactMutation<Doc>[] {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return [[mutation.type, mutation.document]]\n }\n if (mutation.type === 'delete') {\n return [['delete', mutation.id]]\n }\n if (mutation.type === 'patch') {\n return mutation.patches.map(patch =>\n maybeAddRevision(\n mutation.options?.ifRevision,\n encodePatchMutation(mutation.id, patch),\n ),\n )\n }\n\n //@ts-expect-error - all cases are covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n}\n\nfunction encodePatchMutation(\n id: string,\n patch: NodePatch<any>,\n): CompactPatchMutation {\n const {op} = patch\n const path = stringifyPath(patch.path)\n if (op.type === 'unset') {\n return ['patch', 'unset', id, path, []]\n }\n if (op.type === 'diffMatchPatch') {\n return ['patch', 'diffMatchPatch', id, path, [op.value]]\n }\n if (op.type === 'inc' || op.type === 'dec') {\n return ['patch', op.type, id, path, [op.amount]]\n }\n if (op.type === 'set') {\n return ['patch', op.type, id, path, [op.value]]\n }\n if (op.type === 'setIfMissing') {\n return ['patch', op.type, id, path, [op.value]]\n }\n if (op.type === 'insert') {\n return [\n 'patch',\n 'insert',\n id,\n path,\n [op.position, encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'upsert') {\n return [\n 'patch',\n 'upsert',\n id,\n path,\n [op.position, encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'assign') {\n return ['patch', 'assign', id, path, [op.value]]\n }\n if (op.type === 'unassign') {\n return ['patch', 'assign', id, path, [op.keys]]\n }\n if (op.type === 'replace') {\n return [\n 'patch',\n 'replace',\n id,\n path,\n [encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'truncate') {\n return ['patch', 'truncate', id, path, [op.startIndex, op.endIndex]]\n }\n if (op.type === 'remove') {\n return ['patch', 'remove', id, path, [encodeItemRef(op.referenceItem)]]\n }\n // @ts-expect-error all cases are covered\n throw new Error(`Invalid operation type: ${op.type}`)\n}\n\nfunction maybeAddRevision<T extends CompactPatchMutation>(\n revision: string | undefined,\n mut: T,\n): T {\n const [mutType, patchType, id, path, args] = mut\n return (revision ? [mutType, patchType, id, path, args, revision] : mut) as T\n}\n","import {parse, type Path, type SafePath} from '../path'\nimport {arrify} from '../utils/arrify'\nimport {\n type NormalizeReadOnlyArray,\n type Optional,\n type Tuplify,\n} from '../utils/typeUtils'\nimport {type Operation} from './operations/types'\nimport {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type NodePatch,\n type NodePatchList,\n type PatchMutation,\n type PatchOptions,\n type SanityDocumentBase,\n} from './types'\n\nexport function create<Doc extends Optional<SanityDocumentBase, '_id'>>(\n document: Doc,\n): CreateMutation<Doc> {\n return {type: 'create', document}\n}\n\nexport function patch<P extends NodePatchList | NodePatch>(\n id: string,\n patches: P,\n options?: PatchOptions,\n): PatchMutation<NormalizeReadOnlyArray<Tuplify<P>>> {\n return {\n type: 'patch',\n id,\n patches: arrify(patches) as any,\n ...(options ? {options} : {}),\n }\n}\n\nexport function at<const P extends Path, O extends Operation>(\n path: P,\n operation: O,\n): NodePatch<NormalizeReadOnlyArray<P>, O>\n\nexport function at<const P extends string, O extends Operation>(\n path: P,\n operation: O,\n): NodePatch<SafePath<P>, O>\n\nexport function at<O extends Operation>(\n path: Path | string,\n operation: O,\n): NodePatch<Path, O> {\n return {\n path: typeof path === 'string' ? parse(path) : path,\n op: operation,\n }\n}\n\nexport function createIfNotExists<Doc extends SanityDocumentBase>(\n document: Doc,\n): CreateIfNotExistsMutation<Doc> {\n return {type: 'createIfNotExists', document}\n}\n\nexport function createOrReplace<Doc extends SanityDocumentBase>(\n document: Doc,\n): CreateOrReplaceMutation<Doc> {\n return {type: 'createOrReplace', document}\n}\n\nexport function delete_(id: string): DeleteMutation {\n return {type: 'delete', id}\n}\n\nexport const del = delete_\nexport const destroy = delete_\n","import {arrify} from '../../utils/arrify'\nimport {\n type AnyArray,\n type ArrayElement,\n type NormalizeReadOnlyArray,\n} from '../../utils/typeUtils'\nimport {\n type AssignOp,\n type DecOp,\n type DiffMatchPatchOp,\n type IncOp,\n type Index,\n type InsertOp,\n type KeyedPathElement,\n type RelativePosition,\n type RemoveOp,\n type ReplaceOp,\n type SetIfMissingOp,\n type SetOp,\n type TruncateOp,\n type UnassignOp,\n type UnsetOp,\n type UpsertOp,\n} from './types'\n\nexport const set = <const T>(value: T): SetOp<T> => ({type: 'set', value})\n\nexport const assign = <const T extends {[K in string]: unknown}>(\n value: T,\n): AssignOp<T> => ({\n type: 'assign',\n value,\n})\n\nexport const unassign = <const K extends readonly string[]>(\n keys: K,\n): UnassignOp<K> => ({\n type: 'unassign',\n keys,\n})\n\nexport const setIfMissing = <const T>(value: T): SetIfMissingOp<T> => ({\n type: 'setIfMissing',\n value,\n})\n\nexport const unset = (): UnsetOp => ({type: 'unset'})\n\nexport const inc = <const N extends number = 1>(\n amount: N = 1 as N,\n): IncOp<N> => ({\n type: 'inc',\n amount,\n})\n\nexport const dec = <const N extends number = 1>(\n amount: N = 1 as N,\n): DecOp<N> => ({\n type: 'dec',\n amount,\n})\n\nexport const diffMatchPatch = (value: string): DiffMatchPatchOp => ({\n type: 'diffMatchPatch',\n value,\n})\n\nexport function insert<\n const Items extends AnyArray<unknown>,\n const Pos extends RelativePosition,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n position: Pos,\n indexOrReferenceItem: ReferenceItem,\n): InsertOp<NormalizeReadOnlyArray<Items>, Pos, ReferenceItem> {\n return {\n type: 'insert',\n referenceItem: indexOrReferenceItem,\n position,\n items: arrify(items) as any,\n }\n}\n\nexport function append<const Items extends AnyArray<unknown>>(\n items: Items | ArrayElement<Items>,\n) {\n return insert(items, 'after', -1)\n}\n\nexport function prepend<const Items extends AnyArray<unknown>>(\n items: Items | ArrayElement<Items>,\n) {\n return insert(items, 'before', 0)\n}\n\nexport function insertBefore<\n const Items extends AnyArray<unknown>,\n const ReferenceItem extends Index | KeyedPathElement,\n>(items: Items | ArrayElement<Items>, indexOrReferenceItem: ReferenceItem) {\n return insert(items, 'before', indexOrReferenceItem)\n}\n\nexport const insertAfter = <\n const Items extends AnyArray<unknown>,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n indexOrReferenceItem: ReferenceItem,\n) => {\n return insert(items, 'after', indexOrReferenceItem)\n}\n\nexport function truncate(startIndex: number, endIndex?: number): TruncateOp {\n return {\n type: 'truncate',\n startIndex,\n endIndex,\n }\n}\n\n/*\n Use this when you know the ref Items already exists\n */\nexport function replace<\n Items extends any[],\n ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n referenceItem: ReferenceItem,\n): ReplaceOp<Items, ReferenceItem> {\n return {\n type: 'replace',\n referenceItem,\n items: arrify(items) as Items,\n }\n}\n\n/*\n Remove an item from an array by either key or index\n */\nexport function remove<ReferenceItem extends Index | KeyedPathElement>(\n referenceItem: ReferenceItem,\n): RemoveOp<ReferenceItem> {\n return {\n type: 'remove',\n referenceItem,\n }\n}\n\n/*\nuse this when the reference Items may or may not exist\n */\nexport function upsert<\n const Items extends AnyArray<unknown>,\n const Pos extends RelativePosition,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n position: Pos,\n referenceItem: ReferenceItem,\n): UpsertOp<Items, Pos, ReferenceItem> {\n return {\n type: 'upsert',\n items: arrify(items) as Items,\n referenceItem,\n position,\n }\n}\n","import {at} from '../../mutations/creators'\nimport {\n diffMatchPatch,\n insert,\n set,\n setIfMissing,\n unset,\n} from '../../mutations/operations/creators'\nimport {type NodePatch} from '../../mutations/types'\nimport {type KeyedPathElement} from '../../path'\nimport {\n type CompatPath,\n type FormPatchLike,\n type FormPatchPath,\n} from './form-patch-types'\n\nfunction assertCompatible(formPatchPath: FormPatchPath): CompatPath {\n if (formPatchPath.length === 0) {\n return formPatchPath as never[]\n }\n for (const element of formPatchPath) {\n if (Array.isArray(element)) {\n throw new Error('Form patch paths cannot include arrays')\n }\n }\n return formPatchPath as CompatPath\n}\n\n/**\n * Convert a Sanity form patch (ie emitted from an input component) to a {@link NodePatch}\n * Note the lack of encodeMutation here. Sanity forms never emit *mutations*, only patches\n * @param patches - Array of {@link FormPatchLike}\n * @internal\n */\nexport function encodePatches(patches: FormPatchLike[]): NodePatch[] {\n return patches.map(formPatch => {\n const path = assertCompatible(formPatch.path)\n if (formPatch.type === 'unset') {\n return at(path, unset())\n }\n if (formPatch.type === 'set') {\n return at(path, set(formPatch.value))\n }\n if (formPatch.type === 'setIfMissing') {\n return at(path, setIfMissing(formPatch.value))\n }\n if (formPatch.type === 'insert') {\n const arrayPath = path.slice(0, -1)\n const itemRef = formPatch.path[formPatch.path.length - 1]\n return at(\n arrayPath,\n insert(\n formPatch.items,\n formPatch.position,\n itemRef as number | KeyedPathElement,\n ),\n )\n }\n if (formPatch.type === 'diffMatchPatch') {\n return at(path, diffMatchPatch(formPatch.value))\n }\n // @ts-expect-error - should be exhaustive\n throw new Error(`Unknown patch type ${formPatch.type}`)\n })\n}\n","// An example of a compact formatter\n\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {type Index, type KeyedPathElement, stringify} from '../path'\n\nexport type ItemRef = string | number\n\nexport function format<Doc extends SanityDocumentBase>(\n mutations: Mutation[],\n): string {\n return mutations.flatMap(m => encodeMutation<Doc>(m)).join('\\n')\n}\n\nfunction encodeItemRef(ref: Index | KeyedPathElement): ItemRef {\n return typeof ref === 'number' ? ref : ref._key\n}\n\nfunction encodeMutation<Doc extends SanityDocumentBase>(\n mutation: Mutation,\n): string {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return [mutation.type, ': ', JSON.stringify(mutation.document)].join('')\n }\n if (mutation.type === 'delete') {\n return ['delete ', mutation.id].join(': ')\n }\n if (mutation.type === 'patch') {\n const ifRevision = mutation.options?.ifRevision\n return [\n 'patch',\n ' ',\n `id=${mutation.id}`,\n ifRevision ? ` (if revision==${ifRevision})` : '',\n ':\\n',\n mutation.patches\n .map(nodePatch => ` ${formatPatchMutation(nodePatch)}`)\n .join('\\n'),\n ].join('')\n }\n\n //@ts-expect-error - all cases are covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n}\n\nfunction formatPatchMutation(patch: NodePatch<any>): string {\n const {op} = patch\n const path = stringify(patch.path)\n if (op.type === 'unset') {\n return [path, 'unset()'].join(': ')\n }\n if (op.type === 'diffMatchPatch') {\n return [path, `diffMatchPatch(${op.value})`].join(': ')\n }\n if (op.type === 'inc' || op.type === 'dec') {\n return [path, `${op.type}(${op.amount})`].join(': ')\n }\n if (op.type === 'set' || op.type === 'setIfMissing') {\n return [path, `${op.type}(${JSON.stringify(op.value)})`].join(': ')\n }\n if (op.type === 'assign') {\n return [path, `${op.type}(${JSON.stringify(op.value)})`].join(': ')\n }\n if (op.type === 'unassign') {\n return [path, `${op.type}(${JSON.stringify(op.keys)})`].join(': ')\n }\n if (op.type === 'insert' || op.type === 'upsert') {\n return [\n path,\n `${op.type}(${op.position}, ${encodeItemRef(\n op.referenceItem,\n )}, ${JSON.stringify(op.items)})`,\n ].join(': ')\n }\n if (op.type === 'replace') {\n return [\n path,\n `replace(${encodeItemRef(op.referenceItem)}, ${JSON.stringify(\n op.items,\n )})`,\n ].join(': ')\n }\n if (op.type === 'truncate') {\n return [path, `truncate(${op.startIndex}, ${op.endIndex}`].join(': ')\n }\n if (op.type === 'remove') {\n return [path, `remove(${encodeItemRef(op.referenceItem)})`].join(': ')\n }\n // @ts-expect-error all cases are covered\n throw new Error(`Invalid operation type: ${op.type}`)\n}\n","import {type Index, type KeyedPathElement} from '../path'\nimport {isObject} from '../utils/isObject'\nimport {\n insert as _insert,\n replace as _replace,\n upsert as _upsert,\n} from './operations/creators'\nimport {type RelativePosition} from './operations/types'\n\nexport function autoKeys<Item>(generateKey: (item: Item) => string) {\n const ensureKeys = createEnsureKeys(generateKey)\n\n const insert = <\n Pos extends RelativePosition,\n Ref extends Index | KeyedPathElement,\n >(\n position: Pos,\n referenceItem: Ref,\n items: Item[],\n ) => _insert(ensureKeys(items), position, referenceItem)\n const upsert = <\n Pos extends RelativePosition,\n ReferenceItem extends Index | KeyedPathElement,\n >(\n items: Item[],\n position: Pos,\n referenceItem: ReferenceItem,\n ) => _upsert(ensureKeys(items), position, referenceItem)\n\n const replace = <\n Pos extends RelativePosition,\n ReferenceItem extends Index | KeyedPathElement,\n >(\n items: Item[],\n position: Pos,\n referenceItem: ReferenceItem,\n ) => _replace(ensureKeys(items), referenceItem)\n\n const insertBefore = <Ref extends Index | KeyedPathElement>(\n ref: Ref,\n items: Item[],\n ) => insert('before', ref, items)\n\n const prepend = (items: Item[]) => insertBefore(0, items)\n\n const insertAfter = <Ref extends Index | KeyedPathElement>(\n ref: Ref,\n items: Item[],\n ) => insert('after', ref, items)\n\n const append = (items: Item[]) => insert('after', -1, items)\n\n return {insert, upsert, replace, insertBefore, prepend, insertAfter, append}\n}\n\nfunction hasKey<T extends object>(item: T): item is T & {_key: string} {\n return '_key' in item\n}\n\nfunction createEnsureKeys<T>(generateKey: (item: T) => string) {\n return (array: T[]): T[] => {\n let didModify = false\n const withKeys = array.map(item => {\n if (needsKey(item)) {\n didModify = true\n return {...item, _key: generateKey(item)}\n }\n return item\n })\n return didModify ? withKeys : array\n }\n}\n\nfunction needsKey(arrayItem: any): arrayItem is object {\n return isObject(arrayItem) && !hasKey(arrayItem)\n}\n"],"names":["parsePath","encodeMutation","encodeItemRef","patch","stringifyPath","insert","_insert","up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aAAa,GACjDC,WAAS,CAIb,OACA,UACA,kBACGC,OAAQ,WAAW,KAAK,GAAG,UAAU,aAAa,GAEjDC,YAAU,CAId,OACA,UACA,kBACGC,QAAS,WAAW,KAAK,GAAG,aAAa,GAExCC,gBAAe,CACnB,KACA,UACGN,SAAO,UAAU,KAAK,KAAK;AAWhC,SAAO,UAACA,UAAA,QAAQE,UAAQE,SAAAA,WAAS,cAAAE,eAAc,SAT/B,CAAC,UAAkBA,cAAa,GAAG,KAAK,GASA,aAPpC,CAClB,KACA,UACGN,SAAO,SAAS,KAAK,KAAK,GAIsC,QAFtD,CAAC,UAAkBA,SAAO,SAAS,IAAI,KAAK,EAEgB;AAC7E;AAEA,SAAS,OAAyB,MAAqC;AACrE,SAAO,UAAU;AACnB;AAEA,SAAS,iBAAoB,aAAkC;AAC7D,SAAO,CAAC,UAAoB;AAC1B,QAAI,YAAY;AAChB,UAAM,WAAW,MAAM,IAAI,CACrB,SAAA,SAAS,IAAI,KACf,YAAY,IACL,EAAC,GAAG,MAAM,MAAM,YAAY,IAAI,OAElC,IACR;AACD,WAAO,YAAY,WAAW;AAAA,EAChC;AACF;AAEA,SAAS,SAAS,WAAqC;AACrD,SAAO,SAAS,SAAS,KAAK,CAAC,OAAO,SAAS;AACjD;"} | ||
| {"version":3,"file":"index.js","sources":["../src/encoders/compact/decode.ts","../src/encoders/compact/encode.ts","../src/mutations/creators.ts","../src/mutations/operations/creators.ts","../src/encoders/form-compat/encode.ts","../src/formatters/compact.ts","../src/mutations/autoKeys.ts"],"sourcesContent":["import {\n type Mutation,\n type PatchMutation,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {type Index, type KeyedPathElement} from '../../path'\nimport {parse as parsePath} from '../../path/parser/parse'\nimport {\n type CompactMutation,\n type CompactPatchMutation,\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type ItemRef,\n} from './types'\n\nexport {Mutation, SanityDocumentBase}\n\nexport function decode<Doc extends SanityDocumentBase>(\n mutations: CompactMutation<Doc>[],\n): Mutation[] {\n return mutations.map(decodeMutation)\n}\n\nexport function decodeMutation<Doc extends SanityDocumentBase>(\n mutation: CompactMutation<Doc>,\n): Mutation {\n const [type] = mutation\n if (type === 'delete') {\n const [, id] = mutation as DeleteMutation\n return {id, type}\n } else if (type === 'create') {\n const [, document] = mutation as CreateMutation<Doc>\n return {type, document}\n } else if (type === 'createIfNotExists') {\n const [, document] = mutation as CreateIfNotExistsMutation<Doc>\n return {type, document}\n } else if (type === 'createOrReplace') {\n const [, document] = mutation as CreateOrReplaceMutation<Doc>\n return {type, document}\n } else if (type === 'patch') {\n return decodePatchMutation(mutation)\n }\n throw new Error(`Unrecognized mutation: ${JSON.stringify(mutation)}`)\n}\n\nfunction decodePatchMutation(mutation: CompactPatchMutation): PatchMutation {\n const [, type, id, serializedPath, , revisionId] = mutation\n\n const path = parsePath(serializedPath)\n if (type === 'dec' || type === 'inc') {\n const [, , , , [amount]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'inc', amount}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'unset') {\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'unset'}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'insert') {\n const [, , , , [position, ref, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {\n path,\n op: {\n type: 'insert',\n position,\n items,\n referenceItem: typeof ref === 'string' ? {_key: ref} : ref,\n },\n },\n ],\n ...createOpts(revisionId),\n }\n }\n if (type === 'set') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'set', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'setIfMissing') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'setIfMissing', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'diffMatchPatch') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'diffMatchPatch', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'truncate') {\n const [, , , , [startIndex, endIndex]] = mutation\n\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'truncate', startIndex, endIndex}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'assign') {\n const [, , , , [value]] = mutation\n return {\n type: 'patch',\n id,\n patches: [{path, op: {type: 'assign', value}}],\n ...createOpts(revisionId),\n }\n }\n if (type === 'replace') {\n const [, , , , [ref, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {path, op: {type: 'replace', items, referenceItem: decodeItemRef(ref)}},\n ],\n ...createOpts(revisionId),\n }\n }\n if (type === 'upsert') {\n const [, , , , [position, referenceItem, items]] = mutation\n return {\n type: 'patch',\n id,\n patches: [\n {\n path,\n op: {\n type: 'upsert',\n items,\n referenceItem: decodeItemRef(referenceItem),\n position,\n },\n },\n ],\n ...createOpts(revisionId),\n }\n }\n throw new Error(`Invalid mutation type: ${type}`)\n}\n\nfunction decodeItemRef(ref: ItemRef): Index | KeyedPathElement {\n return typeof ref === 'string' ? {_key: ref} : ref\n}\n\nfunction createOpts(revisionId: undefined | string) {\n return revisionId ? {options: {ifRevision: revisionId}} : null\n}\n","// An example of a compact transport/serialization format\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../../mutations/types'\nimport {type Index, type KeyedPathElement} from '../../path'\nimport {stringify as stringifyPath} from '../../path/parser/stringify'\nimport {\n type CompactMutation,\n type CompactPatchMutation,\n type ItemRef,\n} from './types'\n\nexport function encode<Doc extends SanityDocumentBase>(\n mutations: Mutation[],\n): CompactMutation<Doc>[] {\n return mutations.flatMap(m => encodeMutation<Doc>(m))\n}\n\nfunction encodeItemRef(ref: Index | KeyedPathElement): ItemRef {\n return typeof ref === 'number' ? ref : ref._key\n}\n\nfunction encodeMutation<Doc extends SanityDocumentBase>(\n mutation: Mutation,\n): CompactMutation<Doc>[] {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return [[mutation.type, mutation.document]]\n }\n if (mutation.type === 'delete') {\n return [['delete', mutation.id]]\n }\n if (mutation.type === 'patch') {\n return mutation.patches.map(patch =>\n maybeAddRevision(\n mutation.options?.ifRevision,\n encodePatchMutation(mutation.id, patch),\n ),\n )\n }\n\n //@ts-expect-error - all cases are covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n}\n\nfunction encodePatchMutation(\n id: string,\n patch: NodePatch<any>,\n): CompactPatchMutation {\n const {op} = patch\n const path = stringifyPath(patch.path)\n if (op.type === 'unset') {\n return ['patch', 'unset', id, path, []]\n }\n if (op.type === 'diffMatchPatch') {\n return ['patch', 'diffMatchPatch', id, path, [op.value]]\n }\n if (op.type === 'inc' || op.type === 'dec') {\n return ['patch', op.type, id, path, [op.amount]]\n }\n if (op.type === 'set') {\n return ['patch', op.type, id, path, [op.value]]\n }\n if (op.type === 'setIfMissing') {\n return ['patch', op.type, id, path, [op.value]]\n }\n if (op.type === 'insert') {\n return [\n 'patch',\n 'insert',\n id,\n path,\n [op.position, encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'upsert') {\n return [\n 'patch',\n 'upsert',\n id,\n path,\n [op.position, encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'assign') {\n return ['patch', 'assign', id, path, [op.value]]\n }\n if (op.type === 'unassign') {\n return ['patch', 'assign', id, path, [op.keys]]\n }\n if (op.type === 'replace') {\n return [\n 'patch',\n 'replace',\n id,\n path,\n [encodeItemRef(op.referenceItem), op.items],\n ]\n }\n if (op.type === 'truncate') {\n return ['patch', 'truncate', id, path, [op.startIndex, op.endIndex]]\n }\n if (op.type === 'remove') {\n return ['patch', 'remove', id, path, [encodeItemRef(op.referenceItem)]]\n }\n // @ts-expect-error all cases are covered\n throw new Error(`Invalid operation type: ${op.type}`)\n}\n\nfunction maybeAddRevision<T extends CompactPatchMutation>(\n revision: string | undefined,\n mut: T,\n): T {\n const [mutType, patchType, id, path, args] = mut\n return (revision ? [mutType, patchType, id, path, args, revision] : mut) as T\n}\n","import {parse, type Path, type SafePath} from '../path'\nimport {arrify} from '../utils/arrify'\nimport {\n type NormalizeReadOnlyArray,\n type Optional,\n type Tuplify,\n} from '../utils/typeUtils'\nimport {type Operation} from './operations/types'\nimport {\n type CreateIfNotExistsMutation,\n type CreateMutation,\n type CreateOrReplaceMutation,\n type DeleteMutation,\n type NodePatch,\n type NodePatchList,\n type PatchMutation,\n type PatchOptions,\n type SanityDocumentBase,\n} from './types'\n\nexport function create<Doc extends Optional<SanityDocumentBase, '_id'>>(\n document: Doc,\n): CreateMutation<Doc> {\n return {type: 'create', document}\n}\n\nexport function patch<P extends NodePatchList | NodePatch>(\n id: string,\n patches: P,\n options?: PatchOptions,\n): PatchMutation<NormalizeReadOnlyArray<Tuplify<P>>> {\n return {\n type: 'patch',\n id,\n patches: arrify(patches) as any,\n ...(options ? {options} : {}),\n }\n}\n\nexport function at<const P extends Path, O extends Operation>(\n path: P,\n operation: O,\n): NodePatch<NormalizeReadOnlyArray<P>, O>\n\nexport function at<const P extends string, O extends Operation>(\n path: P,\n operation: O,\n): NodePatch<SafePath<P>, O>\n\nexport function at<O extends Operation>(\n path: Path | string,\n operation: O,\n): NodePatch<Path, O> {\n return {\n path: typeof path === 'string' ? parse(path) : path,\n op: operation,\n }\n}\n\nexport function createIfNotExists<Doc extends SanityDocumentBase>(\n document: Doc,\n): CreateIfNotExistsMutation<Doc> {\n return {type: 'createIfNotExists', document}\n}\n\nexport function createOrReplace<Doc extends SanityDocumentBase>(\n document: Doc,\n): CreateOrReplaceMutation<Doc> {\n return {type: 'createOrReplace', document}\n}\n\nexport function delete_(id: string): DeleteMutation {\n return {type: 'delete', id}\n}\n\nexport const del = delete_\nexport const destroy = delete_\n","import {arrify} from '../../utils/arrify'\nimport {\n type AnyArray,\n type ArrayElement,\n type NormalizeReadOnlyArray,\n} from '../../utils/typeUtils'\nimport {\n type AssignOp,\n type DecOp,\n type DiffMatchPatchOp,\n type IncOp,\n type Index,\n type InsertOp,\n type KeyedPathElement,\n type RelativePosition,\n type RemoveOp,\n type ReplaceOp,\n type SetIfMissingOp,\n type SetOp,\n type TruncateOp,\n type UnassignOp,\n type UnsetOp,\n type UpsertOp,\n} from './types'\n\nexport const set = <const T>(value: T): SetOp<T> => ({type: 'set', value})\n\nexport const assign = <const T extends {[K in string]: unknown}>(\n value: T,\n): AssignOp<T> => ({\n type: 'assign',\n value,\n})\n\nexport const unassign = <const K extends readonly string[]>(\n keys: K,\n): UnassignOp<K> => ({\n type: 'unassign',\n keys,\n})\n\nexport const setIfMissing = <const T>(value: T): SetIfMissingOp<T> => ({\n type: 'setIfMissing',\n value,\n})\n\nexport const unset = (): UnsetOp => ({type: 'unset'})\n\nexport const inc = <const N extends number = 1>(\n amount: N = 1 as N,\n): IncOp<N> => ({\n type: 'inc',\n amount,\n})\n\nexport const dec = <const N extends number = 1>(\n amount: N = 1 as N,\n): DecOp<N> => ({\n type: 'dec',\n amount,\n})\n\nexport const diffMatchPatch = (value: string): DiffMatchPatchOp => ({\n type: 'diffMatchPatch',\n value,\n})\n\nexport function insert<\n const Items extends AnyArray<unknown>,\n const Pos extends RelativePosition,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n position: Pos,\n indexOrReferenceItem: ReferenceItem,\n): InsertOp<NormalizeReadOnlyArray<Items>, Pos, ReferenceItem> {\n return {\n type: 'insert',\n referenceItem: indexOrReferenceItem,\n position,\n items: arrify(items) as any,\n }\n}\n\nexport function append<const Items extends AnyArray<unknown>>(\n items: Items | ArrayElement<Items>,\n) {\n return insert(items, 'after', -1)\n}\n\nexport function prepend<const Items extends AnyArray<unknown>>(\n items: Items | ArrayElement<Items>,\n) {\n return insert(items, 'before', 0)\n}\n\nexport function insertBefore<\n const Items extends AnyArray<unknown>,\n const ReferenceItem extends Index | KeyedPathElement,\n>(items: Items | ArrayElement<Items>, indexOrReferenceItem: ReferenceItem) {\n return insert(items, 'before', indexOrReferenceItem)\n}\n\nexport const insertAfter = <\n const Items extends AnyArray<unknown>,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n indexOrReferenceItem: ReferenceItem,\n) => {\n return insert(items, 'after', indexOrReferenceItem)\n}\n\nexport function truncate(startIndex: number, endIndex?: number): TruncateOp {\n return {\n type: 'truncate',\n startIndex,\n endIndex,\n }\n}\n\n/*\n Use this when you know the ref Items already exists\n */\nexport function replace<\n Items extends any[],\n ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n referenceItem: ReferenceItem,\n): ReplaceOp<Items, ReferenceItem> {\n return {\n type: 'replace',\n referenceItem,\n items: arrify(items) as Items,\n }\n}\n\n/*\n Remove an item from an array by either key or index\n */\nexport function remove<ReferenceItem extends Index | KeyedPathElement>(\n referenceItem: ReferenceItem,\n): RemoveOp<ReferenceItem> {\n return {\n type: 'remove',\n referenceItem,\n }\n}\n\n/*\nuse this when the reference Items may or may not exist\n */\nexport function upsert<\n const Items extends AnyArray<unknown>,\n const Pos extends RelativePosition,\n const ReferenceItem extends Index | KeyedPathElement,\n>(\n items: Items | ArrayElement<Items>,\n position: Pos,\n referenceItem: ReferenceItem,\n): UpsertOp<Items, Pos, ReferenceItem> {\n return {\n type: 'upsert',\n items: arrify(items) as Items,\n referenceItem,\n position,\n }\n}\n","import {at} from '../../mutations/creators'\nimport {\n diffMatchPatch,\n insert,\n set,\n setIfMissing,\n unset,\n} from '../../mutations/operations/creators'\nimport {type NodePatch} from '../../mutations/types'\nimport {type KeyedPathElement} from '../../path'\nimport {\n type CompatPath,\n type FormPatchLike,\n type FormPatchPath,\n} from './form-patch-types'\n\nfunction assertCompatible(formPatchPath: FormPatchPath): CompatPath {\n if (formPatchPath.length === 0) {\n return formPatchPath as never[]\n }\n for (const element of formPatchPath) {\n if (Array.isArray(element)) {\n throw new Error('Form patch paths cannot include arrays')\n }\n }\n return formPatchPath as CompatPath\n}\n\n/**\n * Convert a Sanity form patch (ie emitted from an input component) to a {@link NodePatch}\n * Note the lack of encodeMutation here. Sanity forms never emit *mutations*, only patches\n * @param patches - Array of {@link FormPatchLike}\n * @internal\n */\nexport function encodePatches(patches: FormPatchLike[]): NodePatch[] {\n return patches.map(formPatch => {\n const path = assertCompatible(formPatch.path)\n if (formPatch.type === 'unset') {\n return at(path, unset())\n }\n if (formPatch.type === 'set') {\n return at(path, set(formPatch.value))\n }\n if (formPatch.type === 'setIfMissing') {\n return at(path, setIfMissing(formPatch.value))\n }\n if (formPatch.type === 'insert') {\n const arrayPath = path.slice(0, -1)\n const itemRef = formPatch.path[formPatch.path.length - 1]\n return at(\n arrayPath,\n insert(\n formPatch.items,\n formPatch.position,\n itemRef as number | KeyedPathElement,\n ),\n )\n }\n if (formPatch.type === 'diffMatchPatch') {\n return at(path, diffMatchPatch(formPatch.value))\n }\n // @ts-expect-error - should be exhaustive\n throw new Error(`Unknown patch type ${formPatch.type}`)\n })\n}\n","// An example of a compact formatter\n\nimport {\n type Mutation,\n type NodePatch,\n type SanityDocumentBase,\n} from '../mutations/types'\nimport {type Index, type KeyedPathElement, stringify} from '../path'\n\nexport type ItemRef = string | number\n\nexport function format<Doc extends SanityDocumentBase>(\n mutations: Mutation[],\n): string {\n return mutations.flatMap(m => encodeMutation<Doc>(m)).join('\\n')\n}\n\nfunction encodeItemRef(ref: Index | KeyedPathElement): ItemRef {\n return typeof ref === 'number' ? ref : ref._key\n}\n\nfunction encodeMutation<Doc extends SanityDocumentBase>(\n mutation: Mutation,\n): string {\n if (\n mutation.type === 'create' ||\n mutation.type === 'createIfNotExists' ||\n mutation.type === 'createOrReplace'\n ) {\n return [mutation.type, ': ', JSON.stringify(mutation.document)].join('')\n }\n if (mutation.type === 'delete') {\n return ['delete ', mutation.id].join(': ')\n }\n if (mutation.type === 'patch') {\n const ifRevision = mutation.options?.ifRevision\n return [\n 'patch',\n ' ',\n `id=${mutation.id}`,\n ifRevision ? ` (if revision==${ifRevision})` : '',\n ':\\n',\n mutation.patches\n .map(nodePatch => ` ${formatPatchMutation(nodePatch)}`)\n .join('\\n'),\n ].join('')\n }\n\n //@ts-expect-error - all cases are covered\n throw new Error(`Invalid mutation type: ${mutation.type}`)\n}\n\nfunction formatPatchMutation(patch: NodePatch<any>): string {\n const {op} = patch\n const path = stringify(patch.path)\n if (op.type === 'unset') {\n return [path, 'unset()'].join(': ')\n }\n if (op.type === 'diffMatchPatch') {\n return [path, `diffMatchPatch(${op.value})`].join(': ')\n }\n if (op.type === 'inc' || op.type === 'dec') {\n return [path, `${op.type}(${op.amount})`].join(': ')\n }\n if (op.type === 'set' || op.type === 'setIfMissing') {\n return [path, `${op.type}(${JSON.stringify(op.value)})`].join(': ')\n }\n if (op.type === 'assign') {\n return [path, `${op.type}(${JSON.stringify(op.value)})`].join(': ')\n }\n if (op.type === 'unassign') {\n return [path, `${op.type}(${JSON.stringify(op.keys)})`].join(': ')\n }\n if (op.type === 'insert' || op.type === 'upsert') {\n return [\n path,\n `${op.type}(${op.position}, ${encodeItemRef(\n op.referenceItem,\n )}, ${JSON.stringify(op.items)})`,\n ].join(': ')\n }\n if (op.type === 'replace') {\n return [\n path,\n `replace(${encodeItemRef(op.referenceItem)}, ${JSON.stringify(\n op.items,\n )})`,\n ].join(': ')\n }\n if (op.type === 'truncate') {\n return [path, `truncate(${op.startIndex}, ${op.endIndex}`].join(': ')\n }\n if (op.type === 'remove') {\n return [path, `remove(${encodeItemRef(op.referenceItem)})`].join(': ')\n }\n // @ts-expect-error all cases are covered\n throw new Error(`Invalid operation type: ${op.type}`)\n}\n","import {type Index, type KeyedPathElement} from '../path'\nimport {isObject} from '../utils/isObject'\nimport {\n insert as _insert,\n replace as _replace,\n upsert as _upsert,\n} from './operations/creators'\nimport {type RelativePosition} from './operations/types'\n\nexport function autoKeys<Item>(generateKey: (item: Item) => string) {\n const ensureKeys = createEnsureKeys(generateKey)\n\n const insert = <\n Pos extends RelativePosition,\n Ref extends Index | KeyedPathElement,\n >(\n position: Pos,\n referenceItem: Ref,\n items: Item[],\n ) => _insert(ensureKeys(items), position, referenceItem)\n const upsert = <\n Pos extends RelativePosition,\n ReferenceItem extends Index | KeyedPathElement,\n >(\n items: Item[],\n position: Pos,\n referenceItem: ReferenceItem,\n ) => _upsert(ensureKeys(items), position, referenceItem)\n\n const replace = <\n Pos extends RelativePosition,\n ReferenceItem extends Index | KeyedPathElement,\n >(\n items: Item[],\n position: Pos,\n referenceItem: ReferenceItem,\n ) => _replace(ensureKeys(items), referenceItem)\n\n const insertBefore = <Ref extends Index | KeyedPathElement>(\n ref: Ref,\n items: Item[],\n ) => insert('before', ref, items)\n\n const prepend = (items: Item[]) => insertBefore(0, items)\n\n const insertAfter = <Ref extends Index | KeyedPathElement>(\n ref: Ref,\n items: Item[],\n ) => insert('after', ref, items)\n\n const append = (items: Item[]) => insert('after', -1, items)\n\n return {insert, upsert, replace, insertBefore, prepend, insertAfter, append}\n}\n\nfunction hasKey<T extends object>(item: T): item is T & {_key: string} {\n return '_key' in item\n}\n\nfunction createEnsureKeys<T>(generateKey: (item: T) => string) {\n return (array: T[]): T[] => {\n let didModify = false\n const withKeys = array.map(item => {\n if (needsKey(item)) {\n didModify = true\n return {...item, _key: generateKey(item)}\n }\n return item\n })\n return didModify ? withKeys : array\n }\n}\n\nfunction needsKey(arrayItem: any): arrayItem is object {\n return isObject(arrayItem) && !hasKey(arrayItem)\n}\n"],"names":["parsePath","encodeMutation","encodeItemRef","patch","stringifyPath","insert","_insert","upsert","_upsert","replace","_replace","insertBefore"],"mappings":";;;;;AAmBO,SAAS,OACd,WACY;AACZ,SAAO,UAAU,IAAI,cAAc;AACrC;AAEO,SAAS,eACd,UACU;AACV,QAAM,CAAC,IAAI,IAAI;AACf,MAAI,SAAS,UAAU;AACrB,UAAM,CAAA,EAAG,EAAE,IAAI;AACf,WAAO,EAAC,IAAI,KAAA;AAAA,EACd,WAAW,SAAS,U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S,cAAAE,eAAc,SAT/B,CAAC,UAAkBA,cAAa,GAAG,KAAK,GASA,aAPpC,CAClB,KACA,UACGN,SAAO,SAAS,KAAK,KAAK,GAIsC,QAFtD,CAAC,UAAkBA,SAAO,SAAS,IAAI,KAAK,EAAA;AAG7D;AAEA,SAAS,OAAyB,MAAqC;AACrE,SAAO,UAAU;AACnB;AAEA,SAAS,iBAAoB,aAAkC;AAC7D,SAAO,CAAC,UAAoB;AAC1B,QAAI,YAAY;AAChB,UAAM,WAAW,MAAM,IAAI,CAAA,SACrB,SAAS,IAAI,KACf,YAAY,IACL,EAAC,GAAG,MAAM,MAAM,YAAY,IAAI,EAAA,KAElC,IACR;AACD,WAAO,YAAY,WAAW;AAAA,EAChC;AACF;AAEA,SAAS,SAAS,WAAqC;AACrD,SAAO,SAAS,SAAS,KAAK,CAAC,OAAO,SAAS;AACjD;"} |
+3
-3
| { | ||
| "name": "@sanity/mutate", | ||
| "version": "0.12.4", | ||
| "version": "0.12.5", | ||
| "description": "Experimental toolkit for working with Sanity mutations in JavaScript & TypeScript", | ||
@@ -73,3 +73,3 @@ "keywords": [ | ||
| "dependencies": { | ||
| "@sanity/client": "^6.28.3", | ||
| "@sanity/client": "^7.9.0", | ||
| "@sanity/diff-match-patch": "^3.2.0", | ||
@@ -85,3 +85,3 @@ "@sanity/uuid": "^3.0.2", | ||
| "@eslint/js": "^9.22.0", | ||
| "@sanity/pkg-utils": "^6.13.4", | ||
| "@sanity/pkg-utils": "^8.0.4", | ||
| "@sanity/prettier-config": "^1.0.3", | ||
@@ -88,0 +88,0 @@ "@types/diff-match-patch": "^1.0.36", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
756165
0.11%7972
0.06%+ Added
+ Added
- Removed
Updated