-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
828 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,70 @@ | ||
export var prefix = "$"; | ||
|
||
function Map() {} | ||
|
||
Map.prototype = map.prototype = { | ||
has: function(key) { | ||
return (prefix + key) in this; | ||
}, | ||
get: function(key) { | ||
return this[prefix + key]; | ||
}, | ||
set: function(key, value) { | ||
return this[prefix + key] = value; | ||
}, | ||
remove: function(key) { | ||
var property = prefix + key; | ||
return property in this && delete this[property]; | ||
}, | ||
keys: function() { | ||
var keys = []; | ||
for (var property in this) if (property[0] === prefix) keys.push(property.slice(1)); | ||
return keys; | ||
}, | ||
values: function() { | ||
var values = []; | ||
for (var property in this) if (property[0] === prefix) values.push(this[property]); | ||
return values; | ||
}, | ||
entries: function() { | ||
var entries = []; | ||
for (var property in this) if (property[0] === prefix) entries.push({key: property.slice(1), value: this[property]}); | ||
return entries; | ||
}, | ||
size: function() { | ||
var size = 0; | ||
for (var property in this) if (property[0] === prefix) ++size; | ||
return size; | ||
}, | ||
empty: function() { | ||
for (var property in this) if (property[0] === prefix) return false; | ||
return true; | ||
}, | ||
forEach: function(f) { | ||
for (var property in this) if (property[0] === prefix) f.call(this, property.slice(1), this[property]); | ||
} | ||
}; | ||
|
||
function map(object, f) { | ||
var map = new Map; | ||
|
||
// Copy constructor. | ||
if (object instanceof Map) object.forEach(function(key, value) { map.set(key, value); }); | ||
|
||
// Index array by numeric index or specified key function. | ||
else if (Array.isArray(object)) { | ||
var i = -1, | ||
n = object.length, | ||
o; | ||
|
||
if (arguments.length === 1) while (++i < n) map.set(i, object[i]); | ||
else while (++i < n) map.set(f.call(object, o = object[i], i), o); | ||
} | ||
|
||
// Convert object to map. | ||
else for (var key in object) map.set(key, object[key]); | ||
|
||
return map; | ||
} | ||
|
||
export default map; |
This file contains 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 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,29 @@ | ||
import {default as map, prefix} from "./map"; | ||
|
||
function Set() {} | ||
|
||
var proto = map.prototype; | ||
|
||
Set.prototype = set.prototype = { | ||
has: proto.has, | ||
add: function(value) { | ||
value += ""; | ||
this[prefix + value] = true; | ||
return value; | ||
}, | ||
remove: proto.remove, | ||
values: proto.keys, | ||
size: proto.size, | ||
empty: proto.empty, | ||
forEach: function(f) { | ||
for (var property in this) if (property[0] === prefix) f.call(this, property.slice(1)); | ||
} | ||
}; | ||
|
||
function set(array) { | ||
var set = new Set; | ||
if (array) for (var i = 0, n = array.length; i < n; ++i) set.add(array[i]); | ||
return set; | ||
} | ||
|
||
export default set; |
Oops, something went wrong.