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-object-transformer

v1.0.5

Published

Transform flat JS objects according to Rules specified

Downloads

6

Readme

Build Status npm version Coverage Status

NPM

Simple Object Transformer

Declare Rules to transform flat JS object into another flat JS object. Useful for simple transformations between internal state objects and server requests/responses.

How to use

Rules is an object with property names equals to the target property name, and the value is one of:

  1. String - then it is considered the property name of the source object, and should be copied into the target as is to specified target property (same as rule's name).
  2. Function - then this function will be applied to the source object and optional property name, and the result will be assigned to the target's property (same as rule's name).
  3. TODO: null|undefined - just copy the value from the same source's property.

The lib exports converterFactory by default, which allows partial application. I.e. it takes Rules, and returns a Converter, that expects only Source object.

In addition, ordinary converter could be imported by name. It expects both Rules and Source objects as arguments.

Usage with flowtypes

Import module functions from src directory of the package, instead of a default (which is compiled to ES5 version inside dist directory)

Examples

// const transformerFactory = require('simple-object-transformer').transformerFactory;
import { transformerFactory } from 'simple-object-transformer';

const RULES = Object.freeze({
  id: 'productId',
  name: 'firstName',
  fullName: (context) => `${context.firstName} ${context.lastName}`,
});

const SOURCE = Object.freeze({
  productId: 123,
  firstName: 'John',
  lastName: 'Doe',
  trash: 555,
});

// create a transformer based on the rules, specified as argument
const transformer = transformerFactory(RULES);
const target = transformer(SOURCE);

console.log(target); // { id: 123, name: 'John', fullName: 'John Doe' }

For more examples see tests.

Special notes

To use it as a library you should add "stage-0" preset to your babel configuration, because of object destructuring being used in the code. Also you need babel's transform-flow-strip-types plugin as the code uses flowtype.

TODO

  • implement 'undefined' rule.

Author

Eldar A. [email protected]