Skip to content

Commit

Permalink
Use Map shim with _.uniq
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Mar 13, 2015
1 parent f77e099 commit 431b645
Showing 1 changed file with 54 additions and 10 deletions.
64 changes: 54 additions & 10 deletions underscore.js
Expand Up @@ -131,6 +131,55 @@
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
};

// A small Map shim, to support the most basic use cases.
var Map = (function() {
var keyer = function(value) {
return typeof value === 'string' ? 's' + value : value;
};
var Map = function() {
this._values = [];
this._keys = [];
this._primitives = nativeCreate && nativeCreate(null);
this.size = 0;

this._lastKey = void 0;
this._lastIndex = -1;
};
Map.prototype.get = function(key) {
if (this.has(key)) return this._values[this._lastIndex];
};
Map.prototype.has = function(key) {
var index = this._indexOf(key);
return index >= 0 && index < this.size;
};
Map.prototype.set = function(key, value) {
var index = this._indexOf(key);

this._values[index] = value;
if (index >= this.size) {
this.size = index + 1;
this._keys[index] = key;
if (this._primitives && !_.isObject(key)) this._primitives[keyer(key)] = index;
}
};
Map.prototype._indexOf = function(key) {
var index = this._lastIndex;
if (this._lastKey === key && index >= 0) return index;
if (this._primitives && !_.isObject(key)) {
index = this._primitives[keyer(key)];
} else {
index = _.indexOf(this._keys, key);
}
if (index === -1 || index === void 0) index = this.size;

this._lastKey = key;
this._lastIndex = index;
return index;
};
return Map;
})();


// Collection Functions
// --------------------

Expand Down Expand Up @@ -518,21 +567,16 @@
isSorted = false;
}
if (iteratee != null) iteratee = cb(iteratee, context);
var result = [];
var seen = [];
var seen = new Map();
var result = seen._values;
for (var i = 0, length = array.length; i < length; i++) {
var value = array[i],
computed = iteratee ? iteratee(value, i, array) : value;
if (isSorted) {
if (!i || seen !== computed) result.push(value);
if (seen !== computed) result.push(value);
seen = computed;
} else if (iteratee) {
if (!_.contains(seen, computed)) {
seen.push(computed);
result.push(value);
}
} else if (!_.contains(result, value)) {
result.push(value);
} else if (!seen.has(computed)) {
seen.set(computed, value);
}
}
return result;
Expand Down

0 comments on commit 431b645

Please sign in to comment.