@naturalcycles/js-lib 14.161.2 → 14.161.4
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/json-schema/jsonSchemas.d.ts +2 -2
- package/dist/object/object.util.d.ts +3 -3
- package/dist/types.d.ts +8 -8
- package/package.json +1 -1
- package/src/define.ts +2 -2
- package/src/object/object.util.ts +17 -23
- package/src/types.ts +8 -4
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { BaseDBEntity, SavedDBEntity } from '../types';
|
|
2
|
-
export declare const baseDBEntityJsonSchema: import("./jsonSchemaBuilder").JsonSchemaObjectBuilder<BaseDBEntity
|
|
3
|
-
export declare const savedDBEntityJsonSchema: import("./jsonSchemaBuilder").JsonSchemaObjectBuilder<SavedDBEntity
|
|
2
|
+
export declare const baseDBEntityJsonSchema: import("./jsonSchemaBuilder").JsonSchemaObjectBuilder<BaseDBEntity>;
|
|
3
|
+
export declare const savedDBEntityJsonSchema: import("./jsonSchemaBuilder").JsonSchemaObjectBuilder<SavedDBEntity>;
|
|
@@ -50,7 +50,7 @@ export declare function _filterObject<T extends AnyObject>(obj: T, predicate: Ob
|
|
|
50
50
|
*
|
|
51
51
|
* To skip some key-value pairs - use _mapObject instead.
|
|
52
52
|
*/
|
|
53
|
-
export declare function _mapValues<
|
|
53
|
+
export declare function _mapValues<OUT = unknown, IN extends AnyObject = AnyObject>(obj: IN, mapper: ObjectMapper<IN, any>, mutate?: boolean): OUT;
|
|
54
54
|
/**
|
|
55
55
|
* _.mapKeys({ 'a': 1, 'b': 2 }, (key, value) => key + value)
|
|
56
56
|
* // => { 'a1': 1, 'b2': 2 }
|
|
@@ -59,7 +59,7 @@ export declare function _mapValues<T extends AnyObject>(obj: T, mapper: ObjectMa
|
|
|
59
59
|
*
|
|
60
60
|
* To skip some key-value pairs - use _mapObject instead.
|
|
61
61
|
*/
|
|
62
|
-
export declare function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, string>):
|
|
62
|
+
export declare function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, string>): T;
|
|
63
63
|
/**
|
|
64
64
|
* Maps object through predicate - a function that receives (k, v, obj)
|
|
65
65
|
* k - key
|
|
@@ -76,7 +76,7 @@ export declare function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapp
|
|
|
76
76
|
*
|
|
77
77
|
* Non-string keys are passed via String(...)
|
|
78
78
|
*/
|
|
79
|
-
export declare function _mapObject<
|
|
79
|
+
export declare function _mapObject<OUT = unknown, IN extends AnyObject = AnyObject>(obj: IN, mapper: ObjectMapper<IN, KeyValueTuple<string, any> | typeof SKIP>): OUT;
|
|
80
80
|
export declare function _findKeyByValue<T extends AnyObject>(obj: T, v: ValueOf<T>): keyof T | undefined;
|
|
81
81
|
export declare function _objectNullValuesToUndefined<T extends AnyObject>(obj: T, mutate?: boolean): T;
|
|
82
82
|
/**
|
package/dist/types.d.ts
CHANGED
|
@@ -15,22 +15,22 @@ export type AnyObject = Record<string, any>;
|
|
|
15
15
|
export type AnyEnum = NumberEnum;
|
|
16
16
|
export type NumberEnum = Record<string, number | string>;
|
|
17
17
|
export type StringEnum = Record<string, string>;
|
|
18
|
-
export
|
|
18
|
+
export type CreatedUpdated = {
|
|
19
19
|
created: number;
|
|
20
20
|
updated: number;
|
|
21
|
-
}
|
|
21
|
+
};
|
|
22
22
|
export interface CreatedUpdatedId<ID extends string | number = string | number> extends CreatedUpdated {
|
|
23
23
|
id: ID;
|
|
24
24
|
}
|
|
25
|
-
export
|
|
25
|
+
export type ObjectWithId<ID extends string | number = string | number> = {
|
|
26
26
|
id: ID;
|
|
27
|
-
}
|
|
27
|
+
};
|
|
28
28
|
export interface AnyObjectWithId<ID extends string | number = string | number> extends AnyObject, ObjectWithId<ID> {
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* Base interface for any Entity that was saved to DB.
|
|
32
32
|
*/
|
|
33
|
-
export
|
|
33
|
+
export type SavedDBEntity<ID extends string | number = string> = {
|
|
34
34
|
id: ID;
|
|
35
35
|
/**
|
|
36
36
|
* unixTimestamp of when the entity was first created (in the DB).
|
|
@@ -40,14 +40,14 @@ export interface SavedDBEntity<ID extends string | number = string> {
|
|
|
40
40
|
* unixTimestamp of when the entity was last updated (in the DB).
|
|
41
41
|
*/
|
|
42
42
|
updated: UnixTimestampNumber;
|
|
43
|
-
}
|
|
43
|
+
};
|
|
44
44
|
/**
|
|
45
45
|
* Base interface for any Entity that can be saved to DB.
|
|
46
46
|
* This interface fits when entity was NOT YET saved to DB,
|
|
47
47
|
* hence `id`, `created` and `updated` fields CAN BE undefined (yet).
|
|
48
48
|
* When it's known to be saved - `SavedDBEntity` interface can be used instead.
|
|
49
49
|
*/
|
|
50
|
-
export
|
|
50
|
+
export type BaseDBEntity<ID extends string | number = string> = {
|
|
51
51
|
id?: ID;
|
|
52
52
|
/**
|
|
53
53
|
* unixTimestamp of when the entity was first created (in the DB).
|
|
@@ -57,7 +57,7 @@ export interface BaseDBEntity<ID extends string | number = string> {
|
|
|
57
57
|
* unixTimestamp of when the entity was last updated (in the DB).
|
|
58
58
|
*/
|
|
59
59
|
updated?: UnixTimestampNumber;
|
|
60
|
-
}
|
|
60
|
+
};
|
|
61
61
|
export type Saved<T extends Partial<ObjectWithId>> = T extends AnyObject ? Omit<T, 'id' | 'created' | 'updated'> & SavedDBEntity<NonNullable<T['id']>> : T;
|
|
62
62
|
export type Unsaved<T extends Partial<ObjectWithId>> = T extends AnyObject ? Omit<T, 'id' | 'created' | 'updated'> & BaseDBEntity<NonNullable<T['id']>> : T;
|
|
63
63
|
export type UnsavedId<T extends Partial<ObjectWithId>> = Omit<T, 'id'> & {
|
package/package.json
CHANGED
package/src/define.ts
CHANGED
|
@@ -121,7 +121,7 @@ export function _defineProps<T extends AnyObject>(
|
|
|
121
121
|
enumerable: true,
|
|
122
122
|
// value: obj[k], // existing value is already kept by default
|
|
123
123
|
...pd,
|
|
124
|
-
}))
|
|
124
|
+
})),
|
|
125
125
|
)
|
|
126
126
|
}
|
|
127
127
|
|
|
@@ -137,6 +137,6 @@ export function _defineNonNullishProps<T extends AnyObject>(
|
|
|
137
137
|
_mapObject(props, (k, pd) => {
|
|
138
138
|
if (pd.value === null || pd.value === undefined) return SKIP
|
|
139
139
|
return [k as string, pd]
|
|
140
|
-
})
|
|
140
|
+
}),
|
|
141
141
|
)
|
|
142
142
|
}
|
|
@@ -117,18 +117,18 @@ export function _filterObject<T extends AnyObject>(
|
|
|
117
117
|
*
|
|
118
118
|
* To skip some key-value pairs - use _mapObject instead.
|
|
119
119
|
*/
|
|
120
|
-
export function _mapValues<
|
|
121
|
-
obj:
|
|
122
|
-
mapper: ObjectMapper<
|
|
120
|
+
export function _mapValues<OUT = unknown, IN extends AnyObject = AnyObject>(
|
|
121
|
+
obj: IN,
|
|
122
|
+
mapper: ObjectMapper<IN, any>,
|
|
123
123
|
mutate = false,
|
|
124
|
-
):
|
|
124
|
+
): OUT {
|
|
125
125
|
return _objectEntries(obj).reduce(
|
|
126
126
|
(map, [k, v]) => {
|
|
127
127
|
map[k] = mapper(k, v, obj)
|
|
128
128
|
return map
|
|
129
129
|
},
|
|
130
|
-
mutate ? obj : ({} as
|
|
131
|
-
)
|
|
130
|
+
mutate ? obj : ({} as IN),
|
|
131
|
+
) as any
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
/**
|
|
@@ -139,17 +139,11 @@ export function _mapValues<T extends AnyObject>(
|
|
|
139
139
|
*
|
|
140
140
|
* To skip some key-value pairs - use _mapObject instead.
|
|
141
141
|
*/
|
|
142
|
-
export function _mapKeys<T extends AnyObject>(
|
|
143
|
-
obj
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
(map, [k, v]) => {
|
|
148
|
-
map[mapper(k, v, obj)] = v
|
|
149
|
-
return map
|
|
150
|
-
},
|
|
151
|
-
{} as Record<string, T[keyof T]>,
|
|
152
|
-
)
|
|
142
|
+
export function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, string>): T {
|
|
143
|
+
return _objectEntries(obj).reduce((map, [k, v]) => {
|
|
144
|
+
map[mapper(k, v, obj)] = v
|
|
145
|
+
return map
|
|
146
|
+
}, {} as AnyObject) as T
|
|
153
147
|
}
|
|
154
148
|
|
|
155
149
|
/**
|
|
@@ -168,17 +162,17 @@ export function _mapKeys<T extends AnyObject>(
|
|
|
168
162
|
*
|
|
169
163
|
* Non-string keys are passed via String(...)
|
|
170
164
|
*/
|
|
171
|
-
export function _mapObject<
|
|
172
|
-
obj:
|
|
173
|
-
mapper: ObjectMapper<
|
|
174
|
-
):
|
|
165
|
+
export function _mapObject<OUT = unknown, IN extends AnyObject = AnyObject>(
|
|
166
|
+
obj: IN,
|
|
167
|
+
mapper: ObjectMapper<IN, KeyValueTuple<string, any> | typeof SKIP>,
|
|
168
|
+
): OUT {
|
|
175
169
|
return Object.entries(obj).reduce((map, [k, v]) => {
|
|
176
170
|
const r = mapper(k, v, obj)
|
|
177
171
|
if (r !== SKIP) {
|
|
178
172
|
map[r[0]] = r[1]
|
|
179
173
|
}
|
|
180
174
|
return map
|
|
181
|
-
}, {} as AnyObject)
|
|
175
|
+
}, {} as AnyObject) as OUT
|
|
182
176
|
}
|
|
183
177
|
|
|
184
178
|
export function _findKeyByValue<T extends AnyObject>(obj: T, v: ValueOf<T>): keyof T | undefined {
|
|
@@ -186,7 +180,7 @@ export function _findKeyByValue<T extends AnyObject>(obj: T, v: ValueOf<T>): key
|
|
|
186
180
|
}
|
|
187
181
|
|
|
188
182
|
export function _objectNullValuesToUndefined<T extends AnyObject>(obj: T, mutate = false): T {
|
|
189
|
-
return _mapValues(obj, (_k, v) => (v === null ? undefined : v), mutate)
|
|
183
|
+
return _mapValues(obj, (_k, v) => (v === null ? undefined : v), mutate)
|
|
190
184
|
}
|
|
191
185
|
|
|
192
186
|
/**
|
package/src/types.ts
CHANGED
|
@@ -19,7 +19,8 @@ export type AnyEnum = NumberEnum
|
|
|
19
19
|
export type NumberEnum = Record<string, number | string>
|
|
20
20
|
export type StringEnum = Record<string, string>
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
23
|
+
export type CreatedUpdated = {
|
|
23
24
|
created: number
|
|
24
25
|
updated: number
|
|
25
26
|
}
|
|
@@ -29,7 +30,8 @@ export interface CreatedUpdatedId<ID extends string | number = string | number>
|
|
|
29
30
|
id: ID
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
34
|
+
export type ObjectWithId<ID extends string | number = string | number> = {
|
|
33
35
|
id: ID
|
|
34
36
|
}
|
|
35
37
|
|
|
@@ -40,7 +42,8 @@ export interface AnyObjectWithId<ID extends string | number = string | number>
|
|
|
40
42
|
/**
|
|
41
43
|
* Base interface for any Entity that was saved to DB.
|
|
42
44
|
*/
|
|
43
|
-
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
46
|
+
export type SavedDBEntity<ID extends string | number = string> = {
|
|
44
47
|
id: ID
|
|
45
48
|
|
|
46
49
|
/**
|
|
@@ -60,7 +63,8 @@ export interface SavedDBEntity<ID extends string | number = string> {
|
|
|
60
63
|
* hence `id`, `created` and `updated` fields CAN BE undefined (yet).
|
|
61
64
|
* When it's known to be saved - `SavedDBEntity` interface can be used instead.
|
|
62
65
|
*/
|
|
63
|
-
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
67
|
+
export type BaseDBEntity<ID extends string | number = string> = {
|
|
64
68
|
id?: ID
|
|
65
69
|
|
|
66
70
|
/**
|