Skip to content

Commit

Permalink
export mapWithIndex from Map and ReadonlyMap
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jul 22, 2020
1 parent c152b5b commit 210d51c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ high state of flux, you're at risk of it changing without notice.
- **Polish**
- `Array`
- relax `sort` signature (@gcanti)
- `Map`
- export `mapWithIndex` (@gcanti)
- `ReadonlyArray`
- relax `sort` signature (@gcanti)
- `ReadonlyMap`
- export `mapWithIndex` (@gcanti)

# 2.7.0

Expand Down
16 changes: 15 additions & 1 deletion docs/modules/Map.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Added in v2.0.0
- [partitionMap](#partitionmap)
- [Functor](#functor)
- [map](#map)
- [FunctorWithIndex](#functorwithindex)
- [mapWithIndex](#mapwithindex)
- [combinators](#combinators)
- [deleteAt](#deleteat)
- [insertAt](#insertat)
Expand Down Expand Up @@ -142,11 +144,23 @@ use the type constructor `F` to represent some computational context.
**Signature**
```ts
export declare const map: <A, B>(f: (a: A) => B) => <E>(fa: Map<E, A>) => Map<E, B>
export declare const map: <A, B>(f: (a: A) => B) => <K>(fa: Map<K, A>) => Map<K, B>
```
Added in v2.0.0
# FunctorWithIndex
## mapWithIndex
**Signature**
```ts
export declare const mapWithIndex: <K, A, B>(f: (k: K, a: A) => B) => (fa: Map<K, A>) => Map<K, B>
```
Added in v2.7.1
# combinators
## deleteAt
Expand Down
14 changes: 14 additions & 0 deletions docs/modules/ReadonlyMap.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Added in v2.5.0
- [partitionMap](#partitionmap)
- [Functor](#functor)
- [map](#map)
- [FunctorWithIndex](#functorwithindex)
- [mapWithIndex](#mapwithindex)
- [combinators](#combinators)
- [deleteAt](#deleteat)
- [insertAt](#insertat)
Expand Down Expand Up @@ -154,6 +156,18 @@ export declare const map: <A, B>(f: (a: A) => B) => <K>(fa: ReadonlyMap<K, A>) =
Added in v2.5.0
# FunctorWithIndex
## mapWithIndex
**Signature**
```ts
export declare const mapWithIndex: <K, A, B>(f: (k: K, a: A) => B) => (fa: ReadonlyMap<K, A>) => ReadonlyMap<K, B>
```
Added in v2.7.1
# combinators
## deleteAt
Expand Down
8 changes: 7 additions & 1 deletion src/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ export const filterMap: <A, B>(f: (a: A) => Option<B>) => <K>(fa: Map<K, A>) =>
* @category Functor
* @since 2.0.0
*/
export const map: <A, B>(f: (a: A) => B) => <E>(fa: Map<E, A>) => Map<E, B> = RM.map as any
export const map: <A, B>(f: (a: A) => B) => <K>(fa: Map<K, A>) => Map<K, B> = RM.map as any

/**
* @category FunctorWithIndex
* @since 2.7.1
*/
export const mapWithIndex: <K, A, B>(f: (k: K, a: A) => B) => (fa: Map<K, A>) => Map<K, B> = RM.mapWithIndex as any

/**
* @category Filterable
Expand Down
8 changes: 8 additions & 0 deletions src/ReadonlyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,14 @@ export const filterMap: <A, B>(f: (a: A) => Option<B>) => <K>(fa: ReadonlyMap<K,
*/
export const map: <A, B>(f: (a: A) => B) => <K>(fa: ReadonlyMap<K, A>) => ReadonlyMap<K, B> = (f) => (fa) => map_(fa, f)

/**
* @category FunctorWithIndex
* @since 2.7.1
*/
export const mapWithIndex: <K, A, B>(f: (k: K, a: A) => B) => (fa: ReadonlyMap<K, A>) => ReadonlyMap<K, B> = (f) => (
fa
) => mapWithIndex_(fa, f)

/**
* @category Filterable
* @since 2.5.0
Expand Down
22 changes: 12 additions & 10 deletions test/ReadonlyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,16 +791,6 @@ describe('ReadonlyMap', () => {
assert.deepStrictEqual(log, ['a', 'b'])
})

it('mapWithIndex', () => {
const mapWithIndex = W.mapWithIndex
const aa1 = new Map<User, number>([[{ id: 'aa' }, 1]])
const aa3 = new Map<User, number>([[{ id: 'aa' }, 3]])
assert.deepStrictEqual(
mapWithIndex(aa1, (k, a) => a + k.id.length),
aa3
)
})

it('reduce', () => {
const d1 = new Map<User, string>([
[{ id: 'k1' }, 'a'],
Expand Down Expand Up @@ -1134,4 +1124,16 @@ describe('ReadonlyMap', () => {
assert.deepStrictEqual(bs, as)
assert.notStrictEqual(bs, as)
})

it('mapWithIndex', () => {
const aa1 = new Map<User, number>([[{ id: 'aa' }, 1]])
const aa3 = new Map<User, number>([[{ id: 'aa' }, 3]])
assert.deepStrictEqual(
pipe(
aa1,
_.mapWithIndex((k, a) => a + k.id.length)
),
aa3
)
})
})

0 comments on commit 210d51c

Please sign in to comment.