8000 Make microlib · qwtel/typed-array-utils@f1ba6cf · GitHub
[go: up one dir, main page]

Skip to content

Commit f1ba6cf

Browse files
committed
Make microlib
0 parents  commit f1ba6cf

File tree

7 files changed

+248
-0
lines changed

7 files changed

+248
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
*.tgz
3+
/*.d.ts
4+
/*.js
5+
/*.js.map
6+
/*.d.ts.map
7+
cjs

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License
2+
3+
Copyright (c) 2020 Florian Klampfer <mail@qwtel.com> (https://qwtel.com/)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

index.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Buffer Source Conversion
2+
// ------------------------
3+
4+
export const bufferSourceToUint8Array = (bs: BufferSource) => bs instanceof ArrayBuffer
5+
? new Uint8Array(bs)
6+
: new Uint8Array(bs.buffer, bs.byteOffset, bs.byteLength);
7+
8+
const bs2u8 = bufferSourceToUint8Array;
9+
10+
export const bufferSourceToDataView = (bs: BufferSource) => bs instanceof ArrayBuffer
11+
? new DataView(bs)
12+
: new DataView(bs.buffer, bs.byteOffset, bs.byteLength);
13+
14+
const bs2dv = bufferSourceToDataView;
15+
16+
17+
// Hex Functions
18+
// -------------
19+
20+
export const byteToHex = (byte: number) => byte.toString(16).padStart(2, '0');
21+
export const hexToByte = (hexOctet: string) => parseInt(hexOctet, 16);
22+
23+
export const hexStringToBytes = (hexString: string) => new Uint8Array(hexString.match(/[0-9a-f]{1,2}/ig).map(hexToByte));
24+
export const bytesToHexString = (bufferSource: BufferSource) => Array.from(bs2u8(bufferSource), byte => byteToHex(byte)).join('');
25+
26+
export const bytesToHexArray = (bufferSource: BufferSource) => Array.from(bs2u8(bufferSource), byte => byteToHex(byte));
27+
28+
29+
// Concatenation
30+
// -------------
31+
32+
export function concatUint8Arrays(...uint8Arrays: Uint8Array[]) {
33+
const size = uint8Arrays.reduce((size, u8) => size + u8.length, 0);
34+
const res = new Uint8Array(size);
35+
let i = 0;
36+
for (const u8 of uint8Arrays) {
37+
res.set(u8, i);
38+
i += u8.length;
39+
}
40+
return res;
41+
}
42+
43+
export function concatBufferSources(...bufferSources: BufferSource[]) {
44+
return concatUint8Arrays(...bufferSources.map(bs2u8));
45+
}
46+
47+
// Splitting
48+
// ---------
49+
50+
export function splitUint8Array(uint8Array: Uint8Array, ...indices: number[]) {
51+
const result: Uint8Array[] = new Array(indices.length + 1);
52+
let prev = 0;
53+
let i = 0;
54+
for (const index of indices) {
55+
result[i++] = uint8Array.subarray(prev, index);
56+
prev = index;
57+
}
58+
result[i] = uint8Array.subarray(prev);
59+
return result;
60+
}
61+
62+
export function splitBufferSource(bufferSource: BufferSource, ...indices: number[]) {
63+
return splitUint8Array(bs2u8(bufferSource), ...indices);
64+
}
65+
66+
67+
// Comparison
68+
// ----------
69+
70+
export function compareUint8Arrays(u8_1: Uint8Array, ...u8s: Uint8Array[]) {
71+
if (u8s.some(u8_i => u8_1.byteLength !== u8_i.byteLength)) return false;
72+
let res = true;
73+
for (const u8_i of u8s) {
74+
for (let i = 0; i !== u8_1.length; i++) {
75+
const r = u8_1[i] === u8_i[i];
76+
res = r && res;
77+
}
78+
}
79+
return res;
80+
}
81+
82+
function compareDataViewsUint32(dv_1: DataView, ...dvs: DataView[]) {
83+
if (dvs.some(dv_i => dv_1.byteLength !== dv_i.byteLength)) return false;
84+
let res = true;
85+
for (const dv_i of dvs) {
86+
for (let i = 0; i !== dv_1.byteLength; i += 4) {
87+
const r = dv_1.getUint32(i) === dv_i.getUint32(i);
88+
res = r && res;
89+
}
90+
}
91+
return res;
92+
}
93+
94+
export function compareBufferSources(bufferSource: BufferSource, ...bufferSources: BufferSource[]) {
95+
return compareDataViewsUint32(bs2dv(bufferSource), ...bufferSources.map(bs2dv));
96+
}
97+
98+
export function unsafeCompareUint8Arrays(u8_1: Uint8Array, ...u8s: Uint8Array[]) {
99+
if (u8s.some(u8_i => u8_1.byteLength !== u8_i.byteLength)) return false;
100+
return u8s.every(u8_i => {
101+
for (let i = 0; i !== u8_1.length; i++) if (u8_1[i] !== u8_i[i]) return false;
102+
return true;
103+
});
104+
}
105+
106+
function unsafeCompareDataViewsUint32(dv_1: DataView, ...dvs: DataView[]) {
107+
if (dvs.some((dv_i) => dv_1.byteLength !== dv_i.byteLength)) return false;
108+
return dvs.every(dv_i => {
109+
for (let i = 0; i !== dv_i.byteLength; i += 4) {
110+
if (dv_1.getUint32(i) !== dv_i.getUint32(i)) {
111+
return false;
112+
}
113+
}
114+
return true;
115+
});
116+
}
117+
118+
export function unsafeCompareBufferSources(bufferSource: BufferSource, ...bufferSources: BufferSource[]) {
119+
return unsafeCompareDataViewsUint32(bs2dv(bufferSource), ...bufferSources.map(bs2dv));
120+
}

package-lock.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "typed-array-utils",
3+
"version": "0.0.0",
4+
"description": "",
5+
"type": "module",
6+
"main": "cjs/index.js",
7+
"module": "index.js",
8+
"files": [
9+
"index*",
10+
"cjs",
11+
"tsconfig*.json"
12+
],
13+
"exports": {
14+
".": {
15+
"import": "./index.js",
16+
"require": "./cjs/index.cjs"
17+
}
18+
},
19+
"types": "./index.d.ts",
20+
"scripts": {
21+
"clean": "rm -f *.d.ts *.js *.js.map *.d.ts.map",
22+
"test": "echo \"Error: no test specified\" && exit 0",
23+
"build": "npm run build:mjs & npm run build:cjs & wait",
24+
"build:mjs": "tsc -d -p tsconfig.json",
25+
"build:cjs": "tsc -d -p tsconfig.cjs.json && sed -i '' -E 's/\\.js/.cjs/g' cjs/*.js && for f in cjs/*.js; do mv \"$f\" \"${f%.js}.cjs\"; done",
26+
"prepack": "npm run clean && npm run build"
27+
},
28+
"author": "Florian Klampfer <mail@qwtel.com> (https://qwtel.com/)",
29+
"license": "MIT",
30+
"devDependencies": {
31+
"typescript": "^4.1.2"
32+
}
33+
}

tsconfig.cjs.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["ES2020", "WebWorker"],
4+
"target": "ES2015",
5+
"module": "CommonJS",
6+
"outDir": "cjs",
7+
"declaration": true,
8+
"declarationMap": true,
9+
"sourceMap": true,
10+
"inlineSources": true,
11+
},
12+
"include": [
13+
"index.ts"
14+
],
15+
"exclude": [],
16+
}

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["ES2020", "WebWorker"],
4+
"target": "ES2018",
5+
"outDir": ".",
6+
"declaration": true,
7+
"declarationMap": true,
8+
"sourceMap": true,
9+
"inlineSources": true,
10+
},
11+
"include": [
12+
"index.ts",
13+
],
14+
"exclude": [],
15+
}

0 commit comments

Comments
 (0)
0