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

@necrobits/shapeshifter

v0.1.1

Published

A minimal component responsible for shape-shifting the data

Downloads

9

Readme

Shapeshifter.js

A mini Typescript/Javascript library that helps you transform objects faster & easier.

Table of Contents

Installation

Using npm

npm install @necrobits/shapeshifter

or using yarn

yarn add @necrobits/shapeshifter

Usage

// Create a shapeshifter using a schema
const shapeshifter = Shapeshifter.create(schema);
// Transform the data
const newData = shapeshifter.transform(data);

Transformation schema

schema is an object that describes the transformation. There are different ways to define it. Usually, the keys in the schema are the attribute names in the object. There is an exception for virtual attributes, we will come back to that later on. Now let's learn some simple syntax first.

Just copy an attribute

Just use true to tell the Shapeshifter to copy the attribute.

{
   my_attribute : true
}

Rename an attribute

Simply write the new name as value

{
    my_attribute: "my_new_attribute"
}

Use a mapping function

Yes, you can also use a function to map the value

{
    my_attribute: (value) => `This is a new ${value}`
}

Sometimes, you want to construct the value based on other fields. That's the time to utilize the 2. argument in the mapping function. This example transforms the first name field into full name.

{
    first_name: (first_name, obj) => `${first_name} ${obj.last_name}`
}

Full definition

The 3 variants above is a shorthand version for convenience and compactness. You can define the transformation using the full version like this:

{
    my_attribute: {
        to: "my_new_attribute",
        mapping: (value) => `This is a new ${value}`
    }
}

Advanced settings

Nested transformation

You can define a nested transformation rule using the magic key __nested__.

  • If parents is only a object, the Shapeshifter will transform it as usual.
  • If parents is an array of multiple parent objects, the Shapeshifter will automatically apply a map function on the array to transform every element in the array.
{
    first_name: true,
    last_name: true,
    parents: {
        __nested__: true,
        first_name: true,
        last_name: true
    }
}

Virtual attributes

It's kinda irrational to transform first_name into full_name in the above example.
What if you want to keep first_name, last_name but also want to create a new field called full_name? That's where virtual attributes come into play. With virtual attributes you can define new attributes. Because those attributes are not created based on any old attributes, so we call them "virtual".

In order to define virtual attributes, you need to use the following format __virtual<name>__. For example:

{
    first_name: true,
    last_name: true,
    __virtual_fullname__: {
        to: "full_name",
        mapping: (_, obj) => `${obj.first_name} ${obj.last_name}`
    },
    __virtual_fullname2__: {
        to: "full_name_uppercase",
        mapping: (_, obj) => `${obj.first_name} ${obj.last_name}`.toUpperCase()
    }
}

License

MIT