@hjk/arr
Version:
139 lines (138 loc) • 3.88 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var is_1 = __importDefault(require("@hjk/is"));
function Arr(arr) {
try {
this.value = Array.from(arr);
}
catch (e) {
this.value = [];
}
return this;
}
Arr.prototype.stringify = function (char) {
if (char === void 0) { char = ' '; }
return this.value.reduce(function (str, x) {
if (is_1.default.filled(x) && !!x) {
if (str)
str += char;
str += x;
}
return str;
}, '');
};
Arr.prototype.fill = function (size, data) {
var i = -1;
while (++i < size) {
if (is_1.default.obj(data))
this.value.push(__assign({}, data));
else if (is_1.default.func(data))
this.value.push(data(i));
else
this.value.push(data);
}
return this.value;
};
Arr.prototype.union = function (arr) {
var _this = this;
var filtered = arr.filter(function (x) { return !_this.value.includes(x); });
this.value = this.value.concat(filtered);
return this.value;
};
Arr.prototype.has = function (value) {
if (is_1.default.obj(value)) {
return this.value.some(function (item) {
var eq = false;
if (is_1.default.obj(item)) {
for (var k in value) {
eq = value[k] === item[k];
if (!eq)
break;
}
}
return eq;
});
}
return this.value.includes(value);
};
Arr.prototype.remove = function (value) {
var i = this.indexOf(value);
if (i >= 0)
this.value.splice(i, 1);
return this;
};
Arr.prototype.add = function (value) {
this.value.push(value);
return this;
};
Arr.prototype.indexOf = function (value) {
var index = null;
if (is_1.default.obj(value)) {
this.value.some(function (item, i) {
for (var k in value) {
if (value[k] !== item[k])
return false;
}
index = i;
});
}
else {
var i = this.value.indexOf(value);
if (i >= 0)
index = i;
}
return index;
};
Arr.prototype.find = function (handler, defaultValue, breakValue) {
var i = -1;
var found = defaultValue || null;
while (++i < this.value.length) {
var x = handler(this.value[i], i, this.value);
if (x === breakValue || x) {
found = x === breakValue ? breakValue : this.value[i];
break;
}
}
return found;
};
Arr.prototype.each = function (handler) {
var i = -1;
while (++i < this.value.length) {
if (handler(this.value[i], i, this.value) === false)
break;
}
return this;
};
Arr.prototype.map = function (handler) {
var i = -1;
var arr = [];
while (++i < this.value.length) {
arr[i] = handler(this.value[i], i, this.value, arr);
}
this.value = arr;
return this;
};
Arr.prototype.sort = function (handler) {
this.value.sort(handler || (function (a, b) { return a - b; }));
return this;
};
Object.defineProperty(Arr.prototype, 'age', {
get: function () {
return this.value.length ? this.value.length : null;
}
});
exports.default = (function (arr) { return new Arr(arr); });