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

import-to-array

v1.0.0

Published

A small utility function that converts ES6 import objects to an array of its own property values, complete with TypeScript defintions.

Downloads

1,189

Readme

import-to-array

A small utility function that converts ES6 import objects to an array of its own property values, complete with TypeScript defintions.

Installation

Install from NPM with Yarn or NPM:

yarn add import-to-array

# Or use NPM
npm i import-to-array --save

Usage

The main point of this package is to convert an imported object to an array of its own property values, while filtering out the __esModule properties that TypeScript adds, and while maintaining TypeScript definitions for the array values.

import importToArray from "import-to-array";
import * as SomeObject from "./path/to/module";

// SomeObject looks like { hello: "world", foo: 1, bar: false, __esModule: any }

// Convert the import to an array of its properties
const array = importToArray(SomeObject);

// Array looks like ["hello", 1, false]
// Array has TypeScript type (string | number | bar)[];

In this example, the array variable would look like ["hello", 1, false] and wouldn't include the __esModule value. If you're using TypeScript, the variable with have type (string | number | bar)[].

Real world usage

In Gearworks we have a routes folder that contains separate API route modules for an Express server. Each route module exports a single function called registerTopicRoutes that must be called by the server to, well, register those routes.

The problem was that we had to remember to add an import for each route in the server.ts module and then also remember to actually call its function. We simplified the whole process by using Barrelsby to automatically export all of those routes from the routes folder, which lets us import all of them at once in the server module.

That helped a lot, as it let us map the imported object's keys and then iterate over those keys to register the routes:

import * as routeRegistrations from "./routes";

await Promise.all(Object.keys(routeRegistrations).map(async propName => await routeRegistrations[propName](arg1, arg2, etc)));

While this was a big improvement, that presented one other problem: in TypeScript, the routeRegistrations[propName] had a type of any, which could come back to bite us if we ever changed the signature of one of the route registration functions. Luckily, TypeScript soon introduced the Record type, which lets us take an argument and use its properties as a generic type.

That is to say, we can pass in the routeRegistrations object and automatically know the type of all of its property values:

import * as routeRegistrations from "./routes";
import importToArray from "import-to-array";

await Promise.all(importToArray(routeRegistrations).map(async registerRoute => {
    // registerRoute has type: (path: string, requiresAuth: boolean, routingFunction: RoutingFunction) => Promise<void>;

    return await registerRoute(path, requiresAuth, routingFunction);
}))