lzma1 0.0.7 → 0.1.0

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/lib/index.d.ts CHANGED
@@ -3,4 +3,4 @@
3
3
  * Copyright Filip Seman
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- export { compress, decompress } from "./lzma.js";
6
+ export { compress, compressString, decompress, decompressString, LZMA } from "./lzma.js";
package/lib/index.js CHANGED
@@ -3,4 +3,4 @@
3
3
  * Copyright Filip Seman
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- export { compress, decompress } from "./lzma.js";
6
+ export { compress, compressString, decompress, decompressString, LZMA } from "./lzma.js";
package/lib/lzma.d.ts CHANGED
@@ -74,22 +74,39 @@ export declare class LZMA {
74
74
  ReverseGetPrice(Models: number[], startIndex: number, NumBitLevels: number, symbol: number): number;
75
75
  InitBitModels(probs: number[]): void;
76
76
  GetPrice(Prob: number, symbol: number): number;
77
- encode(inputString: string | Uint8Array): number[] | Uint8Array;
78
- compress(data: string | Uint8Array | ArrayBuffer, mode?: keyof typeof this.CompressionModes): Int8Array;
79
- decompress(bytearray: Uint8Array | ArrayBuffer): Int8Array | string;
77
+ encodeString(inputString: string): number[];
78
+ compress(data: Uint8Array | ArrayBuffer, mode?: keyof typeof this.CompressionModes): number[];
79
+ compressString(data: string, mode?: keyof typeof this.CompressionModes): number[];
80
+ decompress(bytearray: Uint8Array | ArrayBuffer): number[];
81
+ decompressString(bytearray: Uint8Array | ArrayBuffer): string;
80
82
  }
81
83
  /**
82
84
  * Compresses data using LZMA algorithm
83
85
  *
84
- * @param data Data to compress - can be string, Uint8Array or ArrayBuffer
86
+ * @param data Data to compress - can be Uint8Array or ArrayBuffer
85
87
  * @param mode Compression mode (1-9), defaults to 5
86
- * @returns Compressed data as Int8Array
88
+ * @returns Compressed data as a byte array
87
89
  */
88
- export declare function compress(data: string | Uint8Array | ArrayBuffer, mode?: keyof LZMA["CompressionModes"]): Int8Array;
90
+ export declare function compress(data: Uint8Array | ArrayBuffer, mode?: keyof LZMA["CompressionModes"]): Uint8Array;
91
+ /**
92
+ * Compresses data using LZMA algorithm
93
+ *
94
+ * @param data String to compress
95
+ * @param mode Compression mode (1-9), defaults to 5
96
+ * @returns Compressed data as byte array
97
+ */
98
+ export declare function compressString(data: string, mode?: keyof LZMA["CompressionModes"]): Uint8Array;
99
+ /**
100
+ * Decompresses LZMA compressed data
101
+ *
102
+ * @param data Compressed data as Uint8Array or ArrayBuffer
103
+ * @returns Decompressed data
104
+ */
105
+ export declare function decompress(data: Uint8Array | ArrayBuffer): Uint8Array;
89
106
  /**
90
107
  * Decompresses LZMA compressed data
91
108
  *
92
109
  * @param data Compressed data as Uint8Array or ArrayBuffer
93
- * @returns Decompressed data as string if input was string, or Int8Array if input was binary
110
+ * @returns Decompressed data as string
94
111
  */
