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

simple-data-processor

v1.0.9

Published

Tool for processing data from one structure to another, for example for Object Relational Mapping (ORM)

Downloads

53

Readme

Simple Data Processor

Find on NPM

Tool for converting data between two formats: mine and theirs. Define the field names on each side and how the value for that field will be calculated:

  1. a string means just get the value of the field named with that string, or
  2. a function means run the function, passing the whole object, with the returned result being the value.

Supports preprocessing and postprocessing the data to allow you to do common calculations first rather than run them multiple times and create aggregate fields based on your newly calculated values.

Installation

npm install simple-data-processor

Usage

Create a new instance of the SimpleDataProcessor class and pass it a configuration object of the form:

{
  mine: {
    fields: {
      // key, value dictionary of all the field names to exist within "mine" objects and what they correspond to on "their" objects
      // this can be either a string for a field name, or a function
    },
  },
  theirs: {
    fields: {
      // same thing as above, but reversed
     },
  }
}

The instance will then expose 2 conversion functions convertToMine() and convertToTheirs().

Processing

You can also define preProcess and postProcess functions, like

{
  mine: {
    // optional processing done before fields are individually processd
    preProcess: (theirObject) => augmentedTheirObject,
    fields: { },
    postProcess (myObject) => enhancedMyObject,
  }
}

The functions in fields will receive their values from the output of the preProcess function. The postProcess function will recieve its data from the output of the field mappings. Note, for a field to be available within postProcess, it must be defined within fields. (You can always remove it from the returned object if you don't want it in the final object).

Example

import SimpleDataProcessor from 'simple-data-processor'

// Setup with configuration
const sdp = new SimpleDataProcessor({
  mine: {
    fields: {
      // this is every field that will exist on "mine" objects

      // create the value of fullName by using firstName and lastName from the original (theirs) object 
      fullName: ({ firstName, lastName }) => [firstName, lastName].join(' '),

      // create the value of age by subtracting dateOfBirth from now
      age: ({dateOfBirth}) => (Number(new Date()) - Number(new Date(dateOfBirth))) / (365 * 24 * 60 * 60 * 1000),

      // dateOfBirth is just mapped straight to their dataOfBirth field
      dateOfBirth: 'dateOfBirth',

      //favouriteFood is spelt with an underscore on theirs
      favoriteFood: 'favorite_food'
    }
  },
  theirs: {
    // set up a preprocessor to work out the firstName and lastName so we don't run the array split twice

    preProcess: ({ fullName, ...fields } => {
      const [firstName, lastName] = fullName.split(' ');

      return {
        firstName,
        lastName,
        ...fields
      }
    }),
    fields {
      // refers to the firstName field on the **preprocessed data**, not the original data, which doesn't have that field
      firstName: 'firstName',
      // refers to the lastName field on the **preprocessed data**, not the original data, which doesn't have that field
      lastName: 'lastName',

      //these field names are the same
      dateOfBirth: 'dateOfBirth',

      //handle spelling change
      favorite_food: 'favoriteFood',
    },
  },
});

// ...

// Now use!

// Theirs -> Mine
const theirData = await fetch('/get/from/some/api');
// use your sdp to convert to your local data structure
const myData = sdp.convertToMine(theirData);

// Mine -> Theirs
// use sdp to convert to a suitable format for saving
const theirData = sdp.convertToMine(myData);
const result = await post('/save/my/changes', theirData);