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 🙏

© 2025 – Pkg Stats / Ryan Hefner

cross-destruct

v1.0.0

Published

A simple function to destructure data

Downloads

15

Readme

cross-destruct

A simple function to destructure data

Why? JS has object destrucutring built in, right?

Picture this: Your server is storing a huge object full of data. As the client you only need part of that data. It would be a total waste to fetch the whole object and then only use the parts you need. Wouldn't it be better to destrucutre on the server-side? Short answer: yes. But. You need a way to tell the server what to destructure. JSON doesn't have destructuring built in, and moving the server to a more advanced query language like GraphQL (don't get me wrong, I :heart:  GraphQL) would require huge buyin both from the frontent and backend teams. Cross-destruct provides a very simple syntax to destructure objects, using objects.

Syntax

Cross-destruct uses a syntax called template objects. Think of a template object like a javascript object with only keys, no values. Cross-destruct will populate those keys with values. Here's an example

// Include the library
const destruct = require('cross-destruct');

// A bunch of data
const lotsOfData = {
    tweets: [
        {
            author: 'user1',
            text: 'hello world! foo bar baz'
        },
        {
            author: 'user2',
            text: 'Lorem ipsum'
        }
    ],
    users: {
        'user1': {
            fullName: 'Jake',
            tweets: 1
        },
        'user2': {
            fullName: 'Joe',
            tweets: 1,
        }
    }
}

// We only want the first tweet's author
const template = {
    // Of the tweets key, ...
    tweets: {
        // we want only the item with the key 0, ...
        "0": {
            // of which we want only the `author` key ...
            author: "*" // The "*" (wildcard) means we want the full value of the key, no further destructuring...
        }
    }
}

console.log(destruct(lotsOfData, template));
// Output:
// {
//    tweets: [
//        {
//            author: 'user1'
//        }
//    ]
// }

Let's break this down: lotsOfData just stores a bunch of data that we need to destructure. The template object is a bit more interesting: We create a javascript object with the key tweets. This means that only the data's tweets key is relevant to us. Since we want data from the first tweet, the value to the key is another template object. Note that eventhough tweets is an array, we still pass a template object. The key "0" is self-explanatory. It retrives the first post from the list. Since we don't want the full post, we pass another template as the value, and continue destructuring. Of the first tweet, we want the author. To signify that we do not wish to destrucutre even deeper, we set the value to "*". This * is refered to as the widcard. That means that no matter what type the value on the actual data is, we want to get the whole thing, as-is and destructure no more.