orxapi.tools.array
Version:
The TypeScript array tools library for orxapi
64 lines • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Make a fill array with number
* @param n
* @returns {number[]} The array with n number
*/
function makeArray(n) {
// When the value of the number is less than 1
if (n < 1) {
return [];
}
return (new Array(n).join().split(",").map(function (item, index) { return ++index; }));
}
exports.makeArray = makeArray;
/**
* Make a fill array with number between min and max value
* @param min
* @param max
* @param offset
* @returns {number[]} The array with n number
* @deprecated use seq method instead of range
*/
function range(min, max, offset) {
if (offset === void 0) { offset = min; }
return (new Array(max - min + 1).join().split(",").map(function (item, index) { return index + offset; }));
}
exports.range = range;
/**
* Make a fill array with number between min and max value, and return the real sequence values
* @param {string} sequence
* @returns {number[]}
* @example const childrenList = seq("2..8").map((value) => { code: `${value}`, label: `${value}` });
*/
function seq(sequence) {
var _a = sequence.split(".."), min = _a[0], max = _a[1];
var interval = parseInt(max, 10) - parseInt(min, 10) + 1;
return makeArray(interval).map(function (item) { return (item + parseInt(min, 10) - 1); });
}
exports.seq = seq;
/**
* Format a Number, Exactly Two in Length
* @param n
* @returns {string} Format 1 to 01
*/
function pad(n) {
return (n < 10 ? "0" : "") + n;
}
exports.pad = pad;
/**
* Split an array on multiple array
* @param arr The array who is has splitting
* @param chunkSize The division size of the painting
* @return { T[][] } return the arrays
*/
function chunk(arr, chunkSize) {
return arr.reduce(function (accumulator, currentValue, index, array) {
return !(index % chunkSize) ?
accumulator.concat([array.slice(index, index + chunkSize)]) :
accumulator;
}, []);
}
exports.chunk = chunk;
//# sourceMappingURL=array.js.map