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

transformobject

v0.3.1

Published

Transform your objects to desired rich ones using declarative rules.

Downloads

587

Readme

object-transform

Transform your objects to desired rich ones using declarative rules.

const transform = require('transformobject').transform;

const obj = {
  secret: 'Secret Data',
  username: 'John Doe',
  nested: {
    username: 'Nested John Doe',
  },
  'flat.nested.username': 'Flat John Doe',
  phoneNumber: '+989191331313',
  date: new Date(),
  nullField: null,
};

const transformer = {
  username: 'username',
  nestedUsername: 'nested.username',
  flatUsername: 'flat.nested.username',
  phone: {
    number: 'phoneNumber',
  },
  constantNumber: 4,
  booleanField: true,
  day: function (originObject) {
    return originObject.date.getDate();
  },
  nullField: 'nullField',
  undefinedField: 'unknownField',
  nonExistingNested: 'nested.username.verification.isVerified',  
};


const transformed = transform(obj, transformer);

// { username: 'John Doe',
//   nestedUsername: 'Nested John Doe',
//   flatUsername: 'Flat John Doe',
//   phone: { number: '+989191331313' },
//   constantNumber: 4,
//   booleanField: true,
//   day: 3,
//   nullField: null,
//   undefinedField: undefined,
//   nonExistingNested: undefined}



Options

Using strict option, transformer changes undefined fields to null fields, so they will not be omitted when stringified

strict

const transformed = transform(obj, transformer, { strict: true });

// { username: 'John Doe',
//   nestedUsername: 'Nested John Doe',
//   flatUsername: 'Flat John Doe',
//   phone: { number: '+989191331313' },
//   constantNumber: 4,
//   booleanField: true,
//   day: 3,
//   nullField: null,
//   undefinedField: null,
//   nonExistingNested: null}

source

Providing a source object will write the transformed values into the source instead of creating a new object

const transformed = transform(obj, transformer, { source: { a: 1, b: 2, house: { name: 'Brambles'} } });

// { 
//   a: 1,
//   b: 2,
//   house: { name: 'Brambles' },
//   username: 'John Doe',
//   nestedUsername: 'Nested John Doe',
//   flatUsername: 'Flat John Doe',
//   phone: { number: '+989191331313' },
//   constantNumber: 4,
//   booleanField: true,
//   day: 3,
//   nullField: null,
//   undefinedField: null,
//   nonExistingNested: null}

actions

Providing an actions object will allow the actions to be reused across transforms, this provides the ability to process fields using an array syntax to share functionality.

The array syntax works as follows:

The first field is the lookup, same as before, all additional fields are executed in order passing the result along the chain. ['username', 'undefinedIfEmptyString']

Example

const ucwords = require('ucwords'); // example npm install
const camelCase = require('lodash.camelcase'); // example npm install

const obj = {
  username: 'John Doe',
  nested: {
    username: 'Nested John Doe',
  },
};

const transformer = {
  username: ['username', 'camelCase', 'undefinedIfEmptyString'],
  nestedUsername: ['nested.username', 'ucwords', 'undefinedIfEmptyString'],
};

const transformed = transform(
  obj, 
  transformer, 
  {
    actions: {
      camelCase: value => camelCase(value),
      ucwords: value => ucwords(value),
      undefinedIfEmptyString: value => value === "" ? undefined : value,
    } 
  }
);

Actions can be combined with both strict and source. If an action is not defined but is used, a console warning will be logged.