locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
30 lines (29 loc) • 1.07 kB
JavaScript
import { getArrayLikeLength, getEntryAtCursor, getPointerState } from "../_helpers/_arrayPointers.js";
export 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 = getPointerState(arr, true);
if (!state) {
return false;
}
const lastIndex = getArrayLikeLength(arr) - 1;
if (lastIndex < 0) {
return false;
}
const entry = getEntryAtCursor(arr, lastIndex);
if (!entry) {
return false;
}
state.setCursor(lastIndex);
return entry[1];
}