@hkdigital/lib-sveltekit 0.0.41 → 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,247 @@
1
+ /* ------------------------------------------------------------------ Imports */
2
+
3
+ import { objectGet } from '../object/index.js';
4
+
5
+ /* ------------------------------------------------------------------ Exports */
6
+
7
+ /**
8
+ * Check if the values of two variables should be considered the same
9
+ *
10
+ * @param {*} value1 - First value for comparison
11
+ * @param {*} value2 - Second value for comparison
12
+ *
13
+ * @return {boolean} true if the two values can be considered the same
14
+ */
15
+ export function equals(value1, value2, _pendingComparisons) {
16
+ // -- Basic sameness comparisons
17
+
18
+ if (typeof value1 !== typeof value2) {
19
+ // Not the same [type]
20
+ return false;
21
+ }
22
+
23
+ if (!(value1 instanceof Object) && value1 !== value2) {
24
+ // [Not an object] and [not the same value]
25
+ return false;
26
+ }
27
+
28
+ if (value1 === value2) {
29
+ // [same value or same object reference]
30
+ return true;
31
+ }
32
+
33
+ if (value1 === null || value2 === null) {
34
+ // [one of the values is null, the other an object]
35
+ return false;
36
+ }
37
+
38
+ // -- Check if variables are already being compared
39
+
40
+ if (!_pendingComparisons) {
41
+ _pendingComparisons = { value1: [], value2: [] };
42
+ } else {
43
+ const values1 = _pendingComparisons.value1;
44
+ const values2 = _pendingComparisons.value2;
45
+
46
+ if (values1.length > 0) {
47
+ const foundIndex = values1.indexOf(value1);
48
+
49
+ if (-1 !== foundIndex && values2[foundIndex] === value2) {
50
+ // Objects comparison in progress (it's safe to return true),
51
+ // since equals will fail on other place if objects differ
52
+ return true;
53
+ }
54
+ }
55
+ }
56
+
57
+ // -- Compare special objects
58
+
59
+ switch (value1.constructor) {
60
+ // Implement other special objects here.
61
+
62
+ case RegExp:
63
+ if (value1.toString() === value2.toString()) {
64
+ return true;
65
+ }
66
+ return false;
67
+
68
+ case Date:
69
+ if (value1.getTime() === value2.getTime()) {
70
+ return true;
71
+ }
72
+ return false;
73
+ }
74
+
75
+ // -- Check if both objects have the same properties
76
+
77
+ for (const prop in value1) {
78
+ if (undefined === value2[prop] && undefined !== value1[prop]) {
79
+ // (defined) property found in value1 that is undefined in value2
80
+ return false;
81
+ }
82
+ }
83
+
84
+ for (const prop in value2) {
85
+ if (undefined === value1[prop] && undefined !== value2[prop]) {
86
+ // (defined) property found in value2 that is undefined in value1
87
+ return false;
88
+ }
89
+ }
90
+
91
+ // -- Check each property for sameness
92
+
93
+ _pendingComparisons.value1.push(value1);
94
+ _pendingComparisons.value2.push(value2);
95
+
96
+ for (const prop in value1) {
97
+ if (false === equals(value1[prop], value2[prop], _pendingComparisons)) {
98
+ return false;
99
+ }
100
+ }
101
+
102
+ return true;
103
+ }
104
+
105
+ // ---------------------------------------------------------------------- Method
106
+
107
+ /**
108
+ * Returns true if x is greater than y
109
+ *
110
+ * @param {*} x - First value
111
+ * @param {*} y - Second value
112
+ */
113
+ export function isGreaterThan(x, y) {
114
+ if (typeof x === 'undefined') {
115
+ if (typeof y === 'undefined') {
116
+ return false;
117
+ }
118
+
119
+ return true;
120
+ }
121
+
122
+ if (typeof y === 'undefined') {
123
+ return false;
124
+ }
125
+
126
+ return x > y;
127
+ }
128
+
129
+ // ---------------------------------------------------------------------- Method
130
+
131
+ /**
132
+ * Returns true if x is less than y
133
+ *
134
+ * @param {*} x - First value
135
+ * @param {*} y - Second value
136
+ */
137
+ export function isLessThan(x, y) {
138
+ if (typeof x === 'undefined') {
139
+ if (typeof y === 'undefined') {
140
+ return false;
141
+ }
142
+
143
+ return false;
144
+ }
145
+
146
+ if (typeof y === 'undefined') {
147
+ return true;
148
+ }
149
+
150
+ return x < y;
151
+ }
152
+
153
+ // ---------------------------------------------------------------------- Method
154
+
155
+ /**
156
+ * Compare function that can be used for sorting smallest values first
157
+ * - undefined values are placed at the ...???FIXME???... of the sorted array
158
+ *
159
+ * @param {*} x - First value
160
+ * @param {*} y - Second value
161
+ *
162
+ * @returns {number} 0 = keep original order,
163
+ * 1 = sort x after y,
164
+ * -1 = sort x before y
165
+ */
166
+ export function smallestFirst(x, y) {
167
+ if (x === undefined) {
168
+ if (y === undefined) {
169
+ return 0;
170
+ }
171
+
172
+ return 1;
173
+ }
174
+
175
+ if (y === undefined) {
176
+ return -1;
177
+ }
178
+
179
+ return x < y ? -1 : x > y ? 1 : 0;
180
+ }
181
+
182
+ // ---------------------------------------------------------------------- Method
183
+
184
+ /**
185
+ * Compare function that can be used for sorting largest values first
186
+ * - undefined values are placed at the ...???FIXME???... of the sorted array
187
+ *
188
+ * @param {*} x - First value
189
+ * @param {*} y - Second value
190
+ *
191
+ * @returns {number} 0 = keep original order,
192
+ * 1 = sort x after y,
193
+ * -1 = sort x before y
194
+ */
195
+ export function largestFirst(x, y) {
196
+ if (x === undefined) {
197
+ if (y === undefined) {
198
+ return 0;
199
+ }
200
+
201
+ return 1;
202
+ }
203
+
204
+ if (y === undefined) {
205
+ return -1;
206
+ }
207
+
208
+ return x < y ? 1 : x > y ? -1 : 0;
209
+ }
210
+
211
+ // ---------------------------------------------------------------------- Method
212
+
213
+ /**
214
+ * Comparator that can be used for sorting using an object path
215
+ *
216
+ * @param {function} compareFn - Function to use to compare the values
217
+ *
218
+ * @param {*} a - First value
219
+ * @param {*} b - Second value
220
+ *
221
+ * @param {string|string[]} path - Object path
222
+ */
223
+ export function compareUsingPath(compareFn, a, b, path) {
224
+ // @note assume a and b are objects
225
+
226
+ const valueA = objectGet(a, path);
227
+ const valueB = objectGet(b, path);
228
+
229
+ return compareFn(valueA, valueB);
230
+ }
231
+
232
+ // ---------------------------------------------------------------------- Method
233
+
234
+ /**
235
+ * Comparator that can be used for sorting using an object key
236
+ *
237
+ * @param {function} compareFn - Function to use to compare the values
238
+ * @param {string|string[]} path - Object path
239
+ *
240
+ * @param {*} x - First value
241
+ * @param {*} y - Second value
242
+ */
243
+ export function compareUsingKey(compareFn, key, a, b) {
244
+ // @note assume a and b are objects
245
+
246
+ return compareFn(a[key], b[key]);
247
+ }
@@ -1,4 +1,3 @@
1
- /** Reusable schema's */
2
1
  /** Exports */
