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

mass-data-transform

v1.0.4

Published

Transforms array of objects into a custom format. Was developed because I wanted my data to look a certain way after getting it from an API.

Downloads

4

Readme

mass-data-transform

Transforms an array of objects into a custom format. Was developed because I wanted my data to look a certain way after retrieving data from an API. This library allows you to transform, add, delete, and rename fields.

Installing

npm install mass-data-transform
yarn add mass-data-transform

Initializing

import transform from "mass-data-transform";
var transform = require("mass-data-transform");

How To Use

Syntax

let transformedData = transform(arrObj, options);

Parameters

arrObj : The array of objects
e.g:
const arrObj = [{id:1,name:'Bulbasaur',{id:2,name:'Squirtle'}}]
options : an object that exists of up to 4 different key values - transformFields, addFields, deleteFields, and renameFields. All 4 are optional.
e.g:
const options = {
  transformFields:/*optional object here*/,
  addFields:/*optional object here*/,
  deleteFields:/*optional array here*/,
  renameFields/*optional object here*/
}
Please refer to examples for more information.

options - in depth

transformFields: object that consists of the key value and functions used to transform the data.
addFields: object that consists of the new key value names and functions used for the new fields.
deleteFields: array that consists of the strings of the keys to be deleted.
renameFields: object that consists of the old key / new key.

The order of precedence for the operations is transform -> add -> delete -> rename.

Examples

Example set 1 - BASIC

//arrObj1 used for all examples in set 1
const arrObj1 = [
  { id: 1, enabled: true, name: "Anakin Skywalker" },
  { id: 2, enabled: true, name: "Ahsoka Tano" },
  { id: 3, enabled: false, name: "Darth Maul" }
];
//transformFields only
const options1 = {
  transformFields: {
    id: (r) => r.id + 1,
    name: (r) => r.name + " The Chosen One",
    enabled: (r) => (r.enabled = !r.enabled)
  }
};

transform(arrObj1, options1);
//output
[
  { id: 2, enabled: false, name: "Anakin Skywalker The Chosen One" },
  { id: 3, enabled: false, name: "Ahsoka Tano The Chosen One" },
  { id: 4, enabled: true, name: "Darth Maul The Chosen One" }
];
//addFields only
const options2 = {
  addFields: {
    alignment: (r) => (r.id < 3 ? "good" : "evil")
  }
};

transform(arrObj1, options2);
//output
[
  { id: 1, enabled: true, name: "Anakin Skywalker", alignment: "good" },
  { id: 2, enabled: true, name: "Ahsoka Tano", alignment: "good" },
  { id: 3, enabled: false, name: "Darth Maul", alignment: "evil" }
];
//deleteFields only
const options3 = {
  deleteFields: ["enabled", "id"]
};

transform(arrObj1, options3);
//output
[{ name: "Anakin Skywalker" }, { name: "Ahsoka Tano" }, { name: "Darth Maul" }];
//renameFields only
const options4 = {
  renameFields: {
    id: "newId",
    enabled: "newEnabled"
  }
};

transform(arrObj1, options4);
//output
[
  { newId: 1, newEnabled: true, name: "Anakin Skywalker" },
  { newId: 2, newEnabled: true, name: "Ahsoka Tano" },
  { newId: 3, newEnabled: false, name: "Darth Maul" }
];

Example set 2 - INTERMEDIATE

//arrObj2 used for all examples in set 2
const arrObj2 = [
  {
    id: 0,
    series: "bleach",
    status: "enabled",
    name: { firstName: "ichigo", lastName: "kurosaki" },
    team: "soul society",
    hasWife: true
  },
  {
    id: 1,
    series: "naruto",
    status: "enabled",
    name: { firstName: "naruto", lastName: "uzumaki" },
    team: "leaf village",
    hasWife: true
  },
  {
    id: 2,
    series: "fairy tail",
    status: "disabled",
    name: { firstName: "natsu", lastName: "dragneel" },
    team: "fairy tail guild",
    hasWife: false
  }
];
options5 = {
  transformFields: {
    id: (r) => ("000" + r.id).slice(-3)
  },
  addFields: {
    firstName: (r) => r.name.firstName,
    lastName: (r) => r.name.lastName,
    enabled: (r) => (r.status === "enabled" ? true : false)
  },
  deleteFields: ["name", "status"],
  renameFields: {
    hasWife: "hasPartner"
  }
};

transform(arrObj2, options5);
//output
[
  {
    id: "000",
    series: "bleach",
    enabled: true,
    firstName: "ichigo",
    lastName: "kurosaki",
    team: "soul society",
    hasPartner: true
  },
  {
    id: "001",
    series: "naruto",
    enabled: true,
    firstName: "naruto",
    lastName: "uzumaki",
    team: "leaf village",
    hasPartner: true
  },
  {
    id: "002",
    series: "fairy tail",
    enabled: false,
    firstName: "natsu",
    lastName: "dragneel",
    team: "fairy tail guild",
    hasPartner: false
  }
];