Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mapify function #58

Merged
merged 4 commits into from
Jul 2, 2024
Merged
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
27 changes: 27 additions & 0 deletions benchmarks/array/mapify.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as _ from 'radashi'
import { bench } from 'vitest'

describe('mapify', () => {
bench('with full list and identity value function', () => {
const list = [
{ id: 'a', word: 'hello' },
{ id: 'b', word: 'bye' },
{ id: 'c', word: 'oh' },
{ id: 'd', word: 'hey' },
{ id: 'e', word: 'ok' },
]
_.mapify(
list,
x => x.id,
x => x,
)
})

bench('with empty list', () => {
_.mapify(
[],
(x: any) => x.id,
x => x,
)
})
})
38 changes: 38 additions & 0 deletions docs/array/mapify.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: mapify
group: "Array"
description: Convert an array to a map
---

## Basic usage

Given an array of items, create a map with keys and values mapped by given
functions. First argument is the array to map. The second argument is the
function to determine the key for each item. The third argument is optional and
determines the value for each item.

```ts
import * as _ from "radashi";

const fish = [
{
name: "Marlin",
weight: 105,
},
{
name: "Bass",
weight: 8,
},
{
name: "Trout",
weight: 13,
},
];

_.mapify(fish, f => f.name); // => Map(3) {'Marlin' => { name: 'Marlin', weight: 105 }, 'Bass' => { name: 'Bass', weight: 8 }, 'Trout' => { name: 'Trout', weight: 13 }}
_.mapify(
fish,
f => f.name,
f => f.weight
); // => Map(3) { 'Marlin' => 105, 'Bass' => 8, 'Trout' => 13 }
```
11 changes: 11 additions & 0 deletions src/array/mapify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function mapify<T, Key, Value = T>(
array: readonly T[],
getKey: (item: T) => Key,
getValue: (item: T) => Value = item => item as unknown as Value,
): Map<Key, Value> {
const map: Map<Key, Value> = new Map()
for (const item of array) {
map.set(getKey(item), getValue(item))
}
return map
}
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './array/intersects.ts'
export * from './array/iterate.ts'
export * from './array/last.ts'
export * from './array/list.ts'
export * from './array/mapify.ts'
export * from './array/max.ts'
export * from './array/merge.ts'
export * from './array/min.ts'
Expand Down
36 changes: 36 additions & 0 deletions tests/array/mapify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as _ from 'radashi'

describe('mapify', () => {
const list = [
{ id: 'a', word: 'hello' },
{ id: 'b', word: 'bye' },
{ id: 'c', word: 'oh' },
{ id: 'd', word: 'hey' },
{ id: 'e', word: 'ok' },
]
test('returns correct map of values', () => {
const result = _.mapify(
list,
x => x.id,
x => x,
)
expect(result).toBeTypeOf(typeof new Map())
expect(result.size).toBe(5)
expect(result.get('a')?.word).toBe('hello')
expect(result.get('b')?.word).toBe('bye')
})
test('does not fail on empty input list', () => {
const result = _.mapify(
[],
(x: any) => x.id,
x => x,
)
expect(result).toBeTypeOf(typeof new Map())
})
test('defaults value to array item', () => {
const result = _.mapify(list.slice(0, 1), x => x.id)
expect(result).toBeTypeOf(typeof new Map())
expect(result.size).toBe(1)
expect(result.get('a')?.word).toBe('hello')
})
})