Skip to content

Commit

Permalink
Let all paramless decoder factories return singletons
Browse files Browse the repository at this point in the history
By returning singleton instances of their decoders, this becomes
slightly faster, takes up less memory, and makes it possible to compare
decoders for identity, should you want that...

    decodeNumber() === decodeNumber()
  • Loading branch information
nvie committed Oct 10, 2017
1 parent affd388 commit 00761f9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
10 changes: 6 additions & 4 deletions src/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import { assertType } from './asserts';
import type { Decoder } from './types';

const booleanDecoder: Decoder<boolean> = blob => {
assertType(blob, 'boolean');
return (blob: boolean);
};

/**
* Decodes a boolean value.
* Will throw a DecodeError if anything other than a boolean value is found.
*/
export function decodeBoolean(): Decoder<boolean> {
return (blob: any) => {
assertType(blob, 'boolean');
return (blob: boolean);
};
return booleanDecoder;
}
13 changes: 8 additions & 5 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import { assertTest, assertType } from './asserts';
import type { Decoder } from './types';

const nullDecoder: Decoder<null> = decodeConstant(null);
const undefinedDecoder: Decoder<void> = blob => {
assertType(blob, 'undefined');
return undefined;
};

/**
* Decodes any hardcoded value, without looking at the input data.
*/
Expand Down Expand Up @@ -31,15 +37,12 @@ export function decodeConstant<T>(value: T): Decoder<T> {
* Decodes the null value.
*/
export function decodeNull(): Decoder<null> {
return decodeConstant(null);
return nullDecoder;
}

/**
* Decodes the undefined value.
*/
export function decodeUndefined(): Decoder<void> {
return (blob: any) => {
assertType(blob, 'undefined');
return undefined;
};
return undefinedDecoder;
}
10 changes: 6 additions & 4 deletions src/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import { assertTest } from './asserts';
import type { Decoder } from './types';

const numberDecoder: Decoder<number> = blob => {
assertTest(blob, Number.isFinite, 'Not a number', 'Expected a finite number');
return (blob: number);
};

/**
* Decodes a finite (!) number (integer or float) value. Will throw
* a `DecodeError` if anything other than a finite number value is found. This
* means that values like `NaN`, or positive and negative `Infinity` are not
* considered valid numbers.
*/
export function decodeNumber(): Decoder<number> {
return (blob: any) => {
assertTest(blob, Number.isFinite, 'Not a number', 'Expected a finite number');
return (blob: number);
};
return numberDecoder;
}
10 changes: 6 additions & 4 deletions src/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import { assertType } from './asserts';
import type { Decoder } from './types';

const stringDecoder: Decoder<string> = blob => {
assertType(blob, 'string');
return (blob: string);
};

/**
* Decodes a string value.
* Will throw a DecodeError if anything other than a string value is found.
*/
export function decodeString(): Decoder<string> {
return (blob: any) => {
assertType(blob, 'string');
return (blob: string);
};
return stringDecoder;
}

0 comments on commit 00761f9

Please sign in to comment.