@twin.org/core 0.0.1-next.33 → 0.0.1-next.35
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/index.cjs +36 -35
- package/dist/esm/index.mjs +36 -35
- package/dist/types/helpers/objectHelper.d.ts +12 -0
- package/docs/changelog.md +1 -1
- package/docs/reference/classes/ObjectHelper.md +42 -0
- package/package.json +1 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -2767,6 +2767,29 @@ class ObjectHelper {
|
|
|
2767
2767
|
const jsonExtended = JsonHelper.stringifyEx(obj);
|
|
2768
2768
|
return JsonHelper.parseEx(jsonExtended);
|
|
2769
2769
|
}
|
|
2770
|
+
/**
|
|
2771
|
+
* Remove empty properties from an object.
|
|
2772
|
+
* @param obj The object to remove the empty properties from.
|
|
2773
|
+
* @param options The options for the removal.
|
|
2774
|
+
* @param options.removeUndefined Remove undefined properties, defaults to true.
|
|
2775
|
+
* @param options.removeNull Remove null properties, defaults to false.
|
|
2776
|
+
* @returns The object with empty properties removed.
|
|
2777
|
+
*/
|
|
2778
|
+
static removeEmptyProperties(obj, options) {
|
|
2779
|
+
if (Is.object(obj)) {
|
|
2780
|
+
const removeUndefined = options?.removeUndefined ?? true;
|
|
2781
|
+
const removeNull = options?.removeNull ?? false;
|
|
2782
|
+
const newObj = {};
|
|
2783
|
+
const keys = Object.keys(obj);
|
|
2784
|
+
for (const key of keys) {
|
|
2785
|
+
if (!((removeUndefined && Is.undefined(obj[key])) || (removeNull && Is.null(obj[key])))) {
|
|
2786
|
+
newObj[key] = ObjectHelper.removeEmptyProperties(obj[key], options);
|
|
2787
|
+
}
|
|
2788
|
+
}
|
|
2789
|
+
return newObj;
|
|
2790
|
+
}
|
|
2791
|
+
return obj;
|
|
2792
|
+
}
|
|
2770
2793
|
}
|
|
2771
2794
|
|
|
2772
2795
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -3987,20 +4010,18 @@ class Compression {
|
|
|
3987
4010
|
static async compress(bytes, type) {
|
|
3988
4011
|
Guards.uint8Array(Compression._CLASS_NAME, "bytes", bytes);
|
|
3989
4012
|
Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
|
|
3990
|
-
const
|
|
3991
|
-
const
|
|
3992
|
-
|
|
3993
|
-
await
|
|
3994
|
-
const
|
|
3995
|
-
const chunks = await Compression.streamToChunks(reader);
|
|
3996
|
-
const concatenated = Uint8ArrayHelper.concat(chunks);
|
|
4013
|
+
const blob = new Blob([bytes]);
|
|
4014
|
+
const compressionStream = new CompressionStream(type);
|
|
4015
|
+
const compressionPipe = blob.stream().pipeThrough(compressionStream);
|
|
4016
|
+
const compressedBlob = await new Response(compressionPipe).blob();
|
|
4017
|
+
const compressedBytes = await compressedBlob.bytes();
|
|
3997
4018
|
// GZIP header contains a byte which specifies the OS the
|
|
3998
4019
|
// compression was performed on. We set this to 3 (Unix) to ensure
|
|
3999
4020
|
// that we produce consistent results.
|
|
4000
|
-
if (type === "gzip" &&
|
|
4001
|
-
|
|
4021
|
+
if (type === "gzip" && compressedBytes.length >= 10) {
|
|
4022
|
+
compressedBytes[9] = 3;
|
|
4002
4023
|
}
|
|
4003
|
-
return
|
|
4024
|
+
return compressedBytes;
|
|
4004
4025
|
}
|
|
4005
4026
|
/**
|
|
4006
4027
|
* Decompress a gzipped compressed byte array.
|
|
@@ -4011,31 +4032,11 @@ class Compression {
|
|
|
4011
4032
|
static async decompress(compressedBytes, type) {
|
|
4012
4033
|
Guards.uint8Array(Compression._CLASS_NAME, "compressedBytes", compressedBytes);
|
|
4013
4034
|
Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
|
|
4014
|
-
const
|
|
4015
|
-
const
|
|
4016
|
-
|
|
4017
|
-
await
|
|
4018
|
-
|
|
4019
|
-
const chunks = await Compression.streamToChunks(reader);
|
|
4020
|
-
return Uint8ArrayHelper.concat(chunks);
|
|
4021
|
-
}
|
|
4022
|
-
/**
|
|
4023
|
-
* Read the stream and create a list of chunks.
|
|
4024
|
-
* @param reader The reader to read the chunks from.
|
|
4025
|
-
* @returns The chunks.
|
|
4026
|
-
* @internal
|
|
4027
|
-
*/
|
|
4028
|
-
static async streamToChunks(reader) {
|
|
4029
|
-
const chunks = [];
|
|
4030
|
-
let done = false;
|
|
4031
|
-
do {
|
|
4032
|
-
const chunk = await reader.read();
|
|
4033
|
-
done = chunk.done;
|
|
4034
|
-
if (!done && Is.uint8Array(chunk.value)) {
|
|
4035
|
-
chunks.push(chunk.value);
|
|
4036
|
-
}
|
|
4037
|
-
} while (!done);
|
|
4038
|
-
return chunks;
|
|
4035
|
+
const blob = new Blob([compressedBytes]);
|
|
4036
|
+
const decompressionStream = new DecompressionStream(type);
|
|
4037
|
+
const decompressionPipe = blob.stream().pipeThrough(decompressionStream);
|
|
4038
|
+
const decompressedBlob = await new Response(decompressionPipe).blob();
|
|
4039
|
+
return decompressedBlob.bytes();
|
|
4039
4040
|
}
|
|
4040
4041
|
}
|
|
4041
4042
|
|
package/dist/esm/index.mjs
CHANGED
|
@@ -2765,6 +2765,29 @@ class ObjectHelper {
|
|
|
2765
2765
|
const jsonExtended = JsonHelper.stringifyEx(obj);
|
|
2766
2766
|
return JsonHelper.parseEx(jsonExtended);
|
|
2767
2767
|
}
|
|
2768
|
+
/**
|
|
2769
|
+
* Remove empty properties from an object.
|
|
2770
|
+
* @param obj The object to remove the empty properties from.
|
|
2771
|
+
* @param options The options for the removal.
|
|
2772
|
+
* @param options.removeUndefined Remove undefined properties, defaults to true.
|
|
2773
|
+
* @param options.removeNull Remove null properties, defaults to false.
|
|
2774
|
+
* @returns The object with empty properties removed.
|
|
2775
|
+
*/
|
|
2776
|
+
static removeEmptyProperties(obj, options) {
|
|
2777
|
+
if (Is.object(obj)) {
|
|
2778
|
+
const removeUndefined = options?.removeUndefined ?? true;
|
|
2779
|
+
const removeNull = options?.removeNull ?? false;
|
|
2780
|
+
const newObj = {};
|
|
2781
|
+
const keys = Object.keys(obj);
|
|
2782
|
+
for (const key of keys) {
|
|
2783
|
+
if (!((removeUndefined && Is.undefined(obj[key])) || (removeNull && Is.null(obj[key])))) {
|
|
2784
|
+
newObj[key] = ObjectHelper.removeEmptyProperties(obj[key], options);
|
|
2785
|
+
}
|
|
2786
|
+
}
|
|
2787
|
+
return newObj;
|
|
2788
|
+
}
|
|
2789
|
+
return obj;
|
|
2790
|
+
}
|
|
2768
2791
|
}
|
|
2769
2792
|
|
|
2770
2793
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -3985,20 +4008,18 @@ class Compression {
|
|
|
3985
4008
|
static async compress(bytes, type) {
|
|
3986
4009
|
Guards.uint8Array(Compression._CLASS_NAME, "bytes", bytes);
|
|
3987
4010
|
Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
|
|
3988
|
-
const
|
|
3989
|
-
const
|
|
3990
|
-
|
|
3991
|
-
await
|
|
3992
|
-
const
|
|
3993
|
-
const chunks = await Compression.streamToChunks(reader);
|
|
3994
|
-
const concatenated = Uint8ArrayHelper.concat(chunks);
|
|
4011
|
+
const blob = new Blob([bytes]);
|
|
4012
|
+
const compressionStream = new CompressionStream(type);
|
|
4013
|
+
const compressionPipe = blob.stream().pipeThrough(compressionStream);
|
|
4014
|
+
const compressedBlob = await new Response(compressionPipe).blob();
|
|
4015
|
+
const compressedBytes = await compressedBlob.bytes();
|
|
3995
4016
|
// GZIP header contains a byte which specifies the OS the
|
|
3996
4017
|
// compression was performed on. We set this to 3 (Unix) to ensure
|
|
3997
4018
|
// that we produce consistent results.
|
|
3998
|
-
if (type === "gzip" &&
|
|
3999
|
-
|
|
4019
|
+
if (type === "gzip" && compressedBytes.length >= 10) {
|
|
4020
|
+
compressedBytes[9] = 3;
|
|
4000
4021
|
}
|
|
4001
|
-
return
|
|
4022
|
+
return compressedBytes;
|
|
4002
4023
|
}
|
|
4003
4024
|
/**
|
|
4004
4025
|
* Decompress a gzipped compressed byte array.
|
|
@@ -4009,31 +4030,11 @@ class Compression {
|
|
|
4009
4030
|
static async decompress(compressedBytes, type) {
|
|
4010
4031
|
Guards.uint8Array(Compression._CLASS_NAME, "compressedBytes", compressedBytes);
|
|
4011
4032
|
Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
|
|
4012
|
-
const
|
|
4013
|
-
const
|
|
4014
|
-
|
|
4015
|
-
await
|
|
4016
|
-
|
|
4017
|
-
const chunks = await Compression.streamToChunks(reader);
|
|
4018
|
-
return Uint8ArrayHelper.concat(chunks);
|
|
4019
|
-
}
|
|
4020
|
-
/**
|
|
4021
|
-
* Read the stream and create a list of chunks.
|
|
4022
|
-
* @param reader The reader to read the chunks from.
|
|
4023
|
-
* @returns The chunks.
|
|
4024
|
-
* @internal
|
|
4025
|
-
*/
|
|
4026
|
-
static async streamToChunks(reader) {
|
|
4027
|
-
const chunks = [];
|
|
4028
|
-
let done = false;
|
|
4029
|
-
do {
|
|
4030
|
-
const chunk = await reader.read();
|
|
4031
|
-
done = chunk.done;
|
|
4032
|
-
if (!done && Is.uint8Array(chunk.value)) {
|
|
4033
|
-
chunks.push(chunk.value);
|
|
4034
|
-
}
|
|
4035
|
-
} while (!done);
|
|
4036
|
-
return chunks;
|
|
4033
|
+
const blob = new Blob([compressedBytes]);
|
|
4034
|
+
const decompressionStream = new DecompressionStream(type);
|
|
4035
|
+
const decompressionPipe = blob.stream().pipeThrough(decompressionStream);
|
|
4036
|
+
const decompressedBlob = await new Response(decompressionPipe).blob();
|
|
4037
|
+
return decompressedBlob.bytes();
|
|
4037
4038
|
}
|
|
4038
4039
|
}
|
|
4039
4040
|
|
|
@@ -92,4 +92,16 @@ export declare class ObjectHelper {
|
|
|
92
92
|
* @returns The object with regular properties.
|
|
93
93
|
*/
|
|
94
94
|
static fromExtended(obj: any): any;
|
|
95
|
+
/**
|
|
96
|
+
* Remove empty properties from an object.
|
|
97
|
+
* @param obj The object to remove the empty properties from.
|
|
98
|
+
* @param options The options for the removal.
|
|
99
|
+
* @param options.removeUndefined Remove undefined properties, defaults to true.
|
|
100
|
+
* @param options.removeNull Remove null properties, defaults to false.
|
|
101
|
+
* @returns The object with empty properties removed.
|
|
102
|
+
*/
|
|
103
|
+
static removeEmptyProperties<T = unknown>(obj: T, options?: {
|
|
104
|
+
removeUndefined?: boolean;
|
|
105
|
+
removeNull?: boolean;
|
|
106
|
+
}): T;
|
|
95
107
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -411,3 +411,45 @@ The object to convert.
|
|
|
411
411
|
`any`
|
|
412
412
|
|
|
413
413
|
The object with regular properties.
|
|
414
|
+
|
|
415
|
+
***
|
|
416
|
+
|
|
417
|
+
### removeEmptyProperties()
|
|
418
|
+
|
|
419
|
+
> `static` **removeEmptyProperties**\<`T`\>(`obj`, `options`?): `T`
|
|
420
|
+
|
|
421
|
+
Remove empty properties from an object.
|
|
422
|
+
|
|
423
|
+
#### Type Parameters
|
|
424
|
+
|
|
425
|
+
• **T** = `unknown`
|
|
426
|
+
|
|
427
|
+
#### Parameters
|
|
428
|
+
|
|
429
|
+
##### obj
|
|
430
|
+
|
|
431
|
+
`T`
|
|
432
|
+
|
|
433
|
+
The object to remove the empty properties from.
|
|
434
|
+
|
|
435
|
+
##### options?
|
|
436
|
+
|
|
437
|
+
The options for the removal.
|
|
438
|
+
|
|
439
|
+
###### removeUndefined
|
|
440
|
+
|
|
441
|
+
`boolean`
|
|
442
|
+
|
|
443
|
+
Remove undefined properties, defaults to true.
|
|
444
|
+
|
|
445
|
+
###### removeNull
|
|
446
|
+
|
|
447
|
+
`boolean`
|
|
448
|
+
|
|
449
|
+
Remove null properties, defaults to false.
|
|
450
|
+
|
|
451
|
+
#### Returns
|
|
452
|
+
|
|
453
|
+
`T`
|
|
454
|
+
|
|
455
|
+
The object with empty properties removed.
|