UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

32 lines (31 loc) 1.04 kB
import { getEntryAtCursor, getPointerState } from "../_helpers/_arrayPointers.js"; import { getPhpObjectEntry } from "../_helpers/_phpRuntimeState.js"; export 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 = getPointerState(arr, true); if (!state) { return false; } const entry = getEntryAtCursor(arr, state.cursor); if (!entry) { return false; } state.setCursor(state.cursor + 1); const key = entry[0]; const value = entry[1]; const returnArrayOnly = getPhpObjectEntry(each, 'returnArrayOnly') === true; if (returnArrayOnly) { return [key, value]; } return { 1: value, value, 0: key, key, }; }