utilium 1.3.3 → 1.5.0
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/fs.d.ts +1 -4
- package/dist/objects.d.ts +16 -2
- package/dist/objects.js +5 -7
- package/package.json +1 -1
- package/src/fs.ts +1 -10
- package/src/objects.ts +22 -7
package/dist/fs.d.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { type JSONValue } from './objects.js';
|
1
2
|
export declare abstract class FileMap<V> implements Map<string, V> {
|
2
3
|
protected readonly path: string;
|
3
4
|
get [Symbol.toStringTag](): string;
|
@@ -15,10 +16,6 @@ export declare abstract class FileMap<V> implements Map<string, V> {
|
|
15
16
|
get entries(): typeof this._map.entries;
|
16
17
|
get forEach(): typeof this._map.forEach;
|
17
18
|
}
|
18
|
-
export type JSONObject<Key extends string | number | symbol = string> = {
|
19
|
-
[K in Key]: JSONValue;
|
20
|
-
};
|
21
|
-
export type JSONValue<Key extends string | number | symbol = string> = string | number | boolean | JSONObject<Key> | Array<JSONValue>;
|
22
19
|
export interface JSONFileMapOptions {
|
23
20
|
/**
|
24
21
|
* Should an invalid JSON file be overwritten
|
package/dist/objects.d.ts
CHANGED
@@ -33,5 +33,19 @@ export interface ConstMap<T extends Partial<Record<keyof any, any>>, K extends k
|
|
33
33
|
export declare function map<const T extends Partial<Record<any, any>>>(items: T): Map<keyof T, T[keyof T]>;
|
34
34
|
export declare function getByString(object: Record<string, any>, path: string, separator?: RegExp): Record<string, any>;
|
35
35
|
export declare function setByString(object: Record<string, any>, path: string, value: unknown, separator?: RegExp): Record<string, any>;
|
36
|
-
|
37
|
-
export
|
36
|
+
export type JSONPrimitive = null | string | number | boolean;
|
37
|
+
export type JSONObject = {
|
38
|
+
[K in string]: JSONValue;
|
39
|
+
};
|
40
|
+
export type JSONValue = JSONPrimitive | JSONObject | JSONValue[];
|
41
|
+
/**
|
42
|
+
* An object `T` with all of its functions bound to a `This` value
|
43
|
+
*/
|
44
|
+
type Bound<T extends object, This = any> = T & {
|
45
|
+
[k in keyof T]: T[k] extends (...args: any[]) => any ? (this: This, ...args: Parameters<T[k]>) => ReturnType<T[k]> : T[k];
|
46
|
+
};
|
47
|
+
/**
|
48
|
+
* Binds a this value for all of the functions in an object (not recursive)
|
49
|
+
*/
|
50
|
+
export declare function bindFunctions<T extends object, This = any>(fns: T, thisValue: This): Bound<T, This>;
|
51
|
+
export {};
|
package/dist/objects.js
CHANGED
@@ -59,11 +59,9 @@ export function setByString(object, path, value, separator = /[.[\]'"]/) {
|
|
59
59
|
.filter(p => p)
|
60
60
|
.reduce((o, p, i) => (o[p] = path.split(separator).filter(p => p).length === ++i ? value : o[p] || {}), object);
|
61
61
|
}
|
62
|
-
/**
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
this[name] = this[name].bind(this);
|
68
|
-
});
|
62
|
+
/**
|
63
|
+
* Binds a this value for all of the functions in an object (not recursive)
|
64
|
+
*/
|
65
|
+
export function bindFunctions(fns, thisValue) {
|
66
|
+
return Object.fromEntries(Object.entries(fns).map(([k, v]) => [k, typeof v == 'function' ? v.bind(thisValue) : v]));
|
69
67
|
}
|
package/package.json
CHANGED
package/src/fs.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { isJSON } from './objects.js';
|
1
|
+
import { isJSON, type JSONValue } from './objects.js';
|
2
2
|
import * as fs from 'fs';
|
3
3
|
|
4
4
|
export abstract class FileMap<V> implements Map<string, V> {
|
@@ -49,15 +49,6 @@ export abstract class FileMap<V> implements Map<string, V> {
|
|
49
49
|
}
|
50
50
|
}
|
51
51
|
|
52
|
-
export type JSONObject<Key extends string | number | symbol = string> = { [K in Key]: JSONValue };
|
53
|
-
|
54
|
-
export type JSONValue<Key extends string | number | symbol = string> =
|
55
|
-
| string
|
56
|
-
| number
|
57
|
-
| boolean
|
58
|
-
| JSONObject<Key>
|
59
|
-
| Array<JSONValue>;
|
60
|
-
|
61
52
|
export interface JSONFileMapOptions {
|
62
53
|
/**
|
63
54
|
* Should an invalid JSON file be overwritten
|
package/src/objects.ts
CHANGED
@@ -113,11 +113,26 @@ export function setByString(object: Record<string, any>, path: string, value: un
|
|
113
113
|
.reduce((o, p, i) => (o[p] = path.split(separator).filter(p => p).length === ++i ? value : o[p] || {}), object);
|
114
114
|
}
|
115
115
|
|
116
|
-
|
117
|
-
|
118
|
-
export
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
116
|
+
export type JSONPrimitive = null | string | number | boolean;
|
117
|
+
|
118
|
+
export type JSONObject = { [K in string]: JSONValue };
|
119
|
+
|
120
|
+
export type JSONValue = JSONPrimitive | JSONObject | JSONValue[];
|
121
|
+
|
122
|
+
/**
|
123
|
+
* An object `T` with all of its functions bound to a `This` value
|
124
|
+
*/
|
125
|
+
type Bound<T extends object, This = any> = T & {
|
126
|
+
[k in keyof T]: T[k] extends (...args: any[]) => any
|
127
|
+
? (this: This, ...args: Parameters<T[k]>) => ReturnType<T[k]>
|
128
|
+
: T[k];
|
129
|
+
};
|
130
|
+
|
131
|
+
/**
|
132
|
+
* Binds a this value for all of the functions in an object (not recursive)
|
133
|
+
*/
|
134
|
+
export function bindFunctions<T extends object, This = any>(fns: T, thisValue: This): Bound<T, This> {
|
135
|
+
return Object.fromEntries(
|
136
|
+
Object.entries(fns).map(([k, v]) => [k, typeof v == 'function' ? v.bind(thisValue) : v])
|
137
|
+
) as Bound<T, This>;
|
123
138
|
}
|