Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -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://github.com"
]
}
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.areEqual = ('function' == typeof(opts.comparator)) ? opts.comparator : null;

if (vals) {
for (var i = 0; i < vals.length; ++i) {
this.add(vals[i]);
}
}
}
};

/**
* Add `val`.
Expand Down Expand Up @@ -57,6 +64,9 @@ 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 (obj == val) return i;
}
Expand Down Expand Up @@ -185,4 +195,3 @@ Set.prototype.intersect = function(set){
Set.prototype.isEmpty = function(){
return 0 == this.vals.length;
};

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
},
"repository": {
"type": "git",
"url": "https://github.com/component/set.git"
"url": "https://github.com/gvilarino/set.git"
}
}