-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from gcanti/fp-ts0.2
upgrade to latest fp-ts
- Loading branch information
Showing
10 changed files
with
118 additions
and
58 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
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 |
---|---|---|
@@ -1,26 +1,29 @@ | ||
import { Lens, Optional } from '../src' | ||
import { some, none } from 'fp-ts/lib/Option' | ||
import { Option, some, none } from 'fp-ts/lib/Option' | ||
import { nameLens, employee } from './Lens' | ||
|
||
const head = new Optional<string, string>( | ||
s => s.length > 0 ? some(s[0]) : none, | ||
(a, s) => a + s.substring(1) | ||
) | ||
|
||
// console.log(head.getOption('')) // => None | ||
// console.log(head.getOption('hello')) // => Some('h') | ||
// console.log(head.set('H', '')) // => 'H' | ||
// console.log(head.set('H', 'hello')) // => 'Hello' | ||
console.log(head.getOption('')) // => None | ||
console.log(head.getOption('hello')) // => Some('h') | ||
console.log(head.set('H', '')) // => 'H' | ||
console.log(head.set('H', 'hello')) // => 'Hello' | ||
|
||
const optional = nameLens.asOptional().compose(head) | ||
|
||
// console.log(JSON.stringify(optional.modify(s => s.toUpperCase(), employee), null, 2)) | ||
console.log(JSON.stringify(optional.modify(s => s.toUpperCase(), employee), null, 2)) | ||
|
||
interface Person { name: string, surname: string | null | undefined } | ||
interface Person { | ||
name: string, | ||
surname: Option<string> | ||
} | ||
|
||
const surname = Optional.fromProp<Person, 'surname', string>('surname') | ||
const surname = Optional.fromProp<Person, 'surname'>('surname') | ||
|
||
const p: Person = { name: 'Giulio', surname: null } | ||
const p: Person = { name: 'Giulio', surname: none } | ||
|
||
// console.log(surname.getOption(p)) | ||
// console.log(surname.set('Canti', p)) | ||
console.log(surname.getOption(p)) | ||
console.log(surname.set(some('Canti'), p)) |
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
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
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,34 @@ | ||
import { Lens } from '../src' | ||
import * as assert from 'assert' | ||
|
||
interface Street { num: number, name: string } | ||
interface Address { city: string, street: Street } | ||
interface Company { name: string, address: Address } | ||
interface Employee { name: string, company: Company } | ||
|
||
const employee: Employee = { | ||
name: "john", | ||
company: { | ||
name: "awesome inc", | ||
address: { | ||
city: "london", | ||
street: { | ||
num: 23, | ||
name: "high street" | ||
} | ||
} | ||
} | ||
} | ||
|
||
function capitalize(s: string): string { | ||
return s.substring(0, 1).toUpperCase() + s.substring(1) | ||
} | ||
|
||
describe('Lens', () => { | ||
|
||
it('fromPath', () => { | ||
const lens = Lens.fromPath<Employee, 'company', 'address', 'street', 'name'>(['company', 'address', 'street', 'name']) | ||
assert.strictEqual(lens.modify(capitalize, employee).company.address.street.name, 'High street') | ||
}) | ||
|
||
}) |
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