Skip to content

Commit

Permalink
refactor: export SimpleMap
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 28, 2016
1 parent c383048 commit f0405ab
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/SimpleMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default class {
constructor () {
this.size = 0;
this.keys = [];
this.values = [];
}

get (key) {
const index = this.keys.indexOf(key);

return this.values[index];
}

set (key, value) {
this.keys.push(key);
this.values.push(value);
this.size = this.keys.length;

return value;
}
}
38 changes: 38 additions & 0 deletions tests/SimpleMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
expect
} from 'chai';
import SimpleMap from './../src/SimpleMap';

describe('SimpleMap', () => {
context('simple map with primitive or object as keys', () => {
const values = [
[1, 'something'],
['1', 'somethingElse'],
[{}, []],
[null, null]
];

let map;

beforeEach(() => {
map = new SimpleMap();
});

it('should set', () => {
values.forEach(([key, value]) => {
map.set(key, value);
});
expect(map.size).to.equal(values.length);
});

it('should get', () => {
values.forEach(([key, value]) => {
map.set(key, value);
});

values.forEach(([key, value]) => {
expect(map.get(key)).to.equal(value);
});
});
});
});

0 comments on commit f0405ab

Please sign in to comment.