shelving 1.89.0 → 1.89.2
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/util/object.d.ts +5 -1
- package/util/object.js +4 -0
- package/util/string.d.ts +10 -9
- package/util/string.js +23 -11
- package/util/transform.d.ts +2 -2
- package/util/validate.d.ts +2 -2
- package/util/validate.js +2 -1
package/package.json
CHANGED
package/util/object.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ImmutableArray } from "./array.js";
|
|
2
|
-
/** Any readonly
|
|
2
|
+
/** Any readonly object. */
|
|
3
3
|
export type ImmutableObject<K extends PropertyKey = PropertyKey, T = unknown> = {
|
|
4
4
|
readonly [KK in K]: T;
|
|
5
5
|
};
|
|
@@ -13,6 +13,8 @@ export type ObjectProp<T extends ImmutableObject = ImmutableObject> = readonly [
|
|
|
13
13
|
export type ObjectKey<T extends ImmutableObject = ImmutableObject> = keyof T;
|
|
14
14
|
/** Value for an object prop. */
|
|
15
15
|
export type ObjectValue<T extends ImmutableObject = ImmutableObject> = T[keyof T];
|
|
16
|
+
/** Something that can be converted to an object. */
|
|
17
|
+
export type PossibleObject<T extends ImmutableObject> = T | Iterable<ObjectProp<T>>;
|
|
16
18
|
/** Is an unknown value an unknown object? */
|
|
17
19
|
export declare const isObject: <T extends ImmutableObject<PropertyKey, unknown>>(value: unknown) => value is T;
|
|
18
20
|
/** Assert that a value is an object */
|
|
@@ -23,6 +25,8 @@ export declare function isPlainObject(value: ImmutableObject | unknown): value i
|
|
|
23
25
|
export declare function assertPlainObject(value: ImmutableObject | unknown): asserts value is ImmutableObject;
|
|
24
26
|
/** Is an unknown value the key for an own prop of an object. */
|
|
25
27
|
export declare const isProp: <T extends ImmutableObject<PropertyKey, unknown>>(obj: T, key: unknown) => key is keyof T;
|
|
28
|
+
/** turn a possible object into an object. */
|
|
29
|
+
export declare function getObject<T extends ImmutableObject>(obj: PossibleObject<T>): T;
|
|
26
30
|
/**
|
|
27
31
|
* Mutable type is the opposite of `Readonly<T>` helper type.
|
|
28
32
|
* - See https://github.com/microsoft/TypeScript/issues/24509
|
package/util/object.js
CHANGED
|
@@ -23,6 +23,10 @@ export function assertPlainObject(value) {
|
|
|
23
23
|
}
|
|
24
24
|
/** Is an unknown value the key for an own prop of an object. */
|
|
25
25
|
export const isProp = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
26
|
+
/** turn a possible object into an object. */
|
|
27
|
+
export function getObject(obj) {
|
|
28
|
+
return isIterable(obj) ? Object.fromEntries(obj) : obj;
|
|
29
|
+
}
|
|
26
30
|
export function getProps(obj) {
|
|
27
31
|
return isIterable(obj) ? obj : Object.entries(obj);
|
|
28
32
|
}
|
package/util/string.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export declare const isStringLength: (str: string, min?: number, max?: number) =
|
|
|
32
32
|
/** Assert that a value has a specific length (or length is in a specific range). */
|
|
33
33
|
export declare function assertStringLength(str: string | unknown, min?: number, max?: number): asserts str is string;
|
|
34
34
|
/** Get a string if it has the specified minimum length. */
|
|
35
|
-
export declare function getStringLength(
|
|
35
|
+
export declare function getStringLength(str: string, min?: number, max?: number): string;
|
|
36
36
|
/** Concatenate an iterable set of strings together. */
|
|
37
37
|
export declare const joinStrings: (strs: Iterable<string> & NotString, joiner?: string) => string;
|
|
38
38
|
/**
|
|
@@ -63,16 +63,17 @@ export declare const sanitizeLines: (str: string) => string;
|
|
|
63
63
|
* - Used when you're running a query against a string entered by a user.
|
|
64
64
|
*
|
|
65
65
|
* @example normalizeString("Däve-is\nREALLY éxcitable—apparęntly!!! 😂"); // Returns "dave is really excitable apparently"
|
|
66
|
-
*/
|
|
67
|
-
export declare const simplifyString: (str: string) => string;
|
|
68
|
-
/**
|
|
69
|
-
* Convert a string to a `kebab-case` URL slug.
|
|
70
|
-
* - Remove any characters not in the range `[a-z0-9-]`
|
|
71
|
-
* - Change all spaces/separators/hyphens/dashes/underscores to `-` single hyphen.
|
|
72
66
|
*
|
|
73
|
-
*
|
|
67
|
+
* @todo Convert letter-like characters (e.g. `ℝ`) to their ASCII equivalent (e.g. `R`).
|
|
74
68
|
*/
|
|
75
|
-
export declare const
|
|
69
|
+
export declare const simplifyString: (str: string) => string;
|
|
70
|
+
/** Convert a string to a `kebab-case` URL slug, or throw `AssertionError` */
|
|
71
|
+
export declare const getOptionalSlug: (str: string) => string | null;
|
|
72
|
+
export declare function getSlug(str: string): string;
|
|
73
|
+
/** Convert a string to a unique ref e.g. `abc123`, or `null` */
|
|
74
|
+
export declare const getOptionalRef: (str: string) => string | null;
|
|
75
|
+
/** Convert a string to a unique ref e.g. `abc123`, or throw `AssertionError` */
|
|
76
|
+
export declare function getRef(str: string): string;
|
|
76
77
|
/**
|
|
77
78
|
* Return an array of the separate words and "quoted phrases" found in a string.
|
|
78
79
|
* - Phrases enclosed "in quotes" are a single word.
|
package/util/string.js
CHANGED
|
@@ -53,9 +53,9 @@ export function assertStringLength(str, min = 1, max = Infinity) {
|
|
|
53
53
|
throw new AssertionError(`Must be string with length ${formatRange(min, max)}`, str);
|
|
54
54
|
}
|
|
55
55
|
/** Get a string if it has the specified minimum length. */
|
|
56
|
-
export function getStringLength(
|
|
57
|
-
assertStringLength(
|
|
58
|
-
return
|
|
56
|
+
export function getStringLength(str, min = 1, max = Infinity) {
|
|
57
|
+
assertStringLength(str, min, max);
|
|
58
|
+
return str;
|
|
59
59
|
}
|
|
60
60
|
/** Concatenate an iterable set of strings together. */
|
|
61
61
|
export const joinStrings = (strs, joiner = "") => getArray(strs).join(joiner);
|
|
@@ -99,6 +99,8 @@ export const sanitizeLines = (str) => str
|
|
|
99
99
|
* - Used when you're running a query against a string entered by a user.
|
|
100
100
|
*
|
|
101
101
|
* @example normalizeString("Däve-is\nREALLY éxcitable—apparęntly!!! 😂"); // Returns "dave is really excitable apparently"
|
|
102
|
+
*
|
|
103
|
+
* @todo Convert letter-like characters (e.g. `ℝ`) to their ASCII equivalent (e.g. `R`).
|
|
102
104
|
*/
|
|
103
105
|
export const simplifyString = (str) => str
|
|
104
106
|
.normalize("NFD") // Convert ligatures (e.g. `ff`) and letters with marks (e.g. `ü`) to separate characters (e.g. `ff` and `u◌̈`)`.
|
|
@@ -106,14 +108,24 @@ export const simplifyString = (str) => str
|
|
|
106
108
|
.replace(/[^\p{L}\p{N} ]+/gu, "") // Strip characters that aren't letters, numbers, spaces.
|
|
107
109
|
.trim()
|
|
108
110
|
.toLowerCase();
|
|
109
|
-
/**
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
111
|
+
/** Convert a string to a `kebab-case` URL slug, or throw `AssertionError` */
|
|
112
|
+
export const getOptionalSlug = (str) => simplifyString(str).replace(/ /g, "-") || null;
|
|
113
|
+
/* Convert a string to a `kebab-case` URL slug, or throw `AssertionError` */
|
|
114
|
+
export function getSlug(str) {
|
|
115
|
+
const slug = getOptionalSlug(str);
|
|
116
|
+
if (slug)
|
|
117
|
+
return slug;
|
|
118
|
+
throw new AssertionError(`String slug cannot be empty (received "${str}")`);
|
|
119
|
+
}
|
|
120
|
+
/** Convert a string to a unique ref e.g. `abc123`, or `null` */
|
|
121
|
+
export const getOptionalRef = (str) => simplifyString(str).replace(/ /g, "") || null;
|
|
122
|
+
/** Convert a string to a unique ref e.g. `abc123`, or throw `AssertionError` */
|
|
123
|
+
export function getRef(str) {
|
|
124
|
+
const ref = getOptionalRef(str);
|
|
125
|
+
if (ref)
|
|
126
|
+
return ref;
|
|
127
|
+
throw new AssertionError(`String ref cannot be empty (received "${str}")`);
|
|
128
|
+
}
|
|
117
129
|
/**
|
|
118
130
|
* Return an array of the separate words and "quoted phrases" found in a string.
|
|
119
131
|
* - Phrases enclosed "in quotes" are a single word.
|
package/util/transform.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { ArrayItem, ImmutableArray } from "./array.js";
|
|
|
2
2
|
import type { Entry } from "./entry.js";
|
|
3
3
|
import { Arguments } from "./function.js";
|
|
4
4
|
import { ImmutableObject, ObjectValue } from "./object.js";
|
|
5
|
-
import { ImmutableDictionary } from "./dictionary.js";
|
|
5
|
+
import { ImmutableDictionary, PossibleDictionary } from "./dictionary.js";
|
|
6
6
|
/** Object that transforms an input value into an output value with its `transform()` method. */
|
|
7
7
|
export interface Transformable<I, O, A extends Arguments = []> {
|
|
8
8
|
transform(input: I, ...args: A): O;
|
|
@@ -38,7 +38,7 @@ export declare function mapObject<T extends ImmutableObject>(obj: T, transformer
|
|
|
38
38
|
export declare function mapObject<I extends ImmutableObject, O extends ImmutableObject, A extends Arguments = []>(obj: I, transformer: (v: ObjectValue<I>, ...args: A) => ObjectValue<O>, ...args: A): O;
|
|
39
39
|
export declare function mapObject<I extends ImmutableObject, O extends ImmutableObject, A extends Arguments = []>(obj: I, transformer: Transformer<ObjectValue<I>, ObjectValue<O>>, ...args: A): O;
|
|
40
40
|
/** Modify the values of a dictionary using a transformer. */
|
|
41
|
-
export declare const mapDictionary: <I, O, A extends Arguments = []>(dictionary:
|
|
41
|
+
export declare const mapDictionary: <I, O, A extends Arguments = []>(dictionary: PossibleDictionary<I>, transformer: Transformer<I, O, A>, ...args: A) => ImmutableDictionary<O>;
|
|
42
42
|
/** Modify the values of a set of entries using a transformer. */
|
|
43
43
|
export declare function mapEntries<K, I, O, A extends Arguments = []>(entries: Iterable<Entry<K, I>>, transformer: Transformer<I, O, A>, ...args: A): Iterable<Entry<K, O>>;
|
|
44
44
|
/** Set of named transformers for a data object (or `undefined` to skip the transform). */
|
package/util/validate.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Entry } from "./entry.js";
|
|
2
|
-
import
|
|
2
|
+
import { ImmutableDictionary, PossibleDictionary } from "./dictionary.js";
|
|
3
3
|
import { Data, DataProp } from "./data.js";
|
|
4
4
|
import { ImmutableArray } from "./array.js";
|
|
5
5
|
/** Object that can validate an unknown value with its `validate()` method. */
|
|
@@ -62,7 +62,7 @@ export declare function validateEntries<T>(unsafeValues: Iterable<Entry<string,
|
|
|
62
62
|
* @throw InvalidFeedback if one or more entry values did not validate.
|
|
63
63
|
* - `feedback.details` will contain an entry for each invalid item (keyed by their count in the input iterable).
|
|
64
64
|
*/
|
|
65
|
-
export declare function validateDictionary<T>(obj:
|
|
65
|
+
export declare function validateDictionary<T>(obj: PossibleDictionary<T>, validator: Validator<T>): ImmutableDictionary<T>;
|
|
66
66
|
/**
|
|
67
67
|
* Validate the props of a data object with a set of validators.
|
|
68
68
|
*
|
package/util/validate.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isFeedback } from "../feedback/Feedback.js";
|
|
2
2
|
import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
3
|
+
import { getDictionaryItems } from "./dictionary.js";
|
|
3
4
|
import { getDataProps } from "./data.js";
|
|
4
5
|
import { getArray } from "./array.js";
|
|
5
6
|
/** Validate an unknown value with a validator. */
|
|
@@ -69,7 +70,7 @@ export function* validateEntries(unsafeValues, validator) {
|
|
|
69
70
|
* - `feedback.details` will contain an entry for each invalid item (keyed by their count in the input iterable).
|
|
70
71
|
*/
|
|
71
72
|
export function validateDictionary(obj, validator) {
|
|
72
|
-
return Object.fromEntries(validateEntries(
|
|
73
|
+
return Object.fromEntries(validateEntries(getDictionaryItems(obj), validator));
|
|
73
74
|
}
|
|
74
75
|
/**
|
|
75
76
|
* Validate the props of a data object with a set of validators.
|