From 98938107a80940d048b7702926a8da0d23d3e8a7 Mon Sep 17 00:00:00 2001 From: Sharon Rolel Date: Sun, 29 May 2016 05:14:46 +0300 Subject: [PATCH 1/3] Replace es6-map with a simple implementation of a map. --- package.json | 3 +-- src/generateAppendClassName.js | 2 +- src/makeConfiguration.js | 2 +- src/simple-map.js | 20 ++++++++++++++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 src/simple-map.js diff --git a/package.json b/package.json index 6a40041..202f34f 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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", diff --git a/src/generateAppendClassName.js b/src/generateAppendClassName.js index b4e6bd8..c896f6e 100644 --- a/src/generateAppendClassName.js +++ b/src/generateAppendClassName.js @@ -1,4 +1,4 @@ -import Map from 'es6-map'; +import Map from './simple-map'; const stylesIndex = new Map(); diff --git a/src/makeConfiguration.js b/src/makeConfiguration.js index 22d0298..3254825 100644 --- a/src/makeConfiguration.js +++ b/src/makeConfiguration.js @@ -1,5 +1,5 @@ import _ from 'lodash'; -import Map from 'es6-map'; +import Map from './simple-map'; const userConfigurationIndex = new Map(); diff --git a/src/simple-map.js b/src/simple-map.js new file mode 100644 index 0000000..87a8512 --- /dev/null +++ b/src/simple-map.js @@ -0,0 +1,20 @@ +export default class SimpleMap { + constructor() { + this.keys = []; + this.values = []; + } + + get(key) { + const index = this.keys.indexOf(key); + if (index === -1) { + return; + } + return this.values[index]; + } + + set(key, value) { + this.keys.push(key); + this.values.push(value); + return value; + } +} \ No newline at end of file From 2b8c720aa73d98f504432fcfa8804cd3ce4d92e8 Mon Sep 17 00:00:00 2001 From: Sharon Rolel Date: Sun, 29 May 2016 13:58:52 +0300 Subject: [PATCH 2/3] code style --- src/simple-map.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/simple-map.js b/src/simple-map.js index 87a8512..0906ff7 100644 --- a/src/simple-map.js +++ b/src/simple-map.js @@ -1,20 +1,19 @@ export default class SimpleMap { - constructor() { + constructor () { this.keys = []; this.values = []; } - get(key) { + get (key) { const index = this.keys.indexOf(key); - if (index === -1) { - return; - } + return this.values[index]; } - - set(key, value) { + + set (key, value) { this.keys.push(key); this.values.push(value); + return value; } -} \ No newline at end of file +} From bcd891fc33c9a56ee95f570b9ad983d6ddf60d9a Mon Sep 17 00:00:00 2001 From: Sharon Rolel Date: Tue, 31 May 2016 10:57:39 +0300 Subject: [PATCH 3/3] Added tests and native Map fallback --- src/simple-map.js | 10 +++++++++- tests/simple-map.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/simple-map.js diff --git a/src/simple-map.js b/src/simple-map.js index 0906ff7..4582e33 100644 --- a/src/simple-map.js +++ b/src/simple-map.js @@ -1,9 +1,13 @@ -export default class SimpleMap { +export class SimpleMap { constructor () { this.keys = []; this.values = []; } + get size () { + return this.keys.length; + } + get (key) { const index = this.keys.indexOf(key); @@ -17,3 +21,7 @@ export default class SimpleMap { return value; } } + +const exportedMap = typeof Map === 'undefined' ? SimpleMap : Map; + +export default exportedMap; diff --git a/tests/simple-map.js b/tests/simple-map.js new file mode 100644 index 0000000..29d12ac --- /dev/null +++ b/tests/simple-map.js @@ -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())); + } +});