Skip to content

Commit 3b8fa8f

Browse files
authored
Merge pull request #14 from Lojdquist/master
Add Decoder.buffer
2 parents 9392764 + e723ed5 commit 3b8fa8f

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "6.1.1",
2+
"version": "6.1.2",
33
"license": "MIT",
44
"main": "dist/index.js",
55
"typings": "dist/index.d.ts",
@@ -42,6 +42,7 @@
4242
"module": "dist/decoders.esm.js",
4343
"devDependencies": {
4444
"@types/jest": "^25.2.1",
45+
"@types/node": "^15.12.2",
4546
"@typescript-eslint/eslint-plugin": "^2.13.0",
4647
"@typescript-eslint/parser": "^2.13.0",
4748
"coveralls": "^3.0.9",

src/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ export class Decoder<T> {
322322
);
323323

324324
/**
325-
* A decoder that accepts undefined.
325+
* A decoder that accepts null.
326326
*
327327
* Example:
328328
* ```
@@ -336,6 +336,22 @@ export class Decoder<T> {
336336
? Result.ok(data)
337337
: Result.fail(makeSingleError('Not null', data))
338338
);
339+
/**
340+
* A decoder that accepts a Buffer.
341+
*
342+
* Example:
343+
* ```
344+
* Decoder.null.run(undefined) // FAIL
345+
* Decoder.null.run(5) // FAIL
346+
* Decoder.null.run(Buffer.from('Hello world')) // OK
347+
* Decoder.null.run(<Buffer 68 65 6c 6c 6f>) // OK
348+
*```
349+
*/
350+
public static buffer: Decoder<Buffer> = new Decoder((data) =>
351+
Buffer.isBuffer(data)
352+
? Result.ok(data)
353+
: Result.fail(makeSingleError('Not Buffer', data))
354+
);
339355

340356
/**
341357
* Decodes a string.

test/decoder.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ describe('Undefined decoder', () => {
246246
});
247247
});
248248

249+
describe('Buffer decoder', () => {
250+
it('decodes Buffer', () => {
251+
const data = Buffer.from('hello');
252+
const result = Decoder.buffer.run(data);
253+
expect(result).toEqual({ type: 'OK', value: data });
254+
});
255+
256+
it('does not decode invalid data', () => {
257+
fc.assert(
258+
fc.property(fc.anything(), (anything: any) => {
259+
fc.pre(!Buffer.isBuffer(anything));
260+
const res = Decoder.buffer.run(anything);
261+
expect(res).toHaveProperty('type', 'FAIL');
262+
})
263+
);
264+
});
265+
});
266+
249267
describe('Methods', () => {
250268
it('map', () => {
251269
const setDecoder = Decoder.array(Decoder.number).map((arr) => new Set(arr));

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,11 @@
11271127
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.5.tgz#3d03acd3b3414cf67faf999aed11682ed121f22b"
11281128
integrity sha512-90hiq6/VqtQgX8Sp0EzeIsv3r+ellbGj4URKj5j30tLlZvRUpnAe9YbYnjl3pJM93GyXU0tghHhvXHq+5rnCKA==
11291129

1130+
"@types/node@^15.12.2":
1131+
version "15.12.2"
1132+
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.12.2.tgz#1f2b42c4be7156ff4a6f914b2fb03d05fa84e38d"
1133+
integrity sha512-zjQ69G564OCIWIOHSXyQEEDpdpGl+G348RAKY0XXy9Z5kU9Vzv1GMNnkar/ZJ8dzXB3COzD9Mo9NtRZ4xfgUww==
1134+
11301135
"@types/parse-json@^4.0.0":
11311136
version "4.0.0"
11321137
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"

0 commit comments

Comments
 (0)