@creejs/commons-lang 2.1.8 → 2.1.9

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.
@@ -153,7 +153,8 @@ var TypeUtils = {
153
153
  isFloat64Array,
154
154
  isBigInt64Array,
155
155
  isBigUint64Array,
156
- isTypedArray
156
+ isTypedArray,
157
+ isArrayBuffer
157
158
  };
158
159
  /**
159
160
  * Checks if the given value is an array.
@@ -483,7 +484,7 @@ function isInt32Array (value) {
483
484
  function isUint32Array (value) {
484
485
  return value instanceof Uint32Array
485
486
  }
486
- // export function isFloat16Array (value) {
487
+ // function isFloat16Array (value) {
487
488
  // return value instanceof Float16Array
488
489
  // }
489
490
  /**
@@ -519,6 +520,15 @@ function isBigUint64Array (value) {
519
520
  return value instanceof BigUint64Array
520
521
  }
521
522
 
523
+ /**
524
+ * Checks if the given value is an ArrayBuffer.
525
+ * @param {*} value - The value to check.
526
+ * @returns {boolean} True if the value is an ArrayBuffer, false otherwise.
527
+ */
528
+ function isArrayBuffer (value) {
529
+ return value instanceof ArrayBuffer
530
+ }
531
+
522
532
  // 3rd
523
533
  // internal
524
534
  // owned
@@ -557,7 +567,8 @@ var TypeAssert = {
557
567
  assertFloat64Array,
558
568
  assertBigInt64Array,
559
569
  assertBigUint64Array,
560
- assertTypedArray
570
+ assertTypedArray,
571
+ assertArrayBuffer
561
572
  };
562
573
  /**
563
574
  * if value is not Array, throw error
@@ -911,6 +922,18 @@ function assertBigUint64Array (value, paramName) {
911
922
  }
912
923
  }
913
924
 
925
+ /**
926
+ * Asserts that the given value is an ArrayBuffer.
927
+ * @param {*} value - The value to check.
928
+ * @param {string} [paramName] - Optional parameter name for error message.
929
+ * @throws {Error} Throws an error if the value is not an ArrayBuffer.
930
+ */
931
+ function assertArrayBuffer (value, paramName) {
932
+ if (!isArrayBuffer(value)) {
933
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not ArrayBuffer`)
934
+ }
935
+ }
936
+
914
937
  // 3rd
915
938
  // internal
916
939
  // owned
@@ -1947,6 +1970,68 @@ function equals (src, target) {
1947
1970
  return true
1948
1971
  }
1949
1972
 
1973
+ var ArrayBufferUtils = {
1974
+ readString,
1975
+ writeString
1976
+ };
1977
+
1978
+ // module vars
1979
+ const textDecoder = new TextDecoder();
1980
+ const textEncoder = new TextEncoder();
1981
+ /**
1982
+ * Reads a string from an ArrayBuffer at the specified offset and length.
1983
+ * 1. if offset >= buffer.byteLength, return undefined
1984
+ * 2. if length is not specified, read all bytes from offset to the end of buffer
1985
+ * 3. if length is specified
1986
+ * * if offset + length >= buffer.byteLength, return a Uint8Array from offset to the end of buffer
1987
+ * * if offset + length < buffer.byteLength, return a Uint8Array from offset to offset + length
1988
+ * @param {ArrayBuffer} buffer - The buffer to read from.
1989
+ * @param {number} [offset=0] - The offset in bytes to start reading from.
1990
+ * @param {number} [length] - The number of bytes to read.
1991
+ * @returns {string|undefined} The decoded string.
1992
+ */
1993
+ function readString (buffer, offset = 0, length) {
1994
+ assertArrayBuffer(buffer);
1995
+ assertNotNegative(offset);
1996
+ if (offset >= buffer.byteLength) {
1997
+ return undefined
1998
+ }
1999
+ let uint8Array = null;
2000
+ if (length != null) {
2001
+ assertPositive(length);
2002
+ if (offset + length >= buffer.byteLength) {
2003
+ uint8Array = new Uint8Array(buffer, offset);
2004
+ } else {
2005
+ uint8Array = new Uint8Array(buffer, offset, length);
2006
+ }
2007
+ } else {
2008
+ uint8Array = new Uint8Array(buffer, offset);
2009
+ }
2010
+
2011
+ // An ArrayBuffer, a TypedArray, or a DataView object containing the encoded text to decode.
2012
+ return textDecoder.decode(uint8Array)
2013
+ }
2014
+
2015
+ /**
2016
+ * Writes a string to an ArrayBuffer at the specified offset.
2017
+ * @param {ArrayBuffer} buffer - The buffer to write to
2018
+ * @param {string} str - The string to write
2019
+ * @param {number} [offset=0] - The offset in bytes to start writing
2020
+ * @returns {void}
2021
+ */
2022
+ function writeString (buffer, str, offset = 0) {
2023
+ assertArrayBuffer(buffer, 'buffer');
2024
+ assertString(str, 'str');
2025
+ assertNotNegative(offset, 'offset');
2026
+ const uint8Array = textEncoder.encode(str);
2027
+ const strByteLength = uint8Array.byteLength;
2028
+ if (offset + strByteLength > buffer.byteLength) {
2029
+ throw new Error(`offset + str.byteLength > buffer.byteLength:${offset + strByteLength} > ${buffer.byteLength}`)
2030
+ }
2031
+ const dataView = new Uint8Array(buffer, offset, uint8Array.byteLength);
2032
+ dataView.set(uint8Array);
2033
+ }
2034
+
1950
2035
  /**
1951
2036
  * @module Lang
1952
2037
  * @description Core language utilities for type checking, string manipulation, and common operations.
@@ -1966,9 +2051,11 @@ var index = {
1966
2051
  ClassProxyUtils,
1967
2052
  InstanceProxyUtils,
1968
2053
  ReflectUtils,
1969
- TypedArrayUtils
2054
+ TypedArrayUtils,
2055
+ ArrayBufferUtils
1970
2056
  };
1971
2057
 
2058
+ exports.ArrayBufferUtils = ArrayBufferUtils;
1972
2059
  exports.ClassProxyUtils = ClassProxyUtils;
1973
2060
  exports.Exec = ExecUtils;
1974
2061
  exports.ExecUtils = ExecUtils;