added format command and added trailing commas to reduce conflicts

This commit is contained in:
dswbx
2025-02-26 20:06:03 +01:00
parent 88b5359f1c
commit 7743f71a11
414 changed files with 3622 additions and 3610 deletions

View File

@@ -6,14 +6,14 @@ import {
getFullPathKeys,
mergeObjectWith,
parse,
stripMark
stripMark,
} from "../utils";
export type SchemaObjectOptions<Schema extends TObject> = {
onUpdate?: (config: Static<Schema>) => void | Promise<void>;
onBeforeUpdate?: (
from: Static<Schema>,
to: Static<Schema>
to: Static<Schema>,
) => Static<Schema> | Promise<Static<Schema>>;
restrictPaths?: string[];
overwritePaths?: (RegExp | string)[];
@@ -29,13 +29,13 @@ export class SchemaObject<Schema extends TObject> {
constructor(
private _schema: Schema,
initial?: Partial<Static<Schema>>,
private options?: SchemaObjectOptions<Schema>
private options?: SchemaObjectOptions<Schema>,
) {
this._default = Default(_schema, {} as any) as any;
this._value = initial
? parse(_schema, structuredClone(initial as any), {
forceParse: this.isForceParse(),
skipMark: this.isForceParse()
skipMark: this.isForceParse(),
})
: this._default;
this._config = Object.freeze(this._value);
@@ -71,7 +71,7 @@ export class SchemaObject<Schema extends TObject> {
async set(config: Static<Schema>, noEmit?: boolean): Promise<Static<Schema>> {
const valid = parse(this._schema, structuredClone(config) as any, {
forceParse: true,
skipMark: this.isForceParse()
skipMark: this.isForceParse(),
});
// regardless of "noEmit" this should always be triggered
const updatedConfig = await this.onBeforeUpdate(this._config, valid);
@@ -159,7 +159,7 @@ export class SchemaObject<Schema extends TObject> {
overwritePaths.some((k2) => {
//console.log("keep?", { k, k2 }, k2 !== k && k2.startsWith(k));
return k2 !== k && k2.startsWith(k);
})
}),
)
: overwritePaths;
//console.log("specific", specific);

View File

@@ -1,7 +1,7 @@
enum Change {
Add = "a",
Remove = "r",
Edit = "e"
Edit = "e",
}
type Object = object;
@@ -50,7 +50,7 @@ function diff(oldObj: Object, newObj: Object): DiffEntry[] {
t: Change.Edit,
p: path,
o: oldValue,
n: newValue
n: newValue,
});
} else if (Array.isArray(oldValue) && Array.isArray(newValue)) {
const maxLength = Math.max(oldValue.length, newValue.length);
@@ -60,14 +60,14 @@ function diff(oldObj: Object, newObj: Object): DiffEntry[] {
t: Change.Add,
p: [...path, i],
o: undefined,
n: newValue[i]
n: newValue[i],
});
} else if (i >= newValue.length) {
diffs.push({
t: Change.Remove,
p: [...path, i],
o: oldValue[i],
n: undefined
n: undefined,
});
} else {
recurse(oldValue[i], newValue[i], [...path, i]);
@@ -83,14 +83,14 @@ function diff(oldObj: Object, newObj: Object): DiffEntry[] {
t: Change.Add,
p: [...path, key],
o: undefined,
n: newValue[key]
n: newValue[key],
});
} else if (!(key in newValue)) {
diffs.push({
t: Change.Remove,
p: [...path, key],
o: oldValue[key],
n: undefined
n: undefined,
});
} else {
recurse(oldValue[key], newValue[key], [...path, key]);
@@ -101,7 +101,7 @@ function diff(oldObj: Object, newObj: Object): DiffEntry[] {
t: Change.Edit,
p: path,
o: oldValue,
n: newValue
n: newValue,
});
}
}

View File

