UNPKG

locutus

Version:

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

33 lines (32 loc) 1.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.end = end; const _arrayPointers_ts_1 = require("../_helpers/_arrayPointers.js"); function end(arr) { // discuss at: https://locutus.io/php/end/ // original by: Kevin van Zonneveld (https://kvz.io) // bugfixed by: Legaev Andrey // revised by: J A R // revised by: Brett Zamir (https://brett-zamir.me) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Kevin van Zonneveld (https://kvz.io) // note 1: Uses global: locutus to store the array pointer // example 1: end({0: 'Kevin', 1: 'van', 2: 'Zonneveld'}) // returns 1: 'Zonneveld' // example 2: end(['Kevin', 'van', 'Zonneveld']) // returns 2: 'Zonneveld' const state = (0, _arrayPointers_ts_1.getPointerState)(arr, true); if (!state) { return false; } const lastIndex = (0, _arrayPointers_ts_1.getArrayLikeLength)(arr) - 1; if (lastIndex < 0) { return false; } const entry = (0, _arrayPointers_ts_1.getEntryAtCursor)(arr, lastIndex); if (!entry) { return false; } state.setCursor(lastIndex); return entry[1]; }