@rwillians/qx 0.1.2 → 0.1.3
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/dist/cjs/bun-sqlite.d.ts +47 -0
- package/dist/cjs/bun-sqlite.js +483 -0
- package/dist/cjs/index.d.ts +1174 -0
- package/dist/cjs/index.js +666 -0
- package/dist/cjs/pretty-logger.d.ts +7 -0
- package/dist/cjs/pretty-logger.js +14 -0
- package/dist/cjs/standard-schema.d.ts +157 -0
- package/dist/cjs/standard-schema.js +236 -0
- package/dist/cjs/utils.d.ts +50 -0
- package/dist/cjs/utils.js +76 -0
- package/dist/esm/bun-sqlite.d.ts +47 -0
- package/dist/esm/bun-sqlite.js +483 -0
- package/dist/esm/index.d.ts +1174 -0
- package/dist/esm/index.js +660 -0
- package/dist/esm/pretty-logger.d.ts +7 -0
- package/dist/esm/pretty-logger.js +11 -0
- package/dist/esm/standard-schema.d.ts +157 -0
- package/dist/esm/standard-schema.js +233 -0
- package/dist/esm/utils.d.ts +50 -0
- package/dist/esm/utils.js +65 -0
- package/package.json +1 -1
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import * as std from '@standard-schema/spec';
|
|
2
|
+
/**
|
|
3
|
+
* @public The Standard Schema interface.
|
|
4
|
+
* @since 0.1.0
|
|
5
|
+
* @version 1
|
|
6
|
+
*/
|
|
7
|
+
export { type StandardSchemaV1 as Schema } from '@standard-schema/spec';
|
|
8
|
+
/**
|
|
9
|
+
* @public Infers the input type of a Standard Schema.
|
|
10
|
+
* @since 0.1.0
|
|
11
|
+
* @version 1
|
|
12
|
+
*/
|
|
13
|
+
export type input<T extends std.StandardSchemaV1> = std.StandardSchemaV1.InferInput<T>;
|
|
14
|
+
/**
|
|
15
|
+
* @public Infers the Input type of a Standard Schema.
|
|
16
|
+
* @since 0.1.0
|
|
17
|
+
* @version 1
|
|
18
|
+
*/
|
|
19
|
+
export type output<T extends std.StandardSchemaV1> = std.StandardSchemaV1.InferOutput<T>;
|
|
20
|
+
/**
|
|
21
|
+
* @public Use any standard schema to parse a value.
|
|
22
|
+
* @since 0.1.0
|
|
23
|
+
* @version 1
|
|
24
|
+
*/
|
|
25
|
+
export declare const parse: <T extends std.StandardSchemaV1>(schema: T, value: unknown) => std.StandardSchemaV1.Result<output<T>>;
|
|
26
|
+
/**
|
|
27
|
+
* @public Defines an array of a given standard schema.
|
|
28
|
+
* @since 0.1.0
|
|
29
|
+
* @version 1
|
|
30
|
+
*/
|
|
31
|
+
export type QxArray<T extends std.StandardSchemaV1> = std.StandardSchemaV1<input<T>[], output<T>[]>;
|
|
32
|
+
/**
|
|
33
|
+
* @public Defines an array of a given standard schema.
|
|
34
|
+
* @since 0.1.0
|
|
35
|
+
* @version 1
|
|
36
|
+
*/
|
|
37
|
+
export declare const array: <T extends std.StandardSchemaV1>(schema: T) => QxArray<T>;
|
|
38
|
+
/**
|
|
39
|
+
* @public Defines a standard schema for boolean values.
|
|
40
|
+
* @since 0.1.0
|
|
41
|
+
* @version 1
|
|
42
|
+
*/
|
|
43
|
+
export type QxBoolean = std.StandardSchemaV1<boolean, boolean>;
|
|
44
|
+
/**
|
|
45
|
+
* @public Defines a standard schema for boolean values.
|
|
46
|
+
* @since 0.1.0
|
|
47
|
+
* @version 1
|
|
48
|
+
*/
|
|
49
|
+
export declare const boolean: () => QxBoolean;
|
|
50
|
+
/**
|
|
51
|
+
* @public Defines a standard schema for Date values that can be
|
|
52
|
+
* coerced from ISO 8601 strings or epoch timestamps in
|
|
53
|
+
* milliseconds.
|
|
54
|
+
* @since 0.1.0
|
|
55
|
+
* @version 1
|
|
56
|
+
*/
|
|
57
|
+
export type QxCoercibleDate = std.StandardSchemaV1<Date | string | number, Date>;
|
|
58
|
+
/**
|
|
59
|
+
* @public Defines a standard schema for Date values that can be
|
|
60
|
+
* coerced from ISO 8601 strings or epoch timestamps in
|
|
61
|
+
* milliseconds.
|
|
62
|
+
* @since 0.1.0
|
|
63
|
+
* @version 1
|
|
64
|
+
*/
|
|
65
|
+
export declare const date: () => QxCoercibleDate;
|
|
66
|
+
/**
|
|
67
|
+
* @public Defines a standard schema that validates instances of a
|
|
68
|
+
* given class.
|
|
69
|
+
* @since 0.1.0
|
|
70
|
+
* @version 1
|
|
71
|
+
*/
|
|
72
|
+
export type QxInstanceOf<T> = std.StandardSchemaV1<T, T>;
|
|
73
|
+
/**
|
|
74
|
+
* @public Defines a standard schema that validates instances of a
|
|
75
|
+
* given class.
|
|
76
|
+
* @since 0.1.0
|
|
77
|
+
* @version 1
|
|
78
|
+
*/
|
|
79
|
+
export declare const instanceOf: <T>(ctor: new (...args: any[]) => T) => QxInstanceOf<T>;
|
|
80
|
+
/**
|
|
81
|
+
* @public Defines a standard schema for integers with optional min
|
|
82
|
+
* and max constraints.
|
|
83
|
+
* @since 0.1.0
|
|
84
|
+
* @version 1
|
|
85
|
+
*/
|
|
86
|
+
export type QxInteger = std.StandardSchemaV1<number, number>;
|
|
87
|
+
/**
|
|
88
|
+
* @public Defines a standard schema for integers with optional min and max constraints.
|
|
89
|
+
* @since 0.1.0
|
|
90
|
+
* @version 1
|
|
91
|
+
*/
|
|
92
|
+
export declare const integer: ({ min, max }?: {
|
|
93
|
+
min?: number;
|
|
94
|
+
max?: number;
|
|
95
|
+
}) => QxInteger;
|
|
96
|
+
/**
|
|
97
|
+
* @public Makes any standard schema nullable.
|
|
98
|
+
* @since 0.1.0
|
|
99
|
+
* @version 1
|
|
100
|
+
*/
|
|
101
|
+
export type QxNullable<T extends std.StandardSchemaV1 = std.StandardSchemaV1> = std.StandardSchemaV1<input<T> | null, output<T> | null>;
|
|
102
|
+
/**
|
|
103
|
+
* @public Makes any standard schema accepts `null` as a valid value.
|
|
104
|
+
* @since 0.1.0
|
|
105
|
+
* @version 1
|
|
106
|
+
*/
|
|
107
|
+
export declare const nullable: <T extends std.StandardSchemaV1>(schema: T) => QxNullable<T>;
|
|
108
|
+
/**
|
|
109
|
+
* @public Defines a standard schema for numbers with optional min
|
|
110
|
+
* and max constraints.
|
|
111
|
+
* @since 0.1.0
|
|
112
|
+
* @version 1
|
|
113
|
+
*/
|
|
114
|
+
export type QxNumber = std.StandardSchemaV1<number, number>;
|
|
115
|
+
/**
|
|
116
|
+
* @public Defines a standard schema for numbers with optional min
|
|
117
|
+
* and max constraints.
|
|
118
|
+
* @since 0.1.0
|
|
119
|
+
* @version 1
|
|
120
|
+
*/
|
|
121
|
+
export declare const number: ({ min, max }?: {
|
|
122
|
+
min?: number;
|
|
123
|
+
max?: number;
|
|
124
|
+
}) => QxNumber;
|
|
125
|
+
/**
|
|
126
|
+
* @public Defines an object schema that does not allow extra fields.
|
|
127
|
+
* @since 0.1.0
|
|
128
|
+
* @version 1
|
|
129
|
+
*/
|
|
130
|
+
type QxStrictObject<T extends Record<string, std.StandardSchemaV1>> = std.StandardSchemaV1<{
|
|
131
|
+
[K in keyof T]: input<T[K]>;
|
|
132
|
+
}, {
|
|
133
|
+
[K in keyof T]: output<T[K]>;
|
|
134
|
+
}>;
|
|
135
|
+
/**
|
|
136
|
+
* @public Defines an object schema that does not allow extra fields.
|
|
137
|
+
* @since 0.1.0
|
|
138
|
+
* @version 1
|
|
139
|
+
*/
|
|
140
|
+
export declare const strictObject: <T extends Record<string, std.StandardSchemaV1>>(shape: T) => QxStrictObject<T>;
|
|
141
|
+
/**
|
|
142
|
+
* @public Defines a standard schema for strings with optional min
|
|
143
|
+
* and max length constraints.
|
|
144
|
+
* @since 0.1.0
|
|
145
|
+
* @version 1
|
|
146
|
+
*/
|
|
147
|
+
export type QxString = std.StandardSchemaV1<string, string>;
|
|
148
|
+
/**
|
|
149
|
+
* @public Defines a standard schema for strings with optional min
|
|
150
|
+
* and max length constraints.
|
|
151
|
+
* @since 0.1.0
|
|
152
|
+
* @version 1
|
|
153
|
+
*/
|
|
154
|
+
export declare const string: ({ min, max }?: {
|
|
155
|
+
min?: number;
|
|
156
|
+
max?: number;
|
|
157
|
+
}) => QxString;
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import * as std from '@standard-schema/spec';
|
|
2
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
3
|
+
// STANDARD SCHEMA API //
|
|
4
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
5
|
+
/**
|
|
6
|
+
* @public The Standard Schema interface.
|
|
7
|
+
* @since 0.1.0
|
|
8
|
+
* @version 1
|
|
9
|
+
*/
|
|
10
|
+
export {} from '@standard-schema/spec';
|
|
11
|
+
/**
|
|
12
|
+
* @public Use any standard schema to parse a value.
|
|
13
|
+
* @since 0.1.0
|
|
14
|
+
* @version 1
|
|
15
|
+
*/
|
|
16
|
+
export const parse = (schema, value) => {
|
|
17
|
+
const parsed = schema['~standard'].validate(value);
|
|
18
|
+
if (parsed instanceof Promise) {
|
|
19
|
+
throw new Error('async standard schema validators are not supported');
|
|
20
|
+
}
|
|
21
|
+
return parsed;
|
|
22
|
+
};
|
|
23
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
24
|
+
// UTILS //
|
|
25
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
26
|
+
const prependPath = (path) => (issue) => ({
|
|
27
|
+
...issue,
|
|
28
|
+
path: [path, ...(issue.path ?? [])],
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* @public Defines an array of a given standard schema.
|
|
32
|
+
* @since 0.1.0
|
|
33
|
+
* @version 1
|
|
34
|
+
*/
|
|
35
|
+
export const array = (schema) => ({
|
|
36
|
+
'~standard': {
|
|
37
|
+
version: 1,
|
|
38
|
+
vendor: 'qx',
|
|
39
|
+
validate: (input) => {
|
|
40
|
+
if (!Array.isArray(input)) {
|
|
41
|
+
return { issues: [{ message: 'must be an array' }] };
|
|
42
|
+
}
|
|
43
|
+
const issues = [];
|
|
44
|
+
const value = [];
|
|
45
|
+
for (let i = 0; i < input.length; i++) {
|
|
46
|
+
const parsed = parse(schema, input[i]);
|
|
47
|
+
parsed.issues
|
|
48
|
+
? issues.push(...parsed.issues.map(prependPath(i)))
|
|
49
|
+
: value.push(parsed.value);
|
|
50
|
+
}
|
|
51
|
+
return issues.length > 0
|
|
52
|
+
? { issues }
|
|
53
|
+
: { value: value };
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* @public Defines a standard schema for boolean values.
|
|
59
|
+
* @since 0.1.0
|
|
60
|
+
* @version 1
|
|
61
|
+
*/
|
|
62
|
+
export const boolean = () => ({
|
|
63
|
+
'~standard': {
|
|
64
|
+
version: 1,
|
|
65
|
+
vendor: 'qx',
|
|
66
|
+
validate: (input) => typeof input !== 'boolean'
|
|
67
|
+
? { issues: [{ message: 'must be a boolean' }] }
|
|
68
|
+
: { value: input },
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
/**
|
|
72
|
+
* @public Defines a standard schema for Date values that can be
|
|
73
|
+
* coerced from ISO 8601 strings or epoch timestamps in
|
|
74
|
+
* milliseconds.
|
|
75
|
+
* @since 0.1.0
|
|
76
|
+
* @version 1
|
|
77
|
+
*/
|
|
78
|
+
export const date = () => ({
|
|
79
|
+
'~standard': {
|
|
80
|
+
version: 1,
|
|
81
|
+
vendor: 'qx',
|
|
82
|
+
validate: (input) => {
|
|
83
|
+
if (input instanceof Date)
|
|
84
|
+
return isNaN(input.getTime())
|
|
85
|
+
? { issues: [{ message: 'must be a valid date' }] }
|
|
86
|
+
: { value: input };
|
|
87
|
+
if (typeof input !== 'string' &&
|
|
88
|
+
typeof input !== 'number')
|
|
89
|
+
return { issues: [{ message: 'must be a valid ISO 8601 string or epoch timestamp in milliseconds' }] };
|
|
90
|
+
const date = new Date(input);
|
|
91
|
+
if (isNaN(date.getTime()))
|
|
92
|
+
return { issues: [{ message: 'must be a valid ISO 8601 string or epoch timestamp in milliseconds' }] };
|
|
93
|
+
return { value: date };
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
/**
|
|
98
|
+
* @public Defines a standard schema that validates instances of a
|
|
99
|
+
* given class.
|
|
100
|
+
* @since 0.1.0
|
|
101
|
+
* @version 1
|
|
102
|
+
*/
|
|
103
|
+
export const instanceOf = (ctor) => ({
|
|
104
|
+
'~standard': {
|
|
105
|
+
version: 1,
|
|
106
|
+
vendor: 'qx',
|
|
107
|
+
validate: (input) => !(input instanceof ctor)
|
|
108
|
+
? { issues: [{ message: `must be an instance of ${ctor.name}` }] }
|
|
109
|
+
: { value: input },
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
/**
|
|
113
|
+
* @public Defines a standard schema for integers with optional min and max constraints.
|
|
114
|
+
* @since 0.1.0
|
|
115
|
+
* @version 1
|
|
116
|
+
*/
|
|
117
|
+
export const integer = ({ min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER } = {}) => ({
|
|
118
|
+
'~standard': {
|
|
119
|
+
version: 1,
|
|
120
|
+
vendor: 'qx',
|
|
121
|
+
validate: (input) => {
|
|
122
|
+
if (typeof input !== 'number')
|
|
123
|
+
return { issues: [{ message: 'must be a integer' }] };
|
|
124
|
+
if (Number.isNaN(input) || !Number.isFinite(input))
|
|
125
|
+
return { issues: [{ message: 'must be a integer' }] };
|
|
126
|
+
if (!Number.isInteger(input))
|
|
127
|
+
return { issues: [{ message: 'must be a integer' }] };
|
|
128
|
+
if (input < min)
|
|
129
|
+
return { issues: [{ message: `must be greater than or equal to ${min}` }] };
|
|
130
|
+
if (input > max)
|
|
131
|
+
return { issues: [{ message: `must be less than or equal to ${max}` }] };
|
|
132
|
+
return { value: input };
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
/**
|
|
137
|
+
* @public Makes any standard schema accepts `null` as a valid value.
|
|
138
|
+
* @since 0.1.0
|
|
139
|
+
* @version 1
|
|
140
|
+
*/
|
|
141
|
+
export const nullable = (schema) => ({
|
|
142
|
+
'~standard': {
|
|
143
|
+
version: 1,
|
|
144
|
+
vendor: 'qx',
|
|
145
|
+
validate: (value) => value === null
|
|
146
|
+
? { value }
|
|
147
|
+
: parse(schema, value),
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
/**
|
|
151
|
+
* @public Defines a standard schema for numbers with optional min
|
|
152
|
+
* and max constraints.
|
|
153
|
+
* @since 0.1.0
|
|
154
|
+
* @version 1
|
|
155
|
+
*/
|
|
156
|
+
export const number = ({ min = Number.MIN_VALUE, max = Number.MAX_VALUE } = {}) => ({
|
|
157
|
+
'~standard': {
|
|
158
|
+
version: 1,
|
|
159
|
+
vendor: 'qx',
|
|
160
|
+
validate: (input) => {
|
|
161
|
+
if (typeof input !== 'number')
|
|
162
|
+
return { issues: [{ message: 'must be a number' }] };
|
|
163
|
+
if (Number.isNaN(input) || !Number.isFinite(input))
|
|
164
|
+
return { issues: [{ message: 'must be a number' }] };
|
|
165
|
+
if (input < min)
|
|
166
|
+
return { issues: [{ message: `must be greater than or equal to ${min}` }] };
|
|
167
|
+
if (input > max)
|
|
168
|
+
return { issues: [{ message: `must be less than or equal to ${max}` }] };
|
|
169
|
+
return { value: input };
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
/**
|
|
174
|
+
* @public Defines an object schema that does not allow extra fields.
|
|
175
|
+
* @since 0.1.0
|
|
176
|
+
* @version 1
|
|
177
|
+
*/
|
|
178
|
+
export const strictObject = (shape) => ({
|
|
179
|
+
'~standard': {
|
|
180
|
+
version: 1,
|
|
181
|
+
vendor: 'qx',
|
|
182
|
+
validate: (input) => {
|
|
183
|
+
if (typeof input !== 'object' || input === null) {
|
|
184
|
+
return { issues: [{ message: 'must be an object' }] };
|
|
185
|
+
}
|
|
186
|
+
const issues = [];
|
|
187
|
+
const inputKeys = new Set(Object.keys(input));
|
|
188
|
+
const shapeKeys = new Set(Object.keys(shape));
|
|
189
|
+
// one issue for each key of `input` that doesn't exist in `shape`
|
|
190
|
+
for (const key of inputKeys.difference(shapeKeys)) {
|
|
191
|
+
issues.push({ path: [key], message: 'unknown field' });
|
|
192
|
+
}
|
|
193
|
+
// one issue for each key of `shape` that doesn't exist in `input`
|
|
194
|
+
for (const key of shapeKeys.difference(inputKeys)) {
|
|
195
|
+
issues.push({ path: [key], message: 'is required' });
|
|
196
|
+
}
|
|
197
|
+
const record = {};
|
|
198
|
+
for (const [key, value] of Object.entries(input)) {
|
|
199
|
+
const parsed = shape[key]['~standard'].validate(value);
|
|
200
|
+
if (parsed instanceof Promise) {
|
|
201
|
+
throw new Error('async validators are not supported');
|
|
202
|
+
}
|
|
203
|
+
parsed.issues
|
|
204
|
+
? issues.push(...parsed.issues.map(prependPath(key)))
|
|
205
|
+
: record[key] = parsed.value;
|
|
206
|
+
}
|
|
207
|
+
return issues.length > 0
|
|
208
|
+
? { issues }
|
|
209
|
+
: { value: record };
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
/**
|
|
214
|
+
* @public Defines a standard schema for strings with optional min
|
|
215
|
+
* and max length constraints.
|
|
216
|
+
* @since 0.1.0
|
|
217
|
+
* @version 1
|
|
218
|
+
*/
|
|
219
|
+
export const string = ({ min, max } = {}) => ({
|
|
220
|
+
'~standard': {
|
|
221
|
+
version: 1,
|
|
222
|
+
vendor: 'qx',
|
|
223
|
+
validate: (input) => {
|
|
224
|
+
if (typeof input !== 'string')
|
|
225
|
+
return { issues: [{ message: 'must be a string' }] };
|
|
226
|
+
if (min !== undefined && input.length < min)
|
|
227
|
+
return { issues: [{ message: `must be at least ${min} characters long` }] };
|
|
228
|
+
if (max !== undefined && input.length > max)
|
|
229
|
+
return { issues: [{ message: `must be at most ${max} characters long` }] };
|
|
230
|
+
return { value: input };
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private Converts a snake_case string to camelCase.
|
|
3
|
+
* @since 0.1.0
|
|
4
|
+
* @version 1
|
|
5
|
+
*/
|
|
6
|
+
export declare const camelCase: (str: string) => string;
|
|
7
|
+
/**
|
|
8
|
+
* @private Simplified check for plain objects.
|
|
9
|
+
* @since 0.1.0
|
|
10
|
+
* @version 1
|
|
11
|
+
*/
|
|
12
|
+
export declare const isPlainObject: (value: unknown) => value is Record<string, any>;
|
|
13
|
+
/**
|
|
14
|
+
* @private Throws an error if the given number is greater than the
|
|
15
|
+
* specified maximum.
|
|
16
|
+
* @since 0.1.0
|
|
17
|
+
* @version 1
|
|
18
|
+
*/
|
|
19
|
+
export declare const lte: (n: number, max: number) => number;
|
|
20
|
+
/**
|
|
21
|
+
* @public Maps over the keys of an object.
|
|
22
|
+
* @since 0.1.0
|
|
23
|
+
* @version 1
|
|
24
|
+
*/
|
|
25
|
+
export declare const mapKeys: <T extends Record<string, any>, U extends string>(obj: T, fn: (key: keyof T) => U) => { [K in U]: T[keyof T]; };
|
|
26
|
+
/**
|
|
27
|
+
* @private Maps over the values of an object.
|
|
28
|
+
* @since 0.1.0
|
|
29
|
+
* @version 1
|
|
30
|
+
*/
|
|
31
|
+
export declare const mapValues: <T extends Record<string, any>, U>(obj: T, fn: (value: T[keyof T], key: keyof T) => U) => { [K in keyof T]: U; };
|
|
32
|
+
/**
|
|
33
|
+
* @private Resolves the given value or function to a value.
|
|
34
|
+
* @since 0.1.0
|
|
35
|
+
* @version 1
|
|
36
|
+
*/
|
|
37
|
+
export declare const resolve: <T>(value: T | (() => T)) => T;
|
|
38
|
+
/**
|
|
39
|
+
* @private Converts a camelCase string to snake_case.
|
|
40
|
+
* @since 0.1.0
|
|
41
|
+
* @version 1
|
|
42
|
+
*/
|
|
43
|
+
export declare const snakeCase: (str: string) => string;
|
|
44
|
+
/**
|
|
45
|
+
* @public Wraps the given value in an array, unless it's already an
|
|
46
|
+
* array.
|
|
47
|
+
* @since 0.1.0
|
|
48
|
+
* @version 1
|
|
49
|
+
*/
|
|
50
|
+
export declare const wrap: <T>(value: T | T[]) => T[];
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private Converts a snake_case string to camelCase.
|
|
3
|
+
* @since 0.1.0
|
|
4
|
+
* @version 1
|
|
5
|
+
*/
|
|
6
|
+
export const camelCase = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
7
|
+
/**
|
|
8
|
+
* @private Simplified check for plain objects.
|
|
9
|
+
* @since 0.1.0
|
|
10
|
+
* @version 1
|
|
11
|
+
*/
|
|
12
|
+
export const isPlainObject = (value) => {
|
|
13
|
+
if (typeof value !== 'object' || value === null)
|
|
14
|
+
return false;
|
|
15
|
+
let proto = Object.getPrototypeOf(value);
|
|
16
|
+
if (proto === null)
|
|
17
|
+
return true;
|
|
18
|
+
return proto === Object.prototype;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* @private Throws an error if the given number is greater than the
|
|
22
|
+
* specified maximum.
|
|
23
|
+
* @since 0.1.0
|
|
24
|
+
* @version 1
|
|
25
|
+
*/
|
|
26
|
+
export const lte = (n, max) => {
|
|
27
|
+
if (n > max)
|
|
28
|
+
throw new Error(`Must be at most ${max}, got ${n}`);
|
|
29
|
+
return n;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* @public Maps over the keys of an object.
|
|
33
|
+
* @since 0.1.0
|
|
34
|
+
* @version 1
|
|
35
|
+
*/
|
|
36
|
+
export const mapKeys = (obj, fn) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [fn(key), value]));
|
|
37
|
+
/**
|
|
38
|
+
* @private Maps over the values of an object.
|
|
39
|
+
* @since 0.1.0
|
|
40
|
+
* @version 1
|
|
41
|
+
*/
|
|
42
|
+
export const mapValues = (obj, fn) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, fn(value, key)]));
|
|
43
|
+
/**
|
|
44
|
+
* @private Resolves the given value or function to a value.
|
|
45
|
+
* @since 0.1.0
|
|
46
|
+
* @version 1
|
|
47
|
+
*/
|
|
48
|
+
export const resolve = (value) => typeof value === 'function'
|
|
49
|
+
? value()
|
|
50
|
+
: value;
|
|
51
|
+
/**
|
|
52
|
+
* @private Converts a camelCase string to snake_case.
|
|
53
|
+
* @since 0.1.0
|
|
54
|
+
* @version 1
|
|
55
|
+
*/
|
|
56
|
+
export const snakeCase = (str) => str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
57
|
+
/**
|
|
58
|
+
* @public Wraps the given value in an array, unless it's already an
|
|
59
|
+
* array.
|
|
60
|
+
* @since 0.1.0
|
|
61
|
+
* @version 1
|
|
62
|
+
*/
|
|
63
|
+
export const wrap = (value) => Array.isArray(value)
|
|
64
|
+
? value
|
|
65
|
+
: [value];
|