https://github.com/darky/mapn
Like JavaScript Array.map, but with multiple returns
https://github.com/darky/mapn
array dimension map multi multiple return
Last synced: 12 months ago
JSON representation
Like JavaScript Array.map, but with multiple returns
- Host: GitHub
- URL: https://github.com/darky/mapn
- Owner: darky
- Created: 2023-01-13T21:02:51.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-14T15:01:47.000Z (about 3 years ago)
- Last Synced: 2025-02-23T20:47:19.963Z (12 months ago)
- Topics: array, dimension, map, multi, multiple, return
- Language: TypeScript
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mapn
Like JavaScript Array.map, but with multiple returns. Or n-dimension Array.map version. TypeScript supports too.
## Examples
#### Basic
```ts
import assert from 'node:assert';
import { mapn } from 'mapn';
const [numbers, strings] = mapn([1, 2, 3], n => [n * 2, `${n}`]);
assert.deepStrictEqual(numbers, [2, 4, 6]);
assert.deepStrictEqual(strings, ['1', '2', '3']);
```
#### Skip
```ts
import assert from 'node:assert';
import { mapn, mapnSkip } from 'mapn';
const [numbers, strings] = mapn([1, 2, 3], n => [n === 2 ? mapnSkip : n * 2, `${n}`]);
assert.deepStrictEqual(numbers, [2, 6]);
assert.deepStrictEqual(strings, ['1', '2', '3']);
```
## Caveat
Please avoid return `undefined` in result array. If you need return "empty" value, need return `null` instead. Or better use `mapnSkip`
#### ❌
```ts
import { mapn } from 'mapn';
mapn([1, 2, 3], n => [n, n === 2 ? undefined : n * 2]);
```
#### ✅
```ts
import { mapn } from 'mapn';
mapn([1, 2, 3], n => [n, n === 2 ? null : n * 2]);
```
#### ✅✅
```ts
import { mapn, mapnSkip } from 'mapn';
mapn([1, 2, 3], n => [n, n === 2 ? mapnSkip : n * 2]);
```