From 210d51c7fe8c67ea1dd605ba404b6edc03567162 Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 21 Jul 2020 15:15:42 +0200 Subject: [PATCH] export `mapWithIndex` from Map and ReadonlyMap --- CHANGELOG.md | 4 ++++ docs/modules/Map.ts.md | 16 +++++++++++++++- docs/modules/ReadonlyMap.ts.md | 14 ++++++++++++++ src/Map.ts | 8 +++++++- src/ReadonlyMap.ts | 8 ++++++++ test/ReadonlyMap.ts | 22 ++++++++++++---------- 6 files changed, 60 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ad9ec52f..feea88a2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/modules/Map.ts.md b/docs/modules/Map.ts.md index e56b6507f..ba725df2a 100644 --- a/docs/modules/Map.ts.md +++ b/docs/modules/Map.ts.md @@ -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) @@ -142,11 +144,23 @@ use the type constructor `F` to represent some computational context. **Signature** ```ts -export declare const map: (f: (a: A) => B) => (fa: Map) => Map +export declare const map: (f: (a: A) => B) => (fa: Map) => Map ``` Added in v2.0.0 +# FunctorWithIndex + +## mapWithIndex + +**Signature** + +```ts +export declare const mapWithIndex: (f: (k: K, a: A) => B) => (fa: Map) => Map +``` + +Added in v2.7.1 + # combinators ## deleteAt diff --git a/docs/modules/ReadonlyMap.ts.md b/docs/modules/ReadonlyMap.ts.md index c92295016..de76c18f6 100644 --- a/docs/modules/ReadonlyMap.ts.md +++ b/docs/modules/ReadonlyMap.ts.md @@ -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) @@ -154,6 +156,18 @@ export declare const map: (f: (a: A) => B) => (fa: ReadonlyMap) = Added in v2.5.0 +# FunctorWithIndex + +## mapWithIndex + +**Signature** + +```ts +export declare const mapWithIndex: (f: (k: K, a: A) => B) => (fa: ReadonlyMap) => ReadonlyMap +``` + +Added in v2.7.1 + # combinators ## deleteAt diff --git a/src/Map.ts b/src/Map.ts index cc42b583b..bb6c07481 100644 --- a/src/Map.ts +++ b/src/Map.ts @@ -277,7 +277,13 @@ export const filterMap: (f: (a: A) => Option) => (fa: Map) => * @category Functor * @since 2.0.0 */ -export const map: (f: (a: A) => B) => (fa: Map) => Map = RM.map as any +export const map: (f: (a: A) => B) => (fa: Map) => Map = RM.map as any + +/** + * @category FunctorWithIndex + * @since 2.7.1 + */ +export const mapWithIndex: (f: (k: K, a: A) => B) => (fa: Map) => Map = RM.mapWithIndex as any /** * @category Filterable diff --git a/src/ReadonlyMap.ts b/src/ReadonlyMap.ts index 350a9017a..cabcdb23d 100644 --- a/src/ReadonlyMap.ts +++ b/src/ReadonlyMap.ts @@ -654,6 +654,14 @@ export const filterMap: (f: (a: A) => Option) => (fa: ReadonlyMap(f: (a: A) => B) => (fa: ReadonlyMap) => ReadonlyMap = (f) => (fa) => map_(fa, f) +/** + * @category FunctorWithIndex + * @since 2.7.1 + */ +export const mapWithIndex: (f: (k: K, a: A) => B) => (fa: ReadonlyMap) => ReadonlyMap = (f) => ( + fa +) => mapWithIndex_(fa, f) + /** * @category Filterable * @since 2.5.0 diff --git a/test/ReadonlyMap.ts b/test/ReadonlyMap.ts index 2902ec828..2d96877d9 100644 --- a/test/ReadonlyMap.ts +++ b/test/ReadonlyMap.ts @@ -791,16 +791,6 @@ describe('ReadonlyMap', () => { assert.deepStrictEqual(log, ['a', 'b']) }) - it('mapWithIndex', () => { - const mapWithIndex = W.mapWithIndex - const aa1 = new Map([[{ id: 'aa' }, 1]]) - const aa3 = new Map([[{ id: 'aa' }, 3]]) - assert.deepStrictEqual( - mapWithIndex(aa1, (k, a) => a + k.id.length), - aa3 - ) - }) - it('reduce', () => { const d1 = new Map([ [{ id: 'k1' }, 'a'], @@ -1134,4 +1124,16 @@ describe('ReadonlyMap', () => { assert.deepStrictEqual(bs, as) assert.notStrictEqual(bs, as) }) + + it('mapWithIndex', () => { + const aa1 = new Map([[{ id: 'aa' }, 1]]) + const aa3 = new Map([[{ id: 'aa' }, 3]]) + assert.deepStrictEqual( + pipe( + aa1, + _.mapWithIndex((k, a) => a + k.id.length) + ), + aa3 + ) + }) })