uint-array.js
This file
Path
lib/utils/uint-array.js
63 lines · 2.2 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 08eb743 point at it.
Upgrade `tsd` and `c8` (#982)ehmickybefore adoption08eb743
lines 1–6why, one step back1import {StringDecoder} from 'node:string_decoder';23const {toString: objectToString} = Object.prototype;45export const isArrayBuffer = value => objectToString.call(value) === '[object ArrayBuffer]';6Allow passing `Buffer` to the `input` option (#1029)ehmickybefore adoption40fdc75
lines 7–8why, one step back7// Is either Uint8Array or Buffer8export const isUint8Array = value => objectToString.call(value) === '[object Uint8Array]';Upgrade `tsd` and `c8` (#982)ehmickybefore adoption08eb743
lines 9–37why, one step back910export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);1112const textEncoder = new TextEncoder();13const stringToUint8Array = string => textEncoder.encode(string);1415const textDecoder = new TextDecoder();16export const uint8ArrayToString = uint8Array => textDecoder.decode(uint8Array);1718export const joinToString = (uint8ArraysOrStrings, encoding) => {19 const strings = uint8ArraysToStrings(uint8ArraysOrStrings, encoding);20 return strings.join('');21};2223const uint8ArraysToStrings = (uint8ArraysOrStrings, encoding) => {24 if (encoding === 'utf8' && uint8ArraysOrStrings.every(uint8ArrayOrString => typeof uint8ArrayOrString === 'string')) {25 return uint8ArraysOrStrings;26 }2728 const decoder = new StringDecoder(encoding);29 const strings = uint8ArraysOrStrings30 .map(uint8ArrayOrString => typeof uint8ArrayOrString === 'string'31 ? stringToUint8Array(uint8ArrayOrString)32 : uint8ArrayOrString)33 .map(uint8Array => decoder.write(uint8Array));34 const finalString = decoder.end();35 return finalString === '' ? strings : [...strings, finalString];36};37TweaksSindre Sorhusbefore adoption698a1b8
line 38why, one step back38export const joinToUint8Array = uint8ArraysOrStrings => uint8ArraysOrStrings.length === 1 && isUint8Array(uint8ArraysOrStrings[0]) ? uint8ArraysOrStrings[0] : concatUint8Arrays(stringsToUint8Arrays(uint8ArraysOrStrings));Upgrade `tsd` and `c8` (#982)ehmickybefore adoption08eb743
lines 39–63why, one step back3940const stringsToUint8Arrays = uint8ArraysOrStrings => uint8ArraysOrStrings.map(uint8ArrayOrString => typeof uint8ArrayOrString === 'string'41 ? stringToUint8Array(uint8ArrayOrString)42 : uint8ArrayOrString);4344export const concatUint8Arrays = uint8Arrays => {45 const result = new Uint8Array(getJoinLength(uint8Arrays));4647 let index = 0;48 for (const uint8Array of uint8Arrays) {49 result.set(uint8Array, index);50 index += uint8Array.length;51 }5253 return result;54};5556const getJoinLength = uint8Arrays => {57 let joinLength = 0;58 for (const uint8Array of uint8Arrays) {59 joinLength += uint8Array.length;60 }6162 return joinLength;63};