@hkdigital/lib-sveltekit 0.0.42 → 0.0.44

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.
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Captizalize the first character of a string
3
+ *
4
+ * @param {string} str - Input string
5
+ *
6
+ * @returns {string} string with first letter capitalized
7
+ */
8
+ export function capitalizeFirst(str: string): string;
9
+ /**
10
+ * Interpolate: substitute variables in a string
11
+ *
12
+ * - Uses mustache template style expression substitution:
13
+ * Variables and expressions are surrounded by {{...}}
14
+ *
15
+ * TODO: full mustache support, see https://github.com/janl/mustache.js
16
+ *
17
+ * --
18
+ *
19
+ * @eg const template = `Hello {{name}}`;
20
+ *
21
+ * --
22
+ *
23
+ * @param {string} template - Template string to interpolate
24
+ * @param {object} templateData - Template data to use for interpolation
25
+ *
26
+ * @returns {string} interpolated string
27
+ */
28
+ export function interpolate(template: string, templateData: object, expressionRegexp?: RegExp): string;
29
+ /**
30
+ * Remove strange characters from a string and replace whitespace by
31
+ * dashes.
32
+ *
33
+ * @returns {string} string that can be used as uri
34
+ */
35
+ export function toUriName(str: any): string;
36
+ /**
37
+ * Convert a path string to an array path
38
+ * - The path string will be spit at the `pathSeparator` token
39
+ * - If the supplied path is already an array, the original array will
40
+ * be returned
41
+ *
42
+ * @param {string|string[]} path
43
+ * String or array path (e.g. "some.path.to")
44
+ *
45
+ * @param {string} [pathSeparator=PATH_SEPARATOR]
46
+ * A custom path separator to use instead of the default "."
47
+ *
48
+ * @returns {string[]} array path (e.g. ["some", "path", "to"])
49
+ */
50
+ export function toStringPath(path: string | string[], pathSeparator?: string): string[];
51
+ /**
52
+ * Prefix a numeric string with 0's
53
+ *
54
+ * @param {string|number} input
55
+ *
56
+ * @returns {string}
57
+ */
58
+ export function padDigits(input: string | number, targetLength?: number, padString?: string): string;
59
+ export const RE_JS_EXPRESSION: RegExp;
60
+ export const RE_MUSTACHE: RegExp;
@@ -0,0 +1,184 @@
1
+ /* ------------------------------------------------------------------ Imports */
2
+
3
+ import * as expect from '../expect/index.js';
4
+
5
+ import { objectGet, PATH_SEPARATOR } from '../object/index.js';
6
+
7
+ import { toArrayPath } from '../array/index.js';
8
+
9
+ /* ------------------------------------------------------------------ Exports */
10
+
11
+ export const RE_JS_EXPRESSION = /\$\{([^${}]*)\}/g;
12
+ export const RE_MUSTACHE = /\{\{([^{}]*)\}\}/g;
13
+
14
+ // -----------------------------------------------------------------------------
15
+
16
+ /**
17
+ * Captizalize the first character of a string
18
+ *
19
+ * @param {string} str - Input string
20
+ *
21
+ * @returns {string} string with first letter capitalized
22
+ */
23
+ export function capitalizeFirst(str) {
24
+ if (!str.length) {
25
+ return str;
26
+ }
27
+
28
+ return str.charAt(0).toUpperCase() + str.slice(1);
29
+ }
30
+
31
+ // -----------------------------------------------------------------------------
32
+
33
+ /**
34
+ * Interpolate: substitute variables in a string
35
+ *
36
+ * - Uses mustache template style expression substitution:
37
+ * Variables and expressions are surrounded by {{...}}
38
+ *
39
+ * TODO: full mustache support, see https://github.com/janl/mustache.js
40
+ *
41
+ * --
42
+ *
43
+ * @eg const template = `Hello {{name}}`;
44
+ *
45
+ * --
46
+ *
47
+ * @param {string} template - Template string to interpolate
48
+ * @param {object} templateData - Template data to use for interpolation
49
+ *
50
+ * @returns {string} interpolated string
51
+ */
52
+ export function interpolate(template, templateData, expressionRegexp = RE_MUSTACHE) {
53
+ expect.string(template, 'Missing or invalid variable [template]');
54
+
55
+ expect.object(templateData, 'Missing or invalid variable [templateData]');
56
+
57
+ return template.replace(
58
+ expressionRegexp,
59
+
60
+ (match, expression) => {
61
+ const path = toArrayPath(expression);
62
+
63
+ const replacement = objectGet(templateData, path, undefined);
64
+
65
+ if (
66
+ typeof replacement !== 'string' &&
67
+ typeof replacement !== 'number' &&
68
+ typeof replacement !== 'boolean'
69
+ ) {
70
+ throw new Error(
71
+ 'Failed to interpolate template: Missing or invalid value for ' +
72
+ `expression [${expression}] (expected string, number or boolean)`
73
+ );
74
+ }
75
+
76
+ return replacement;
77
+ }
78
+ );
79
+ }
80
+
81
+ // -----------------------------------------------------------------------------
82
+
83
+ /**
84
+ * Remove strange characters from a string and replace whitespace by
85
+ * dashes.
86
+ *
87
+ * @returns {string} string that can be used as uri
88
+ */
89
+ export function toUriName(str) {
90
+ expectString(str, 'Missing or invalid variable [str]');
91
+
92
+ str = str.toLowerCase().replace(/[^a-z0-9]+/gi, '-');
93
+
94
+ // TODO: remove duplicate dashes
95
+
96
+ return str;
97
+ }
98
+
99
+ // -----------------------------------------------------------------------------
100
+
101
+ /**
102
+ * Convert a path string to an array path
103
+ * - The path string will be spit at the `pathSeparator` token
104
+ * - If the supplied path is already an array, the original array will
105
+ * be returned
106
+ *
107
+ * @param {string|string[]} path
108
+ * String or array path (e.g. "some.path.to")
109
+ *
110
+ * @param {string} [pathSeparator=PATH_SEPARATOR]
111
+ * A custom path separator to use instead of the default "."
112
+ *
113
+ * @returns {string[]} array path (e.g. ["some", "path", "to"])
114
+ */
115
+ export function toStringPath(path, pathSeparator = PATH_SEPARATOR) {
116
+ if (Array.isArray(path)) {
117
+ return path.join(pathSeparator);
118
+ } else if (typeof path === 'string') {
119
+ // path is already a string
120
+ return path;
121
+ } else {
122
+ throw new Error('Missing or invalid parameter [path] (expected string or array)');
123
+ }
124
+ }
125
+
126
+ // -----------------------------------------------------------------------------
127
+
128
+ /**
129
+ * Prefix a numeric string with 0's
130
+ *
131
+ * @param {string|number} input
132
+ *
133
+ * @returns {string}
134
+ */
135
+ export function padDigits(input, targetLength = 2, padString = '0') {
136
+ return ('' + input).padStart(targetLength, padString);
137
+ }
138
+
139
+ // -----------------------------------------------------------------------------
140
+
141
+ /**
142
+ * Make sure that the outputted path is an array path
143
+ * - The input value may be a array path
144
+ * - The input value may be a string path (no conversion needed)
145
+ *
146
+ * @param {string|string[]} path
147
+ *
148
+ * @returns {string[]} array path (list of strings)
149
+ */
150
+ // export function fromPath( path )
151
+ // {
152
+ // if( typeof path === "string" )
153
+ // {
154
+ // return path;
155
+ // }
156
+ // else {
157
+ // expect.array( path,
158
+ // "Missing or invalid parameter [path] (expected string or string[])" );
159
+
160
+ // let strPath = proc.arrayToStringPathWeakMap.get( path );
161
+
162
+ // if( strPath )
163
+ // {
164
+ // // std.debug( "Using cached value", path );
165
+ // return strPath;
166
+ // }
167
+
168
+ // // Check array path
169
+ // for( let j = 0, n = path.length; j < n; j = j + 1 )
170
+ // {
171
+ // if( typeof path[j] !== "string" )
172
+ // {
173
+ // throw new Error("Invalid array path. Expected array of strings");
174
+ // }
175
+ // }
176
+
177
+ // strPath = path.join("/");
178
+
179
+ // proc.safeArrayPathsWeakMap.set( path, true );
180
+ // proc.arrayToStringPathWeakMap.set( path, strPath );
181
+
182
+ // return strPath;
183
+ // }
184
+ // }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-sveltekit",
3
- "version": "0.0.42",
3
+ "version": "0.0.44",
4
4
  "author": "Jens Kleinhout, HKdigital (https://hkdigital.nl)",
5
5
  "license": "ISC",
6
6
  "repository": {