@js-ak/excel-toolbox 1.8.2 → 1.8.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.
@@ -17,21 +17,29 @@ const node_buffer_1 = require("node:buffer");
17
17
  * @returns {Buffer} - A new Buffer of exactly `len` bytes containing:
18
18
  * 1. The value's bytes in little-endian order (least significant byte first)
19
19
  * 2. Zero padding in any remaining higher-order bytes
20
- * @throws {RangeError} - If the value requires more bytes than `len` to represent
21
- * (though this is currently not explicitly checked)
20
+ *
21
+ * @throws {RangeError} - If the length is not positive or the value is negative.
22
22
  */
23
23
  function toBytes(value, len) {
24
- // Allocate a new Buffer of the requested length, automatically zero-filled
24
+ if (len <= 0)
25
+ throw new RangeError("Length must be a positive integer");
26
+ if (value < 0)
27
+ throw new RangeError("Negative values are not supported");
28
+ if (!Number.isSafeInteger(value))
29
+ throw new RangeError("Value must be a safe integer");
30
+ // Use BigInt to correctly handle 53-bit values
31
+ let bigint = BigInt(value);
25
32
  const buf = node_buffer_1.Buffer.alloc(len);
26
- // Process each byte position from least significant to most significant
27
33
  for (let i = 0; i < len; i++) {
28
- // Store the least significant byte of the current value
29
- buf[i] = value & 0xff; // Mask to get bottom 8 bits
30
- // Right-shift the value by 8 bits to process the next byte
31
- // Note: This uses unsigned right shift (>>> would be signed)
32
- value >>= 8;
33
- // If the loop completes with value != 0, we've overflowed the buffer length,
34
- // but this isn't currently checked/handled
34
+ // Extract the least significant byte and assign to buffer
35
+ buf[i] = Number(bigint & 0xffn);
36
+ bigint >>= 8n;
37
+ // Stop early if all remaining bits are zero
38
+ if (bigint === 0n)
39
+ break;
35
40
  }
41
+ // If bigint is still non-zero, it means we overflowed the buffer length
42
+ if (bigint > 0n)
43
+ throw new RangeError("Value exceeds the maximum size for the specified length");
36
44
  return buf;
37
45
  }
@@ -14,21 +14,29 @@ import { Buffer } from "node:buffer";
14
14
  * @returns {Buffer} - A new Buffer of exactly `len` bytes containing:
15
15
  * 1. The value's bytes in little-endian order (least significant byte first)
16
16
  * 2. Zero padding in any remaining higher-order bytes
17
- * @throws {RangeError} - If the value requires more bytes than `len` to represent
18
- * (though this is currently not explicitly checked)
17
+ *
18
+ * @throws {RangeError} - If the length is not positive or the value is negative.
19
19
  */
20
20
  export function toBytes(value, len) {
21
- // Allocate a new Buffer of the requested length, automatically zero-filled
21
+ if (len <= 0)
22
+ throw new RangeError("Length must be a positive integer");
23
+ if (value < 0)
24
+ throw new RangeError("Negative values are not supported");
25
+ if (!Number.isSafeInteger(value))
26
+ throw new RangeError("Value must be a safe integer");
27
+ // Use BigInt to correctly handle 53-bit values
28
+ let bigint = BigInt(value);
22
29
  const buf = Buffer.alloc(len);
23
- // Process each byte position from least significant to most significant
24
30
  for (let i = 0; i < len; i++) {
25
- // Store the least significant byte of the current value
26
- buf[i] = value & 0xff; // Mask to get bottom 8 bits
27
- // Right-shift the value by 8 bits to process the next byte
28
- // Note: This uses unsigned right shift (>>> would be signed)
29
- value >>= 8;
30
- // If the loop completes with value != 0, we've overflowed the buffer length,
31
- // but this isn't currently checked/handled
31
+ // Extract the least significant byte and assign to buffer
32
+ buf[i] = Number(bigint & 0xffn);
33
+ bigint >>= 8n;
34
+ // Stop early if all remaining bits are zero
35
+ if (bigint === 0n)
36
+ break;
32
37
  }
38
+ // If bigint is still non-zero, it means we overflowed the buffer length
39
+ if (bigint > 0n)
40
+ throw new RangeError("Value exceeds the maximum size for the specified length");
33
41
  return buf;
34
42
  }
@@ -14,7 +14,7 @@ import { Buffer } from "node:buffer";
14
14
  * @returns {Buffer} - A new Buffer of exactly `len` bytes containing:
15
15
  * 1. The value's bytes in little-endian order (least significant byte first)
16
16
  * 2. Zero padding in any remaining higher-order bytes
17
- * @throws {RangeError} - If the value requires more bytes than `len` to represent
18
- * (though this is currently not explicitly checked)
17
+ *
18
+ * @throws {RangeError} - If the length is not positive or the value is negative.
19
19
  */
20
20
  export declare function toBytes(value: number, len: number): Buffer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@js-ak/excel-toolbox",
3
- "version": "1.8.2",
3
+ "version": "1.8.3",
4
4
  "description": "excel-toolbox",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -44,7 +44,9 @@
44
44
  "lint": "eslint . --ext .ts",
45
45
  "postbuild:esm": "node scripts/write-esm-package.js",
46
46
  "postbuild:cjs": "node scripts/write-cjs-package.js",
47
- "test": "vitest run",
47
+ "test:unit": "vitest run --config vitest.unit.config.js",
48
+ "test:integration": "vitest run --config vitest.integration.config.js",
49
+ "test": "npm run test:unit && npm run test:integration",
48
50
  "test:watch": "vitest",
49
51
  "test:coverage": "vitest run --coverage"
50
52
  },