A safe version of .at
#fix_ecmascript_at
Native .at
method is very harmful:
const arr = [1, 2, 3];
console.log(arr.at("foo")); // => 1
console.log(arr.at(NaN)); // => 1
console.log(arr.at(1.5)); // => 2
You can use safe-at
instead:
import safeAt from "safe-at";
const arr = [1, 2, 3];
console.log(safeAt(arr, "foo")); // => undefined
console.log(safeAt(arr, NaN)); // => undefined
console.log(safeAt(arr, 1.5)); // => undefined
npm install safe-at
// ES Modules
import safeAt from "safe-at";
// CommonJS
const safeAt = require("safe-at");
import safeAt from "https://deno.land/x/safe_at/mod.ts";