Skip to content

Commit 630611b

Browse files
committed
Initial
0 parents  commit 630611b

26 files changed

+796
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
/node_modules
3+
/npm-debug.log
4+
/.lintcache

.lint

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@root
2+
3+
module
4+
5+
indent 2
6+
maxlen 80
7+
tabs
8+
9+
ass
10+
nomen
11+
plusplus

.travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- 0.8
4+
- 0.10
5+
- 0.11
6+
7+
notifications:
8+
email:
9+

LICENCE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) 2013 Mariusz Nowak (www.medikoo.com)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# es6-map
2+
## Map collection as specified in ECMAScript6
3+
4+
### Usage
5+
6+
If you want to make sure your environment implements `Map`, do:
7+
8+
```javascript
9+
require('es6-map/implement');
10+
```
11+
12+
If you'd like to use native version when it exists and fallback to polyfill if it doesn't, but without implementing `Map` on global scope, do:
13+
14+
```javascript
15+
var Map = require('es6-map');
16+
```
17+
18+
If you strictly want to use polyfill even if native `Map` exists, do:
19+
20+
```javascript
21+
var Map = require('es6-map/polyfill');
22+
```
23+
24+
#### API
25+
26+
Best is to refer to [specification](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-map-objects). Still if you want quick look, follow examples:
27+
28+
```javascript
29+
var Map = require('es6-map');
30+
31+
var x = {}, y = {}, map = new Map([['raz', 'one'], ['dwa', 'two'], [x, y]]);
32+
33+
map.size; // 3
34+
map.get('raz'); // 'one'
35+
map.get(x); // y
36+
map.has('raz'); // true
37+
map.has(x); // true
38+
map.has('foo'); // false
39+
map.set('trzy', 'three'); // map
40+
map.size // 4
41+
map.get('trzy'); // 'three'
42+
map.has('trzy'); // true
43+
map.has('dwa'); // true
44+
map.delete('dwa'); // true
45+
map.size; // 3
46+
47+
map.forEach(function (value, key) {
48+
// { 'raz', 'one' }, { x, y }, { 'trzy', 'three' } iterated
49+
});
50+
51+
// FF nightly only:
52+
for (value of map) {
53+
// ['raz', 'one'], [x, y], ['trzy', 'three'] iterated
54+
}
55+
56+
var iterator = map.values();
57+
58+
iterator.next(); // { done: false, value: 'one' }
59+
iterator.next(); // { done: false, value: y }
60+
iterator.next(); // { done: false, value: 'three' }
61+
iterator.next(); // { done: true, value: undefined }
62+
63+
map.clear(); // undefined
64+
map.size; // 0
65+
```
66+
67+
### Installation
68+
#### NPM
69+
70+
In your project path:
71+
72+
$ npm install es6-map
73+
74+
##### Browser
75+
76+
You can easily bundle _es6-map_ for browser with [modules-webmake](https://github.com/medikoo/modules-webmake)
77+
78+
## Tests [![Build Status](https://travis-ci.org/medikoo/es6-map.png)](https://travis-ci.org/medikoo/es6-map)
79+
80+
$ npm test

_iterator.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
var setPrototypeOf = require('es5-ext/object/set-prototype-of')
4+
, primitiveSet = require('es5-ext/object/primitive-set')
5+
, d = require('d/d')
6+
, Iterator = require('es6-iterator')
7+
8+
, defineProperties = Object.defineProperties
9+
, kinds = primitiveSet('key', 'value', 'key+value')
10+
, unBind = Iterator.prototype._unBind
11+
, MapIterator;
12+
13+
MapIterator = module.exports = function (map, kind) {
14+
if (!(this instanceof MapIterator)) return new MapIterator(map, kind);
15+
Iterator.call(this, map.__mapKeysData__, map);
16+
if (!kind || !kinds[kind]) kind = 'key+value';
17+
defineProperties(this, {
18+
__kind__: d('', kind),
19+
__values__: d('w', map.__mapValuesData__)
20+
});
21+
};
22+
if (setPrototypeOf) setPrototypeOf(MapIterator, Iterator);
23+
24+
MapIterator.prototype = Object.create(Iterator.prototype, {
25+
constructor: d(MapIterator),
26+
_resolve: d(function (i) {
27+
if (this.__kind__ === 'value') return this.__values__[i];
28+
if (this.__kind__ === 'key') return this.__list__[i];
29+
return [this.__list__[i], this.__values__[i]];
30+
}),
31+
_unBind: d(function () {
32+
this.__values__ = null;
33+
unBind.call(this);
34+
}),
35+
toString: d(function () { return '[object Map Iterator]'; })
36+
});

implement.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
if (!require('./is-implemented')()) {
4+
Object.defineProperty(require('es5-ext/global'), 'Map',
5+
{ value: require('./polyfill'), configurable: true, enumerable: false,
6+
writable: true });
7+
}

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('./is-implemented')() ? Map : require('./polyfill');

is-implemented.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
module.exports = function () {
4+
var map, iterator, result;
5+
if (typeof Map !== 'function') return false;
6+
map = new Map([['raz', 'one'], ['dwa', 'two'], ['trzy', 'three']]);
7+
if (map.size !== 3) return false;
8+
if (typeof map.clear !== 'function') return false;
9+
if (typeof map.delete !== 'function') return false;
10+
if (typeof map.entries !== 'function') return false;
11+
if (typeof map.forEach !== 'function') return false;
12+
if (typeof map.get !== 'function') return false;
13+
if (typeof map.has !== 'function') return false;
14+
if (typeof map.keys !== 'function') return false;
15+
if (typeof map.set !== 'function') return false;
16+
if (typeof map.values !== 'function') return false;
17+
18+
iterator = map.entries();
19+
result = iterator.next();
20+
if (result.done !== true) return false;
21+
if (!result.value) return false;
22+
if (result.value[0] !== 'raz') return false;
23+
if (result.value[1] !== 'one') return false;
24+
return true;
25+
};

is-map.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
var toString = Object.prototype.toString
4+
5+
, id = '[object Map]'
6+
, Global = (typeof Map === 'undefined') ? null : Map;
7+
8+
module.exports = function (x) {
9+
return (x && ((Global && (x instanceof Global)) ||
10+
(toString.call(x) === id) || (x['@@toStringTag'] === 'Map'))) || false;
11+
};

is-native-implemented.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Exports true if environment provides native `Map` implementation,
2+
// whatever that is.
3+
4+
'use strict';
5+
6+
module.exports = (function () {
7+
if (typeof Map === 'undefined') return false;
8+
return (Object.prototype.toString.call(Map.prototype) === '[object Map]');
9+
}());

package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "es6-map",
3+
"version": "0.0.0",
4+
"description": "ECMAScript6 Map polyfill",
5+
"author": "Mariusz Nowak <[email protected]> (http://www.medikoo.com/)",
6+
"keywords": [
7+
"collection",
8+
"es6",
9+
"shim",
10+
"harmony",
11+
"list",
12+
"hash",
13+
"map",
14+
"polyfill",
15+
"ecmascript"
16+
],
17+
"repository": {
18+
"type": "git",
19+
"url": "git://github.com/medikoo/es6-map.git"
20+
},
21+
"scripts": {
22+
"test": "node ./node_modules/tad/bin/tad"
23+
},
24+
"dependencies": {
25+
"es5-ext": "git://github.com/medikoo/es5-ext.git"
26+
},
27+
"devDependencies": {
28+
"tad": "~0.1.20"
29+
},
30+
"license": "MIT"
31+
}

