Parsers Reference
๐๏ธโ๐จ๏ธ Parsers without nonsense.
Instead of throwing or returning values like NaN
, the parsers in this library
either return the expected parsed value or undefined
(making use of the
Maybe type).
Usage
๐ฆ Node
Install @lou.codes/parsers
as a dependency:
1pnpm add @lou.codes/parsers2# or3npm install @lou.codes/parsers4# or5yarn add @lou.codes/parsers
Import it and use it:
1import { parseDecimal } from "@lou.codes/parsers";2
3parseDecimal("101"); // 1014parseDecimal("nope"); // undefined
๐ฆ Deno
Import @lou.codes/parsers
using the npm:
prefix, and use it directly:
1import { parseDecimal } from "npm:@lou.codes/parsers";2
3parseDecimal("101"); // 1014parseDecimal("nope"); // undefined
๐ Browser
Import @lou.codes/parsers
using esm.sh, and use it directly:
1<script type="module">2 import { parseDecimal } from "https://esm.sh/@lou.codes/parsers";3
4 parseDecimal("101"); // 1015 parseDecimal("nope"); // undefined6</script>
Useful links
- ๐ Documentation: TypeDoc generated documentation.
- โณ Changelog: List of changes between versions.
- โ Tests Coverage: Coveralls page with tests coverage.
Common
attempt()
1function attempt<Arguments, Output>(2 wrappedFunction: (...wrappedArguments: Arguments) => Output,3): (...parameters: Arguments) => Maybe<Output>;
Wrapper for try
/catch
.
Type parameters
Type parameter | Description |
---|---|
Arguments extends ReadOnlyArray | Type of the arguments of the function to be wrapped. |
Output | - |
Parameters
Parameter | Type | Description |
---|---|---|
wrappedFunction | (โฆwrappedArguments : Arguments ) => Output | Function to be wrapped. |
Returns
Function
Function wrapped in try
/catch
.
Parameters
Parameter | Type |
---|---|
โฆparameters | Arguments |
Returns
Maybe
<Output
>
Remarks
This functions tries to run a function and silences the throw
s by wrapping it
with a try/catch
and returning undefined
instead.
Example
1const willFail = () => {2 throw new Error("fail");3};4
5const safeWillFail = attempt(willFail);6safeWillFail(); // undefined
clone()
1function clone<Type>(value: Type): Maybe<Type>;
Safely clones a value, returning undefined
if any part of the input value is
not serializable.
Type parameters
Type parameter | Description |
---|---|
Type | Type of the value to be cloned. |
Parameters
Parameter | Type | Description |
---|---|---|
value | Type | Value to be cloned. |
Returns
Maybe
<Type
>
Clone of the value or undefined
if canโt be serialized.
Remarks
This util makes use of structuredClone
to clone the given value, but instead
of throwing a DataCloneError
, it simply returns undefined
when the value is
not serializable.
Example
1clone({ foo: "bar" }); // { foo: "bar" }2clone({ function: () => {} }); // undefined
See
JSON
omitProtoReviver()
1function omitProtoReviver(key: string, value: unknown): unknown;
Custom reviver that omits "__proto__"
for safer parsing.
Parameters
Parameter | Type | Description |
---|---|---|
key | string | Current key. |
value | unknown | Current value. |
Returns
unknown
The current value or undefined
if the key is "__proto__"
.
Remarks
JSON parsing has a proto poisoning vulnerability that can be exploited by
passing a JSON string with a __proto__
key. This reviver can be used to
prevent that.
Example
1JSON.parse('{"__proto__":"๐"}', omitProto); // {}
See
parseJSON()
1function parseJSON<Output>(...parameters: [string]): Maybe<Output>;
Safely parses a JSON string or returns undefined
if is invalid.
Type parameters
Type parameter | Value | Description |
---|---|---|
Output extends JSONValue | JSONValue | Generic of the output (has to be a JSONValue ). |
Parameters
Parameter | Type |
---|---|
โฆparameters | [string ] |
Returns
Maybe
<Output
>
Parsed string or undefined
if invalid JSON.
Remarks
JSON.parse
is unsafe by default, allowing proto poisoning. This function
takes care of it while making its types safer as well.
Example
1parseJSON('{"__proto__":"๐"}'); // {}2parseJSON("invalid"); // undefined
See
Number
parseBinary()
1function parseBinary(string: string): Maybe<number>;
Parses a string
to a binary number
.
Parameters
Parameter | Type | Description |
---|---|---|
string | string | String to be parsed. |
Returns
Maybe
<number
>
Parsed number
or undefined
if it fails.
Remarks
Parses a string
to a binary number
, returning undefined
instead of NaN
if it fails.
Example
1parseBinary("101"); // 0b101 -> 52parseBinary("101.5"); // 0b101 -> 53parseBinary("invalid"); // undefined
See
parseDecimal()
1function parseDecimal(string: string): Maybe<number>;
Parses a string
to a decimal number
.
Parameters
Parameter | Type | Description |
---|---|---|
string | string | String to be parsed. |
Returns
Maybe
<number
>
Parsed number
or undefined
if it fails.
Remarks
Parses a string
to a decimal number
, returning undefined
instead of NaN
if it fails.
Example
1parseDecimal("101"); // 1012parseDecimal("101.5"); // 1013parseDecimal("invalid"); // undefined
See
parseFloatingPoint()
1function parseFloatingPoint(string: string): Maybe<number>;
Safe parseFloat
alternative.
Parameters
Parameter | Type | Description |
---|---|---|
string | string | String to parse. |
Returns
Maybe
<number
>
Parsed number or undefined
if invalid.
Remarks
Parses a string
to a float number
. Returns undefined
instead of NaN
if
it fails.
Example
1parseFloatingPoint(10); // 102parseFloatingPoint(13.1); // 13.13parseFloatingPoint("invalid"); // undefined
See
parseHexadecimal()
1function parseHexadecimal(string: string): Maybe<number>;
Parses a string
to a hexadecimal number
.
Parameters
Parameter | Type | Description |
---|---|---|
string | string | String to be parsed. |
Returns
Maybe
<number
>
Parsed number
or undefined
if it fails.
Remarks
Parses a string
to a hexadecimal number
, returning undefined
instead of
NaN
if it fails.
Example
1parseHexadecimal("101"); // 0x101 -> 2572parseHexadecimal("101.5"); // 0x101 -> 2573parseHexadecimal("invalid"); // undefined
See
parseInteger()
1function parseInteger(radix: Radix): (string: string) => Maybe<number>;
Curried function to parse strings to numbers based on different radixes.
Parameters
Parameter | Type | Description |
---|---|---|
radix | Radix | Radix to use for parsing (16 for hexadecimal, 10 for decimal, and so on). |
Returns
Function
Curried function with radix
in context.
Curried function with radix
set.
Parameters
Parameter | Type | Description |
---|---|---|
string | string | String to parse. |
Returns
Maybe
<number
>
Parsed number
or undefined
if it fails.
See
Remarks
Parses a string
to a number
with the given radix
, returning undefined
instead of NaN
if it fails.
Example
1const parseDecimal = parseInteger(10);2
3parseDecimal("101"); // 1014parseDecimal("101.5"); // 1015parseDecimal("invalid"); // undefined
See
parseOctal()
1function parseOctal(string: string): Maybe<number>;
Parses a string
to a octal number
.
Parameters
Parameter | Type | Description |
---|---|---|
string | string | String to be parsed. |
Returns
Maybe
<number
>
Parsed number
or undefined
if it fails.
Remarks
Parses a string
to a octal number
, returning undefined
instead of NaN
if
it fails.
Example
1parseOctal("101"); // 0o101 -> 652parseOctal("101.5"); // 0o101 -> 653parseOctal("invalid"); // undefined
See
undefineNaN()
1function undefineNaN<Number>(number: Number): Maybe<Number>;
NaN handler.
Type parameters
Type parameter | Description |
---|---|
Number extends number | Generic of the number to handle. |
Parameters
Parameter | Type | Description |
---|---|---|
number | Number | Number to handle. |
Returns
Maybe
<Number
>
Number if itโs not NaN
, otherwise undefined
.
Remarks
Takes a number
that could be NaN
and makes it undefined
if it is NaN
.
Example
1undefineNaN(10); // 102undefineNaN(13.1); // 13.13undefineNaN(NaN); // undefined