Skip to content

Replace es6-map with a simple implementation of a map. #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
},
"license": "BSD-3-Clause",
"dependencies": {
"es6-map": "^0.1.3",
"hoist-non-react-statics": "^1.0.5",
"lodash": "^4.6.1",
"object-unfreeze": "^1.0.2"
Expand All @@ -35,7 +34,7 @@
"react-dom": "^15.0.0-rc.1"
},
"scripts": {
"pragmatist": "node ./node_modules/.bin/pragmatist --es5",
"pragmatist": "pragmatist --es5",
"lint": "npm run pragmatist lint",
"test": "npm run pragmatist test --type-annotations",
"build": "npm run pragmatist build",
Expand Down
2 changes: 1 addition & 1 deletion src/generateAppendClassName.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Map from 'es6-map';
import Map from './simple-map';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simple-map is very generic. What about calling it simple-weak-map?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that is fitting, as keys are not weakly referenced like in a WeakMap. Your call though.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a weak map. Nor is a weak map required here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I have suggested "weak map" is because keys are objects. @benjamingr Can you suggest a better name?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map, in regular ES2015 maps keys can be objects.

A WeakMap is a map that does not interfere with the garbage collection of its keys. If you'd like to see an example of why one would want to use it - see http://stackoverflow.com/questions/29413222/what-are-the-actual-uses-of-es6-weakmap . The "weakness" of the weak map is in that it holds the reference to the keys weakly.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats useful. Thank you. Will keep it as simple-map then.


const stylesIndex = new Map();

Expand Down
2 changes: 1 addition & 1 deletion src/makeConfiguration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash';
import Map from 'es6-map';
import Map from './simple-map';

const userConfigurationIndex = new Map();

Expand Down
27 changes: 27 additions & 0 deletions src/simple-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class SimpleMap {
constructor () {
this.keys = [];
this.values = [];
}

get size () {
return this.keys.length;
}

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

return this.values[index];
}

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

return value;
}
}

const exportedMap = typeof Map === 'undefined' ? SimpleMap : Map;

export default exportedMap;
35 changes: 35 additions & 0 deletions tests/simple-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
expect
} from 'chai';
import {SimpleMap} from './../src/simple-map';

const getTests = (map) => {
return () => {
const values = [
[1, 'something'],
['1', 'somethingElse'],
[{}, []],
[null, null]
];

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]) => {
expect(map.get(key)).to.equal(value);
});
});
};
};

describe('SimpleMap', () => {
context('simple map with primitive or object as keys', getTests(new SimpleMap()));
if (typeof Map !== 'undefined') {
context('sanity - running tests against native Map', getTests(new Map()));
}
});