3
2
  /**
4
3
  * Throws a validation error if value is not a string
@@ -6,6 +5,24 @@
6
5
  * @param {*} value
7
6
  */
8
7
  export function string(value: any): void;
8
+ /**
9
+ * Throws a validation error if value is not a boolean
10
+ *
11
+ * @param {*} value
12
+ */
13
+ export function boolean(value: any): void;
14
+ /**
15
+ * Throws a validation error if value is not a number
16
+ *
17
+ * @param {*} value
18
+ */
19
+ export function number(value: any): void;
20
+ /**
21
+ * Throws a validation error if value is not a Symbol
22
+ *
23
+ * @param {*} value
24
+ */
25
+ export function symbol(value: any): void;
9
26
  /**
10
27
  * Throws a validation error if value is not an array of strings
11
28
  *
@@ -18,6 +35,36 @@ export function stringArray(value: any): void;
18
35
  * @param {*} value
19
36
  */
20
37
  export function objectArray(value: any): void;
38
+ /**
39
+ * Throws a validation error if value is not a Promise
40
+ *
41
+ * @param {*} value
42
+ */
43
+ export function promise_(value: any): void;
44
+ /**
45
+ * Throws a validation error if value is not a Map
46
+ *
47
+ * @param {*} value
48
+ */
49
+ export function map_(value: any): void;
50
+ /**
51
+ * Throws a validation error if value is not a Set
52
+ *
53
+ * @param {*} value
54
+ */
55
+ export function set_(value: any): void;
56
+ /**
57
+ * Throws a validation error if value is not an Error instance
58
+ *
59
+ * @param {*} value
60
+ */
61
+ export function error_(value: any): void;
62
+ /**
63
+ * Expect a value not to be null
64
+ *
65
+ * @param {*} value
66
+ */
67
+ export function notNull(value: any): void;
21
68
  /**
22
69
  * Expect a value to be a boolean and true
23
70
  *
@@ -30,10 +77,74 @@ export function _true(value: any): void;
30
77
  * @param {*} value
31
78
  */
