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

transmog

v2.2.1

Published

Simple rule-based object transformer

Downloads

18

Readme

transmog

Greenkeeper badge

npm Build Status codecov dependencies Status devDependencies Status

transmog is a utility for converting JavaScript objects of a certain shape to JavaScript objects of a different shape. This transformation is based on standardized rules to make it easy to express and comprehend the transformation.

Features:

  • reading/writing from/to nested properties
  • whitelisting of properties
  • conversion of property values
  • default values for missing property values

API

transmog()

transmog(
  rules: object,
  object: object
): object

Transforms object according to rules.

transmog will apply the rules to object without mutating it and will always return a newly created object. Note however that the property values of the resulting object may reference the same objects as the source object does. There's no cloning of objects involved unless you specify individual rules which do so.

rules

The rules object consists of key-value pairs which specify how every property of the resulting object is determined.

rules = {
    "path.to.property": <boolean | function | string | object>,
    ...
}

The key of a rule specifies the property name or property path in the resulting object. A property path is a . delimited string of valid property names and can be used to create nested objects.

The value of a rule is a description for how to calculate the property value based on the source object. Different common use cases can be expressed based on the type of the rule.

boolean rule

true | false

If true the value of the source object under the same path is written to the resulting object. If false the rule is ignored. (This is just for consistency. Instead of specifying false the rule can be omitted as well.)

function rule
function (
    sourceValue: any,
    sourceObject: object
): any

The specified function is called with

  • the sourceValue which is the property value of the source object under the same path as the rule describes
  • the source object itself.

It should return the value which will be written to the resulting object under the path which the rule describes.

string rule

"source.property.path"

The specified string is interpreted as a property path of the source object whose value will be written to the resulting object.

object rule
{
    converter?: (sourceValue: any, sourceObject: object) => any,
    sourcePath?: string,
    defaultTo?: (sourceObject: object) => any
}

An object rule is the generalization of the boolean, function and string rule.

The converter function will be called with the sourceValue and sourceObject and should return the value which will be written to the resulting object. If omitted the identity function will be used as a default.

The sourcePath specifies the property path under which the sourceValue for the converter function will be read. If omitted the destination path of the rule will be used as a default.

The defaultTo function will be called if the sourceValue is null, undefined or the source object has no property under the specified sourcePath. It will be called with the sourceObject as an argument and should return the default value which will be written to the resulting object. If defaultTo is omitted no value will be written to the resulting object if sourceValue is null, undefined or the source object has no property under the specified sourcePath.

Example

TODO