uint8-util
Advanced tools
Comparing version 2.1.5 to 2.1.6
16
_node.js
@@ -9,14 +9,12 @@ import { createHash } from 'node:crypto' | ||
export const text2arr = str => { | ||
return new Uint8Array(Buffer.from(str, 'utf-8')) | ||
} | ||
export const text2arr = str => new Uint8Array(Buffer.from(str, 'utf-8')) | ||
export const arr2base = buffer => { | ||
return Buffer.from(buffer).toString('base64') | ||
} | ||
export const arr2base = buffer => Buffer.from(buffer).toString('base64') | ||
export const base2arr = str => { | ||
return new Uint8Array(Buffer.from(str, 'base64')) | ||
} | ||
export const base2arr = str => new Uint8Array(Buffer.from(str, 'base64')) | ||
export const hex2bin = hex => Buffer.from(hex, 'hex').toString('binary') | ||
export const bin2hex = bin => Buffer.from(bin, 'binary').toString('hex') | ||
export const hash = async (data, format, algo = 'sha1') => { | ||
@@ -23,0 +21,0 @@ algo = algo.replace('sha-', 'sha') |
@@ -1,2 +0,2 @@ | ||
import { arr2hex } from './util.js' | ||
import { arr2hex, alphabet } from './util.js' | ||
import { decode, encode } from 'base64-arraybuffer' | ||
@@ -6,18 +6,41 @@ | ||
// 50% slower at < 48 chars, but little impact at 4M OPS/s vs 8M OPS/s | ||
export const arr2text = (buffer) => { | ||
return decoder.decode(buffer) | ||
} | ||
export const arr2text = (buffer) => decoder.decode(buffer) | ||
// sacrifice ~20% speed for bundle size | ||
const encoder = new TextEncoder() | ||
export const text2arr = string => { | ||
return encoder.encode(string) | ||
} | ||
export const text2arr = string => encoder.encode(string) | ||
export const arr2base = buffer => { | ||
return encode(buffer) | ||
export const arr2base = buffer => encode(buffer) | ||
export const base2arr = str => new Uint8Array(decode(str)) | ||
export const bin2hex = str => { | ||
let res = '' | ||
let c | ||
let i = 0 | ||
const len = str.length | ||
while (i < len) { | ||
c = str.charCodeAt(i++) | ||
res += alphabet[c >> 4] | ||
res += alphabet[c & 0xF] | ||
} | ||
return res | ||
} | ||
export const base2arr = str => { | ||
return new Uint8Array(decode(str)) | ||
const MAX_ARGUMENTS_LENGTH = 0x10000 | ||
export const hex2bin = hex => { | ||
const points = new Array(hex.length / 2) | ||
for (let i = 0, l = hex.length / 2; i < l; ++i) { | ||
points[i] = parseInt(hex.substr(i * 2, 2), 16) | ||
} | ||
if (points.length <= MAX_ARGUMENTS_LENGTH) return String.fromCharCode(...points) | ||
let res = '' | ||
let i = 0 | ||
while (i < points.length) { | ||
res += String.fromCharCode(...points.slice(i, i += MAX_ARGUMENTS_LENGTH)) | ||
} | ||
return res | ||
} | ||
@@ -24,0 +47,0 @@ |
{ | ||
"name": "uint8-util", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"description": "Fastest possible buffer-like utilities for uint8.", | ||
@@ -45,4 +45,8 @@ "main": "index.js", | ||
"scripts": { | ||
"bench-node-bin2hex": "node benchmark/bin2hex.js", | ||
"bench-node-hex2bin": "node benchmark/hex2bin.js", | ||
"bench-node-2text": "node benchmark/arr2text.js", | ||
"bench-node-2arr": "node benchmark/text2arr.js", | ||
"bench-browser-bin2hex": "airtap --preset local -- benchmark/bin2hex.js", | ||
"bench-browser-hex2bin": "airtap --preset local -- benchmark/hex2bin.js", | ||
"bench-browser-2text": "airtap --preset local -- benchmark/arr2text.js", | ||
@@ -49,0 +53,0 @@ "bench-browser-2arr": "airtap --preset local -- benchmark/text2arr.js", |
@@ -6,3 +6,3 @@ /* Common package for dealing with hex/string/uint8 conversions (and sha1 hashing) | ||
*/ | ||
const alphabet = '0123456789abcdef' | ||
export const alphabet = '0123456789abcdef' | ||
const encodeLookup = [] | ||
@@ -9,0 +9,0 @@ const decodeLookup = [] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2765002
12
675