locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
35 lines (34 loc) • 1.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.each = each;
const _arrayPointers_ts_1 = require("../_helpers/_arrayPointers.js");
const _phpRuntimeState_ts_1 = require("../_helpers/_phpRuntimeState.js");
function each(arr) {
// discuss at: https://locutus.io/php/each/
// original by: Ates Goral (https://magnetiq.com)
// revised by: Brett Zamir (https://brett-zamir.me)
// note 1: Uses global: locutus to store the array pointer
// example 1: each({a: "apple", b: "balloon"})
// returns 1: {0: "a", 1: "apple", key: "a", value: "apple"}
const state = (0, _arrayPointers_ts_1.getPointerState)(arr, true);
if (!state) {
return false;
}
const entry = (0, _arrayPointers_ts_1.getEntryAtCursor)(arr, state.cursor);
if (!entry) {
return false;
}
state.setCursor(state.cursor + 1);
const key = entry[0];
const value = entry[1];
const returnArrayOnly = (0, _phpRuntimeState_ts_1.getPhpObjectEntry)(each, 'returnArrayOnly') === true;
if (returnArrayOnly) {
return [key, value];
}
return {
1: value,
value,
0: key,
key,
};
}