-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: add strict input check allowing only byte arrays as …
…inputs (#87) From this release, only instances of Array with numeric values <= 255 are accepted as inputs. No strings or anything else except shapes like `Buffer` or `Uint8Array`.
- Loading branch information
Showing
4 changed files
with
44 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"singleQuote": true | ||
"singleQuote": true, | ||
"maxLineLength": 80 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { isByteArray } from './utils'; | ||
|
||
describe('isByteArray', () => { | ||
test('positives', () => { | ||
expect(isByteArray(Buffer.from('hello'))).toBe(true); | ||
expect(isByteArray(new Uint8Array(0))).toBe(true); | ||
expect(isByteArray(new Uint8Array(1))).toBe(true); | ||
expect(isByteArray([])).toBe(true); | ||
expect(isByteArray([1])).toBe(true); | ||
}); | ||
|
||
test('negatives', () => { | ||
expect(isByteArray(null)).toBe(false); | ||
expect(isByteArray('')).toBe(false); | ||
expect(isByteArray('hello')).toBe(false); | ||
expect(isByteArray(123)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// May also check if every element is a number <= 255 but | ||
// it a little bit slower | ||
export const isByteArray = (input: any): input is Uint8Array => { | ||
if (input == null || typeof input != 'object') return false; | ||
|
||
return isFinite(input.length) && input.length >= 0; | ||
}; |