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

@chriscodesthings/extensible-read-only-array

v1.0.1

Published

Creates a read only array, allowing extension with custom methods

Downloads

5

Readme

extensible-read-only-array · Test workflow status NPM Version License: MIT

Creates a read only array, allowing extension with custom methods

Description

Extensible Read Only Array uses a proxy to create a makeshift API to your array.

The array can be read as normal, with direct requests for specific indexes, the length property and iterator function, however all requests to set an index are blocked.

The array can then be extended by providing an object or class containing your own code.

See...

Install

npm install --save @chriscodesthings/extensible-read-only-array

Usage

import makeReadOnlyArray from '@chriscodesthings/extensible-read-only-array';

const numbers = makeReadOnlyArray([], {
    addSquareNumber(n, arr) {
        return arr.push(n * n);
    }
});

console.log(numbers);
// => []

numbers.addSquareNumber(2);
numbers.addSquareNumber(4);
numbers.addSquareNumber(6);

console.log(numbers);
// => [ 4, 16, 36 ]

Note, the array is always passed by the proxy as the last argument to the function.

Important! Do not return the array since this will allow direct access to it.

Syntax

makeReadOnlyArray(arr, obj, allow, allowDefaults);

Parameters

  • arr: the array to make read only
  • obj: your object containing methods/properties to redirect to
  • allow: an array containing a list of array methods to allow
  • allowDefaults: Default true. If false, blocks access to the methods allowed by default.

See also, Allowing Array Methods

Return Value

Returns a proxy attached to the original array.

Examples

Use with a handler object

We can create an array that will only store people's names by using a handler object with a method to add a person to the array.

Note, within your code, you have full access to the array.

import makeReadOnlyArray from '@chriscodesthings/extensible-read-only-array';

const peopleHandler = {
    addPerson(first, last, age, arr) {
        arr.push({
            firstname: first,
            lastname: last,
            age: age
        });
    }
};

const people = makeReadOnlyArray([], peopleHandler);

Use with a class

You can use Extensible Read Only Array directly from your constructor to allow read only access to an array, while keeping it private within your class.

import makeReadOnlyArray from '@chriscodesthings/extensible-read-only-array';

class extendedReadOnlyArrayClass {
    #people = [];

    constructor() {
        return makeReadOnlyArray(this.#arr, this);
    }

    addPerson(first, last, age) {
        this.#people.push({
            firstname: first,
            lastname: last,
            age: age
        });
    }
}

const testArray = new extendedReadOnlyArrayClass();

Remember! Even though it isn't used, the array is still passed as the last argument to the function.

Allowing Array Methods

Routing priority

The proxy will route the request in the following order:

  1. A matching method in obj. This allows you to override any of the built in array methods.
  2. A matching property in obj. This allows you to override any of the built in array properties.
  3. If request is a number >= 0, an index from the array.
  4. If the request is in the allow list (see below), the request is passed to the native array.
  5. Returns undefined.

Access to the native array methods is divided into 3 categories.

Always allowed

These properties/methods are always allowed since they are required for normal array iteration.

  • length property
  • [@@iterator] method

Allowed by default

These methods are allowed by default, unless allowDefaults is set to false. They are all considered to be 'read only' methods which do not modify, or allow modification in any way, of the original array.

There is little point in excluding access to these methods since they could easily be replicated by simply first copying the array contents into a new array, element by element.

  • at
  • concat
  • entries
  • every
  • find
  • findIndex
  • findLast
  • findLastIndex
  • forEach
  • includes
  • indexOf
  • join
  • keys
  • lastIndexOf
  • map
  • reduce
  • reduceRight
  • some
  • toLocaleString
  • toReversed
  • toSorted
  • toSpliced
  • toString
  • values
  • with

Allow specific methods

To allow access to certain methods, specify these in an array when creating the Extensible Read Only Array.

For example, to allow access to the reverse() and sort() methods:

const readOnlyArray = makeReadOnlyArray([], arrayHandler, ["reverse", "sort"]);