32
79
  export function notEmptyString(value: any): void;
80
+ /**
81
+ * Throws a validation error if value is not iterable
82
+ *
83
+ * @param {*} value
84
+ */
85
+ export function iterable(value: any): void;
86
+ /**
87
+ * Throws a validation error if value is not a a store (has not subscribe
88
+ * method)
89
+ *
90
+ * @param {*} value
91
+ */
92
+ export function store(value: any): void;
93
+ /**
94
+ * Throws a validation error if value is not array like
95
+ * - Checks if the value is an object and has a property `length`
96
+ *
97
+ * @param {*} value
98
+ */
99
+ export function arrayLike(value: any): void;
100
+ /**
101
+ * Throws a validation error if value is not an object or the value
102
+ * is an array
103
+ *
104
+ * @param {*} value
105
+ */
106
+ export function objectNoArray(value: any): void;
107
+ /**
108
+ * Throws a validation error if value is not an object or the value
109
+ * is a function
110
+ *
111
+ * @param {*} value
112
+ */
113
+ export function objectNoFunction(value: any): void;
114
+ /**
115
+ * Throws a validation error if value is not an object or null
116
+ *
117
+ * @param {*} value
118
+ */
119
+ export function objectOrNull(value: any): void;
120
+ /**
121
+ * Throws a validation error if value is not an array or Set
122
+ *
123
+ * @param {*} value
124
+ */
125
+ export function arrayOrSet(value: any): void;
126
+ /**
127
+ * Throws a validation error if value is undefined
128
+ *
129
+ * @param {*} value
130
+ */
131
+ declare function undefined_(value: any): void;
132
+ /**
133
+ * Throws a validation error if value is not an object
134
+ *
135
+ * @param {*} value
136
+ */
137
+ declare function object_(value: any): void;
138
+ /**
139
+ * Throws a validation error if value is not an array
140
+ *
141
+ * @param {*} value
142
+ */
143
+ declare function array_(value: any): void;
33
144
  /**
34
145
  * Throws a validation error if value is not a function
35
146
  *
36
147
  * @param {*} value
37
148
  */
38
149
  declare function _function(value: any): void;
39
- export { _function as function, _function as function, _function as class, _class as class, _true as true, _true as true };
150
+ export { undefined_ as undefined, object_ as object, array_ as array, _function as function, _function as function, _function as class, _class as class, promise_ as promise, map_ as map, set_ as set, error_ as error, _true as true, _true as true };
@@ -19,10 +19,7 @@
19
19
 
20
20
  import * as v from 'valibot';
21
21
 
22
- /** Reusable schema's */
23
-
24
- // const StringSchema = v.string();
25
- // const FunctionSchema = v.function();
22
+ import { isObject } from '../is/index.js';
26
23
 
27
24
  /** Exports */
28
25
 
@@ -34,19 +31,71 @@ import * as v from 'valibot';
34
31
  * @param {*} value
35
32
  */
