shelving 1.164.0 → 1.165.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/schema/DataSchema.d.ts +3 -0
- package/schema/DataSchema.js +7 -0
- package/util/template.d.ts +3 -0
- package/util/template.js +8 -3
package/package.json
CHANGED
package/schema/DataSchema.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Data, Database, PartialData } from "../util/data.js";
|
|
2
2
|
import type { Identifier, Item } from "../util/item.js";
|
|
3
|
+
import { type Key } from "../util/object.js";
|
|
3
4
|
import type { NullableSchema } from "./NullableSchema.js";
|
|
4
5
|
import type { SchemaOptions, Schemas } from "./Schema.js";
|
|
5
6
|
import { Schema } from "./Schema.js";
|
|
@@ -15,6 +16,8 @@ export declare class DataSchema<T extends Data> extends Schema<unknown> {
|
|
|
15
16
|
readonly props: Schemas<T>;
|
|
16
17
|
constructor({ one, title, props, value: partialValue, ...options }: DataSchemaOptions<T>);
|
|
17
18
|
validate(unsafeValue?: unknown): T;
|
|
19
|
+
pick<K extends Key<T>>(...keys: K[]): DataSchema<Pick<T, K>>;
|
|
20
|
+
omit<K extends Key<T>>(...keys: K[]): DataSchema<Omit<T, K>>;
|
|
18
21
|
}
|
|
19
22
|
/** Set of named data schemas. */
|
|
20
23
|
export type DataSchemas<T extends Database> = {
|
package/schema/DataSchema.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ValueFeedback } from "../feedback/Feedback.js";
|
|
2
2
|
import { isData } from "../util/data.js";
|
|
3
|
+
import { omitProps, pickProps } from "../util/object.js";
|
|
3
4
|
import { mapProps } from "../util/transform.js";
|
|
4
5
|
import { validateData } from "../util/validate.js";
|
|
5
6
|
import { NULLABLE } from "./NullableSchema.js";
|
|
@@ -19,6 +20,12 @@ export class DataSchema extends Schema {
|
|
|
19
20
|
throw new ValueFeedback("Must be object", unsafeValue);
|
|
20
21
|
return validateData(unsafeValue, this.props);
|
|
21
22
|
}
|
|
23
|
+
pick(...keys) {
|
|
24
|
+
return new DataSchema({ ...this, props: pickProps(this.props, ...keys) });
|
|
25
|
+
}
|
|
26
|
+
omit(...keys) {
|
|
27
|
+
return new DataSchema({ ...this, props: omitProps(this.props, ...keys) });
|
|
28
|
+
}
|
|
22
29
|
}
|
|
23
30
|
function _getSchemaValue([, { value }]) {
|
|
24
31
|
return value;
|
package/util/template.d.ts
CHANGED
|
@@ -26,6 +26,9 @@ export declare function getPlaceholders(template: string): readonly string[];
|
|
|
26
26
|
*
|
|
27
27
|
* @param templates Either a single template string, or an iterator that returns multiple template template strings.
|
|
28
28
|
* - Template strings can include placeholders (e.g. `:name-${country}/{city}`).
|
|
29
|
+
* - Template strings do not match the `/` character.
|
|
30
|
+
* - The `**` double splat placeholder _can_ match multiple path segments (e.g. `a/b/c`).
|
|
31
|
+
*
|
|
29
32
|
* @param target The string containing values, e.g. `Dave-UK/Manchester`
|
|
30
33
|
*
|
|
31
34
|
* @return An object containing values, e.g. `{ name: "Dave", country: "UK", city: "Manchester" }`, or undefined if target didn't match the template.
|
package/util/template.js
CHANGED
|
@@ -5,7 +5,7 @@ import { setMapItem } from "./map.js";
|
|
|
5
5
|
import { isObject } from "./object.js";
|
|
6
6
|
import { getString } from "./string.js";
|
|
7
7
|
// RegExp to find named variables in several formats e.g. `:a`, `${b}`, `{{c}}` or `{d}`
|
|
8
|
-
const R_PLACEHOLDERS = /(
|
|
8
|
+
const R_PLACEHOLDERS = /(\*\*?|:[a-z][a-z0-9]*|\$\{[a-z][a-z0-9]*\}|\{\{[a-z][a-z0-9]*\}\}|\{[a-z][a-z0-9]*\})/i;
|
|
9
9
|
// Find actual name within template placeholder e.g. `${name}` → `name`
|
|
10
10
|
const R_NAME = /[a-z0-9]+/i;
|
|
11
11
|
/**
|
|
@@ -26,7 +26,7 @@ function _splitTemplate(template, caller) {
|
|
|
26
26
|
const post = matches[i + 1];
|
|
27
27
|
if (i > 1 && !pre.length)
|
|
28
28
|
throw new ValueError("Template placeholders must be separated by at least one character", { received: template, caller });
|
|
29
|
-
const name = placeholder === "*" ? String(asterisks++) : R_NAME.exec(placeholder)?.[0] || "";
|
|
29
|
+
const name = placeholder[0] === "*" ? String(asterisks++) : R_NAME.exec(placeholder)?.[0] || "";
|
|
30
30
|
chunks.push({ pre, placeholder, name, post });
|
|
31
31
|
}
|
|
32
32
|
return chunks;
|
|
@@ -53,6 +53,9 @@ function _getPlaceholder({ name }) {
|
|
|
53
53
|
*
|
|
54
54
|
* @param templates Either a single template string, or an iterator that returns multiple template template strings.
|
|
55
55
|
* - Template strings can include placeholders (e.g. `:name-${country}/{city}`).
|
|
56
|
+
* - Template strings do not match the `/` character.
|
|
57
|
+
* - The `**` double splat placeholder _can_ match multiple path segments (e.g. `a/b/c`).
|
|
58
|
+
*
|
|
56
59
|
* @param target The string containing values, e.g. `Dave-UK/Manchester`
|
|
57
60
|
*
|
|
58
61
|
* @return An object containing values, e.g. `{ name: "Dave", country: "UK", city: "Manchester" }`, or undefined if target didn't match the template.
|
|
@@ -70,13 +73,15 @@ export function matchTemplate(template, target, caller = matchTemplate) {
|
|
|
70
73
|
// Loop through the placeholders (placeholders are at all the even-numbered positions in `chunks`).
|
|
71
74
|
let startIndex = firstChunk.pre.length;
|
|
72
75
|
const values = {};
|
|
73
|
-
for (const { name, post } of chunks) {
|
|
76
|
+
for (const { name, post, placeholder } of chunks) {
|
|
74
77
|
const stopIndex = !post ? Number.POSITIVE_INFINITY : target.indexOf(post, startIndex);
|
|
75
78
|
if (stopIndex < 0)
|
|
76
79
|
return undefined; // Target doesn't match template because chunk post wasn't found.
|
|
77
80
|
const value = target.slice(startIndex, stopIndex);
|
|
78
81
|
if (!value.length)
|
|
79
82
|
return undefined; // Target doesn't match template because chunk value was missing.
|
|
83
|
+
if (placeholder !== "**" && value.includes("/"))
|
|
84
|
+
return undefined; // Placeholders can't consume multiple path segments.
|
|
80
85
|
values[name] = value;
|
|
81
86
|
startIndex = stopIndex + post.length;
|
|
82
87
|
}
|