From 2d1233d4bcee8378b18cd075fdf2fc18fb104f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Van=20Keisbelck?= Date: Thu, 1 Oct 2020 02:40:05 +0200 Subject: [PATCH 1/5] CHANGELOG --- CHANGELOG.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f558d45..5bf1fea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,26 @@ # Changelog -## v1.1.0 (01/10/2020) +## v1.1.1 (01/10/2020) + +#### closed + +- [**closed**] upgrade react-scripts in samples [#29](https://github.com/vankeisb/react-tea-cup/pull/29) +--- + +## v1.1.0 (01/10/2020) - [**closed**] Do not rely on state in Program [#25](https://github.com/vankeisb/react-tea-cup/pull/25) - [**closed**] fix #26 : use componentWillUnmount instead of componentWillMount [#28](https://github.com/vankeisb/react-tea-cup/pull/28) - [**closed**] dispatch initial cmd on mount [#23](https://github.com/vankeisb/react-tea-cup/pull/23) +--- + +## v1.0.1 (01/10/2020) + +--- + +## v1.0.2 (01/10/2020) + +--- + +## v1.0.0 (01/10/2020) From 3434bb9dde1781b3d9d90a03d4be1e523b639e01 Mon Sep 17 00:00:00 2001 From: Frank Wagner Date: Fri, 16 Oct 2020 18:41:31 +0200 Subject: [PATCH 2/5] fix decoder for any value --- core/src/TeaCup/Decode.test.ts | 5 +++++ core/src/TeaCup/Decode.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/core/src/TeaCup/Decode.test.ts b/core/src/TeaCup/Decode.test.ts index 19ac0ad..e01fb98 100644 --- a/core/src/TeaCup/Decode.test.ts +++ b/core/src/TeaCup/Decode.test.ts @@ -260,3 +260,8 @@ test('oneOf', () => { test('from string', () => { expect(num.decodeString('123')).toEqual(ok(123)); }); + +test('any value', () => { + const anyValue = { foo: 'bar' }; + expect(Decode.value.decodeValue(anyValue)).toEqual(ok(anyValue)); +}); diff --git a/core/src/TeaCup/Decode.ts b/core/src/TeaCup/Decode.ts index da05fce..78d4de6 100644 --- a/core/src/TeaCup/Decode.ts +++ b/core/src/TeaCup/Decode.ts @@ -439,7 +439,7 @@ export class Decode { /** * Decoder for any value */ - static value: Decoder = new Decoder((o) => o); + static value: Decoder = new Decoder((o) => ok(o)); /** * Decoder for null From 654047d23d1d5d479b689572e6107211f48b36f7 Mon Sep 17 00:00:00 2001 From: Frank Wagner Date: Sat, 17 Oct 2020 13:34:47 +0200 Subject: [PATCH 3/5] add optionalField decoder --- core/src/TeaCup/Decode.test.ts | 27 +++++++++++++++++++++++++++ core/src/TeaCup/Decode.ts | 12 ++++++++++++ 2 files changed, 39 insertions(+) diff --git a/core/src/TeaCup/Decode.test.ts b/core/src/TeaCup/Decode.test.ts index e01fb98..2a27e4a 100644 --- a/core/src/TeaCup/Decode.test.ts +++ b/core/src/TeaCup/Decode.test.ts @@ -265,3 +265,30 @@ test('any value', () => { const anyValue = { foo: 'bar' }; expect(Decode.value.decodeValue(anyValue)).toEqual(ok(anyValue)); }); + +describe('optional field', () => { + test("is present", () => { + const value = { foo: 'bar', gnu: 13 }; + expect(Decode.optionalField('gnu', Decode.num).decodeValue(value)).toEqual(ok(13)); + }) + test("is missing", () => { + const value = { foo: 'bar' }; + expect(Decode.optionalField('gnu', Decode.num).decodeValue(value)).toEqual(ok(undefined)); + }) + + test("typical use case", () => { + type MyType = { + foo: string; + gnu?: number; + } + const value = { foo: 'bar' }; + const expected: MyType = { + foo: 'bar' + }; + expect(Decode.map2( + (foo, gnu) => { return { foo, gnu } }, + Decode.field('foo', Decode.str), + Decode.optionalField('gnu', Decode.num)).decodeValue(value) + ).toEqual(ok(expected)); + }) +}); diff --git a/core/src/TeaCup/Decode.ts b/core/src/TeaCup/Decode.ts index 78d4de6..25ce3da 100644 --- a/core/src/TeaCup/Decode.ts +++ b/core/src/TeaCup/Decode.ts @@ -178,6 +178,18 @@ export class Decode { return Decode.at([key], d); } + /** + * Decoder for optinal object fields + * @param key the name of the field + * @param d the decoder for the field's value + */ + static optionalField(key: string, d: Decoder): Decoder { + return Decode.andThen( + (value) => value.map>(() => Decode.field(key, d)).withDefault(Decode.succeed(undefined)), + Decode.maybe(Decode.field(key, Decode.value)) + ); + } + /** * Decoder for navigable object properties * @param keys a list of fields to navigate From e82ecf0a32e23fcb9bd76ed44bc7d67db0eca462 Mon Sep 17 00:00:00 2001 From: Frank Wagner Date: Sat, 17 Oct 2020 13:43:46 +0200 Subject: [PATCH 4/5] improved implementation --- core/src/TeaCup/Decode.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/TeaCup/Decode.ts b/core/src/TeaCup/Decode.ts index 25ce3da..666f7e5 100644 --- a/core/src/TeaCup/Decode.ts +++ b/core/src/TeaCup/Decode.ts @@ -180,12 +180,12 @@ export class Decode { /** * Decoder for optinal object fields - * @param key the name of the field + * @param key the name of the optinal field * @param d the decoder for the field's value */ static optionalField(key: string, d: Decoder): Decoder { return Decode.andThen( - (value) => value.map>(() => Decode.field(key, d)).withDefault(Decode.succeed(undefined)), + (value) => value.map>(v => new Decoder(() => d.decodeValue(v))).withDefault(Decode.succeed(undefined)), Decode.maybe(Decode.field(key, Decode.value)) ); } From edcb33c9ec4c4b71b16a39631d9b9762ee65f7d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Van=20Keisbelck?= Date: Mon, 19 Oct 2020 09:46:53 +0200 Subject: [PATCH 5/5] bump to 1.1.2 --- core/package.json | 2 +- samples/package.json | 2 +- tea-cup/package.json | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/package.json b/core/package.json index 219b7d7..e3d1425 100644 --- a/core/package.json +++ b/core/package.json @@ -1,6 +1,6 @@ { "name": "tea-cup-core", - "version": "1.1.1", + "version": "1.1.2", "description": "react-tea-cup core classes and utilities (Maybe etc)", "author": "Rémi Van Keisbelck ", "license": "MIT", diff --git a/samples/package.json b/samples/package.json index 793217c..cfcdbcf 100644 --- a/samples/package.json +++ b/samples/package.json @@ -11,7 +11,7 @@ "react": "^16.7.0", "react-dom": "^16.7.0", "react-scripts": "3.4.3", - "react-tea-cup": "1.1.1" + "react-tea-cup": "1.1.2" }, "scripts": { "start": "react-scripts start", diff --git a/tea-cup/package.json b/tea-cup/package.json index b366831..6f54358 100644 --- a/tea-cup/package.json +++ b/tea-cup/package.json @@ -1,6 +1,6 @@ { "name": "react-tea-cup", - "version": "1.1.1", + "version": "1.1.2", "description": "Put some TEA in your React.", "author": "Rémi Van Keisbelck ", "license": "MIT", @@ -19,7 +19,7 @@ "samples": "tsc --outFile ./dist/Samples/index.js && cp ./src/Samples/index.html ./dist/Samples" }, "dependencies": { - "tea-cup-core": "1.1.1", + "tea-cup-core": "1.1.2", "react": "^16.7.0" }, "devDependencies": {