Skip to content

Commit

Permalink
Merge pull request #2 from gcanti/optional
Browse files Browse the repository at this point in the history
fix Optional.fromProp
  • Loading branch information
gcanti authored Mar 24, 2017
2 parents d56fce6 + a0ae4c6 commit 3741818
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monocle-ts",
"version": "0.0.3",
"version": "0.0.4",
"description": "A porting of scala monocle library to TypeScript",
"files": [
"lib",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ export class Optional<S, A> {
){}

/** generate an optional from a type and a prop */
static fromProp<T, P extends keyof T, V extends T[P]>(prop: P): Optional<T, V> {
return new Optional<T, V>(
s => s[prop] != null ? option.some(s[prop]) : option.none,
static fromProp<T extends { [K in P]: Option<any> }, P extends keyof T>(prop: P): Optional<T, T[P]> {
return new Optional<T, T[P]>(
s => s[prop],
(a, s) => Object.assign({}, s, { [prop as any]: a })
)
}
Expand Down
22 changes: 22 additions & 0 deletions test/Optional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Option, none, some } from 'fp-ts/lib/Option'
import { Optional } from '../src'
import { eqOptions as eq } from './helpers'

type A = {
a: Option<number>
}

const optional = Optional.fromProp<A, 'a'>('a')

describe('Optional', () => {

it('getOption', () => {
eq(optional.getOption({ a: none }), none)
})

it('set', () => {
eq(optional.set(none, { a: some(1) }).a, none)
eq(optional.set(some(2), { a: some(1) }).a, some(2))
})

})
10 changes: 10 additions & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as assert from 'assert'
import { HKTOption, isSome, isNone } from 'fp-ts/lib/Option'

export function eqOptions<A>(x: HKTOption<A>, y: HKTOption<A>) {
if (isSome(x) && isSome(y)) {
assert.deepEqual(x.value, y.value)
} else {
assert.strictEqual(isNone(x), isNone(y))
}
}
16 changes: 16 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"strictNullChecks": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"lib": [
"es6", "dom"
]
}
}

0 comments on commit 3741818

Please sign in to comment.