Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 2.4.0

- **New Feature**
- `Tuple`
- add `_1` constructor (@toastal)
- add `_2` constructor (@toastal)

# 2.3.11

- **Bug Fix**
Expand Down
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": "2.3.11",
"version": "2.4.0",
"description": "A porting of scala monocle library to TypeScript",
"main": "lib/index.js",
"module": "es6/index.js",
Expand Down
24 changes: 24 additions & 0 deletions src/Tuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @since 2.4.0
*/
import { fst, snd, map, mapLeft } from 'fp-ts/lib/Tuple'
import { constant } from 'fp-ts/lib/function'
import { Lens } from '.'

// TODO on fp-ts upgrade: map → mapFst
const one = new Lens<[any, any], any>(fst, (x) => map(constant(x)))

/**
* @category constructor
* @since 2.4.0
*/
export const _1 = <A, B>(): Lens<[A, B], A> => one

// TODO on fp-ts upgrade: mapLeft → mapSnd
const two = new Lens<[any, any], any>(snd, (y) => mapLeft(constant(y)))

/**
* @category constructor
* @since 2.4.0
*/
export const _2 = <A, B>(): Lens<[A, B], B> => two
17 changes: 17 additions & 0 deletions test/Tuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as assert from 'assert'
import { _1, _2 } from '../src/Tuple'
import * as U from './util'

describe('Tuple', () => {
const mock: [number, string] = [1, 'a']

it('gets', () => {
assert.strictEqual(_1().get(mock), 1)
assert.strictEqual(_2().get(mock), 'a')
})

it('sets', () => {
U.deepStrictEqual(_1().set(2)(mock), [2, 'a'])
U.deepStrictEqual(_2().set('b')(mock), [1, 'b'])
})
})