@sanity/mutate
Version:
Experimental toolkit for working with Sanity mutations in JavaScript & TypeScript
74 lines • 8.87 kB
TypeScript
type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
type NormalizeReadOnlyArray<T> = T extends readonly [infer NP, ...infer Rest] ? [NP, ...Rest] : T extends readonly (infer NP)[] ? NP[] : T;
type EmptyArray = never[] | readonly never[] | [] | readonly [];
type AnyArray<T = any> = T[] | readonly T[];
type ArrayLength<T extends AnyArray> = T extends never[] ? 0 : T['length'];
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
/**
* Formats an intersection object type, so it outputs as `{"foo": 1, "bar": 1}` instead of `{"foo": 1} & {"bar": 2}``
*/
type Format<A> = A extends { [Key in keyof A]: A[Key] } ? { [Key in keyof A]: A[Key] } : A;
type Tuplify<T> = T extends readonly [infer NP, ...infer Rest] ? [NP, ...Rest] : T extends readonly (infer NP)[] ? NP[] : [T];
type KeyedPathElement = {
_key: string;
};
type PropertyName = string;
type Index = number;
type PathElement = PropertyName | Index | KeyedPathElement;
type Path = PathElement[] | readonly PathElement[];
type ByIndex<P extends number, T extends AnyArray> = T[P];
type ElementType<T extends AnyArray> = T extends AnyArray<infer E> ? E : unknown;
type FindInArray<P extends KeyedPathElement | number, T extends AnyArray> = P extends KeyedPathElement ? FindBy<P, T> : P extends number ? ByIndex<P, T> : never;
type AnyEmptyArray = [] | readonly [];
type FindBy<P, T extends AnyArray> = T extends AnyEmptyArray ? undefined : T[0] extends P ? T[0] : T extends [any, ...infer Tail] | readonly [any, ...infer Tail] ? FindBy<P, Tail> : ElementType<T>;
type Get<P extends number | KeyedPathElement | Readonly<KeyedPathElement> | string, T> = T extends AnyArray ? P extends KeyedPathElement | Readonly<KeyedPathElement> | number ? FindInArray<P, T> : undefined : P extends keyof T ? T[P] : never;
type GetAtPath<P extends readonly PathElement[], T> = P extends [] ? T : P extends [infer Head, ...infer Tail] ? Head extends PathElement ? Tail extends PathElement[] ? GetAtPath<Tail, Get<Head, T>> : undefined : undefined : undefined;
declare function getAtPath<const Head extends PathElement, const T>(path: [head: Head], value: T): Get<Head, T>;
declare function getAtPath<const Head extends PathElement, const Tail extends PathElement[], T>(path: [head: Head, ...tail: Tail], value: T): GetAtPath<[Head, ...Tail], T>;
declare function getAtPath<T>(path: [], value: T): T;
declare function getAtPath(path: Path, value: unknown): unknown;
type ParseError<T extends string = 'unknown'> = T & {
error: true;
};
type SplitAll<S extends string, Char extends string> = S extends `${infer First}${Char}${infer Remainder}` ? [First, ...SplitAll<Remainder, Char>] : [S];
type Split<S extends string, Char extends string, IncludeSeparator extends boolean = false> = S extends `${infer First}${Char}${infer Remainder}` ? [First, `${IncludeSeparator extends true ? Char : ''}${Remainder}`] : [S];
type TrimLeft<Str extends string, Char extends string = ' '> = string extends Str ? Str : Str extends `${Char}${infer Trimmed}` ? TrimLeft<Trimmed, Char> : Str;
type TrimRight<Str extends string, Char extends string = ' '> = string extends Str ? Str : Str extends `${infer Trimmed}${Char}` ? TrimRight<Trimmed, Char> : Str;
type Trim<S extends string, Char extends string = ' '> = TrimRight<TrimLeft<S, Char>, Char>;
type ParseKVPair<S extends string> = Split<S, '=='> extends [`${infer LHS}`, `${infer RHS}`] ? ParseValue<Trim<RHS>> extends infer Res ? Res extends [null, infer Value] ? Ok<{ [P in Trim<LHS>]: Value }> : Err<ParseError<`Can't parse right hand side as a value in "${S}" (Invalid value ${RHS})`>> : never : Err<ParseError<`Can't parse key value pair from ${S}`>>;
type ParseObject<S extends string> = S extends `${infer Pair},${infer Remainder}` ? Trim<Remainder> extends '' ? Ok<Record<never, never>> : MergeInner<ParseKVPair<Pair>, ParseObject<Remainder>> : ParseKVPair<S>;
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type OnlyDigits<S> = S extends `${infer Head}${infer Tail}` ? Head extends Digit ? Tail extends '' ? true : OnlyDigits<Tail> extends true ? true : false : false : false;
type ParseNumber<S extends string> = S extends `${infer Head}${infer Tail}` ? Head extends '-' ? OnlyDigits<Tail> extends true ? Ok<ToNumber<S>> : Err<ParseError<`Invalid integer value "${S}"`>> : OnlyDigits<S> extends true ? Ok<ToNumber<S>> : Err<ParseError<`Invalid integer value "${S}"`>> : Err<ParseError<`Invalid integer value "${S}"`>>;
type ToNumber<T extends string> = T extends `${infer N extends number}` ? N : never;
type ParseValue<S extends string> = string extends S ? Err<ParseError<'ParseValue got generic string type'>> : S extends 'null' ? Ok<null> : S extends 'true' ? Ok<true> : S extends 'false' ? Ok<false> : S extends `"${infer Value}"` ? Ok<Value> : Try<ParseNumber<S>, Err<ParseError<`ParseValue failed. Can't parse "${S}" as a value.`>>>;
type Result<E, V> = [E, V];
type Err<E> = Result<E, null>;
type Ok<V> = Result<null, V>;
type Try<R extends Result<any, any>, Handled> = R[1] extends null ? Handled : R;
type Concat<R extends Result<any, any>, Arr extends any[]> = R[1] extends any[] ? Ok<[...R[1], ...Arr]> : R;
type ConcatInner<R extends Result<any, any>, R2 extends Result<any, any>> = R2[1] extends any[] ? Concat<R, R2[1]> : R2;
type Merge<R extends Result<any, any>, E> = R[0] extends null ? Ok<R[1] & E> : R;
type MergeInner<R extends Result<any, any>, R2 extends Result<any, any>> = R2[0] extends null ? Merge<R, R2[1]> : R;
type ToArray<R extends Result<any, any>> = R extends [infer E, infer V] ? E extends null ? V extends any[] ? R : Ok<[R[1]]> : R : R;
type ParseInnerExpression<S extends string> = S extends '' ? Err<ParseError<'Saw an empty expression'>> : Try<ParseNumber<S>, ParseObject<S>>;
type ParseExpressions<S extends string> = S extends `[${infer Expr}]${infer Remainder}` ? Trim<Remainder> extends '' ? ToArray<ParseInnerExpression<Trim<Expr>>> : ConcatInner<ToArray<ParseInnerExpression<Trim<Expr>>>, ParseExpressions<Remainder>> : Err<ParseError<`Cannot parse object from "${S}"`>>;
type ParseProperty<S extends string> = Trim<S> extends '' ? Err<ParseError<'Empty property'>> : Split<Trim<S>, '[', true> extends [`${infer Prop}`, `${infer Expression}`] ? Trim<Prop> extends '' ? ParseExpressions<Trim<Expression>> : ConcatInner<Ok<[Trim<Prop>]>, ParseExpressions<Trim<Expression>>> : Ok<[Trim<S>]>;
type ParseAllProps<Props extends string[]> = Props extends [`${infer Head}`, ...infer Tail] ? Tail extends string[] ? ConcatInner<ParseProperty<Trim<Head>>, ParseAllProps<Tail>> : ParseProperty<Trim<Head>> : Ok<[]>;
type Unwrap<R extends Result<any, any>> = R extends [infer E, infer V] ? E extends null ? V : E : never;
type StringToPath<S extends string> = Unwrap<ParseAllProps<SplitAll<Trim<S>, '.'>>>;
type StripError<S extends StringToPath<string> | ParseError<string>> = S extends ParseError<string> ? never : S;
type SafePath<S extends string> = StripError<StringToPath<S>>;
declare function parse<const T extends string>(path: T): StringToPath<T>;
declare function stringify(pathArray: Path): string;
declare function normalize(path: string | Readonly<Path>): Readonly<Path>;
declare function startsWith(parentPath: Path, path: Path): boolean;
declare function isEqual(path: Path, otherPath: Path): boolean;
declare function isElementEqual(segmentA: PathElement, segmentB: PathElement): boolean;
declare function isKeyElement(segment: PathElement): segment is KeyedPathElement;
declare function isIndexElement(segment: PathElement): segment is number;
declare function isKeyedElement(element: PathElement): element is KeyedPathElement;
declare function isArrayElement(element: PathElement): element is KeyedPathElement | number;
declare function isPropertyElement(element: PathElement): element is string;
export { PathElement as $, SafePath as A, Try as B, ParseInnerExpression as C, ParseProperty as D, ParseObject as E, ToArray as F, AnyEmptyArray as G, Get as H, ToNumber as I, FindBy as J, ByIndex as K, Trim as L, SplitAll as M, StringToPath as N, ParseValue as O, StripError as P, Path as Q, TrimLeft as R, ParseExpressions as S, ParseNumber as T, GetAtPath as U, Unwrap as V, getAtPath as W, Index as X, FindInArray as Y, KeyedPathElement as Z, MergeInner as _, isKeyElement as a, Format as at, ParseAllProps as b, startsWith as c, Tuplify as ct, parse as d, PropertyName as et, Concat as f, Merge as g, Err as h, isIndexElement as i, EmptyArray as it, Split as j, Result as k, normalize as l, Digit as m, isElementEqual as n, ArrayElement as nt, isKeyedElement as o, NormalizeReadOnlyArray as ot, ConcatInner as p, ElementType as q, isEqual as r, ArrayLength as rt, isPropertyElement as s, Optional as st, isArrayElement as t, AnyArray as tt, stringify as u, Ok as v, ParseKVPair as w, ParseError as x, OnlyDigits as y, TrimRight as z };
//# sourceMappingURL=index2.d.ts.map