Skip to content

Commit

Permalink
Stop trying to make Map a polyfill.
Browse files Browse the repository at this point in the history
ES6's map does provide items(), but it returns an iterator. It only really makes
sense to use it if we have `for-of` loops, so there's little point in trying to
fill in that behavior at the moment.
  • Loading branch information
int3 committed Mar 3, 2013
1 parent 81959fb commit 388535b
Showing 1 changed file with 28 additions and 44 deletions.
72 changes: 28 additions & 44 deletions lib/util.coffee
Expand Up @@ -10,47 +10,31 @@ exports.Util =
enumerable: false
configurable: true

# polyfill
if Map?

if not Map.prototype.items
Map.prototype.items = () ->
results = []
i = this.iterator();
size = this.size()
if size
for num in [1..size]
results.push(i.next())
results

exports.Map = Map

else
class exports.Map
constructor: ->
@cache = Object.create null
@proto_cache = undefined
@proto_set = false

get: (key) ->
key = key.toString()
return @cache[key] unless key is '__proto__'
return @proto_cache

has: (key) ->
key = key.toString()
return key of @cache unless key is '__proto__'
return @proto_set

set: (key, value) ->
unless key.toString() is '__proto__'
@cache[key] = value
else
@proto_cache = value
@proto_set = true
value

items: ->
items = ([k,v] for k, v of @cache)
items.push ['__proto__', @proto_cache] if @proto_set
items
class exports.Map
constructor: ->
@cache = Object.create null
@proto_cache = undefined
@proto_set = false

get: (key) ->
key = key.toString()
return @cache[key] unless key is '__proto__'
return @proto_cache

has: (key) ->
key = key.toString()
return key of @cache unless key is '__proto__'
return @proto_set

set: (key, value) ->
unless key.toString() is '__proto__'
@cache[key] = value
else
@proto_cache = value
@proto_set = true
value

items: ->
items = ([k,v] for k, v of @cache)
items.push ['__proto__', @proto_cache] if @proto_set
items

0 comments on commit 388535b

Please sign in to comment.