@@ -4,12 +4,12 @@ const expressions = [
exp(
"$eq",
(v: Primitive) => isPrimitive(v),
(e, a) => e === a
(e, a) => e === a,
),
exp(
"$ne",
(v: Primitive) => isPrimitive(v),
(e, a) => e !== a
(e, a) => e !== a,
),
exp(
"$like",
@@ -25,7 +25,7 @@ const expressions = [
default:
return false;
}
}
},
),
exp(
"$regex",
@@ -39,54 +39,54 @@ const expressions = [
return regex.test(a);
}
return false;
}
},
),
exp(
"$isnull",
(v: boolean | 1 | 0) => true,
(e, a) => (e ? a === null : a !== null)
(e, a) => (e ? a === null : a !== null),
),
exp(
"$notnull",
(v: boolean | 1 | 0) => true,
(e, a) => (e ? a !== null : a === null)
(e, a) => (e ? a !== null : a === null),
),
exp(
"$in",
(v: (string | number)[]) => Array.isArray(v),
(e: any, a: any) => e.includes(a)
(e: any, a: any) => e.includes(a),
),
exp(
"$notin",
(v: (string | number)[]) => Array.isArray(v),
(e: any, a: any) => !e.includes(a)
(e: any, a: any) => !e.includes(a),
),
exp(
"$gt",
(v: number) => typeof v === "number",
(e: any, a: any) => a > e
(e: any, a: any) => a > e,
),
exp(
"$gte",
(v: number) => typeof v === "number",
(e: any, a: any) => a >= e
(e: any, a: any) => a >= e,
),
exp(
"$lt",
(v: number) => typeof v === "number",
(e: any, a: any) => a < e
(e: any, a: any) => a < e,
),
exp(
"$lte",
(v: number) => typeof v === "number",
(e: any, a: any) => a <= e
(e: any, a: any) => a <= e,
),
exp(
"$between",
(v: [number, number]) =>
Array.isArray(v) && v.length === 2 && v.every((n) => typeof n === "number"),
(e: any, a: any) => e[0] <= a && a <= e[1]
)
(e: any, a: any) => e[0] <= a && a <= e[1],
),
];
export type ObjectQuery = FilterQuery<typeof expressions>;

View File

@@ -13,7 +13,7 @@ export class Expression<Key, Expect = unknown, CTX = any> {
constructor(
public key: Key,
public valid: (v: Expect) => boolean,
public validate: (e: any, a: any, ctx: CTX) => any
public validate: (e: any, a: any, ctx: CTX) => any,
) {}
}
export type TExpression<Key, Expect = unknown, CTX = any> = Expression<Key, Expect, CTX>;
@@ -21,7 +21,7 @@ export type TExpression<Key, Expect = unknown, CTX = any> = Expression<Key, Expe
export function exp<const Key, const Expect, CTX = any>(
key: Key,
valid: (v: Expect) => boolean,
validate: (e: Expect, a: unknown, ctx: CTX) => any
validate: (e: Expect, a: unknown, ctx: CTX) => any,
): Expression<Key, Expect, CTX> {
return new Expression(key, valid, validate);
}
@@ -38,7 +38,7 @@ type ExpressionCondition<Exps extends Expressions> = {
function getExpression<Exps extends Expressions>(
expressions: Exps,
key: string
key: string,
): Expression<any, any> {
const exp = expressions.find((e) => e.key === key);
if (!exp) throw new Error(`Expression does not exist: "${key}"`);
@@ -61,7 +61,7 @@ export type FilterQuery<Exps extends Expressions> =
function _convert<Exps extends Expressions>(
$query: FilterQuery<Exps>,
expressions: Exps,
path: string[] = []
path: string[] = [],
): FilterQuery<Exps> {
//console.log("-----------------");
const ExpressionConditionKeys = expressions.map((e) => e.key);
@@ -98,7 +98,7 @@ function _convert<Exps extends Expressions>(
} else if (typeof value === "object") {
// when object is given, check if all keys are expressions
const invalid = Object.keys(value).filter(
(f) => !ExpressionConditionKeys.includes(f as any)
(f) => !ExpressionConditionKeys.includes(f as any),
);
if (invalid.length === 0) {
newQuery[key] = {};
@@ -109,7 +109,7 @@ function _convert<Exps extends Expressions>(
}
} else {
throw new Error(
`Invalid key(s) at "${key}": ${invalid.join(", ")}. Expected expressions.`
`Invalid key(s) at "${key}": ${invalid.join(", ")}. Expected expressions.`,
);
}
}
@@ -128,7 +128,7 @@ type BuildOptions = {
function _build<Exps extends Expressions>(
_query: FilterQuery<Exps>,
expressions: Exps,
options: BuildOptions
options: BuildOptions,
): ValidationResults {
const $query = options.convert ? _convert<Exps>(_query, expressions) : _query;
@@ -137,7 +137,7 @@ function _build<Exps extends Expressions>(
const result: ValidationResults = {
$and: [],
$or: [],
keys: new Set<string>()
keys: new Set<string>(),
};
const { $or, ...$and } = $query;
@@ -187,7 +187,7 @@ function _build<Exps extends Expressions>(
function _validate(results: ValidationResults): boolean {
const matches: { $and?: boolean; $or?: boolean } = {
$and: undefined,
$or: undefined
$or: undefined,
};
matches.$and = results.$and.every((r) => Boolean(r));
@@ -204,6 +204,6 @@ export function makeValidator<Exps extends Expressions>(expressions: Exps) {
validate: (query: FilterQuery<Exps>, options: BuildOptions) => {
const fns = _build(query, expressions, options);
return _validate(fns);
}
},
};
}