@saulx/utils
Advanced tools
Comparing version 3.2.0 to 3.2.1
"use strict"; | ||
// Faster in some browsers e.g. chrome then native textEncoder (dont use in nodejs!) | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// UTF-8 array to JS string and vice versa | ||
exports.uft8ToString = (aBytes) => { | ||
@@ -10,12 +10,11 @@ let sView = ''; | ||
nPart = aBytes[nIdx]; | ||
sView += String.fromCodePoint(nPart > 251 && nPart < 254 && nIdx + 5 < nLen /* six bytes */ | ||
? /* (nPart - 252 << 30) may be not so safe in ECMAScript! So…: */ | ||
(nPart - 252) * 1073741824 + | ||
((aBytes[++nIdx] - 128) << 24) + | ||
((aBytes[++nIdx] - 128) << 18) + | ||
((aBytes[++nIdx] - 128) << 12) + | ||
((aBytes[++nIdx] - 128) << 6) + | ||
aBytes[++nIdx] - | ||
128 | ||
: nPart > 247 && nPart < 252 && nIdx + 4 < nLen /* five bytes */ | ||
sView += String.fromCodePoint(nPart > 251 && nPart < 254 && nIdx + 5 < nLen | ||
? (nPart - 252) * 1073741824 + | ||
((aBytes[++nIdx] - 128) << 24) + | ||
((aBytes[++nIdx] - 128) << 18) + | ||
((aBytes[++nIdx] - 128) << 12) + | ||
((aBytes[++nIdx] - 128) << 6) + | ||
aBytes[++nIdx] - | ||
128 | ||
: nPart > 247 && nPart < 252 && nIdx + 4 < nLen | ||
? ((nPart - 248) << 24) + | ||
@@ -27,3 +26,3 @@ ((aBytes[++nIdx] - 128) << 18) + | ||
128 | ||
: nPart > 239 && nPart < 248 && nIdx + 3 < nLen /* four bytes */ | ||
: nPart > 239 && nPart < 248 && nIdx + 3 < nLen | ||
? ((nPart - 240) << 18) + | ||
@@ -34,3 +33,3 @@ ((aBytes[++nIdx] - 128) << 12) + | ||
128 | ||
: nPart > 223 && nPart < 240 && nIdx + 2 < nLen /* three bytes */ | ||
: nPart > 223 && nPart < 240 && nIdx + 2 < nLen | ||
? ((nPart - 224) << 12) + | ||
@@ -40,6 +39,5 @@ ((aBytes[++nIdx] - 128) << 6) + | ||
128 | ||
: nPart > 191 && nPart < 224 && nIdx + 1 < nLen /* two bytes */ | ||
: nPart > 191 && nPart < 224 && nIdx + 1 < nLen | ||
? ((nPart - 192) << 6) + aBytes[++nIdx] - 128 | ||
: /* nPart < 127 ? */ /* one byte */ | ||
nPart); | ||
: nPart); | ||
} | ||
@@ -52,3 +50,2 @@ return sView; | ||
let nArrLen = 0; | ||
/* mapping… */ | ||
for (let nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) { | ||
@@ -73,3 +70,2 @@ nChr = str.codePointAt(nMapIdx); | ||
const aBytes = new Uint8Array(nArrLen); | ||
/* transcription… */ | ||
let nIdx = 0; | ||
@@ -80,7 +76,5 @@ let nChrIdx = 0; | ||
if (nChr < 128) { | ||
/* one byte */ | ||
aBytes[nIdx++] = nChr; | ||
} | ||
else if (nChr < 0x800) { | ||
/* two bytes */ | ||
aBytes[nIdx++] = 192 + (nChr >>> 6); | ||
@@ -90,3 +84,2 @@ aBytes[nIdx++] = 128 + (nChr & 63); | ||
else if (nChr < 0x10000) { | ||
/* three bytes */ | ||
aBytes[nIdx++] = 224 + (nChr >>> 12); | ||
@@ -97,3 +90,2 @@ aBytes[nIdx++] = 128 + ((nChr >>> 6) & 63); | ||
else if (nChr < 0x200000) { | ||
/* four bytes */ | ||
aBytes[nIdx++] = 240 + (nChr >>> 18); | ||
@@ -106,3 +98,2 @@ aBytes[nIdx++] = 128 + ((nChr >>> 12) & 63); | ||
else if (nChr < 0x4000000) { | ||
/* five bytes */ | ||
aBytes[nIdx++] = 248 + (nChr >>> 24); | ||
@@ -114,5 +105,5 @@ aBytes[nIdx++] = 128 + ((nChr >>> 18) & 63); | ||
nChrIdx++; | ||
} /* if (nChr <= 0x7fffffff) */ | ||
} | ||
else { | ||
/* six bytes */ | ||
// dont support more then 7 byte chars... | ||
aBytes[nIdx++] = 252 + (nChr >>> 30); | ||
@@ -119,0 +110,0 @@ aBytes[nIdx++] = 128 + ((nChr >>> 24) & 63); |
{ | ||
"name": "@saulx/utils", | ||
"main": "./dist/index.js", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"scripts": { | ||
@@ -41,3 +41,3 @@ "build": "tsc", | ||
"is-plain-obj": "^3.0.0", | ||
"@saulx/hash": "^1.1.0" | ||
"@saulx/hash": "^2.0.0" | ||
}, | ||
@@ -44,0 +44,0 @@ "devDependencies": { |
@@ -193,3 +193,3 @@ # utils | ||
import { createEncoder } from '@saulx/utils' | ||
const { encode, decode } = createEncoder(['🥹'], '@') | ||
const { encode, decode } = createEncoder(['🥹'], ['@']) | ||
console.log(encode('hello 🥹')) // "hello @a" | ||
@@ -202,3 +202,3 @@ ``` | ||
import { createEncoder } from '@saulx/utils' | ||
const { encode, decode } = createEncoder(['hello'], '@') | ||
const { encode, decode } = createEncoder(['hello'], ['@']) | ||
console.log(encode('hello 🥹')) // "@a 🥹" | ||
@@ -205,0 +205,0 @@ ``` |
Sorry, the diff of this file is not supported yet
91581
1335
+ Added@saulx/hash@2.0.0(transitive)
- Removed@saulx/hash@1.1.0(transitive)
Updated@saulx/hash@^2.0.0