@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.
@@ -1968,7 +1968,8 @@ function equals (src, target) {
1968
1968
 
1969
1969
  var ArrayBufferUtils = {
1970
1970
  readString,
1971
- writeString
1971
+ writeString,
1972
+ writeArrayBuffer
1972
1973
  };
1973
1974
 
1974
1975
  // module vars
@@ -2028,6 +2029,67 @@ function writeString (buffer, str, offset = 0) {
2028
2029
  dataView.set(uint8Array);
2029
2030
  }
2030
2031
 
2032
+ /**
2033
+ * Writes an ArrayBuffer into a target ArrayBuffer at the specified offset.
2034
+ * 1. targetOffset
2035
+ * * Negative index counts back from the end of the buffer
2036
+ * * if -target.length <= targetOffset < 0, start + target.length is used.
2037
+ * * If targetOffset < -buffer.length or targetOffset is omitted, 0 is used.
2038
+ * * If targetOffset >= buffer.length, throw "Out of target Bounds" Error
2039
+ * 2. dataOffset
2040
+ * * Negative index counts back from the end of the buffer
2041
+ * * if -dataArrayBuffer.length <= dataOffset < 0, start + dataArrayBuffer.length is used.
2042
+ * * If dataOffset < -buffer.length or dataOffset is omitted, 0 is used.
2043
+ * * If dataOffset >= buffer.length, throw "Out of data Bounds" Error
2044
+ * 3. dataLength
2045
+ * * if dataLength <= 0, throw "Not Positive" Error
2046
+ * * if dataLength is omitted, read all bytes from dataOffset to the end of dataArrayBuffer
2047
+ * * if dataLength + dataOffset > dataArrayBuffer.length, read all bytes from dataOffset to the end of dataArrayBuffer
2048
+ * @param {ArrayBuffer} target - The target buffer to write into.
2049
+ * @param {ArrayBuffer} dataArrayBuffer - The data ArrayBuffer to write.
2050
+ * @param {number} [targetOffset=0] - Zero-based index at which to start write.
2051
+ * @param {number} [dataOffset=0] - The offset in the dataArrayBuffer
2052
+ * @param {number} [dataLength] - how many bytes extract from dataArrayBuffer
2053
+ * @returns {void}
2054
+ */
2055
+ function writeArrayBuffer (target, dataArrayBuffer, targetOffset = 0, dataOffset = 0, dataLength) {
2056
+ assertArrayBuffer(target);
2057
+ assertArrayBuffer(dataArrayBuffer);
2058
+ assertNumber(targetOffset);
2059
+ assertNumber(dataOffset);
2060
+ const targetLength = target.byteLength;
2061
+ if (targetOffset < -1 * targetLength) { // targetOffset < -buffer.length
2062
+ targetOffset = 0;
2063
+ } else if (targetOffset < 0) { // -target.length <= targetOffset < 0
2064
+ targetOffset += targetLength;
2065
+ } else if (targetOffset >= targetLength) {
2066
+ throw new Error(`Out of target Bounds: targetOffset(${targetOffset}) >= targetLength(${targetLength})`)
2067
+ }
2068
+
2069
+ const dataArrayBufferLength = dataArrayBuffer.byteLength;
2070
+ if (dataOffset < -1 * dataArrayBufferLength) {
2071
+ dataOffset = 0;
2072
+ } else if (dataOffset < 0) {
2073
+ dataOffset += dataArrayBufferLength;
2074
+ } else if (dataOffset >= dataArrayBufferLength) {
2075
+ throw new Error(`Out of data Bounds: dataOffset(${dataOffset}) >= dataArrayBufferLength(${dataArrayBufferLength})`)
2076
+ }
2077
+
2078
+ if (dataLength != null) {
2079
+ assertPositive(dataLength, 'dataLength');
2080
+ // offset+dataLengt out of dataArrayBuffer bounds, treat as no limitation, read to end
2081
+ if (dataOffset + dataLength > dataArrayBufferLength) {
2082
+ dataLength = undefined;
2083
+ }
2084
+ }
2085
+ const dataView = new Uint8Array(dataArrayBuffer, dataOffset, dataLength);
2086
+ if (dataView.byteLength > targetLength - targetOffset) {
2087
+ throw new Error(`Out of target Bounds: from targetOffset(${targetOffset}), No Space to store dataArrayBuffer(${dataOffset}, ${dataLength ?? 'NoLimit'})`)
2088
+ }
2089
+ const targetView = new Uint8Array(target);
2090
+ targetView.set(dataView, targetOffset);
2091
+ }
2092
+
2031
2093
  /**
2032
2094
  * @module Lang
2033
2095
  * @description Core language utilities for type checking, string manipulation, and common operations.