@portabletext/patches
Advanced tools
Comparing version
@@ -39,3 +39,7 @@ /** @beta */ | ||
/** @public */ | ||
export declare function insert(items: any[], position: InsertPosition_2, path?: Path): InsertPatch | ||
export declare function insert( | ||
items: any[], | ||
position: InsertPosition_2, | ||
path?: Path, | ||
): InsertPatch | ||
@@ -42,0 +46,0 @@ /** @public */ |
@@ -10,3 +10,5 @@ import isObject from "lodash/isObject.js"; | ||
if (position !== BEFORE && position !== AFTER) | ||
throw new Error(`Invalid position "${position}", must be either ${BEFORE} or ${AFTER}`); | ||
throw new Error( | ||
`Invalid position "${position}", must be either ${BEFORE} or ${AFTER}` | ||
); | ||
const items = flatten(...args); | ||
@@ -21,3 +23,5 @@ if (array.length === 0) | ||
} | ||
const hasOwn = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty); | ||
const hasOwn = Object.prototype.hasOwnProperty.call.bind( | ||
Object.prototype.hasOwnProperty | ||
); | ||
function move(arr, from, to) { | ||
@@ -68,3 +72,5 @@ const nextValue = arr.slice(), val = nextValue[from]; | ||
if (typeof index != "number") | ||
throw new Error(`Expected array index to be a number, instead got "${index}"`); | ||
throw new Error( | ||
`Expected array index to be a number, instead got "${index}"` | ||
); | ||
return nextValue.splice(index, 1), nextValue; | ||
@@ -71,0 +77,0 @@ } |
{ | ||
"name": "@portabletext/patches", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Portable Text Patches", | ||
@@ -39,23 +39,18 @@ "keywords": [ | ||
"dependencies": { | ||
"@sanity/diff-match-patch": "^3.1.1", | ||
"@sanity/diff-match-patch": "^3.1.2", | ||
"lodash": "^4.17.21" | ||
}, | ||
"devDependencies": { | ||
"@sanity/pkg-utils": "^6.10.6", | ||
"@types/lodash": "^4.17.7", | ||
"@typescript-eslint/eslint-plugin": "^7.17.0", | ||
"@typescript-eslint/parser": "^7.17.0", | ||
"eslint": "^8.57.0", | ||
"eslint-plugin-import": "^2.29.1", | ||
"eslint-plugin-simple-import-sort": "^12.1.1", | ||
"typescript": "^5.5.3" | ||
"@sanity/pkg-utils": "^6.12.0", | ||
"@types/lodash": "^4.17.13", | ||
"typescript": "5.7.2" | ||
}, | ||
"scripts": { | ||
"build": "pkg-utils build --strict --check --clean", | ||
"check:lint": "eslint .", | ||
"check:lint": "biome lint .", | ||
"check:types": "tsc --noEmit --pretty", | ||
"clean": "del .turbo && del dist && del node_modules", | ||
"dev": "pkg-utils watch", | ||
"lint:fix": "eslint . --fix" | ||
"lint:fix": "biome lint --write ." | ||
} | ||
} |
import {isObject, isString} from 'lodash' | ||
import applyArrayPatch from './array' | ||
@@ -13,3 +12,6 @@ import applyObjectPatch from './object' | ||
function applyPatch(value: string, patch: {type: string; path: any[]; value: any}) { | ||
function applyPatch( | ||
value: string, | ||
patch: {type: string; path: any[]; value: any}, | ||
) { | ||
if (Array.isArray(value)) { | ||
@@ -27,3 +29,6 @@ return applyArrayPatch(value, patch as any) | ||
export default function _apply(value: string, patch: {type: string; path: any[]; value: any}) { | ||
export default function _apply( | ||
value: string, | ||
patch: {type: string; path: any[]; value: any}, | ||
) { | ||
const res = applyPatch(value, patch) | ||
@@ -30,0 +35,0 @@ // console.log('applyPatch(%o, %o) : %o (noop? %o)', value, patch, res, value === res) |
import {findIndex} from 'lodash' | ||
import applyPatch from './applyPatch' | ||
@@ -7,3 +6,5 @@ import insert from './arrayInsert' | ||
const hasOwn = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty) | ||
const hasOwn = Object.prototype.hasOwnProperty.call.bind( | ||
Object.prototype.hasOwnProperty, | ||
) | ||
@@ -36,3 +37,2 @@ function move(arr: any[], from: number, to: any) { | ||
if (!Array.isArray(patch.value)) { | ||
// eslint-disable-line max-depth | ||
throw new Error('Cannot set value of an array to a non-array') | ||
@@ -43,3 +43,2 @@ } | ||
if (!Array.isArray(patch.value)) { | ||
// eslint-disable-line max-depth | ||
throw new Error('Cannot set value of an array to a non-array') | ||
@@ -51,4 +50,7 @@ } | ||
} else if (patch.type === 'move') { | ||
if (!patch.value || !hasOwn(patch.value, 'from') || !hasOwn(patch.value, 'to')) { | ||
// eslint-disable-line max-depth | ||
if ( | ||
!patch.value || | ||
!hasOwn(patch.value, 'from') || | ||
!hasOwn(patch.value, 'to') | ||
) { | ||
throw new Error( | ||
@@ -80,3 +82,5 @@ `Invalid value of 'move' patch. Expected a value with "from" and "to" indexes, instead got: ${JSON.stringify( | ||
if (typeof index !== 'number') { | ||
throw new Error(`Expected array index to be a number, instead got "${index}"`) | ||
throw new Error( | ||
`Expected array index to be a number, instead got "${index}"`, | ||
) | ||
} | ||
@@ -83,0 +87,0 @@ nextValue.splice(index, 1) |
export const BEFORE = 'before' | ||
export const AFTER = 'after' | ||
export default function insert(array: any[], position: string, index: number, ...args: any[]) { | ||
export default function insert( | ||
array: any[], | ||
position: string, | ||
index: number, | ||
...args: any[] | ||
) { | ||
if (position !== BEFORE && position !== AFTER) { | ||
throw new Error(`Invalid position "${position}", must be either ${BEFORE} or ${AFTER}`) | ||
throw new Error( | ||
`Invalid position "${position}", must be either ${BEFORE} or ${AFTER}`, | ||
) | ||
} | ||
@@ -8,0 +15,0 @@ |
import {clone, isObject, omit} from 'lodash' | ||
import applyPatch from './applyPatch' | ||
export default function apply(value: any, patch: {type: any; path: any; value: any}) { | ||
export default function apply( | ||
value: any, | ||
patch: {type: any; path: any; value: any}, | ||
) { | ||
const nextValue = clone(value) | ||
@@ -11,3 +13,2 @@ if (patch.path.length === 0) { | ||
if (!isObject(patch.value)) { | ||
// eslint-disable-line max-depth | ||
throw new Error('Cannot set value of an object to a non-object') | ||
@@ -14,0 +15,0 @@ } |
import {makePatches, stringifyPatches} from '@sanity/diff-match-patch' | ||
import { | ||
type DiffMatchPatch, | ||
type InsertPatch, | ||
type InsertPosition, | ||
type Path, | ||
type PathSegment, | ||
type SetIfMissingPatch, | ||
type SetPatch, | ||
type UnsetPatch, | ||
import type { | ||
DiffMatchPatch, | ||
InsertPatch, | ||
InsertPosition, | ||
Path, | ||
PathSegment, | ||
SetIfMissingPatch, | ||
SetPatch, | ||
UnsetPatch, | ||
} from './types' | ||
@@ -35,3 +34,7 @@ | ||
/** @public */ | ||
export function insert(items: any[], position: InsertPosition, path: Path = []): InsertPatch { | ||
export function insert( | ||
items: any[], | ||
position: InsertPosition, | ||
path: Path = [], | ||
): InsertPatch { | ||
return { | ||
@@ -56,3 +59,6 @@ type: 'insert', | ||
/** @internal */ | ||
export function prefixPath<T extends {path: Path}>(patch: T, segment: PathSegment): T { | ||
export function prefixPath<T extends {path: Path}>( | ||
patch: T, | ||
segment: PathSegment, | ||
): T { | ||
return { | ||
@@ -59,0 +65,0 @@ ...patch, |
@@ -16,3 +16,8 @@ /** @public */ | ||
/** @public */ | ||
export type JSONValue = number | string | boolean | {[key: string]: JSONValue} | JSONValue[] | ||
export type JSONValue = | ||
| number | ||
| string | ||
| boolean | ||
| {[key: string]: JSONValue} | ||
| JSONValue[] | ||
@@ -19,0 +24,0 @@ /** @public */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
63904
1.29%3
-62.5%17
6.25%956
5.29%