From 998156df32d2de4e4c3d263d94ecffc92022c89b Mon Sep 17 00:00:00 2001 From: Guido Date: Mon, 8 Sep 2014 12:38:10 -0300 Subject: [PATCH 1/2] Add constructor param to support custom comparator function --- component.json | 13 ++++++++++--- index.js | 27 ++++++++++++++++++++++----- package.json | 2 +- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/component.json b/component.json index 109c0de..febaf47 100644 --- a/component.json +++ b/component.json @@ -2,9 +2,16 @@ "name": "set", "version": "1.0.0", "description": "set container", - "keywords": ["set"], - "scripts": ["index.js"], + "keywords": [ + "set" + ], + "scripts": [ + "index.js" + ], "development": { "component/assert": "*" - } + }, + "remotes": [ + "https://raw.githubusercontent.com" + ] } \ No newline at end of file diff --git a/index.js b/index.js index 9c3fc48..b2766f2 100644 --- a/index.js +++ b/index.js @@ -5,22 +5,29 @@ module.exports = Set; + /** * Initialize a new `Set` with optional `vals` * - * @param {Array} vals + * @param {Array} vals source array + * @param {Object} opts options parameter to specify custom `comparator` function * @api public */ -function Set(vals) { - if (!(this instanceof Set)) return new Set(vals); +function Set(vals, opts) { + if (!(this instanceof Set)) return new Set(vals, opts); this.vals = []; + + // Manage options + opts = opts || {}; + this.compare = ('function' == typeof(opts.comparator)) ? opts.comparator : this._defaultComparator; + if (vals) { for (var i = 0; i < vals.length; ++i) { this.add(vals[i]); } } -} +}; /** * Add `val`. @@ -58,7 +65,7 @@ Set.prototype.indexOf = function(val){ for (var i = 0, len = this.vals.length; i < len; ++i) { var obj = this.vals[i]; if (obj.equals && obj.equals(val)) return i; - if (obj == val) return i; + if (this.compare(obj, val)) return i; } return -1; }; @@ -186,3 +193,13 @@ Set.prototype.isEmpty = function(){ return 0 == this.vals.length; }; +/** + * Default comparator + * + * Compares two possible members of the `Set` to determine equality. + * Should be overriden through `Set` constructor. + * + * @return {Boolean} + * @api private + */ +var _defaultComparator = function(a, b) { return a == b; }; diff --git a/package.json b/package.json index 03b8f3f..ebc82a8 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,6 @@ }, "repository": { "type": "git", - "url": "https://github.com/component/set.git" + "url": "https://github.com/gvilarino/set.git" } } \ No newline at end of file From eae66eda2c4fd16130bac6c2843db9ac73603d52 Mon Sep 17 00:00:00 2001 From: Guido Date: Mon, 8 Sep 2014 13:13:01 -0300 Subject: [PATCH 2/2] Fix comparation logic --- index.js | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index b2766f2..9d74d04 100644 --- a/index.js +++ b/index.js @@ -20,7 +20,7 @@ function Set(vals, opts) { // Manage options opts = opts || {}; - this.compare = ('function' == typeof(opts.comparator)) ? opts.comparator : this._defaultComparator; + this.areEqual = ('function' == typeof(opts.comparator)) ? opts.comparator : null; if (vals) { for (var i = 0; i < vals.length; ++i) { @@ -64,8 +64,11 @@ Set.prototype.has = function(val){ Set.prototype.indexOf = function(val){ for (var i = 0, len = this.vals.length; i < len; ++i) { var obj = this.vals[i]; + + // Comparation logic hierarchy + if (this.areEqual && this.areEqual(obj, val)) return i; if (obj.equals && obj.equals(val)) return i; - if (this.compare(obj, val)) return i; + if (obj == val) return i; } return -1; }; @@ -192,14 +195,3 @@ Set.prototype.intersect = function(set){ Set.prototype.isEmpty = function(){ return 0 == this.vals.length; }; - -/** - * Default comparator - * - * Compares two possible members of the `Set` to determine equality. - * Should be overriden through `Set` constructor. - * - * @return {Boolean} - * @api private - */ -var _defaultComparator = function(a, b) { return a == b; };