UNPKG

3.69 kBJavaScriptView Raw
1/*
2 Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*/
24
25/*global module:true*/
26(function () {
27 'use strict';
28
29 var Map;
30
31 if (typeof global.Map !== 'undefined') {
32 // ES6 Map
33 Map = global.Map;
34 } else {
35 Map = function Map() {
36 this.__data = {};
37 };
38
39 Map.prototype.get = function MapGet(key) {
40 key = '$' + key;
41 if (this.__data.hasOwnProperty(key)) {
42 return this.__data[key];
43 }
44 };
45
46 Map.prototype.has = function MapHas(key) {
47 key = '$' + key;
48 return this.__data.hasOwnProperty(key);
49 };
50
51 Map.prototype.set = function MapSet(key, val) {
52 key = '$' + key;
53 this.__data[key] = val;
54 };
55
56 Map.prototype['delete'] = function MapDelete(key) {
57 key = '$' + key;
58 return delete this.__data[key];
59 };
60
61 Map.prototype.clear = function MapClear() {
62 this.__data = {};
63 };
64
65 Map.prototype.forEach = function MapForEach(callback, thisArg) {
66 var real, key;
67 for (real in this.__data) {
68 if (this.__data.hasOwnProperty(real)) {
69 key = real.substring(1);
70 callback.call(thisArg, this.__data[real], key, this);
71 }
72 }
73 };
74
75 Map.prototype.keys = function MapKeys() {
76 var real, result;
77 result = [];
78 for (real in this.__data) {
79 if (this.__data.hasOwnProperty(real)) {
80 result.push(real.substring(1));
81 }
82 }
83 return result;
84 };
85
86 Map.prototype.values = function MapValues() {
87 var real, result;
88 result = [];
89 for (real in this.__data) {
90 if (this.__data.hasOwnProperty(real)) {
91 result.push(this.__data[real]);
92 }
93 }
94 return result;
95 };
96
97 Map.prototype.items = function MapItems() {
98 var real, result;
99 result = [];
100 for (real in this.__data) {
101 if (this.__data.hasOwnProperty(real)) {
102 result.push([real.substring(1), this.__data[real]]);
103 }
104 }
105 return result;
106 };
107 }
108
109 module.exports = Map;
110}());