fluentnode
Version:
Fluent apis for node (based on the concepts used in C#'s FluentSharp
143 lines (119 loc) • 3.2 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var slice = [].slice,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Array.prototype.add = function() {
var item, j, len, value;
value = 1 <= arguments.length ? slice.call(arguments, 0) : [];
for (j = 0, len = value.length; j < len; j++) {
item = value[j];
this.push(item);
}
return this;
};
Array.prototype.contains = function(value) {
var item, j, len;
if (value instanceof Array) {
for (j = 0, len = value.length; j < len; j++) {
item = value[j];
if (!(indexOf.call(this, item) >= 0)) {
return false;
}
}
return true;
} else {
}
return indexOf.call(this, value) >= 0;
};
Array.prototype.duplicates = function() {
return this.filter(function(x, i, self) {
return self.indexOf(x) === i && i !== self.lastIndexOf(x);
});
};
Array.prototype.empty = function() {
return this.length === 0;
};
Array.prototype.item = function(index) {
if (typeof index === 'number') {
if ((this.length > index && index > -1)) {
return this[index];
}
}
return null;
};
Array.prototype.nth = Array.prototype.item;
Array.prototype.first = function() {
return this.item(0);
};
Array.prototype.second = function() {
return this.item(1);
};
Array.prototype.third = function() {
return this.item(2);
};
Array.prototype.fourth = function() {
return this.item(3);
};
Array.prototype.last = function() {
if (this.length) {
return this[this.length - 1];
} else {
return null;
}
};
Array.prototype.log = function() {
this._str().log();
return this;
};
Array.prototype.not_Contains = function(value) {
return indexOf.call(this, value) < 0;
};
Array.prototype.not_Empty = function() {
return this.length !== 0;
};
Array.prototype.remove_At = function(index) {
this.splice(index, 1);
return this;
};
Array.prototype.remove_First = function() {
return this.remove_At(0);
};
Array.prototype.remove_If_Contains = function(value) {
if (!value) {
return this;
}
return this.filter(function(word) {
return word._str().not_Contains(value._str());
});
};
Array.prototype.size = function() {
return this.length;
};
Array.prototype.starts_With = function(value) {
var item, j, len, ref, results;
ref = this;
results = [];
for (j = 0, len = ref.length; j < len; j++) {
item = ref[j];
if (value && item.starts_With(value)) {
results.push(item);
}
}
return results;
};
Array.prototype.take = function(size) {
if (size === -1) {
return this;
} else {
return this.slice(0, size);
}
};
Array.prototype.unique = function() {
var j, key, output, ref;
output = {};
for (key = j = 0, ref = this.length; 0 <= ref ? j < ref : j > ref; key = 0 <= ref ? ++j : --j) {
output[this[key]] = this[key];
}
return output.keys_Own();
};
}).call(this);