Skip to content
Myr

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 file

Take it

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 1–2why, one step back
1import {Buffer} from 'node:buffer';
2

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 3why, one step back
3const {toString: objectToString} = Object.prototype;

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 4why, one step back
4

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 5–8why, one step back
5export const isArrayBuffer = value => objectToString.call(value) === '[object ArrayBuffer]';
67export const isUint8Array = value => objectToString.call(value) === '[object Uint8Array]' && !Buffer.isBuffer(value);8

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 9–16why, one step back
9export 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);16

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 17why, one step back
17export const joinToString = uint8ArraysOrStrings => {

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 18–21why, one step back
18 if (uint8ArraysOrStrings.length === 1 && typeof uint8ArraysOrStrings[0] === 'string') {
19 return uint8ArraysOrStrings[0];20 }21

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 22why, one step back
22 const strings = uint8ArraysToStrings(uint8ArraysOrStrings);

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 23–25why, one step back
23 return strings.join('');
24};25

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 26why, one step back
26const uint8ArraysToStrings = uint8ArraysOrStrings => {

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 27–28why, one step back
27 const decoder = new TextDecoder();
28 const strings = uint8ArraysOrStrings.map(uint8ArrayOrString => isUint8Array(uint8ArrayOrString)

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 29why, one step back
29 ? decoder.decode(uint8ArrayOrString, {stream: true})

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 30–39why, one step back
30 : 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 }39

Add support for `all` option to `execaSync()` (#956)ehmickybefore adoption63b5ac4

lines 40–47why, one step back
40 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 back
48 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};