-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
169 additions
and
16 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
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,64 @@ | ||
--- | ||
title: JsonFromString.ts | ||
nav_order: 16 | ||
parent: Modules | ||
--- | ||
|
||
# JsonFromString overview | ||
|
||
Added in v0.5.14 | ||
|
||
--- | ||
|
||
<h2 class="text-delta">Table of contents</h2> | ||
|
||
- [JsonArray (interface)](#jsonarray-interface) | ||
- [JsonRecord (interface)](#jsonrecord-interface) | ||
- [Json (type alias)](#json-type-alias) | ||
- [JSONFromString](#jsonfromstring) | ||
|
||
--- | ||
|
||
# JsonArray (interface) | ||
|
||
**Signature** | ||
|
||
```ts | ||
export interface JsonArray extends ReadonlyArray<Json> {} | ||
``` | ||
|
||
Added in v0.5.14 | ||
|
||
# JsonRecord (interface) | ||
|
||
**Signature** | ||
|
||
```ts | ||
export interface JsonRecord { | ||
readonly [key: string]: Json | ||
} | ||
``` | ||
|
||
Added in v0.5.14 | ||
|
||
# Json (type alias) | ||
|
||
Copied from `fp-ts/Either` module. | ||
|
||
**Signature** | ||
|
||
```ts | ||
export type Json = boolean | number | string | null | JsonArray | JsonRecord | ||
``` | ||
Added in v0.5.14 | ||
# JSONFromString | ||
**Signature** | ||
```ts | ||
export const JSONFromString: t.Type<Json, string, string> = ... | ||
``` | ||
|
||
Added in v0.5.14 |
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,6 +1,6 @@ | ||
--- | ||
title: NonEmptyString.ts | ||
nav_order: 18 | ||
nav_order: 19 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: NumberFromString.ts | ||
nav_order: 19 | ||
nav_order: 20 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: UUID.ts | ||
nav_order: 26 | ||
nav_order: 27 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: mapOutput.ts | ||
nav_order: 16 | ||
nav_order: 17 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: nonEmptyArray.ts | ||
nav_order: 17 | ||
nav_order: 18 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: option.ts | ||
nav_order: 20 | ||
nav_order: 21 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: optionFromNullable.ts | ||
nav_order: 21 | ||
nav_order: 22 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: readonlyNonEmptyArray.ts | ||
nav_order: 22 | ||
nav_order: 23 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: readonlySetFromArray.ts | ||
nav_order: 23 | ||
nav_order: 24 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: regexp.ts | ||
nav_order: 24 | ||
nav_order: 25 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: setFromArray.ts | ||
nav_order: 25 | ||
nav_order: 26 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: withEncode.ts | ||
nav_order: 27 | ||
nav_order: 28 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: withFallback.ts | ||
nav_order: 28 | ||
nav_order: 29 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: withMessage.ts | ||
nav_order: 29 | ||
nav_order: 30 | ||
parent: Modules | ||
--- | ||
|
||
|
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,6 +1,6 @@ | ||
--- | ||
title: withValidate.ts | ||
nav_order: 30 | ||
nav_order: 31 | ||
parent: Modules | ||
--- | ||
|
||
|
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,43 @@ | ||
/** | ||
* @since 0.5.14 | ||
*/ | ||
import * as t from 'io-ts' | ||
|
||
/** | ||
* Copied from `fp-ts/Either` module. | ||
* | ||
* @since 0.5.14 | ||
*/ | ||
export type Json = boolean | number | string | null | JsonArray | JsonRecord | ||
|
||
/** | ||
* @since 0.5.14 | ||
*/ | ||
export interface JsonRecord { | ||
readonly [key: string]: Json | ||
} | ||
|
||
/** | ||
* @since 0.5.14 | ||
*/ | ||
export interface JsonArray extends ReadonlyArray<Json> {} | ||
|
||
const JsonArray: t.Type<JsonArray> = t.recursion('JsonArray', () => t.readonlyArray(Json)) | ||
const JsonRecord: t.Type<JsonRecord> = t.recursion('JsonRecord', () => t.record(t.string, Json)) | ||
const Json: t.Type<Json> = t.union([t.boolean, t.number, t.string, t.null, JsonArray, JsonRecord], 'Json') | ||
|
||
/** | ||
* @since 0.5.14 | ||
*/ | ||
export const JSONFromString = new t.Type<Json, string, string>( | ||
'JSONFromString', | ||
Json.is, | ||
(s, c) => { | ||
try { | ||
return t.success(JSON.parse(s)) | ||
} catch (e) { | ||
return t.failure(s, c) | ||
} | ||
}, | ||
json => JSON.stringify(json) | ||
) |
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,36 @@ | ||
import * as assert from 'assert' | ||
import { JSONFromString } from '../src' | ||
import { assertFailure, assertSuccess } from './helpers' | ||
|
||
describe('JSONFromString', () => { | ||
it('is', () => { | ||
assert.deepStrictEqual(JSONFromString.is(null), true) | ||
assert.deepStrictEqual(JSONFromString.is('a'), true) | ||
assert.deepStrictEqual(JSONFromString.is(1), true) | ||
assert.deepStrictEqual(JSONFromString.is(true), true) | ||
assert.deepStrictEqual(JSONFromString.is(false), true) | ||
assert.deepStrictEqual(JSONFromString.is([]), true) | ||
assert.deepStrictEqual(JSONFromString.is([1]), true) | ||
assert.deepStrictEqual(JSONFromString.is({}), true) | ||
assert.deepStrictEqual(JSONFromString.is({ a: 1 }), true) | ||
assert.deepStrictEqual(JSONFromString.is({ a: Date }), false) | ||
}) | ||
|
||
it('decode', () => { | ||
const T = JSONFromString | ||
assertSuccess(T.decode('null'), null) | ||
assertSuccess(T.decode('1'), 1) | ||
assertSuccess(T.decode('"a"'), 'a') | ||
assertSuccess(T.decode('true'), true) | ||
assertSuccess(T.decode('false'), false) | ||
assertSuccess(T.decode('[]'), []) | ||
assertSuccess(T.decode('{}'), {}) | ||
assertFailure(T, '{', ['Invalid value "{" supplied to : JSONFromString']) | ||
assertFailure(T, '{"a":undefined}', ['Invalid value "{\\"a\\":undefined}" supplied to : JSONFromString']) | ||
}) | ||
|
||
it('encode', () => { | ||
const T = JSONFromString | ||
assert.deepEqual(T.encode({}), '{}') | ||
}) | ||
}) |