36
33
  export function string(value) {
37
- v.parse(v.string(), value);
34
+ v.parse(v.string(), value);
35
+ }
36
+
37
+ /**
38
+ * Throws a validation error if value is not a boolean
39
+ *
40
+ * @param {*} value
41
+ */
42
+ export function boolean(value) {
43
+ v.parse(v.boolean(), value);
44
+ }
45
+
46
+ /**
47
+ * Throws a validation error if value is not a number
48
+ *
49
+ * @param {*} value
50
+ */
51
+ export function number(value) {
52
+ v.parse(v.number(), value);
53
+ }
54
+
55
+ /**
56
+ * Throws a validation error if value is not a Symbol
57
+ *
58
+ * @param {*} value
59
+ */
60
+ export function symbol(value) {
61
+ v.parse(v.symbol(), value);
38
62
  }
39
63
 
40
- // boolean
41
- // number
42
- // symbol
43
- // defined
64
+ /**
65
+ * Throws a validation error if value is undefined
66
+ *
67
+ * @param {*} value
68
+ */
69
+ function undefined_(value) {
70
+ v.parse(v.undefined(), value);
71
+ }
72
+
73
+ export { undefined_ as undefined };
44
74
 
45
75
  // > Base objects
46
76
 
47
- // object
77
+ /**
78
+ * Throws a validation error if value is not an object
79
+ *
80
+ * @param {*} value
81
+ */
82
+ function object_(value) {
83
+ v.parse(v.custom(isObject), value);
84
+ // v.parse(v.looseObject({}), value);
85
+ }
48
86
 
49
- // array
87
+ export { object_ as object };
88
+
89
+ /**
90
+ * Throws a validation error if value is not an array
91
+ *
92
+ * @param {*} value
93
+ */
94
+ function array_(value) {
95
+ v.parse(v.instance(Array), value);
96
+ }
97
+
98
+ export { array_ as array };
50
99
 
51
100
  /**
52
101
  * Throws a validation error if value is not an array of strings
@@ -54,7 +103,7 @@ export function string(value) {
54
103
  * @param {*} value
55
104
  */
56
105
  export function stringArray(value) {
57
- v.parse(v.array(v.string()), value);
106
+ v.parse(v.array(v.string()), value);
58
107
  }
59
108
 
60
109
  /**
@@ -63,7 +112,7 @@ export function stringArray(value) {
63
112
  * @param {*} value
64
113
  */
65
114
  export function objectArray(value) {
66
- v.parse(v.array(v.looseObject({})), value);
115
+ v.parse(v.array(v.looseObject({})), value);
67
116
  }
68
117
 
69
118
  /**
@@ -72,21 +121,67 @@ export function objectArray(value) {
72
121
  * @param {*} value
73
122
  */
74
123
  function _function(value) {
75
- v.parse(v.function(), value);
124
+ v.parse(v.function(), value);
76
125
  }
77
126
 
78
127
  export { _function as function };
79
128
 
80
129
  export { _function as class };
81
130
 
82
- // promise
83
- // map
84
- // set
85
- // error
131
+ /**
132
+ * Throws a validation error if value is not a Promise
133
+ *
134
+ * @param {*} value
135
+ */
136
+ export function promise_(value) {
137
+ v.parse(v.instance(Promise), value);
138
+ }
139
+
140
+ export { promise_ as promise };
141
+
142
+ /**
143
+ * Throws a validation error if value is not a Map
144
+ *
145
+ * @param {*} value
146
+ */
147
+ export function map_(value) {
148
+ v.parse(v.instance(Map), value);
149
+ }
150
+
151
+ export { map_ as map };
152
+
153
+ /**
154
+ * Throws a validation error if value is not a Set
155
+ *
156
+ * @param {*} value
157
+ */
158
+ export function set_(value) {
159
+ v.parse(v.instance(Set), value);
160
+ }
161
+
162
+ export { set_ as set };
163
+
164
+ /**
165
+ * Throws a validation error if value is not an Error instance
166
+ *
167
+ * @param {*} value
168
+ */
169
+ export function error_(value) {
170
+ v.parse(v.instance(Map), value);
171
+ }
172
+
173
+ export { error_ as error };
86
174
 
87
175
  // > Common values
88
176
 
