Skip to content

Commit

Permalink
add Prism.fromPredicate fix #4 (#5)
Browse files Browse the repository at this point in the history
fix Optional.fromProp
  • Loading branch information
gcanti authored Apr 9, 2017
1 parent b8e05f4 commit d1e281c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/Optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ const surname = Optional.fromProp<Person, 'surname'>('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))
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand All @@ -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",
Expand Down
19 changes: 13 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -43,7 +43,7 @@ export class Iso<S, A> {
/** view a ISO as a Prism */
asPrism(): Prism<S, A> {
return new Prism(
s => new Some(this.get(s)),
s => some(this.get(s)),
this.reverseGet
)
}
Expand Down Expand Up @@ -101,7 +101,7 @@ export class Lens<S, A> {
/** view a Lens as a Option */
asOptional(): Optional<S, A> {
return new Optional(
s => new Some(this.get(s)),
s => some(this.get(s)),
this.set
)
}
Expand All @@ -118,6 +118,13 @@ export class Prism<S, A> {
public reverseGet: (a: A) => S
) { }

static fromPredicate<A>(predicate: Predicate<A>): Prism<A, A> {
return new Prism<A, A>(
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)
Expand Down Expand Up @@ -157,11 +164,11 @@ export class Optional<S, A> {
public set: (a: A, s: S) => S
){}

/** generate an optional from a type and a prop */
static fromProp<T extends { [K in P]: Option<any> }, P extends keyof T>(prop: P): Optional<T, T[P]> {
/** generate an optional from a type and a prop which is a `Option` */
static fromProp<T extends { [K in P]: Option<any> }, P extends keyof T>(prop: P): Optional<T, T[P]['_A']> {
return new Optional<T, T[P]>(
s => s[prop],
(a, s) => Object.assign({}, s, { [prop as any]: a })
(a, s) => Object.assign({}, s, { [prop as any]: some(a) })
)
}

Expand Down
4 changes: 2 additions & 2 deletions test/Optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

})
13 changes: 13 additions & 0 deletions test/Prism.ts
Original file line number Diff line number Diff line change
@@ -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<number>(n => n % 1 === 0)
eq(prism.getOption(1), some(1))
eq(prism.getOption(1.1), none)
})

})

0 comments on commit d1e281c

Please sign in to comment.