@poppinss/utils 6.0.0-2 → 6.0.0-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/README.md +4 -6
- package/build/src/object_builder.d.ts +16 -8
- package/build/src/object_builder.js +7 -6
- package/build/src/types.d.ts +4 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1035,16 +1035,14 @@ if (b) {
|
|
|
1035
1035
|
}
|
|
1036
1036
|
```
|
|
1037
1037
|
|
|
1038
|
-
Instead of writing conditionals,
|
|
1038
|
+
Instead of writing conditionals, you can consider using the Object builder fluent API.
|
|
1039
1039
|
|
|
1040
1040
|
```ts
|
|
1041
1041
|
const builder = new ObjectBuilder({ a: 1 })
|
|
1042
1042
|
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
// Get the plain object back
|
|
1047
|
-
const plainObject = builder.toObject()
|
|
1043
|
+
const plainObject = builder
|
|
1044
|
+
.add('b', b)
|
|
1045
|
+
.toObject()
|
|
1048
1046
|
```
|
|
1049
1047
|
|
|
1050
1048
|
By default, only the `undefined` values are ignored. However, you can also ignore `null` values.
|
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
import { OmitProperties } from './types.js';
|
|
2
|
+
export declare class ObjectBuilder<ReturnType extends Record<string, any>, IgnoreNull extends boolean = false> {
|
|
2
3
|
#private;
|
|
3
|
-
|
|
4
|
-
constructor(initialValue
|
|
5
|
-
add(key:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
values: ReturnType;
|
|
5
|
+
constructor(initialValue: ReturnType, ignoreNull?: IgnoreNull);
|
|
6
|
+
add<Prop extends string>(key: Prop, value: undefined): this;
|
|
7
|
+
add<Prop extends string, Value>(key: Prop, value: Value): ObjectBuilder<ReturnType & {
|
|
8
|
+
[P in Prop]: Value;
|
|
9
|
+
}, IgnoreNull>;
|
|
10
|
+
remove<K extends keyof ReturnType>(key: K): this;
|
|
11
|
+
has<K extends keyof ReturnType>(key: K): boolean;
|
|
12
|
+
get<K extends keyof ReturnType>(key: K): ReturnType[K];
|
|
13
|
+
toObject(): IgnoreNull extends true ? {
|
|
14
|
+
[K in keyof OmitProperties<ReturnType, null>]: ReturnType[K];
|
|
15
|
+
} : {
|
|
16
|
+
[K in keyof ReturnType]: ReturnType[K];
|
|
17
|
+
};
|
|
10
18
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export class ObjectBuilder {
|
|
2
2
|
#ignoreNull;
|
|
3
|
-
|
|
3
|
+
values;
|
|
4
4
|
constructor(initialValue, ignoreNull) {
|
|
5
|
-
this.
|
|
5
|
+
this.values = initialValue;
|
|
6
6
|
this.#ignoreNull = ignoreNull === true ? true : false;
|
|
7
7
|
}
|
|
8
8
|
add(key, value) {
|
|
@@ -12,20 +12,21 @@ export class ObjectBuilder {
|
|
|
12
12
|
if (this.#ignoreNull === true && value === null) {
|
|
13
13
|
return this;
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
;
|
|
16
|
+
this.values[key] = value;
|
|
16
17
|
return this;
|
|
17
18
|
}
|
|
18
19
|
remove(key) {
|
|
19
|
-
delete this.
|
|
20
|
+
delete this.values[key];
|
|
20
21
|
return this;
|
|
21
22
|
}
|
|
22
23
|
has(key) {
|
|
23
24
|
return this.get(key) !== undefined;
|
|
24
25
|
}
|
|
25
26
|
get(key) {
|
|
26
|
-
return this.
|
|
27
|
+
return this.values[key];
|
|
27
28
|
}
|
|
28
29
|
toObject() {
|
|
29
|
-
return this.
|
|
30
|
+
return this.values;
|
|
30
31
|
}
|
|
31
32
|
}
|
package/build/src/types.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
declare type PickKeysByValue<T, V> = {
|
|
2
|
+
[K in keyof T]: T[K] extends V ? K : never;
|
|
3
|
+
}[keyof T];
|
|
4
|
+
export declare type OmitProperties<T, P> = Omit<T, PickKeysByValue<T, P>>;
|
|
1
5
|
declare type ScanFsBaseOptions = {
|
|
2
6
|
ignoreMissingRoot?: boolean;
|
|
3
7
|
filter?: (filePath: string, index: number) => boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@poppinss/utils",
|
|
3
|
-
"version": "6.0.0-
|
|
3
|
+
"version": "6.0.0-3",
|
|
4
4
|
"description": "Handy utilities for repetitive work",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -60,9 +60,9 @@
|
|
|
60
60
|
"@japa/run-failed-tests": "^1.1.0",
|
|
61
61
|
"@japa/runner": "^2.2.2",
|
|
62
62
|
"@japa/spec-reporter": "^1.3.2",
|
|
63
|
-
"@swc/core": "^1.3.
|
|
63
|
+
"@swc/core": "^1.3.11",
|
|
64
64
|
"@types/fs-extra": "^9.0.13",
|
|
65
|
-
"@types/node": "^18.11.
|
|
65
|
+
"@types/node": "^18.11.7",
|
|
66
66
|
"c8": "^7.12.0",
|
|
67
67
|
"del-cli": "^5.0.0",
|
|
68
68
|
"eslint": "^8.26.0",
|