npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@humanwhocodes/array-with-default

v1.1.0

Published

An array with default values for missing indices.

Downloads

26

Readme

Array with Default Utility

by Nicholas C. Zakas

If you find this useful, please consider supporting my work with a donation.

Description

A class that represents an array where missing elements return a specified default value.

The built-in JavaScript Array class automatically returns undefined when you attempt to access a missing element. Here's an example:

const items = [1, , 3];
console.log(items[1]);      // undefined

In most cases that is okay, but in some cases you may want an alternate default value returned. The ArrayWithDefault class does that:

const items = new ArrayWithDefault({
    default: 0
    elements: [1, , 3]
});

console.log(items[1]);      // 0

The primary use case for this class is when an array is an optional argument for a function or when an array may have holes.

Usage

Node.js

Install using npm or yarn:

npm install @humanwhocodes/array-with-default --save

# or

yarn add @humanwhocodes/array-with-default

Import into your Node.js project:

// CommonJS
const { ArrayWithDefault } = require("@humanwhocodes/array-with-default");

// ESM
import { ArrayWithDefault } from "@humanwhocodes/array-with-default";

Deno

Import into your Deno project:

import { ArrayWithDefault } from "https://cdn.skypack.dev/@humanwhocodes/array-with-default?dts";

Browser

It's recommended to import the minified version to save bandwidth:

import { ArrayWithDefault } from "https://cdn.skypack.dev/@humanwhocodes/array-with-default?min";

However, you can also import the unminified version for debugging purposes:

import { ArrayWithDefault } from "https://cdn.skypack.dev/@humanwhocodes/array-with-default";

API

After importing, create a new instance of ArrayWithDefault. The constructor expects one object argument with the following properties:

  • default (required) - the default value to return for the missing items.
  • elements - an optional iterable object used to populate the array.
  • length - an optional value to set the array's length property to.
  • outOfRange - an optional value that, when set to true, indicates that numeric indices after the end of the array should also return the default value.

Here are some examples:

const items = new ArrayWithDefault({
    default: 0,
    elements: [1, , 3]
});

// missing elements return the default
console.log(items[1]);      // 0

// elements after the end of the array return undefined
console.log(items[10])      // undefined

// elements whose value are undefined return the default
items.push(undefined);
console.log(items[3]);      // 0

const emptyItems = new ArrayWithDefault({
    default: 0,
    length: 5
});

// all elements return 0
console.log(emptyItems[0]);     // 0
console.log(emptyItems[1]);     // 0
console.log(emptyItems[2]);     // 0
console.log(emptyItems[3]);     // 0
console.log(emptyItems[4]);     // 0

// items past the end still return undefined
console.log(emptyItems[5]);     // undefined

const numbers = new ArrayWithDefault({
    default: 0,
    elements: [1, 2, 3],
    outOfRange: true
});

// all elements return 0
console.log(emptyItems[0]);     // 1
console.log(emptyItems[1]);     // 2
console.log(emptyItems[2]);     // 3
console.log(emptyItems[3]);     // 0
console.log(emptyItems[4]);     // 0
console.log(emptyItems[5]);     // 0

Developer Setup

  1. Fork the repository
  2. Clone your fork
  3. Run npm install to setup dependencies
  4. Run npm test to run tests

License

Apache 2.0