Skip to content

Commit

Permalink
fix: remove unnecessary Buffer.from usage
Browse files Browse the repository at this point in the history
  • Loading branch information
yue4u committed Jul 24, 2024
1 parent 8195937 commit f83bcaf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 23 deletions.
10 changes: 2 additions & 8 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,12 @@ import type { KTX2Container } from './container.js';

/** Encodes text to an ArrayBuffer. */
export function encodeText(text: string): Uint8Array {
if (typeof TextEncoder !== 'undefined') {
return new TextEncoder().encode(text);
}
return Buffer.from(text);
return new TextEncoder().encode(text);
}

/** Decodes an ArrayBuffer to text. */
export function decodeText(buffer: Uint8Array): string {
if (typeof TextDecoder !== 'undefined') {
return new TextDecoder().decode(buffer);
}
return Buffer.from(buffer).toString('utf8');
return new TextDecoder().decode(buffer);
}

/** Concatenates N ArrayBuffers. */
Expand Down
17 changes: 2 additions & 15 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { URL } from 'url';
import { readFileSync } from 'fs';
import { readFile } from 'fs/promises';
import { basename, join } from 'path';
import { TextDecoder, TextEncoder } from 'util';
import test from 'ava';
import { glob } from 'glob';
import { KTX2Container, read, write } from 'ktx-parse';
Expand Down Expand Up @@ -189,20 +188,8 @@ test('write::uastc', (t) => {
});

test('platform::web', (t) => {
// Emulate browser API.
global.TextEncoder = TextEncoder as any;
global.TextDecoder = TextDecoder as any;
const _from = Buffer.from;
Buffer.from = (() => {
throw new Error('Should not be called.');
}) as any;

try {
const result = write(read(SAMPLE_UASTC));
t.true(result instanceof Uint8Array, 'success');
} finally {
Buffer.from = _from;
}
const result = write(read(SAMPLE_UASTC));
t.true(result instanceof Uint8Array, 'success');
});

test('data format descriptors', (t) => {
Expand Down

0 comments on commit f83bcaf

Please sign in to comment.