Skip to content

Commit 168805f

Browse files
author
Stephen Fraser
committed
feat($utility): Added utility for mapping objects to arrays with callable
1 parent de8dbe7 commit 168805f

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export function mapToArray(map) {
22
return Object.keys(map).map(k => map[k]);
3+
}
4+
5+
export function mapObject(object, fn) {
6+
return mapToArray(object).map(fn);
37
}

tests/index-test.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
import expect from 'expect';
22

3-
import { mapToArray } from 'src/index';
3+
import { mapToArray, mapObject } from 'src/index';
44

5-
describe('Map to template', () => {
6-
it('should create an array from a map', () => {
7-
expect(mapToArray({
8-
'one': { name: "something" },
9-
'two': { name: "something else" },
10-
}).length).toBe(2);
11-
expect(mapToArray({
12-
'one': { name: "something" },
13-
'two': { name: "something else" },
14-
'three': { name: "something else" },
15-
})[2].name).toContain('something else');
5+
describe('NWB Semantic Release', () => {
6+
describe('mapToArray', () => {
7+
it('should create an array from a map', () => {
8+
expect(mapToArray({
9+
'one': { name: "something" },
10+
'two': { name: "something else" },
11+
}).length).toBe(2);
12+
expect(mapToArray({
13+
'one': { name: "something" },
14+
'two': { name: "something else" },
15+
'three': { name: "something else" },
16+
})[2].name).toContain('something else');
17+
});
18+
});
19+
describe('mapObject', () => {
20+
it('should map an object using callable', () => {
21+
expect(mapObject({
22+
'one': { name: "something" },
23+
'two': { name: "something else" },
24+
'three': { name: "something something" },
25+
}, object => object.name).length).toBe(3);
26+
27+
expect(mapObject({
28+
'one': { name: "something" },
29+
'two': { name: "something else" },
30+
'three': { name: "something something" },
31+
}, object => object.name)[0]).toBe('something');
32+
33+
expect(mapObject({
34+
'one': { name: "something" },
35+
'two': { name: "something else" },
36+
'three': { name: "something something" },
37+
}, object => object.name)[2]).toBe('something something');
38+
});
1639
});
1740
});

0 commit comments

Comments
 (0)