fp-ts-std
Version:
The missing pseudo-standard library for fp-ts.
136 lines • 3.67 kB
TypeScript
/**
* Various functions to aid in working with JSON.
*
* @since 0.1.0
*/
import type { Either } from "fp-ts/Either";
import type { Json } from "fp-ts/Json";
import type { Option } from "fp-ts/Option";
import type { Newtype } from "newtype-ts";
type JSONStringSymbol = {
readonly JSONString: unique symbol;
};
/**
* Newtype representing stringified JSON.
*
* @example
* import { JSONString, stringifyPrimitive } from 'fp-ts-std/JSON'
*
* const safeToParse: JSONString = stringifyPrimitive('foo')
*
* @category 0 Types
* @since 0.5.0
*/
export type JSONString = Newtype<JSONStringSymbol, string>;
/**
* Unwrap a `JSONString` newtype back to its underlying string representation.
*
* @category 3 Functions
* @since 0.6.0
*/
export declare const unJSONString: (x: JSONString) => string;
/**
* Stringify some arbitrary data.
*
* @example
* import { stringify } from 'fp-ts-std/JSON'
* import * as E from 'fp-ts/Either'
* import { constant } from 'fp-ts/function'
*
* const f = stringify(constant('e'))
*
* const valid = 'abc'
* const invalid = () => {}
*
* assert.deepStrictEqual(f(valid), E.right('"abc"'))
* assert.deepStrictEqual(f(invalid), E.left('e'))
*
* @category 3 Functions
* @since 0.1.0
*/
export declare const stringify: <E>(f: (e: TypeError) => E) => (x: unknown) => Either<E, JSONString>;
/**
* Stringify some arbitrary data, returning an `Option`.
*
* @example
* import { stringifyO } from 'fp-ts-std/JSON'
* import * as O from 'fp-ts/Option'
*
* const valid = 'abc'
* const invalid = () => {}
*
* assert.deepStrictEqual(stringifyO(valid), O.some('"abc"'))
* assert.deepStrictEqual(stringifyO(invalid), O.none)
*
* @category 3 Functions
* @since 0.1.0
*/
export declare const stringifyO: (data: unknown) => Option<JSONString>;
/**
* Stringify a primitive value with no possibility of failure.
*
* @example
* import { stringifyPrimitive } from 'fp-ts-std/JSON'
*
* assert.strictEqual(stringifyPrimitive('abc'), '"abc"')
*
* @category 3 Functions
* @since 0.1.0
*/
export declare const stringifyPrimitive: (x: string | number | boolean | null) => JSONString;
/**
* Parse a string as JSON. This is safe provided there have been no shenanigans
* with the `JSONString` newtype.
*
* @example
* import { unstringify, stringifyPrimitive } from 'fp-ts-std/JSON'
* import { flow } from 'fp-ts/function'
*
* const f = flow(stringifyPrimitive, unstringify)
*
* assert.strictEqual(f('abc'), 'abc')
*
* @category 3 Functions
* @since 0.5.0
*/
export declare const unstringify: (x: JSONString) => unknown;
/**
* Parse a string as JSON. The `Json` type on the right side comes from `fp-ts`
* and is a union of all possible parsed types.
*
* @example
* import { parse } from 'fp-ts-std/JSON'
* import * as E from 'fp-ts/Either'
* import { constant } from 'fp-ts/function'
*
* const f = parse(constant('e'))
*
* const valid = '"abc"'
* const invalid = 'abc'
*
* assert.deepStrictEqual(f(valid), E.right('abc'))
* assert.deepStrictEqual(f(invalid), E.left('e'))
*
* @category 3 Functions
* @since 0.1.0
*/
export declare const parse: <E>(f: (e: SyntaxError) => E) => (x: string) => Either<E, Json>;
/**
* Parse a string as JSON, returning an `Option`.
*
* @example
* import { parseO } from 'fp-ts-std/JSON'
* import * as O from 'fp-ts/Option'
*
* const valid = '"abc"'
* const invalid = 'abc'
*
* assert.deepStrictEqual(parseO(valid), O.some('abc'))
* assert.deepStrictEqual(parseO(invalid), O.none)
*
* @category 3 Functions
* @since 0.1.0
*/
export declare const parseO: (stringified: string) => Option<unknown>;
export {};
//# sourceMappingURL=JSON.d.ts.map