locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
75 lines (74 loc) • 2.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.array_slice = array_slice;
const is_int_ts_1 = require("../var/is_int.js");
function array_slice(arr, offst, lgth, preserveKeys) {
// discuss at: https://locutus.io/php/array_slice/
// parity verified: PHP 8.3
// original by: Brett Zamir (https://brett-zamir.me)
// input by: Brett Zamir (https://brett-zamir.me)
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// note 1: Relies on is_int because !isNaN accepts floats
// example 1: array_slice(["a", "b", "c", "d", "e"], 2, -1)
// returns 1: [ 'c', 'd' ]
// example 2: array_slice(["a", "b", "c", "d", "e"], 2, -1, true)
// returns 2: {2: 'c', 3: 'd'}
const preserve = preserveKeys === true;
let result;
if (Array.isArray(arr) && !(preserve && offst !== 0)) {
if (lgth === undefined) {
result = arr.slice(offst);
}
else if (lgth >= 0) {
result = arr.slice(offst, offst + lgth);
}
else {
result = arr.slice(offst, lgth);
}
}
else {
const sourceAssoc = {};
let sourceLength = 0;
if (Array.isArray(arr)) {
for (let index = 0; index < arr.length; index += 1) {
sourceLength += 1;
sourceAssoc[String(index)] = arr[index];
}
}
else {
for (const key in arr) {
sourceLength += 1;
sourceAssoc[key] = arr[key];
}
}
const normalizedOffset = offst < 0 ? sourceLength + offst : offst;
const resolvedLength = lgth === undefined ? sourceLength : lgth < 0 ? sourceLength + lgth - normalizedOffset : lgth;
const sliced = {};
let started = false;
let sourceIndex = -1;
let collected = 0;
let sequentialKey = 0;
for (const key in sourceAssoc) {
sourceIndex += 1;
if (collected >= resolvedLength) {
break;
}
if (sourceIndex === normalizedOffset) {
started = true;
}
if (!started) {
continue;
}
collected += 1;
if ((0, is_int_ts_1.is_int)(key) && !preserve) {
sliced[String(sequentialKey)] = sourceAssoc[key];
sequentialKey += 1;
}
else {
sliced[key] = sourceAssoc[key];
}
}
result = sliced;
}
return result;
}