@creejs/commons-lang 2.1.9 → 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.
@@ -1972,7 +1972,8 @@ function equals (src, target) {
1972
1972
 
1973
1973
  var ArrayBufferUtils = {
1974
1974
  readString,
1975
- writeString
1975
+ writeString,
1976
+ writeArrayBuffer
1976
1977
  };
1977
1978
 
1978
1979
  // module vars
@@ -2032,6 +2033,67 @@ function writeString (buffer, str, offset = 0) {
2032
2033
  dataView.set(uint8Array);
2033
2034
  }
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
+
2035
2097
  /**
2036
2098
  * @module Lang
2037
2099
  * @description Core language utilities for type checking, string manipulation, and common operations.