From d1e281c4c677c088281e8add56360e9adf58a63a Mon Sep 17 00:00:00 2001 From: Giulio Canti Date: Sun, 9 Apr 2017 17:30:38 +0200 Subject: [PATCH] add Prism.fromPredicate fix #4 (#5) fix Optional.fromProp --- examples/Optional.ts | 2 +- package.json | 7 ++++--- src/index.ts | 19 +++++++++++++------ test/Optional.ts | 4 ++-- test/Prism.ts | 13 +++++++++++++ 5 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 test/Prism.ts diff --git a/examples/Optional.ts b/examples/Optional.ts index d6eff0f..ccd2079 100644 --- a/examples/Optional.ts +++ b/examples/Optional.ts @@ -26,4 +26,4 @@ const surname = Optional.fromProp('surname') const p: Person = { name: 'Giulio', surname: none } console.log(surname.getOption(p)) -console.log(surname.set(some('Canti'), p)) +console.log(surname.set('Canti', p)) diff --git a/package.json b/package.json index d69522b..2a3e535 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "monocle-ts", - "version": "0.1.0", + "version": "0.1.1", "description": "A porting of scala monocle library to TypeScript", "files": [ "lib", @@ -10,7 +10,8 @@ "jsnext:main": "lib-jsnext/index.js", "typings": "lib/index.d.ts", "scripts": { - "test": "mocha -r ts-node/register test/*.ts", + "mocha": "mocha -r ts-node/register test/*.ts", + "test": "npm run mocha", "clean": "rm -rf lib/* && rm -rf lib-jsnext/*", "build": "npm run clean && tsc && tsc -m es6 --outDir lib-jsnext" }, @@ -25,7 +26,7 @@ }, "homepage": "https://github.com/gcanti/monocle-ts", "dependencies": { - "fp-ts": "^0.2.0" + "fp-ts": "^0.2.1" }, "devDependencies": { "@types/mocha": "^2.2.38", diff --git a/src/index.ts b/src/index.ts index 64af5e7..71712f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import { StaticApplicative } from 'fp-ts/lib/Applicative' import { StaticFoldable, foldMap } from 'fp-ts/lib/Foldable' import { StaticTraversable } from 'fp-ts/lib/Traversable' import * as option from 'fp-ts/lib/Option' -import { Some, Option } from 'fp-ts/lib/Option' +import { Option, none, some } from 'fp-ts/lib/Option' import { identity, constant, Predicate } from 'fp-ts/lib/function' import * as id from 'fp-ts/lib/Identity' import * as con from 'fp-ts/lib/Const' @@ -43,7 +43,7 @@ export class Iso { /** view a ISO as a Prism */ asPrism(): Prism { return new Prism( - s => new Some(this.get(s)), + s => some(this.get(s)), this.reverseGet ) } @@ -101,7 +101,7 @@ export class Lens { /** view a Lens as a Option */ asOptional(): Optional { return new Optional( - s => new Some(this.get(s)), + s => some(this.get(s)), this.set ) } @@ -118,6 +118,13 @@ export class Prism { public reverseGet: (a: A) => S ) { } + static fromPredicate(predicate: Predicate): Prism { + return new Prism( + s => predicate(s) ? some(s) : none, + a => a + ) + } + modify(f: (a: A) => A, s: S): S { return this.modifyOption(f, s) .fold(constant(s), identity) @@ -157,11 +164,11 @@ export class Optional { public set: (a: A, s: S) => S ){} - /** generate an optional from a type and a prop */ - static fromProp }, P extends keyof T>(prop: P): Optional { + /** generate an optional from a type and a prop which is a `Option` */ + static fromProp }, P extends keyof T>(prop: P): Optional { return new Optional( s => s[prop], - (a, s) => Object.assign({}, s, { [prop as any]: a }) + (a, s) => Object.assign({}, s, { [prop as any]: some(a) }) ) } diff --git a/test/Optional.ts b/test/Optional.ts index 472ac74..45830bf 100644 --- a/test/Optional.ts +++ b/test/Optional.ts @@ -12,11 +12,11 @@ describe('Optional', () => { it('getOption', () => { eq(optional.getOption({ a: none }), none) + eq(optional.getOption({ a: some(1) }), some(1)) }) it('set', () => { - eq(optional.set(none, { a: some(1) }).a, none) - eq(optional.set(some(2), { a: some(1) }).a, some(2)) + eq(optional.set(2, { a: some(1) }).a, some(2)) }) }) diff --git a/test/Prism.ts b/test/Prism.ts new file mode 100644 index 0000000..e338f5c --- /dev/null +++ b/test/Prism.ts @@ -0,0 +1,13 @@ +import { none, some } from 'fp-ts/lib/Option' +import { Prism } from '../src' +import { eqOptions as eq } from './helpers' + +describe('Prism', () => { + + it('fromPredicate', () => { + const prism = Prism.fromPredicate(n => n % 1 === 0) + eq(prism.getOption(1), some(1)) + eq(prism.getOption(1.1), none) + }) + +})