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

@teamawesome/access

v1.0.18

Published

Provide a unified interface for objects.

Downloads

14

Readme

Access

Access provides a unified interface for all sorts of objects. Supported types are:

Alternatively, you could write your own handler to add support for a custom type.

Installation

npm i @teamawesome/access

Usage

The interface implements get, set, has, delete, clear, keys, values, entries and size.

import access from '@teamawesome/access';

access.get(obj, key);
access.set(obj, key, value);
access.has(obj, key);
access.delete(obj, key);
access.clear(obj, key);
access.keys(obj);
access.values(obj);
access.entries(obj);
access.size(obj);

Alternatively, wrap an object to provide the interface. An added benefit for this is better performance. Note that if a handler is necessary for the object, it must be registered before wrapping.

import { wrap } from '@teamawesome/access'

const wrapped = wrap(obj);

wrapped.get(key);
wrapped.set(key, value);
wrapped.has(key);
wrapped.delete(key);
wrapped.clear();
wrapped.keys();
wrapped.values();
wrapped.entries();
wrapped[Symbol.iterator]();
wrapped.size;

Custom types

Objects that implement the Map interface are supported out of the box. To use your own type with access, you must register a handler for it.

class Type {
    _getById(id) {
        //
    }

    del(id) {
        //
    }
}

access.register(Type, {
    get(obj, key) {
        return obj._getById(key);
    },
    
    // Alias methods in one line:
    delete: (obj, key) => obj.del(key)
});

If a type implements one of the methods with the same signature, it is not necessary to add it to the handler. It will be called automatically for you. For example, Map and WeakMap are fully compatible without being registered. A proxied method has precedence over auto-detected methods.

class Type {
    get(key) {
        // Called automatically because it is detected.
    }
    
    set(key, value) {
        // Not called automatically because it is proxied.
    }
}

access.register(Type, {
    set(obj, key, value) {
        //
    }
});

Handler information

Access provides two helper functions to register and unregister types.

access.register(type, handler);
access.unregister(type);

If you would like to access a handler, you can import types. types is a regular Map of constructors and handlers.

import { types } from '@teamawesome/access';

const handler = types.get(type);
// Same as access.register(type, handler)
types.set(type, handler);
// Same as access.unregister(type)
types.delete(type);

Note that calling types.clear() removes all types, not just custom ones.