uint-array.js
This file
Path
lib/stdio/uint-array.js
69 lines · 2.3 KiB · JavaScript
Last changed by
Improve `encoding` option (#958)ehmickybefore adoption507b09c
History of this fileTake it
Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 1why, one step back1import {Buffer} from 'node:buffer';Improve `encoding` option (#958)ehmickybefore adoption507b09c
line 2why, one step back2import {StringDecoder} from 'node:string_decoder';Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 3why, one step back3Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 4why, one step back4const {toString: objectToString} = Object.prototype;Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 5why, one step back5Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 6–9why, one step back6export const isArrayBuffer = value => objectToString.call(value) === '[object ArrayBuffer]';78export const isUint8Array = value => objectToString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);9Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 10–17why, one step back10export 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);17Improve `encoding` option (#958)ehmickybefore adoption507b09c
lines 18–19why, one step back18export const joinToString = (uint8ArraysOrStrings, encoding) => {19 const strings = uint8ArraysToStrings(uint8ArraysOrStrings, encoding);Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 20–22why, one step back20 return strings.join('');21};22Improve `encoding` option (#958)ehmickybefore adoption507b09c
lines 23–34why, one step back23const 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();Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 35–42why, one step back35 return finalString === '' ? strings : [...strings, finalString];36};3738export const joinToUint8Array = uint8ArraysOrStrings => {39 if (uint8ArraysOrStrings.length === 1 && isUint8Array(uint8ArraysOrStrings[0])) {40 return uint8ArraysOrStrings[0];41 }42Add support for `all` option to `execaSync()` (#956)ehmickybefore adoption63b5ac4
lines 43–50why, one step back43 return concatUint8Arrays(stringsToUint8Arrays(uint8ArraysOrStrings));44};4546const stringsToUint8Arrays = uint8ArraysOrStrings => uint8ArraysOrStrings.map(uint8ArrayOrString => typeof uint8ArrayOrString === 'string'47 ? stringToUint8Array(uint8ArrayOrString)48 : uint8ArrayOrString);4950export const concatUint8Arrays = uint8Arrays => {Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 51–69why, one step back51 const result = new Uint8Array(getJoinLength(uint8Arrays));5253 let index = 0;54 for (const uint8Array of uint8Arrays) {55 result.set(uint8Array, index);56 index += uint8Array.length;57 }5859 return result;60};6162const getJoinLength = uint8Arrays => {63 let joinLength = 0;64 for (const uint8Array of uint8Arrays) {65 joinLength += uint8Array.length;66 }6768 return joinLength;69};