Skip to content

Commit bcd891f

Browse files
author
Sharon Rolel
committed
Added tests and native Map fallback
1 parent 2b8c720 commit bcd891f

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/simple-map.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
export default class SimpleMap {
1+
export class SimpleMap {
22
constructor () {
33
this.keys = [];
44
this.values = [];
55
}
66

7+
get size () {
8+
return this.keys.length;
9+
}
10+
711
get (key) {
812
const index = this.keys.indexOf(key);
913

@@ -17,3 +21,7 @@ export default class SimpleMap {
1721
return value;
1822
}
1923
}
24+
25+
const exportedMap = typeof Map === 'undefined' ? SimpleMap : Map;
26+
27+
export default exportedMap;

tests/simple-map.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
expect
3+
} from 'chai';
4+
import {SimpleMap} from './../src/simple-map';
5+
6+
const getTests = (map) => {
7+
return () => {
8+
const values = [
9+
[1, 'something'],
10+
['1', 'somethingElse'],
11+
[{}, []],
12+
[null, null]
13+
];
14+
15+
it('should set', () => {
16+
values.forEach(([key, value]) => {
17+
map.set(key, value);
18+
});
19+
expect(map.size).to.equal(values.length);
20+
});
21+
22+
it('should get', () => {
23+
values.forEach(([key, value]) => {
24+
expect(map.get(key)).to.equal(value);
25+
});
26+
});
27+
};
28+
};
29+
30+
describe('SimpleMap', () => {
31+
context('simple map with primitive or object as keys', getTests(new SimpleMap()));
32+
if (typeof Map !== 'undefined') {
33+
context('sanity - running tests against native Map', getTests(new Map()));
34+
}
35+
});

0 commit comments

Comments
 (0)