shelving 1.89.0 → 1.89.1
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/string.d.ts +10 -9
- package/util/string.js +23 -11
package/package.json
CHANGED
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.
|