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

arraymake

v0.0.33

Published

A new static method for the Array constructor that let you create an array with predefined length and filling values

Downloads

15

Readme

arraymake

introduction

A new ES3 compliant static method for the Array constructor that let you create an array with predefined length and filling values.

Array.make(3, 42); // [42, 42, 42]

Array.make(3, (_, i) => i); // [0, 1, 2]

Array.make(3, () => ({ safe: true })); // [{ safe: true }, { safe: true }, { safe: true }]

installation

Install it from npm:

$ npm i -S arraymake

then import it:

require("arraymake");

motivation

Currently there isn't a way to create an array by pre-setting its length and an optional filling value that is both straightforward and safe.
We pass from cryptic and verbous solutions like Array.apply(null, Array(N)).map(mapperFn), to only verbous solution like Array.from({length:N}, mapperFn) and [...Array(N)].map(mapperFn) or a dangerous one like Array(N).fill(value).

Why is the last dangerous? Spot the bug here:

const grid = Array(2).fill(Array(2).fill(false));

grid[0][0] = true;
grid[0][1] = true;

console.log(String(grid));
// why is true, true, true, true
// instead of true, true, false, false?

solution

The new static method was developed with safety and simplicity in mind, without straying too far from the dynamic nature of the language.

Calling Array.make without arguments is allowed and an empty array is returned:

Array.make(); // []

Remember that undefined means the absence of value in JavaScript:

Array.make(undefined); // []

If provided, the first argument should be a number or a string coercible into a valid numeric value. There are some restriction about the numeric values to be considered valid: the value should be different from NaN, positive, integer and finite:

Array.make(NaN); // TypeError
Array.make(Infinite); // TypeError
Array.make(-1); // TypeError
Array.make(3.7); // TypeError
Array.make(null); // TypeError
Array.make({}); // TypeError
Array.make([]); // TypeError
Array.make(() => {}); // TypeError
Array.make(Symbol()); // TypeError
Array.make("foo"); // TypeError

Array.make(0); // Ok
Array.make(10); // Ok
Array.make("42"); // Ok

The default value used to fill the array is undefined. Forget about empty slots:

Array.make(5); // [undefined, undefined, undefined, undefined, undefined]

The second argument is used to set the filling value/values:

Array.make(5, "foo"); // ["foo", "foo", "foo", "foo", "foo"]

To avoid the Array.prototype.fill dangerousness, you cannot pass an object nor an array as second argument:

Array.make(5, {}); // TypeError
Array.make(5, []); // TypeError

That will prevent creating an array with more than one reference to the same entity.
This is an unwelcome behavior intrinsically linked to the fill method; something that should be clearly reported with an exception instead of being allowed, causing hard to catch bugs.

When you need to fill the array with non primitive entities, pass a function as second argument. It will act both as a factory function and a mapper function:

Array.make(3, (_, index, arrayUnderCostruction) => ({ safe: true, id: index }));
// [{ safe: true, id: 0 }, { safe: true, id: 1 }, { safe: true, id: 2 }]

pitfalls

Yes, I'm adding a static method to a native entity. If you don't like this approach, jump to the alternative section.
I don't think that such a method will be ever added to the language, but if it were so I'm sure that a different name will be chosen. Like fromLength. TC39 is going to hate me for this.

Peace ✌🏻.

alternative

If you disagree with the "extending natives" modus operandi, you can import the arrayMake function without affecting the Array constructor:

const { arrayMake } = require("arraymake/dist/functional.js");