polyfill.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'use strict';
2+
3+
var clear = require('es5-ext/array/#/clear')
4+
, setPrototypeOf = require('es5-ext/object/set-prototype-of')
5+
, callable = require('es5-ext/object/valid-callable')
6+
, validValue = require('es5-ext/object/valid-value')
7+
, d = require('d/d')
8+
, ee = require('event-emitter/lib/core')
9+
, iterator = require('es6-iterator/valid-iterable')
10+
, forOf = require('es6-iterator/for-of')
11+
, getSetData = require('es6-set/_get-set-data')
12+
, Iterator = require('./_iterator')
13+
, isNative = require('./is-native-implemented')
14+
15+
, call = Function.prototype.call, defineProperties = Object.defineProperties
16+
, MapPoly;
17+
18+
module.exports = MapPoly = function (/*iterable, comparator*/) {
19+
var iterable = arguments[0], comparator = arguments[1], keys, values;
20+
if (!(this instanceof MapPoly)) return new MapPoly(iterable, comparator);
21+
if (this.__mapKeysData__ !== undefined) {
22+
throw new TypeError(this + " cannot be reinitialized");
23+
}
24+
if (iterable != null) iterator(iterable);
25+
if (comparator != null) {
26+
if (comparator !== 'is') throw new RangeError("Unknown comparator");
27+
}
28+
defineProperties(this, {
29+
__mapKeysData__: d('', keys = getSetData(comparator)),
30+
__mapValuesData__: d('', values = [])
31+
});
32+
if (!iterable) return;
33+
forOf(iterable, function (value) {
34+
var key = validValue(value)[0];
35+
value = value[1];
36+
if (keys.eIndexOf(key) !== -1) return;
37+
keys.push(key);
38+
values.push(value);
39+
}, this);
40+
};
41+
42+
if (isNative) {
43+
if (setPrototypeOf) setPrototypeOf(MapPoly, Map);
44+
MapPoly.prototype = Object.create(Map.prototype, {
45+
constructor: d(MapPoly)
46+
});
47+
}
48+
49+
ee(defineProperties(MapPoly.prototype, {
50+
clear: d(function () {
51+
if (!this.__mapKeysData__.length) return;
52+
this.__mapKeysData__.clear();
53+
clear.call(this.__mapValuesData__);
54+
this.emit('_clear');
55+
}),
56+
delete: d(function (key) {
57+
var index = this.__mapKeysData__.eIndexOf(key);
58+
if (index === -1) return false;
59+
this.__mapKeysData__.splice(index, 1);
60+
this.__mapValuesData__.splice(index, 1);
61+
this.emit('_delete', index, key);
62+
return true;
63+
}),
64+
entries: d(function () { return new Iterator(this, 'key+value'); }),
65+
forEach: d(function (cb/*, thisArg*/) {
66+
var thisArg = arguments[1], iterator, result;
67+
callable(cb);
68+
iterator = this.entries();
69+
result = iterator.next();
70+
while (!result.done) {
71+
call.call(cb, thisArg, result.value[1], result.value[0], this);
72+
result = iterator.next();
73+
}
74+
}),
75+
get: d(function (key) {
76+
var index = this.__mapKeysData__.eIndexOf(key);
77+
if (index === -1) return;
78+
return this.__mapValuesData__[index];
79+
}),
80+
has: d(function (key) {
81+
return (this.__mapKeysData__.eIndexOf(key) !== -1);
82+
}),
83+
keys: d(function () { return new Iterator(this, 'key'); }),
84+
set: d(function (key, value) {
85+
var index = this.__mapKeysData__.eIndexOf(key);
86+
if (index === -1) index = this.__mapKeysData__.push(key) - 1;
87+
this.__mapValuesData__[index] = value;
88+
this.emit('_add', index, key);
89+
return this;
90+
}),
91+
size: d.gs(function () { return this.__mapKeysData__.length; }),
92+
values: d(function () { return new Iterator(this, 'value'); }),
93+
'@@iterator': d(function () { return this.entries(); }),
94+
'@@toStringTag': d('c', 'Map'),
95+
toString: d(function () { return '[object Map]'; })
96+
}));

0 commit comments

Comments
 (0)