Registry / serialization / build-array

build-array

JSON →
library1.0.0jsnpmunverified

The `build-array` package provides a minimalist utility for programmatically creating arrays of a specified `length`, optionally pre-filled with a given `value`. Currently stable at version 1.0.0, this package is likely in a maintenance state, receiving updates only for critical bug fixes if any, due to its straightforward and complete functionality. It offers a lightweight alternative to more verbose patterns like `new Array(length).fill(value)`, particularly useful in environments or coding styles where brevity and explicit initialization are preferred. Its primary differentiator is its single-function API, simplifying array creation and initial population without requiring chained methods or polyfills for older `Array.prototype.fill` behavior.

npm install build-array
INSTALL
IMPORT
SIG · BUILD-ARRAY
B
build-array
serializationjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

buildArray
const buildArray = require('build-array');
CommonJS syntax, suitable for Node.js environments.
buildArray
import buildArray from 'build-array';
import { buildArray } from 'build-array';
ESM syntax. Given the package's age (v1.0.0), it likely uses a default export. Modern bundlers typically handle this, but explicit default import is the most reliable.

This quickstart demonstrates how to use `build-array` to create arrays of various lengths, with and without initial fill values, and highlights a key behavior when filling with object references.

// CommonJS example in a Node.js environment const buildArray = require('build-array'); // Example 1: Create an array of 5 undefined elements const arr1 = buildArray(5); console.log('Array 1 (undefined):', arr1); // Expected: [undefined, undefined, undefined, undefined, undefined] // Example 2: Create an array of 3 elements, all initialized to 0 const arr2 = buildArray(3, 0); console.log('Array 2 (filled with 0):', arr2); // Expected: [0, 0, 0] // Example 3: Create an array of 2 elements, filled with a string const arr3 = buildArray(2, 'hello'); console.log('Array 3 (filled with "hello"):', arr3); // Expected: ['hello', 'hello'] // Example 4: Demonstrating the "same reference" gotcha when filling with objects const obj = { id: 1 }; const arr4 = buildArray(2, obj); arr4[0].id = 99; // Modifying the object in the first element console.log('Array 4 (object reference):', arr4); // Expected: [{ id: 99 }, { id: 99 }] - both elements updated because they share the same object reference // Example 5: Using it with dynamic length (modern JS syntax for nullish coalescing) const maybeLength = Math.random() > 0.5 ? 4 : null; const arr5 = buildArray(maybeLength ?? 0, 'dynamic'); console.log('Array 5 (dynamic length):', arr5);
Debug
Known issues
gotchaWhen `buildArray` is used to fill an array with an object (or array) as the `value` argument, all elements in the resulting array will hold references to the *exact same object*. Modifying one element's object will affect all other elements.
fix
If independent objects are required for each array element, iterate over the created array and assign new object instances (e.g., `Array(length).map(() => ({...template}))`).
affects: >=1.0.0
gotchaThis package primarily targets older Node.js environments (>=0.8). While it functions in modern environments, it might not leverage newer JavaScript features or optimizations available in `Array.prototype.fill` or `Array.from`.
fix
For new projects targeting modern JavaScript environments, consider native methods like `Array(length).fill(value)` or `Array.from({ length }, () => value)` which offer similar functionality and are widely supported.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: buildArray is not a function
The `buildArray` function was not correctly imported or required. This often happens when mixing CommonJS `require()` with ES Module `import` syntax, or attempting to use a named import for a default export.
fix
For CommonJS, use `const buildArray = require('build-array');`. For ES Modules, use `import buildArray from 'build-array';`. Ensure your environment supports the chosen module system.
TypeError: The 'length' argument must be an integer, not undefined
The first argument, intended as the array's length, was not provided or evaluated to `undefined` or a non-numeric type, which `build-array` cannot process as a valid length.
fix
Ensure the first argument passed to `buildArray` is a non-negative integer. Use nullish coalescing (e.g., `length ?? 0`) or type checking if the length might be dynamic or optional.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
build-array — npm install build-array · libregistry