@creejs/commons-lang 2.1.8 → 2.1.10

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,130 @@ function equals (src, target) {
1947
1970
  return true
1948
1971
  }
1949
1972
 
1973
+ var ArrayBufferUtils = {
1974
+ readString,
1975
+ writeString,
1976
+ writeArrayBuffer
1977
+ };
1978
+
1979
+ // module vars
1980
+ const textDecoder = new TextDecoder();
1981
+ const textEncoder = new TextEncoder();
1982
+ /**
1983
+ * Reads a string from an ArrayBuffer at the specified offset and length.
1984
+ * 1. if offset >= buffer.byteLength, return undefined
1985
+ * 2. if length is not specified, read all bytes from offset to the end of buffer
1986
+ * 3. if length is specified
1987
+ * * if offset + length >= buffer.byteLength, return a Uint8Array from offset to the end of buffer
1988
+ * * if offset + length < buffer.byteLength, return a Uint8Array from offset to offset + length
1989
+ * @param {ArrayBuffer} buffer - The buffer to read from.
1990
+ * @param {number} [offset=0] - The offset in bytes to start reading from.
1991
+ * @param {number} [length] - The number of bytes to read.
1992
+ * @returns {string|undefined} The decoded string.
1993
+ */
1994
+ function readString (buffer, offset = 0, length) {
1995
+ assertArrayBuffer(buffer);
1996
+ assertNotNegative(offset);
1997
+ if (offset >= buffer.byteLength) {
1998
+ return undefined
1999
+ }
2000
+ let uint8Array = null;
2001
+ if (length != null) {
2002
+ assertPositive(length);
2003
+ if (offset + length >= buffer.byteLength) {
2004
+ uint8Array = new Uint8Array(buffer, offset);
2005
+ } else {
2006
+ uint8Array = new Uint8Array(buffer, offset, length);
2007
+ }
2008
+ } else {
2009
+ uint8Array = new Uint8Array(buffer, offset);
2010
+ }
2011
+
2012
+ // An ArrayBuffer, a TypedArray, or a DataView object containing the encoded text to decode.
2013
+ return textDecoder.decode(uint8Array)
2014
+ }
2015
+
2016
+ /**
2017
+ * Writes a string to an ArrayBuffer at the specified offset.
2018
+ * @param {ArrayBuffer} buffer - The buffer to write to
2019
+ * @param {string} str - The string to write
2020
+ * @param {number} [offset=0] - The offset in bytes to start writing
2021
+ * @returns {void}
2022
+ */
2023
+ function writeString (buffer, str, offset = 0) {
2024
+ assertArrayBuffer(buffer, 'buffer');
2025
+ assertString(str, 'str');
2026
+ assertNotNegative(offset, 'offset');
2027
+ const uint8Array = textEncoder.encode(str);
2028
+ const strByteLength = uint8Array.byteLength;
2029
+ if (offset + strByteLength > buffer.byteLength) {
2030
+ throw new Error(`offset + str.byteLength > buffer.byteLength:${offset + strByteLength} > ${buffer.byteLength}`)
2031
+ }
2032
+ const dataView = new Uint8Array(buffer, offset, uint8Array.byteLength);
2033
+ dataView.set(uint8Array);
2034
+ }
2035
+
2036
+ /**
2037
+ * Writes an ArrayBuffer into a target ArrayBuffer at the specified offset.
2038
+ * 1. targetOffset
2039
+ * * Negative index counts back from the end of the buffer
2040
+ * * if -target.length <= targetOffset < 0, start + target.length is used.
2041
+ * * If targetOffset < -buffer.length or targetOffset is omitted, 0 is used.
2042
+ * * If targetOffset >= buffer.length, throw "Out of target Bounds" Error
2043
+ * 2. dataOffset
2044
+ * * Negative index counts back from the end of the buffer
2045
+ * * if -dataArrayBuffer.length <= dataOffset < 0, start + dataArrayBuffer.length is used.
2046
+ * * If dataOffset < -buffer.length or dataOffset is omitted, 0 is used.
2047
+ * * If dataOffset >= buffer.length, throw "Out of data Bounds" Error
2048
+ * 3. dataLength
2049
+ * * if dataLength <= 0, throw "Not Positive" Error
2050
+ * * if dataLength is omitted, read all bytes from dataOffset to the end of dataArrayBuffer
2051
+ * * if dataLength + dataOffset > dataArrayBuffer.length, read all bytes from dataOffset to the end of dataArrayBuffer
2052
+ * @param {ArrayBuffer} target - The target buffer to write into.
2053
+ * @param {ArrayBuffer} dataArrayBuffer - The data ArrayBuffer to write.
2054
+ * @param {number} [targetOffset=0] - Zero-based index at which to start write.
2055
+ * @param {number} [dataOffset=0] - The offset in the dataArrayBuffer
2056
+ * @param {number} [dataLength] - how many bytes extract from dataArrayBuffer
2057
+ * @returns {void}
2058
+ */
2059
+ function writeArrayBuffer (target, dataArrayBuffer, targetOffset = 0, dataOffset = 0, dataLength) {
2060
+ assertArrayBuffer(target);
2061
+ assertArrayBuffer(dataArrayBuffer);
2062
+ assertNumber(targetOffset);
2063
+ assertNumber(dataOffset);
2064
+ const targetLength = target.byteLength;
2065
+ if (targetOffset < -1 * targetLength) { // targetOffset < -buffer.length
2066
+ targetOffset = 0;
2067
+ } else if (targetOffset < 0) { // -target.length <= targetOffset < 0
2068
+ targetOffset += targetLength;
2069
+ } else if (targetOffset >= targetLength) {
2070
+ throw new Error(`Out of target Bounds: targetOffset(${targetOffset}) >= targetLength(${targetLength})`)
2071
+ }
2072
+
2073
+ const dataArrayBufferLength = dataArrayBuffer.byteLength;
2074
+ if (dataOffset < -1 * dataArrayBufferLength) {
2075
+ dataOffset = 0;
2076
+ } else if (dataOffset < 0) {
2077
+ dataOffset += dataArrayBufferLength;
2078
+ } else if (dataOffset >= dataArrayBufferLength) {
2079
+ throw new Error(`Out of data Bounds: dataOffset(${dataOffset}) >= dataArrayBufferLength(${dataArrayBufferLength})`)
2080
+ }
2081
+
2082
+ if (dataLength != null) {
2083
+ assertPositive(dataLength, 'dataLength');
2084
+ // offset+dataLengt out of dataArrayBuffer bounds, treat as no limitation, read to end
2085
+ if (dataOffset + dataLength > dataArrayBufferLength) {
2086
+ dataLength = undefined;
2087
+ }
2088
+ }
2089
+ const dataView = new Uint8Array(dataArrayBuffer, dataOffset, dataLength);
2090
+ if (dataView.byteLength > targetLength - targetOffset) {
2091
+ throw new Error(`Out of target Bounds: from targetOffset(${targetOffset}), No Space to store dataArrayBuffer(${dataOffset}, ${dataLength ?? 'NoLimit'})`)
2092
+ }
2093
+ const targetView = new Uint8Array(target);
2094
+ targetView.set(dataView, targetOffset);
2095
+ }
2096
+
1950
2097
  /**
1951
2098
  * @module Lang
1952
2099
  * @description Core language utilities for type checking, string manipulation, and common operations.
@@ -1966,9 +2113,11 @@ var index = {
1966
2113
  ClassProxyUtils,
1967
2114
  InstanceProxyUtils,
1968
2115
  ReflectUtils,
1969
- TypedArrayUtils
2116
+ TypedArrayUtils,
2117
+ ArrayBufferUtils
1970
2118
  };
1971
2119
 
2120
+ exports.ArrayBufferUtils = ArrayBufferUtils;
1972
2121
  exports.ClassProxyUtils = ClassProxyUtils;
1973
2122
  exports.Exec = ExecUtils;
1974
2123
  exports.ExecUtils = ExecUtils;