95
- export declare function decompress(data: Uint8Array | ArrayBuffer): string | Int8Array;
112
+ export declare function decompressString(data: Uint8Array | ArrayBuffer): string;
package/lib/lzma.js CHANGED
@@ -2485,7 +2485,7 @@ export class LZMA {
2485
2485
  GetPrice(Prob, symbol) {
2486
2486
  return this.#probPrices[((Prob - symbol ^ -symbol) & 0x7FF) >>> 2];
2487
2487
  }
2488
- #decode(utf) {
2488
+ #decodeString(utf) {
2489
2489
  let j = 0, x, y, z, l = utf.length, buf = [], charCodes = [];
2490
2490
  for (let i = 0; i < l; ++i, ++j) {
2491
2491
  x = utf[i] & 0xFF;
@@ -2547,15 +2547,9 @@ export class LZMA {
2547
2547
  }
2548
2548
  return buf.join("");
2549
2549
  }
2550
- encode(inputString) {
2550
+ encodeString(inputString) {
2551
2551
  let ch, chars = [], elen = 0, l = inputString.length;
2552
- // Be able to handle binary arrays and buffers.
2553
- if (typeof inputString === "object") {
2554
- return inputString;
2555
- }
2556
- else {
2557
- this.#getChars(inputString, 0, l, chars, 0);
2558
- }
2552
+ this.#getChars(inputString, 0, l, chars, 0);
2559
2553
  // Add extra spaces in the array to break up the unicode symbols.
2560
2554
  for (let i = 0; i < l; ++i) {
2561
2555
  ch = chars[i];
@@ -2589,43 +2583,72 @@ export class LZMA {
2589
2583
  return data;
2590
2584
  }
2591
2585
  compress(data, mode = 5) {
2592
- const encodedData = this.encode(data);
2593
2586
  const compressionMode = this.CompressionModes[mode];
2594
- this.#byteArrayCompressor(encodedData, compressionMode);
2587
+ this.#byteArrayCompressor(data, compressionMode);
2595
2588
  while (this.#processChunkEncode())
2596
2589
  ;
2597
2590
  const compressedByteArray = this.#toByteArray(this.#compressor.output);
2598
- return new Int8Array(compressedByteArray);
2591
+ return compressedByteArray;
2592
+ }
2593
+ compressString(data, mode = 5) {
2594
+ const encodedData = this.encodeString(data);
2595
+ return compress(encodedData, mode);
2599
2596
  }
2600
2597
  decompress(bytearray) {
2601
2598
  this.#byteArrayDecompressor(bytearray);
2602
2599
  while (this.#processChunkDecode())
2603
2600
  ;
2604
2601
  const decodedByteArray = this.#toByteArray(this.#decompressor.output);
2605
- const decoded = this.#decode(decodedByteArray);
2606
- return decoded instanceof Array
2607
- ? new Int8Array(decoded)
2608
- : decoded;
2602
+ return decodedByteArray;
2603
+ }
2604
+ decompressString(bytearray) {
2605
+ this.#byteArrayDecompressor(bytearray);
2606
+ while (this.#processChunkDecode())
2607
+ ;
2608
+ const decodedByteArray = this.#toByteArray(this.#decompressor.output);
2609
+ const decoded = this.#decodeString(decodedByteArray);
2610
+ return decoded;
2609
2611
  }
2610
2612
  }
2611
2613
  /**
2612
2614
  * Compresses data using LZMA algorithm
2613
2615
  *
2614
- * @param data Data to compress - can be string, Uint8Array or ArrayBuffer
2616
+ * @param data Data to compress - can be Uint8Array or ArrayBuffer
2615
2617
  * @param mode Compression mode (1-9), defaults to 5
2616
- * @returns Compressed data as Int8Array
2618
+ * @returns Compressed data as a byte array
2617
2619
  */
2618
2620
  export function compress(data, mode = 5) {
2619
2621
  const lzma = new LZMA();
2620
- return lzma.compress(data, mode);
2622
+ return new Uint8Array(lzma.compress(data, mode));
2623
+ }
2624
+ /**
2625
+ * Compresses data using LZMA algorithm
2626
+ *
2627
+ * @param data String to compress
2628
+ * @param mode Compression mode (1-9), defaults to 5
2629
+ * @returns Compressed data as byte array
2630
+ */
2631
+ export function compressString(data, mode = 5) {
2632
+ const lzma = new LZMA();
2633
+ return lzma.compressString(data, mode);
2621
2634
  }
2622
2635
  /**
2623
2636
  * Decompresses LZMA compressed data
2624
2637
  *
2625
2638
  * @param data Compressed data as Uint8Array or ArrayBuffer
2626
- * @returns Decompressed data as string if input was string, or Int8Array if input was binary
2639
+ * @returns Decompressed data
2627
2640
  */
2628
2641
  export function decompress(data) {
2629
2642
  const lzma = new LZMA();
2630
- return lzma.decompress(data);
2643
+ return new Uint8Array(lzma.decompress(data));
2644
+ }
2645
+ /**
2646
+ * Decompresses LZMA compressed data
2647
+ *
2648
+ * @param data Compressed data as Uint8Array or ArrayBuffer
2649
+ * @returns Decompressed data as string
2650
+ */
2651
+ export function decompressString(data) {
2652
+ const lzma = new LZMA();
2653
+ return lzma.decompressString(data);
2631
2654
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "lzma1",
3
3
  "type": "module",
4
- "version": "0.0.7",
4
+ "version": "0.1.0",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Filip Seman <filip.seman@pm.me>",
7
7
  "description": "A JavaScript implementation of the Lempel-Ziv-Markov (LZMA) chain compression algorithm",