@enbox/dwn-sdk-js 0.3.5 → 0.3.6
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/browser.mjs +5 -5
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/src/core/message.js +1 -1
- package/dist/esm/src/core/message.js.map +1 -1
- package/dist/esm/src/dwn.js +9 -4
- package/dist/esm/src/dwn.js.map +1 -1
- package/dist/esm/src/handlers/protocols-query.js +1 -1
- package/dist/esm/src/handlers/protocols-query.js.map +1 -1
- package/dist/esm/src/interfaces/messages-subscribe.js +1 -1
- package/dist/esm/src/interfaces/messages-subscribe.js.map +1 -1
- package/dist/esm/src/interfaces/messages-sync.js +1 -1
- package/dist/esm/src/interfaces/messages-sync.js.map +1 -1
- package/dist/esm/src/interfaces/protocols-query.js +1 -1
- package/dist/esm/src/interfaces/protocols-query.js.map +1 -1
- package/dist/esm/src/interfaces/records-count.js +1 -1
- package/dist/esm/src/interfaces/records-count.js.map +1 -1
- package/dist/esm/src/interfaces/records-delete.js +1 -1
- package/dist/esm/src/interfaces/records-delete.js.map +1 -1
- package/dist/esm/src/interfaces/records-query.js +1 -1
- package/dist/esm/src/interfaces/records-query.js.map +1 -1
- package/dist/esm/src/interfaces/records-read.js +1 -1
- package/dist/esm/src/interfaces/records-read.js.map +1 -1
- package/dist/esm/src/interfaces/records-subscribe.js +1 -1
- package/dist/esm/src/interfaces/records-subscribe.js.map +1 -1
- package/dist/esm/src/interfaces/records-write-signing.js +1 -1
- package/dist/esm/src/interfaces/records-write-signing.js.map +1 -1
- package/dist/esm/src/interfaces/records-write.js +1 -1
- package/dist/esm/src/interfaces/records-write.js.map +1 -1
- package/dist/esm/src/store/index-level.js +1 -1
- package/dist/esm/src/store/index-level.js.map +1 -1
- package/dist/esm/src/utils/messages.js +1 -1
- package/dist/esm/src/utils/messages.js.map +1 -1
- package/dist/esm/src/utils/records.js +1 -1
- package/dist/esm/src/utils/records.js.map +1 -1
- package/dist/esm/src/utils/time.js +5 -9
- package/dist/esm/src/utils/time.js.map +1 -1
- package/dist/esm/tests/fuzz/object.fuzz.spec.js +1 -1
- package/dist/esm/tests/fuzz/object.fuzz.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-subscribe.spec.js +2 -1
- package/dist/esm/tests/handlers/records-subscribe.spec.js.map +1 -1
- package/dist/esm/tests/utils/poller.js +2 -2
- package/dist/esm/tests/utils/poller.js.map +1 -1
- package/dist/esm/tests/utils/test-data-generator.js +1 -1
- package/dist/esm/tests/utils/test-data-generator.js.map +1 -1
- package/dist/types/src/dwn.d.ts.map +1 -1
- package/dist/types/src/utils/time.d.ts +3 -6
- package/dist/types/src/utils/time.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-subscribe.spec.d.ts.map +1 -1
- package/package.json +4 -3
- package/src/core/message.ts +1 -1
- package/src/dwn.ts +21 -4
- package/src/handlers/protocols-query.ts +1 -1
- package/src/interfaces/messages-subscribe.ts +1 -1
- package/src/interfaces/messages-sync.ts +1 -1
- package/src/interfaces/protocols-query.ts +1 -1
- package/src/interfaces/records-count.ts +1 -1
- package/src/interfaces/records-delete.ts +1 -1
- package/src/interfaces/records-query.ts +1 -1
- package/src/interfaces/records-read.ts +1 -1
- package/src/interfaces/records-subscribe.ts +1 -1
- package/src/interfaces/records-write-signing.ts +1 -1
- package/src/interfaces/records-write.ts +2 -2
- package/src/store/index-level.ts +1 -1
- package/src/utils/messages.ts +1 -1
- package/src/utils/records.ts +1 -1
- package/src/utils/time.ts +5 -10
- package/dist/esm/src/utils/object.js +0 -37
- package/dist/esm/src/utils/object.js.map +0 -1
- package/dist/esm/tests/utils/object.spec.js +0 -36
- package/dist/esm/tests/utils/object.spec.js.map +0 -1
- package/dist/types/src/utils/object.d.ts +0 -13
- package/dist/types/src/utils/object.d.ts.map +0 -1
- package/dist/types/tests/utils/object.spec.d.ts +0 -2
- package/dist/types/tests/utils/object.spec.d.ts.map +0 -1
- package/src/utils/object.ts +0 -39
package/src/dwn.ts
CHANGED
|
@@ -33,6 +33,18 @@ import { StorageController } from './store/storage-controller.js';
|
|
|
33
33
|
import { DidDht, DidJwk, DidKey, DidResolverCacheMemory, DidWeb, UniversalResolver } from '@enbox/dids';
|
|
34
34
|
import { DwnInterfaceName, DwnMethodName } from './enums/dwn-interface-method.js';
|
|
35
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Structural shape for `DidResolver` implementations that expose
|
|
38
|
+
* optional lifecycle hooks (e.g. cache-backed resolvers that need to
|
|
39
|
+
* open/close a store). The base `DidResolver` interface in
|
|
40
|
+
* `@enbox/dids` doesn't declare these methods, so `Dwn.open()` /
|
|
41
|
+
* `Dwn.close()` probe via this narrowly-typed cast (no `any`).
|
|
42
|
+
*/
|
|
43
|
+
type LifecycleResolver = {
|
|
44
|
+
open: () => Promise<void>;
|
|
45
|
+
close: () => Promise<void>;
|
|
46
|
+
};
|
|
47
|
+
|
|
36
48
|
export class Dwn {
|
|
37
49
|
private readonly methodHandlers: { [key:string]: MethodHandler };
|
|
38
50
|
private readonly didResolver: DidResolver;
|
|
@@ -125,8 +137,12 @@ export class Dwn {
|
|
|
125
137
|
*/
|
|
126
138
|
public async open(): Promise<void> {
|
|
127
139
|
// Open the resolver's cache if the DWN owns it (created via defaults).
|
|
128
|
-
|
|
129
|
-
|
|
140
|
+
// The base `DidResolver` interface in `@enbox/dids` doesn't declare
|
|
141
|
+
// optional lifecycle hooks; cache-backed implementations expose them
|
|
142
|
+
// structurally. Narrowly-typed probe (no `any`).
|
|
143
|
+
const lifecycleResolver = this.didResolver as Partial<LifecycleResolver>;
|
|
144
|
+
if (this.ownsResolver && typeof lifecycleResolver.open === 'function') {
|
|
145
|
+
await lifecycleResolver.open();
|
|
130
146
|
}
|
|
131
147
|
|
|
132
148
|
await this.messageStore.open();
|
|
@@ -146,8 +162,9 @@ export class Dwn {
|
|
|
146
162
|
await this.stateIndex.close();
|
|
147
163
|
|
|
148
164
|
// Close the resolver's cache if the DWN owns it.
|
|
149
|
-
|
|
150
|
-
|
|
165
|
+
const lifecycleResolver = this.didResolver as Partial<LifecycleResolver>;
|
|
166
|
+
if (this.ownsResolver && typeof lifecycleResolver.close === 'function') {
|
|
167
|
+
await lifecycleResolver.close();
|
|
151
168
|
}
|
|
152
169
|
}
|
|
153
170
|
|
|
@@ -5,7 +5,7 @@ import { authenticate } from '../core/auth.js';
|
|
|
5
5
|
import { DwnErrorCode } from '../core/dwn-error.js';
|
|
6
6
|
import { messageReplyFromError } from '../core/message-reply.js';
|
|
7
7
|
import { ProtocolsQuery } from '../interfaces/protocols-query.js';
|
|
8
|
-
import { removeUndefinedProperties } from '
|
|
8
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
9
9
|
|
|
10
10
|
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
|
|
11
11
|
|
|
@@ -5,7 +5,7 @@ import type { MessagesSubscribeDescriptor, MessagesSubscribeMessage } from '../t
|
|
|
5
5
|
|
|
6
6
|
import { AbstractMessage } from '../core/abstract-message.js';
|
|
7
7
|
import { Message } from '../core/message.js';
|
|
8
|
-
import { removeUndefinedProperties } from '
|
|
8
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
9
9
|
import { Time } from '../utils/time.js';
|
|
10
10
|
import { validateProtocolUrlNormalized } from '../utils/url.js';
|
|
11
11
|
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
|
|
@@ -3,7 +3,7 @@ import type { MessagesSyncAction, MessagesSyncDescriptor, MessagesSyncMessage }
|
|
|
3
3
|
|
|
4
4
|
import { AbstractMessage } from '../core/abstract-message.js';
|
|
5
5
|
import { Message } from '../core/message.js';
|
|
6
|
-
import { removeUndefinedProperties } from '
|
|
6
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
7
7
|
import { Time } from '../utils/time.js';
|
|
8
8
|
import { validateProtocolUrlNormalized } from '../utils/url.js';
|
|
9
9
|
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
|
|
@@ -7,7 +7,7 @@ import { AbstractMessage } from '../core/abstract-message.js';
|
|
|
7
7
|
import { Message } from '../core/message.js';
|
|
8
8
|
import { PermissionsProtocol } from '../protocols/permissions.js';
|
|
9
9
|
import { ProtocolsGrantAuthorization } from '../core/protocols-grant-authorization.js';
|
|
10
|
-
import { removeUndefinedProperties } from '
|
|
10
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
11
11
|
import { Time } from '../utils/time.js';
|
|
12
12
|
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
|
13
13
|
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
|
|
@@ -7,7 +7,7 @@ import { Message } from '../core/message.js';
|
|
|
7
7
|
import { PermissionGrant } from '../protocols/permission-grant.js';
|
|
8
8
|
import { Records } from '../utils/records.js';
|
|
9
9
|
import { RecordsGrantAuthorization } from '../core/records-grant-authorization.js';
|
|
10
|
-
import { removeUndefinedProperties } from '
|
|
10
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
11
11
|
import { Time } from '../utils/time.js';
|
|
12
12
|
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
|
13
13
|
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
|
|
@@ -8,7 +8,7 @@ import { Message } from '../core/message.js';
|
|
|
8
8
|
import { PermissionGrant } from '../protocols/permission-grant.js';
|
|
9
9
|
import { Records } from '../utils/records.js';
|
|
10
10
|
import { RecordsGrantAuthorization } from '../core/records-grant-authorization.js';
|
|
11
|
-
import { removeUndefinedProperties } from '
|
|
11
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
12
12
|
import { Time } from '../utils/time.js';
|
|
13
13
|
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
|
|
14
14
|
|
|
@@ -9,7 +9,7 @@ import { Message } from '../core/message.js';
|
|
|
9
9
|
import { PermissionGrant } from '../protocols/permission-grant.js';
|
|
10
10
|
import { Records } from '../utils/records.js';
|
|
11
11
|
import { RecordsGrantAuthorization } from '../core/records-grant-authorization.js';
|
|
12
|
-
import { removeUndefinedProperties } from '
|
|
12
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
13
13
|
import { Time } from '../utils/time.js';
|
|
14
14
|
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
|
15
15
|
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
|
|
@@ -8,7 +8,7 @@ import { Message } from '../core/message.js';
|
|
|
8
8
|
import { PermissionGrant } from '../protocols/permission-grant.js';
|
|
9
9
|
import { Records } from '../utils/records.js';
|
|
10
10
|
import { RecordsGrantAuthorization } from '../core/records-grant-authorization.js';
|
|
11
|
-
import { removeUndefinedProperties } from '
|
|
11
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
12
12
|
import { Time } from '../utils/time.js';
|
|
13
13
|
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
|
14
14
|
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
|
|
@@ -9,7 +9,7 @@ import { Message } from '../core/message.js';
|
|
|
9
9
|
import { PermissionGrant } from '../protocols/permission-grant.js';
|
|
10
10
|
import { Records } from '../utils/records.js';
|
|
11
11
|
import { RecordsGrantAuthorization } from '../core/records-grant-authorization.js';
|
|
12
|
-
import { removeUndefinedProperties } from '
|
|
12
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
13
13
|
import { Time } from '../utils/time.js';
|
|
14
14
|
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
|
15
15
|
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
|
|
@@ -8,7 +8,7 @@ import { Encoder } from '../utils/encoder.js';
|
|
|
8
8
|
import { Encryption } from '../utils/encryption.js';
|
|
9
9
|
import { GeneralJwsBuilder } from '../jose/jws/general/builder.js';
|
|
10
10
|
import { Jws } from '../utils/jws.js';
|
|
11
|
-
import { removeUndefinedProperties } from '
|
|
11
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
12
12
|
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -21,7 +21,7 @@ import { Message } from '../core/message.js';
|
|
|
21
21
|
import { PermissionGrant } from '../protocols/permission-grant.js';
|
|
22
22
|
import { Records } from '../utils/records.js';
|
|
23
23
|
import { RecordsGrantAuthorization } from '../core/records-grant-authorization.js';
|
|
24
|
-
import { removeUndefinedProperties } from '
|
|
24
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
25
25
|
import { Time } from '../utils/time.js';
|
|
26
26
|
import {
|
|
27
27
|
createAttestation,
|
|
@@ -225,7 +225,7 @@ export class RecordsWrite implements MessageInterface<RecordsWriteMessage> {
|
|
|
225
225
|
this._message = message;
|
|
226
226
|
|
|
227
227
|
if (message.authorization !== undefined) {
|
|
228
|
-
this._author = Message.getAuthor(message
|
|
228
|
+
this._author = Message.getAuthor(message);
|
|
229
229
|
|
|
230
230
|
this._signaturePayload = Jws.decodePlainObjectPayload(message.authorization.signature);
|
|
231
231
|
|
package/src/store/index-level.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { EqualFilter, Filter, KeyValues, PaginationCursor, QueryOptions, RangeFilter } from '../types/query-types.js';
|
|
2
2
|
import type { LevelWrapperBatchOperation, LevelWrapperIteratorOptions, } from './level-wrapper.js';
|
|
3
3
|
|
|
4
|
-
import { isEmptyObject } from '
|
|
4
|
+
import { isEmptyObject } from '@enbox/common';
|
|
5
5
|
import { lexicographicalCompare } from '../utils/string.js';
|
|
6
6
|
import { SortDirection } from '../types/query-types.js';
|
|
7
7
|
import {
|
package/src/utils/messages.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { MessagesFilter } from '../types/messages-types.js';
|
|
|
4
4
|
|
|
5
5
|
import { FilterUtility } from './filter.js';
|
|
6
6
|
import { normalizeProtocolUrl } from './url.js';
|
|
7
|
-
import { isEmptyObject, removeUndefinedProperties } from '
|
|
7
|
+
import { isEmptyObject, removeUndefinedProperties } from '@enbox/common';
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
/**
|
package/src/utils/records.ts
CHANGED
|
@@ -12,7 +12,7 @@ import { FilterUtility } from './filter.js';
|
|
|
12
12
|
import { Jws } from './jws.js';
|
|
13
13
|
import { Message } from '../core/message.js';
|
|
14
14
|
import { PermissionGrant } from '../protocols/permission-grant.js';
|
|
15
|
-
import { removeUndefinedProperties } from '
|
|
15
|
+
import { removeUndefinedProperties } from '@enbox/common';
|
|
16
16
|
import { SortDirection } from '../types/query-types.js';
|
|
17
17
|
import { X25519 } from '@enbox/crypto';
|
|
18
18
|
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
package/src/utils/time.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sleep } from '@enbox/common';
|
|
1
2
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
3
|
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
|
3
4
|
|
|
@@ -5,21 +6,15 @@ import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
|
|
5
6
|
* Time related utilities.
|
|
6
7
|
*/
|
|
7
8
|
export class Time {
|
|
8
|
-
/**
|
|
9
|
-
* sleeps for the desired duration
|
|
10
|
-
* @param durationInMillisecond the desired amount of sleep time
|
|
11
|
-
* @returns when the provided duration has passed
|
|
12
|
-
*/
|
|
13
|
-
public static async sleep(durationInMillisecond: number): Promise<void> {
|
|
14
|
-
return new Promise(resolve => setTimeout(resolve, durationInMillisecond));
|
|
15
|
-
}
|
|
16
|
-
|
|
17
9
|
/**
|
|
18
10
|
* We must sleep for at least 2ms to avoid timestamp collisions during testing.
|
|
19
11
|
* https://github.com/enboxorg/enbox/issues/481
|
|
12
|
+
*
|
|
13
|
+
* For arbitrary-duration sleeps, use `sleep` from `@enbox/common`
|
|
14
|
+
* directly — this class only retains the DWN-specific minimum.
|
|
20
15
|
*/
|
|
21
16
|
public static async minimalSleep(): Promise<void> {
|
|
22
|
-
await
|
|
17
|
+
await sleep(2);
|
|
23
18
|
}
|
|
24
19
|
|
|
25
20
|
/**
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks whether the given object has any properties.
|
|
3
|
-
*/
|
|
4
|
-
export function isEmptyObject(obj) {
|
|
5
|
-
if (typeof (obj) !== 'object') {
|
|
6
|
-
return false;
|
|
7
|
-
}
|
|
8
|
-
return Object.keys(obj).length === 0;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Recursively removes all properties with an empty object or array as its value from the given object.
|
|
12
|
-
*/
|
|
13
|
-
export function removeEmptyObjects(obj) {
|
|
14
|
-
Object.keys(obj).forEach(key => {
|
|
15
|
-
if (typeof (obj[key]) === 'object') {
|
|
16
|
-
// recursive remove empty object or array properties in nested objects
|
|
17
|
-
removeEmptyObjects(obj[key]);
|
|
18
|
-
}
|
|
19
|
-
if (isEmptyObject(obj[key])) {
|
|
20
|
-
delete obj[key];
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Recursively removes all properties with `undefined` as its value from the given object.
|
|
26
|
-
*/
|
|
27
|
-
export function removeUndefinedProperties(obj) {
|
|
28
|
-
Object.keys(obj).forEach(key => {
|
|
29
|
-
if (obj[key] === undefined) {
|
|
30
|
-
delete obj[key];
|
|
31
|
-
}
|
|
32
|
-
else if (typeof (obj[key]) === 'object') {
|
|
33
|
-
removeUndefinedProperties(obj[key]); // recursive remove `undefined` properties in nested objects
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
//# sourceMappingURL=object.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"object.js","sourceRoot":"","sources":["../../../../src/utils/object.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,GAAY;IACxC,IAAI,OAAM,CAAC,GAAG,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,MAAM,CAAC,IAAI,CAAC,GAAa,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;AACjD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAA4B;IAC7D,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAC7B,IAAI,OAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC;YAClC,sEAAsE;YACtE,kBAAkB,CAAC,GAAG,CAAC,GAAG,CAA4B,CAAC,CAAC;QAC1D,CAAC;QAED,IAAI,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;YAC5B,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,yBAAyB,CAAC,GAA4B;IACpE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAC7B,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE,CAAC;YAC3B,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,OAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC;YACzC,yBAAyB,CAAC,GAAG,CAAC,GAAG,CAA4B,CAAC,CAAC,CAAC,4DAA4D;QAC9H,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { removeEmptyObjects, removeUndefinedProperties } from '../../src/utils/object.js';
|
|
3
|
-
describe('Object', () => {
|
|
4
|
-
describe('removeUndefinedProperties', () => {
|
|
5
|
-
it('should remove all `undefined` properties of a nested object', () => {
|
|
6
|
-
const mockObject = {
|
|
7
|
-
a: true,
|
|
8
|
-
b: undefined,
|
|
9
|
-
c: {
|
|
10
|
-
a: 0,
|
|
11
|
-
b: undefined,
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
const expectedResult = {
|
|
15
|
-
a: true,
|
|
16
|
-
c: {
|
|
17
|
-
a: 0
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
removeUndefinedProperties(mockObject);
|
|
21
|
-
expect(mockObject).toEqual(expectedResult);
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
describe('removeEmptyObjects', () => {
|
|
25
|
-
it('should remove all empty objects', () => {
|
|
26
|
-
const obj = {
|
|
27
|
-
foo: {},
|
|
28
|
-
bar: { baz: {} },
|
|
29
|
-
buzz: 'hello'
|
|
30
|
-
};
|
|
31
|
-
removeEmptyObjects(obj);
|
|
32
|
-
expect(obj).toEqual({ buzz: 'hello' });
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
//# sourceMappingURL=object.spec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"object.spec.js","sourceRoot":"","sources":["../../../../tests/utils/object.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAE1F,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,QAAQ,CAAC,2BAA2B,EAAE,GAAG,EAAE;QACzC,EAAE,CAAC,6DAA6D,EAAE,GAAG,EAAE;YACrE,MAAM,UAAU,GAAG;gBACjB,CAAC,EAAG,IAAI;gBACR,CAAC,EAAG,SAAS;gBACb,CAAC,EAAG;oBACF,CAAC,EAAG,CAAC;oBACL,CAAC,EAAG,SAAS;iBACd;aACF,CAAC;YACF,MAAM,cAAc,GAAG;gBACrB,CAAC,EAAG,IAAI;gBACR,CAAC,EAAG;oBACF,CAAC,EAAE,CAAC;iBACL;aACF,CAAC;YAEF,yBAAyB,CAAC,UAAU,CAAC,CAAC;YAEtC,MAAM,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;QAC7C,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,oBAAoB,EAAE,GAAG,EAAE;QAClC,EAAE,CAAC,iCAAiC,EAAE,GAAG,EAAE;YACzC,MAAM,GAAG,GAAG;gBACV,GAAG,EAAI,EAAE;gBACT,GAAG,EAAI,EAAE,GAAG,EAAE,EAAE,EAAE;gBAClB,IAAI,EAAG,OAAO;aACf,CAAC;YACF,kBAAkB,CAAC,GAAG,CAAC,CAAC;YAExB,MAAM,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;QACzC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks whether the given object has any properties.
|
|
3
|
-
*/
|
|
4
|
-
export declare function isEmptyObject(obj: unknown): boolean;
|
|
5
|
-
/**
|
|
6
|
-
* Recursively removes all properties with an empty object or array as its value from the given object.
|
|
7
|
-
*/
|
|
8
|
-
export declare function removeEmptyObjects(obj: Record<string, unknown>): void;
|
|
9
|
-
/**
|
|
10
|
-
* Recursively removes all properties with `undefined` as its value from the given object.
|
|
11
|
-
*/
|
|
12
|
-
export declare function removeUndefinedProperties(obj: Record<string, unknown>): void;
|
|
13
|
-
//# sourceMappingURL=object.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../../../src/utils/object.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAMnD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAWrE;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAQ5E"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"object.spec.d.ts","sourceRoot":"","sources":["../../../../tests/utils/object.spec.ts"],"names":[],"mappings":""}
|
package/src/utils/object.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks whether the given object has any properties.
|
|
3
|
-
*/
|
|
4
|
-
export function isEmptyObject(obj: unknown): boolean {
|
|
5
|
-
if (typeof(obj) !== 'object') {
|
|
6
|
-
return false;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
return Object.keys(obj as object).length === 0;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Recursively removes all properties with an empty object or array as its value from the given object.
|
|
14
|
-
*/
|
|
15
|
-
export function removeEmptyObjects(obj: Record<string, unknown>): void {
|
|
16
|
-
Object.keys(obj).forEach(key => {
|
|
17
|
-
if (typeof(obj[key]) === 'object') {
|
|
18
|
-
// recursive remove empty object or array properties in nested objects
|
|
19
|
-
removeEmptyObjects(obj[key] as Record<string, unknown>);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (isEmptyObject(obj[key])) {
|
|
23
|
-
delete obj[key];
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Recursively removes all properties with `undefined` as its value from the given object.
|
|
30
|
-
*/
|
|
31
|
-
export function removeUndefinedProperties(obj: Record<string, unknown>): void {
|
|
32
|
-
Object.keys(obj).forEach(key => {
|
|
33
|
-
if (obj[key] === undefined) {
|
|
34
|
-
delete obj[key];
|
|
35
|
-
} else if (typeof(obj[key]) === 'object') {
|
|
36
|
-
removeUndefinedProperties(obj[key] as Record<string, unknown>); // recursive remove `undefined` properties in nested objects
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}
|