-
-
Notifications
You must be signed in to change notification settings - Fork 215
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Map from 'es6-map'; | ||
import Map from './simple-map'; | ||
|
||
const stylesIndex = new Map(); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 itsimple-weak-map
?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.