@darwish/utils-core
Version:
128 lines (127 loc) • 4.14 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var utils_is_1 = require("@darwish/utils-is");
var ExtendArray = /** @class */ (function (_super) {
__extends(ExtendArray, _super);
function ExtendArray(value) {
var _this = _super.call(this) || this;
_this.value = [];
_this.toChunk = _a.chunk;
/**
* Don't use this method
* @private
* @description Unique array
* @param array data source
* @returns unique array
*/
_this.unique = function (array) {
var length = array === null ? 0 : array.length;
if (!length) {
return [];
}
var index = -1;
var result = [];
while (++index < length) {
var value = array[index];
if (result.indexOf(value) === -1) {
result.push(value);
}
}
return result;
};
_this.toShuffle = _a.shuffle;
if ((0, utils_is_1.isArray)(value)) {
_this.value = value;
}
return _this;
}
var _a;
_a = ExtendArray;
/**
* @description Chunk array
* @param array Data source
* @param size Chunk size
* @returns A new data of chunked array
*/
ExtendArray.chunk = function (array, size) {
if (!(0, utils_is_1.isArray)(array)) {
return [];
}
var count = 0;
var index = 0;
var result = [];
array.forEach(function (it) {
if (count === 0) {
result.push([]);
}
result[index].push(it);
if (count === size - 1) {
index++;
count = 0;
}
else {
count++;
}
});
return result;
};
/**
* @property `static` chunk array
* @description Shuffle array
* @param array Data source
* @returns A new data of shuffled array
*/
ExtendArray.shuffle = function (array) {
if (!(0, utils_is_1.isArray)(array)) {
return [];
}
var index = -1;
var lastIndex = length - 1;
var result = __spreadArray([], array, true);
while (++index < length) {
var rand = index + Math.floor(Math.random() * (lastIndex - index + 1));
var value = result[rand];
result[rand] = result[index];
result[index] = value;
}
return result;
};
ExtendArray.flattenOneDimArr = function (array) {
var ans = [];
array.forEach(function (item) {
if ((0, utils_is_1.isArray)(item)) {
ans = ans.concat(_a.flattenOneDimArr(item));
}
else {
ans.push(item);
}
});
return ans;
};
return ExtendArray;
}(Array));
exports.default = ExtendArray;