89
- // notNull
177
+ /**
178
+ * Expect a value not to be null
179
+ *
180
+ * @param {*} value
181
+ */
182
+ export function notNull(value) {
183
+ v.notValue(null, value);
184
+ }
90
185
 
91
186
  /**
92
187
  * Expect a value to be a boolean and true
@@ -94,7 +189,7 @@ export { _function as class };
94
189
  * @param {*} value
95
190
  */
96
191
  export function _true(value) {
97
- v.value(true, value);
192
+ v.value(true, value);
98
193
  }
99
194
 
100
195
  export { _true as true };
@@ -115,31 +210,123 @@ export { _true as true };
115
210
  * @param {*} value
116
211
  */
117
212
  export function notEmptyString(value) {
118
- const schema = v.pipe(v.string(), v.minLength(1));
213
+ const schema = v.pipe(v.string(), v.minLength(1));
119
214
 
120
- v.parse(schema, value);
215
+ v.parse(schema, value);
121
216
  }
122
217
 
123
218
  // notEmptyStringOrNull
124
219
 
125
220
  // asyncIterator
126
221
  // iterable
222
+
223
+ /**
224
+ * Throws a validation error if value is not iterable
225
+ *
226
+ * @param {*} value
227
+ */
228
+ export function iterable(value) {
229
+ const schema = v.pipe(v.looseObject({ [Symbol.iterator]: v.function() }));
230
+
231
+ v.parse(schema, value);
232
+ }
233
+
127
234
  // iterator
128
235
 
129
- // store
236
+ /**
237
+ * Throws a validation error if value is not a a store (has not subscribe
238
+ * method)
239
+ *
240
+ * @param {*} value
241
+ */
242
+ export function store(value) {
243
+ v.parse(
244
+ v.custom((value) => {
245
+ if (!isObject(value) || typeof value.subscribe !== 'function') {
246
+ return false;
247
+ }
248
+
249
+ return true;
250
+ }),
251
+ value
252
+ );
253
+ }
130
254
 
131
255
  // notEmptyArray
132
256
  // arrayLike
257
+ /**
258
+ * Throws a validation error if value is not array like
259
+ * - Checks if the value is an object and has a property `length`
260
+ *
261
+ * @param {*} value
262
+ */
263
+ export function arrayLike(value) {
264
+ v.parse(v.object({ length: v.number() }), value);
265
+ }
266
+
133
267
  // ArrayBuffer
134
- // ArrayOfStrings
135
268
  // arrayOrUndefined
136
269
  // arangoCollectionId
137
270
  // uriComponent
138
- // objectNoArray
139
- // objectNoFunction
140
- // objectOrNull
271
+
272
+ /**
273
+ * Throws a validation error if value is not an object or the value
274
+ * is an array
275
+ *
276
+ * @param {*} value
277
+ */
278
+ export function objectNoArray(value) {
279
+ v.parse(
280
+ v.custom((value) => {
281
+ if (!isObject(value) || value instanceof Array) {
282
+ return false;
283
+ }
284
+
285
+ return true;
286
+ }),
287
+ value
288
+ );
289
+ }
290
+
291
+ /**
292
+ * Throws a validation error if value is not an object or the value
293
+ * is a function
294
+ *
295
+ * @param {*} value
296
+ */
297
+ export function objectNoFunction(value) {
298
+ v.parse(
299
+ v.custom((value) => {
300
+ if (!isObject(value) || typeof value === 'function') {
301
+ return false;
302
+ }
303
+
304
+ return true;
305
+ }),
306
+ value
307
+ );
308
+ }
309
+
310
+ /**
311
+ * Throws a validation error if value is not an object or null
312
+ *
313
+ * @param {*} value
314
+ */
315
+ export function objectOrNull(value) {
316
+ v.parse(v.union([v.value(null), v.looseObject({})]), value);
317
+ }
318
+
141
319
  // objectOrUndefined
142
320
  // objectPath
143
- // arrayOrSet
321
+
322
+ /**
323
+ * Throws a validation error if value is not an array or Set
324
+ *
325
+ * @param {*} value
326
+ */
327
+ export function arrayOrSet(value) {
328
+ v.parse(v.union([v.instance(Array), v.instance(Set)]), value);
329
+ }
330
+
144
331
  // setOfStrings
145
332
  // emptyStringOrSymbol