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

arraylike-proxy

v0.1.0

Published

Make things behave like an array

Downloads

11

Readme

ArrayLike Proxy

Treat everything like an array! ArrayLike Proxy wraps a function, arrow function, object, arguments, or an array and allows you to treat it just like an array.

Uses

Objects

Have an ArrayLike Object and you want to use it like an array? Too bad! You have to convert it first:

import ArrayLike from 'src/ArrayLike';

const array = {
    0: 'zero',
    1: 'one',
    length: 2
}

// X Cant do this!
// array.concat([1, 2, 3]);

// Now you can!
ArrayLike(array).concat([1, 2, 3])  // [ 'zero', 'one', 1, 2, 3]

// Or this!
ArrayLike(array).splice(array.length, 0, 1, 2, 3);
array.length; // 5

Functions

Want a method to maintain its own list?

import ArrayLike from 'src/ArrayLike';

const addMiddleware = (route, handler) => {
	middleware.push({
		route,
		handler
	});
	return middleware;
};
const middleware = ArrayLike(addMiddleware);

// Now we have a functional list builder
middleware('/user/:id', () => {}).middleware('/admin', () => {}).middleware('/', () => {});

Or maybe we want to collect and build

import ArrayLike from 'src/ArrayLike';

const builder = function (name) {
	this.name = name;
	this.assignments = [... StudentAssignments]
}
const StudentAssignments = ArrayLike(builder);
StudentAssignments.push('Take home Test A');
StudentAssignments.push('Homework 1');
StudentAssignments.push('Quiz I');
const studentRecords = [
	new StudentAssignments('Joe Smith');
	new StudentAssignments('Jane Doe');
];

Ok. Maybe that last example is a solution looking for a problem....

Arguments

How about an anarchronistic example. Nobody should be using arguments anymore!

import ArrayLike from 'src/ArrayLike';

function methodOne(one, two) {
	if (typeof two === 'number') {
		throw new Error();
	}
	
	// So fancy!
	return methodTwo(ArrayLike(arguments).reverse());
}

function methodTwo(two, one) {
	if (typeof two === 'number') {
		throw new Error();
	}
	
	return `${ two } is number: ${ one }`;
}

methodOne(1, 'I');  // I did it!

Installation & Usage

This package is supported on modern browsers and Node 6+

npm install arraylike-proxy --save

Node.js

const ArrayLike = require('arraylike-proxy');

AMD

define([ 'arraylike-proxy' ], (ArrayLike) => {
	
});

TypeScript

import ArrayLike from 'arraylike-proxy'

Ambient declarations are available as part of the npm package. TypeScript source is available from the github repo.

Fin

Wow! You made it! Well I guess you deserve a story. I made this library because JavaScript, for all its flexibility, didn't have a good way of creating a method that could also behave like an array. Sure, we have this concept of arraylike objects, but those tend to break down when you want to use direct assignments or, you know, actually use the thing like an array. Plus I get to do terrible things with Proxy and Symbol. Win-win!