Skip to content

Commit 19e2ba9

Browse files
✨ feat: More conversion functions.
Undeprecate toObject. Add fromObject, toMap, and fromMap. Fixes #102.
1 parent 0a38010 commit 19e2ba9

File tree

10 files changed

+80
-6
lines changed

10 files changed

+80
-6
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import {enumerate} from '@iterable-iterator/zip';
1414
reflect(enumerate('ab')); // ['a', 0] ['b', 1]
1515

1616
// You can convert to and from Object and Map
17-
Object.fromEntries(mapping) -> Object
18-
Object.entries(object) -> mapping
19-
new Map(mapping) -> Map
20-
map.entries() -> mapping
17+
import {toObject, fromObject, toMap, fromMap} from '@iterable-iterator/mapping' ;
18+
toObject(mapping) -> Object
19+
fromObject(Object) -> mapping
20+
toMap(mapping) -> Map
21+
fromMap(Map) -> mapping
2122
```
2223

2324
[![License](https://img.shields.io/github/license/iterable-iterator/mapping.svg)](https://raw.githubusercontent.com/iterable-iterator/mapping/main/LICENSE)

src/fromMap.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Instantiate a mapping from a map.
3+
*
4+
* @param {Map} map The input map.
5+
* @return {IterableIterator} The output mapping.
6+
*/
7+
const fromMap = (map) => map.entries();
8+
export default fromMap;

src/fromObject.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Instantiate a mapping from an object.
3+
*
4+
* @param {Object} object The input object.
5+
* @return {IterableIterator} The output mapping.
6+
*/
7+
const fromObject = (object) => Object.entries(object)[Symbol.iterator]();
8+
export default fromObject;

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export {default as constant} from './constant.js';
2+
export {default as fromMap} from './fromMap.js';
3+
export {default as fromObject} from './fromObject.js';
24
export {default as reflect} from './reflect.js';
5+
export {default as toMap} from './toMap.js';
36
export {default as toObject} from './toObject.js';

src/toMap.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Instantiate a map from a mapping.
3+
*
4+
* @param {Iterable} mapping The input mapping.
5+
* @return {Map} The output map.
6+
*/
7+
const toMap = (mapping) => new Map(mapping);
8+
export default toMap;

src/toObject.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/**
22
* Instantiate an object from a mapping.
33
*
4-
* @deprecated Use Object.fromEntries(mapping) directly.
5-
*
64
* @param {Iterable} mapping The input mapping.
75
* @return {Object} The output object.
86
*/

test/src/_fixtures.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const repr = (x) => JSON.stringify(x);

test/src/fromMap.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import test from 'ava';
2+
3+
import {enumerate} from '@iterable-iterator/zip';
4+
import {fromMap} from '../../src/index.js';
5+
6+
import {repr} from './_fixtures.js';
7+
8+
const macro = (t, mapping) => {
9+
t.deepEqual(Array.from(fromMap(new Map(mapping))), Array.from(mapping));
10+
};
11+
12+
macro.title = (title, mapping) =>
13+
title ?? `fromMap(new Map(${repr(mapping)})) is ${repr(mapping)}`;
14+
15+
test(macro, []);
16+
test(macro, Array.from(enumerate('abcde')));

test/src/fromObject.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import test from 'ava';
2+
3+
import {fromObject} from '../../src/index.js';
4+
5+
test('fromObject', (t) => {
6+
t.deepEqual(Array.from(fromObject({})), []);
7+
8+
t.deepEqual(Array.from(fromObject({a: 0, b: 1, c: 2, d: 3, e: 4})), [
9+
['a', 0],
10+
['b', 1],
11+
['c', 2],
12+
['d', 3],
13+
['e', 4],
14+
]);
15+
});

test/src/toMap.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import test from 'ava';
2+
3+
import {enumerate} from '@iterable-iterator/zip';
4+
import {toMap} from '../../src/index.js';
5+
6+
import {repr} from './_fixtures.js';
7+
8+
const macro = (t, mapping) => {
9+
t.deepEqual(toMap(mapping), new Map(mapping));
10+
};
11+
12+
macro.title = (title, mapping) =>
13+
title ?? `toMap(${repr(mapping)}) is new Map(${repr(mapping)})`;
14+
15+
test(macro, []);
16+
test(macro, Array.from(enumerate('abcde')));

0 commit comments

Comments
 (0)