uint-array.js
This file
Path
lib/stdio/uint-array.js
66 lines · 2.1 KiB · JavaScript
Last changed by
Fix `lines` option (#957)ehmickybefore adoption15ab4e6
History of this fileTake it
Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 1–2why, one step back1import {Buffer} from 'node:buffer';2Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 3why, one step back3const {toString: objectToString} = Object.prototype;Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 4why, one step back4Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 5–8why, one step back5export const isArrayBuffer = value => objectToString.call(value) === '[object ArrayBuffer]';67export const isUint8Array = value => objectToString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);8Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 9–16why, one step back9export const bufferToUint8Array = buffer => new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);1011const textEncoder = new TextEncoder();12const stringToUint8Array = string => textEncoder.encode(string);1314const textDecoder = new TextDecoder();15export const uint8ArrayToString = uint8Array => textDecoder.decode(uint8Array);16Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 17why, one step back17export const joinToString = uint8ArraysOrStrings => {Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 18–21why, one step back18 if (uint8ArraysOrStrings.length === 1 && typeof uint8ArraysOrStrings[0] === 'string') {19 return uint8ArraysOrStrings[0];20 }21Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 22why, one step back22 const strings = uint8ArraysToStrings(uint8ArraysOrStrings);Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 23–25why, one step back23 return strings.join('');24};25Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 26why, one step back26const uint8ArraysToStrings = uint8ArraysOrStrings => {Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 27–28why, one step back27 const decoder = new TextDecoder();28 const strings = uint8ArraysOrStrings.map(uint8ArrayOrString => isUint8Array(uint8ArrayOrString)Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 29why, one step back29 ? decoder.decode(uint8ArrayOrString, {stream: true})Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 30–39why, one step back30 : uint8ArrayOrString);31 const finalString = decoder.decode();32 return finalString === '' ? strings : [...strings, finalString];33};3435export const joinToUint8Array = uint8ArraysOrStrings => {36 if (uint8ArraysOrStrings.length === 1 && isUint8Array(uint8ArraysOrStrings[0])) {37 return uint8ArraysOrStrings[0];38 }39Add support for `all` option to `execaSync()` (#956)ehmickybefore adoption63b5ac4
lines 40–47why, one step back40 return concatUint8Arrays(stringsToUint8Arrays(uint8ArraysOrStrings));41};4243const stringsToUint8Arrays = uint8ArraysOrStrings => uint8ArraysOrStrings.map(uint8ArrayOrString => typeof uint8ArrayOrString === 'string'44 ? stringToUint8Array(uint8ArrayOrString)45 : uint8ArrayOrString);4647export const concatUint8Arrays = uint8Arrays => {Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 48–66why, one step back48 const result = new Uint8Array(getJoinLength(uint8Arrays));4950 let index = 0;51 for (const uint8Array of uint8Arrays) {52 result.set(uint8Array, index);53 index += uint8Array.length;54 }5556 return result;57};5859const getJoinLength = uint8Arrays => {60 let joinLength = 0;61 for (const uint8Array of uint8Arrays) {62 joinLength += uint8Array.length;63 }6465 return joinLength;66};