Skip to content

Commit

Permalink
Add includes method (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar authored Jul 5, 2024
1 parent 7fb099c commit 13ff2f7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
13 changes: 13 additions & 0 deletions benchmark.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ import {
uint8ArrayToBase64,
uint8ArrayToHex,
uint8ArrayToString,
getUintBE,
indexOf,
includes,
} from './index.js';

const oneMb = 1024 * 1024;
const largeUint8Array = new Uint8Array(randomBytes(oneMb).buffer);
// eslint-disable-next-line unicorn/prefer-spread
const largeUint8ArrayDuplicate = largeUint8Array.slice();
const partOfUint8Array = largeUint8Array.slice(-1024);
const textFromUint8Array = uint8ArrayToString(largeUint8Array);
const base64FromUint8Array = Buffer.from(textFromUint8Array).toString('base64');
const hexFromUint8Array = uint8ArrayToHex(largeUint8Array);
const view = new DataView(largeUint8Array.buffer, 0, 10);

const suite = new benchmark.Suite();

Expand All @@ -38,6 +43,8 @@ suite.add('concatUint8Arrays with 4 arrays', () => concatUint8Arrays([largeUint8

suite.add('uint8ArrayToString', () => uint8ArrayToString(largeUint8Array));

suite.add('uint8ArrayToString - latin1', () => uint8ArrayToString(largeUint8Array, 'latin1'));

suite.add('stringToUint8Array', () => stringToUint8Array(textFromUint8Array));

suite.add('uint8ArrayToBase64', () => uint8ArrayToBase64(largeUint8Array));
Expand All @@ -50,5 +57,11 @@ suite.add('uint8ArrayToHex', () => uint8ArrayToHex(largeUint8Array));

suite.add('hexToUint8Array', () => hexToUint8Array(hexFromUint8Array));

suite.add('getUintBE', () => getUintBE(view));

suite.add('indexOf', () => indexOf(largeUint8Array, partOfUint8Array));

suite.add('includes', () => includes(largeUint8Array, partOfUint8Array));

suite.on('cycle', event => console.log(event.target.toString()));
suite.run({async: false});
18 changes: 18 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,21 @@ console.log(indexOf(byteArray, new Uint8Array([0x78, 0x90])));
```
*/
export function indexOf(array: Uint8Array, value: Uint8Array): number;

/**
Checks if the given sequence of bytes (`value`) is within the given `Uint8Array` (`array`).
Returns true if the value is included, otherwise false.
Replacement for [`Buffer#includes`](https://nodejs.org/api/buffer.html#bufincludesvalue-byteoffset-encoding). `Uint8Array#includes` only takes a number which is different from Buffer's `includes` implementation.
```
import {includes} from 'uint8array-extras';
const byteArray = new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]);
console.log(includes(byteArray, new Uint8Array([0x78, 0x90])));
//=> true
```
*/
export function includes(array: Uint8Array, value: Uint8Array): boolean;
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,12 @@ export function indexOf(array, value) {

return -1;
}

/**
@param {Uint8Array} array
@param {Uint8Array} value
@returns {boolean}
*/
export function includes(array, value) {
return indexOf(array, value) !== -1;
}
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,20 @@ const byteArray = new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef
console.log(indexOf(byteArray, new Uint8Array([0x78, 0x90])));
//=> 3
```

### `includes(array: Uint8Array, value: Uint8Array): boolean`

Checks if the given sequence of bytes (`value`) is within the given `Uint8Array` (`array`).

Returns true if the value is included, otherwise false.

Replacement for [`Buffer#includes`](https://nodejs.org/api/buffer.html#bufincludesvalue-byteoffset-encoding). `Uint8Array#includes` only takes a number which is different from Buffer's `includes` implementation.

```js
import {includes} from 'uint8array-extras';

const byteArray = new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]);

console.log(includes(byteArray, new Uint8Array([0x78, 0x90])));
//=> true
```
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
hexToUint8Array,
getUintBE,
indexOf,
includes,
} from './index.js';

test('isUint8Array', t => {
Expand Down Expand Up @@ -228,3 +229,9 @@ test('indexOf - single element found', t => {
const singleElement = [0x56];
t.is(indexOf(new Uint8Array(fixture), new Uint8Array(singleElement)), 2);
});

test('includes', t => {
const fixture = [0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]; // eslint-disable-line unicorn/number-literal-case
t.true(includes(new Uint8Array(fixture), new Uint8Array([0x78, 0x90])));
t.false(includes(new Uint8Array(fixture), new Uint8Array([0x90, 0x78])));
});

0 comments on commit 13ff2f7

Please sign in to comment.