@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.
@@ -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 cs = new CompressionStream(type);
3991
- const writer = cs.writable.getWriter();
3992
- await writer.write(bytes);
3993
- await writer.close();
3994
- const reader = cs.readable.getReader();
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" && concatenated.length >= 10) {
4001
- concatenated[9] = 3;
4021
+ if (type === "gzip" && compressedBytes.length >= 10) {
4022
+ compressedBytes[9] = 3;
4002
4023
  }
4003
- return concatenated;
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 cs = new DecompressionStream(type);
4015
- const writer = cs.writable.getWriter();
4016
- await writer.write(compressedBytes);
4017
- await writer.close();
4018
- const reader = cs.readable.getReader();
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
 
@@ -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 cs = new CompressionStream(type);
3989
- const writer = cs.writable.getWriter();
3990
- await writer.write(bytes);
3991
- await writer.close();
3992
- const reader = cs.readable.getReader();
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" && concatenated.length >= 10) {
3999
- concatenated[9] = 3;
4019
+ if (type === "gzip" && compressedBytes.length >= 10) {
4020
+ compressedBytes[9] = 3;
4000
4021
  }
4001
- return concatenated;
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 cs = new DecompressionStream(type);
4013
- const writer = cs.writable.getWriter();
4014
- await writer.write(compressedBytes);
4015
- await writer.close();
4016
- const reader = cs.readable.getReader();
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
@@ -1,5 +1,5 @@
1
1
  # @twin.org/core - Changelog
2
2
 
3
- ## 0.0.1-next.33
3
+ ## 0.0.1-next.35
4
4
 
5
5
  - Initial Release
@@ -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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.0.1-next.33",
3
+ "version": "0.0.1-next.35",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",