Skip to content

petamoriken/safe-at

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

safe-at

A safe version of .at #fix_ecmascript_at

See this TC39 discourse topic


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

Install

npm install safe-at

Import

Node.js

// ES Modules
import safeAt from "safe-at";
// CommonJS
const safeAt = require("safe-at");

Deno

import safeAt from "https://deno.land/x/safe_at/mod.ts";