@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.
- package/dist/cjs/index-dev.cjs +153 -4
- package/dist/cjs/index-dev.cjs.map +1 -1
- package/dist/cjs/index-min.cjs +1 -1
- package/dist/cjs/index-min.cjs.map +1 -1
- package/dist/esm/index-dev.js +153 -5
- package/dist/esm/index-dev.js.map +1 -1
- package/dist/esm/index-min.js +1 -1
- package/dist/esm/index-min.js.map +1 -1
- package/dist/umd/index.dev.js +153 -4
- package/dist/umd/index.dev.js.map +1 -1
- package/dist/umd/index.min.js +1 -1
- package/dist/umd/index.min.js.map +1 -1
- package/package.json +1 -1
- package/types/array-buffer-utils.d.ts +51 -0
- package/types/index.d.ts +3 -1
- package/types/type-assert.d.ts +64 -56
- package/types/type-utils.d.ts +99 -92
package/dist/umd/index.dev.js
CHANGED
|
@@ -155,7 +155,8 @@
|
|
|
155
155
|
isFloat64Array,
|
|
156
156
|
isBigInt64Array,
|
|
157
157
|
isBigUint64Array,
|
|
158
|
-
isTypedArray
|
|
158
|
+
isTypedArray,
|
|
159
|
+
isArrayBuffer
|
|
159
160
|
};
|
|
160
161
|
/**
|
|
161
162
|
* Checks if the given value is an array.
|
|
@@ -485,7 +486,7 @@
|
|
|
485
486
|
function isUint32Array (value) {
|
|
486
487
|
return value instanceof Uint32Array
|
|
487
488
|
}
|
|
488
|
-
//
|
|
489
|
+
// function isFloat16Array (value) {
|
|
489
490
|
// return value instanceof Float16Array
|
|
490
491
|
// }
|
|
491
492
|
/**
|
|
@@ -521,6 +522,15 @@
|
|
|
521
522
|
return value instanceof BigUint64Array
|
|
522
523
|
}
|
|
523
524
|
|
|
525
|
+
/**
|
|
526
|
+
* Checks if the given value is an ArrayBuffer.
|
|
527
|
+
* @param {*} value - The value to check.
|
|
528
|
+
* @returns {boolean} True if the value is an ArrayBuffer, false otherwise.
|
|
529
|
+
*/
|
|
530
|
+
function isArrayBuffer (value) {
|
|
531
|
+
return value instanceof ArrayBuffer
|
|
532
|
+
}
|
|
533
|
+
|
|
524
534
|
// 3rd
|
|
525
535
|
// internal
|
|
526
536
|
// owned
|
|
@@ -559,7 +569,8 @@
|
|
|
559
569
|
assertFloat64Array,
|
|
560
570
|
assertBigInt64Array,
|
|
561
571
|
assertBigUint64Array,
|
|
562
|
-
assertTypedArray
|
|
572
|
+
assertTypedArray,
|
|
573
|
+
assertArrayBuffer
|
|
563
574
|
};
|
|
564
575
|
/**
|
|
565
576
|
* if value is not Array, throw error
|
|
@@ -913,6 +924,18 @@
|
|
|
913
924
|
}
|
|
914
925
|
}
|
|
915
926
|
|
|
927
|
+
/**
|
|
928
|
+
* Asserts that the given value is an ArrayBuffer.
|
|
929
|
+
* @param {*} value - The value to check.
|
|
930
|
+
* @param {string} [paramName] - Optional parameter name for error message.
|
|
931
|
+
* @throws {Error} Throws an error if the value is not an ArrayBuffer.
|
|
932
|
+
*/
|
|
933
|
+
function assertArrayBuffer (value, paramName) {
|
|
934
|
+
if (!isArrayBuffer(value)) {
|
|
935
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not ArrayBuffer`)
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
916
939
|
// 3rd
|
|
917
940
|
// internal
|
|
918
941
|
// owned
|
|
@@ -1949,6 +1972,130 @@
|
|
|
1949
1972
|
return true
|
|
1950
1973
|
}
|
|
1951
1974
|
|
|
1975
|
+
var ArrayBufferUtils = {
|
|
1976
|
+
readString,
|
|
1977
|
+
writeString,
|
|
1978
|
+
writeArrayBuffer
|
|
1979
|
+
};
|
|
1980
|
+
|
|
1981
|
+
// module vars
|
|
1982
|
+
const textDecoder = new TextDecoder();
|
|
1983
|
+
const textEncoder = new TextEncoder();
|
|
1984
|
+
/**
|
|
1985
|
+
* Reads a string from an ArrayBuffer at the specified offset and length.
|
|
1986
|
+
* 1. if offset >= buffer.byteLength, return undefined
|
|
1987
|
+
* 2. if length is not specified, read all bytes from offset to the end of buffer
|
|
1988
|
+
* 3. if length is specified
|
|
1989
|
+
* * if offset + length >= buffer.byteLength, return a Uint8Array from offset to the end of buffer
|
|
1990
|
+
* * if offset + length < buffer.byteLength, return a Uint8Array from offset to offset + length
|
|
1991
|
+
* @param {ArrayBuffer} buffer - The buffer to read from.
|
|
1992
|
+
* @param {number} [offset=0] - The offset in bytes to start reading from.
|
|
1993
|
+
* @param {number} [length] - The number of bytes to read.
|
|
1994
|
+
* @returns {string|undefined} The decoded string.
|
|
1995
|
+
*/
|
|
1996
|
+
function readString (buffer, offset = 0, length) {
|
|
1997
|
+
assertArrayBuffer(buffer);
|
|
1998
|
+
assertNotNegative(offset);
|
|
1999
|
+
if (offset >= buffer.byteLength) {
|
|
2000
|
+
return undefined
|
|
2001
|
+
}
|
|
2002
|
+
let uint8Array = null;
|
|
2003
|
+
if (length != null) {
|
|
2004
|
+
assertPositive(length);
|
|
2005
|
+
if (offset + length >= buffer.byteLength) {
|
|
2006
|
+
uint8Array = new Uint8Array(buffer, offset);
|
|
2007
|
+
} else {
|
|
2008
|
+
uint8Array = new Uint8Array(buffer, offset, length);
|
|
2009
|
+
}
|
|
2010
|
+
} else {
|
|
2011
|
+
uint8Array = new Uint8Array(buffer, offset);
|
|
2012
|
+
}
|
|
2013
|
+
|
|
2014
|
+
// An ArrayBuffer, a TypedArray, or a DataView object containing the encoded text to decode.
|
|
2015
|
+
return textDecoder.decode(uint8Array)
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
/**
|
|
2019
|
+
* Writes a string to an ArrayBuffer at the specified offset.
|
|
2020
|
+
* @param {ArrayBuffer} buffer - The buffer to write to
|
|
2021
|
+
* @param {string} str - The string to write
|
|
2022
|
+
* @param {number} [offset=0] - The offset in bytes to start writing
|
|
2023
|
+
* @returns {void}
|
|
2024
|
+
*/
|
|
2025
|
+
function writeString (buffer, str, offset = 0) {
|
|
2026
|
+
assertArrayBuffer(buffer, 'buffer');
|
|
2027
|
+
assertString(str, 'str');
|
|
2028
|
+
assertNotNegative(offset, 'offset');
|
|
2029
|
+
const uint8Array = textEncoder.encode(str);
|
|
2030
|
+
const strByteLength = uint8Array.byteLength;
|
|
2031
|
+
if (offset + strByteLength > buffer.byteLength) {
|
|
2032
|
+
throw new Error(`offset + str.byteLength > buffer.byteLength:${offset + strByteLength} > ${buffer.byteLength}`)
|
|
2033
|
+
}
|
|
2034
|
+
const dataView = new Uint8Array(buffer, offset, uint8Array.byteLength);
|
|
2035
|
+
dataView.set(uint8Array);
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
/**
|
|
2039
|
+
* Writes an ArrayBuffer into a target ArrayBuffer at the specified offset.
|
|
2040
|
+
* 1. targetOffset
|
|
2041
|
+
* * Negative index counts back from the end of the buffer
|
|
2042
|
+
* * if -target.length <= targetOffset < 0, start + target.length is used.
|
|
2043
|
+
* * If targetOffset < -buffer.length or targetOffset is omitted, 0 is used.
|
|
2044
|
+
* * If targetOffset >= buffer.length, throw "Out of target Bounds" Error
|
|
2045
|
+
* 2. dataOffset
|
|
2046
|
+
* * Negative index counts back from the end of the buffer
|
|
2047
|
+
* * if -dataArrayBuffer.length <= dataOffset < 0, start + dataArrayBuffer.length is used.
|
|
2048
|
+
* * If dataOffset < -buffer.length or dataOffset is omitted, 0 is used.
|
|
2049
|
+
* * If dataOffset >= buffer.length, throw "Out of data Bounds" Error
|
|
2050
|
+
* 3. dataLength
|
|
2051
|
+
* * if dataLength <= 0, throw "Not Positive" Error
|
|
2052
|
+
* * if dataLength is omitted, read all bytes from dataOffset to the end of dataArrayBuffer
|
|
2053
|
+
* * if dataLength + dataOffset > dataArrayBuffer.length, read all bytes from dataOffset to the end of dataArrayBuffer
|
|
2054
|
+
* @param {ArrayBuffer} target - The target buffer to write into.
|
|
2055
|
+
* @param {ArrayBuffer} dataArrayBuffer - The data ArrayBuffer to write.
|
|
2056
|
+
* @param {number} [targetOffset=0] - Zero-based index at which to start write.
|
|
2057
|
+
* @param {number} [dataOffset=0] - The offset in the dataArrayBuffer
|
|
2058
|
+
* @param {number} [dataLength] - how many bytes extract from dataArrayBuffer
|
|
2059
|
+
* @returns {void}
|
|
2060
|
+
*/
|
|
2061
|
+
function writeArrayBuffer (target, dataArrayBuffer, targetOffset = 0, dataOffset = 0, dataLength) {
|
|
2062
|
+
assertArrayBuffer(target);
|
|
2063
|
+
assertArrayBuffer(dataArrayBuffer);
|
|
2064
|
+
assertNumber(targetOffset);
|
|
2065
|
+
assertNumber(dataOffset);
|
|
2066
|
+
const targetLength = target.byteLength;
|
|
2067
|
+
if (targetOffset < -1 * targetLength) { // targetOffset < -buffer.length
|
|
2068
|
+
targetOffset = 0;
|
|
2069
|
+
} else if (targetOffset < 0) { // -target.length <= targetOffset < 0
|
|
2070
|
+
targetOffset += targetLength;
|
|
2071
|
+
} else if (targetOffset >= targetLength) {
|
|
2072
|
+
throw new Error(`Out of target Bounds: targetOffset(${targetOffset}) >= targetLength(${targetLength})`)
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
const dataArrayBufferLength = dataArrayBuffer.byteLength;
|
|
2076
|
+
if (dataOffset < -1 * dataArrayBufferLength) {
|
|
2077
|
+
dataOffset = 0;
|
|
2078
|
+
} else if (dataOffset < 0) {
|
|
2079
|
+
dataOffset += dataArrayBufferLength;
|
|
2080
|
+
} else if (dataOffset >= dataArrayBufferLength) {
|
|
2081
|
+
throw new Error(`Out of data Bounds: dataOffset(${dataOffset}) >= dataArrayBufferLength(${dataArrayBufferLength})`)
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
if (dataLength != null) {
|
|
2085
|
+
assertPositive(dataLength, 'dataLength');
|
|
2086
|
+
// offset+dataLengt out of dataArrayBuffer bounds, treat as no limitation, read to end
|
|
2087
|
+
if (dataOffset + dataLength > dataArrayBufferLength) {
|
|
2088
|
+
dataLength = undefined;
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
const dataView = new Uint8Array(dataArrayBuffer, dataOffset, dataLength);
|
|
2092
|
+
if (dataView.byteLength > targetLength - targetOffset) {
|
|
2093
|
+
throw new Error(`Out of target Bounds: from targetOffset(${targetOffset}), No Space to store dataArrayBuffer(${dataOffset}, ${dataLength ?? 'NoLimit'})`)
|
|
2094
|
+
}
|
|
2095
|
+
const targetView = new Uint8Array(target);
|
|
2096
|
+
targetView.set(dataView, targetOffset);
|
|
2097
|
+
}
|
|
2098
|
+
|
|
1952
2099
|
/**
|
|
1953
2100
|
* @module Lang
|
|
1954
2101
|
* @description Core language utilities for type checking, string manipulation, and common operations.
|
|
@@ -1968,9 +2115,11 @@
|
|
|
1968
2115
|
ClassProxyUtils,
|
|
1969
2116
|
InstanceProxyUtils,
|
|
1970
2117
|
ReflectUtils,
|
|
1971
|
-
TypedArrayUtils
|
|
2118
|
+
TypedArrayUtils,
|
|
2119
|
+
ArrayBufferUtils
|
|
1972
2120
|
};
|
|
1973
2121
|
|
|
2122
|
+
exports.ArrayBufferUtils = ArrayBufferUtils;
|
|
1974
2123
|
exports.ClassProxyUtils = ClassProxyUtils;
|
|
1975
2124
|
exports.Exec = ExecUtils;
|
|
1976
2125
|
exports.ExecUtils = ExecUtils;
|