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

json-xformer

v1.0.4

Published

A node module for transforming json to json objects

Downloads

2

Readme

json-xformer

This is a node.js utility module implementing the following transformations:

  1. json to json
  2. json to properties
  3. properties to json

Installation

npm install --save json-xformer

Usage

The json-xformer API comprises the following methods:

registerCustomTransformer(customFormatterName, customFormatter)

This method is used to register a user-defined custom formatting function (customFormatter), which will be referenced in the transformation descriptors by the specified name (customFormatterName). The custom formatter must have the following signature:

  function customFormatterExample(sourceLocale, targetLocale, sourcePtr, targetPtr, sourceElement) {...}

The custom formatter will be invoked by the json-xformer as dictated by the transformation descriptors on the source element, and the value returned by the custom formatter will be assigned to the target object.

Map Json To Json

mapToJson(sourceObject, targetObject, xformMap)

This method is used to map one json object into another by applying the xformMap onto the sourceObject, and filling the results of the transformation into the targetObject

Map Json To Properties

mapToProperties(sourceObject, targetPropertiesArray, xformMap)

This method is used to map one json object into an array of property strings (key = value) by applying the xformMap onto the sourceObject, and filling the results of the transformation into the targetPropertiesArray

Map Properties To Json

mapFromProperties(sourcePropertiesArray, targetObject, xformMap)

This method is used to map an array of properties in the argument sourcePropertiesArray into a json object specified by the argument targetObject

Tranformation Schema

The json-xformer module is driven by a transformation map, which is a json object in itself. The transformation map is governed by the following json schema:

{
    "id": "http://www.gaaiat.com/json-xformer#",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "JSON Transformer Schema",
    "title": "JSON Transformer Schema",
    "type": "object",
    "required": ["transformMap"],
    "properties": {
        "sourceLocale": {
            "type": "string",
            "default": "en-US"
        },
        "targetLocale": {
            "type": "string",
            "default": "en-US"
        },
        "transformMap": {
            "type": "array",
            "minItems": 1,
            "items": {
                "type": "object",
                "properties": {
                    "sourcePtr": {
                        "type": "string"
                    },
                    "targetPtr": {
                        "type": "string"
                    },
                    "customFormatter": {
                        "type": "string"
                    },
                    "defaultValue": {
                        "oneOf": [{
                            "type": ["array", "object", "boolean", "integer", "number", "string"]
                        }]
                    }
                },
                "requiredProperties": ["sourcePtr", "targetPtr"]
            }
        }
    }
}

The transformation schema includes the following elements:

  1. The source locale: An optional locale used with the source object, default value is "en-US"
  2. The target locale: An optional locale used with the target object, default value is "en-US"
  3. An array of transformation descriptors (required, minimum 1 element in the array): Each descriptor has the following elements:
  • sourcePtr (required): A json pointer string identifying the source element in the source object
  • targetPtr (required): A json pointer string identifying the target element in the target object
  • customFomatter (optional): The name of a function that will be used to customize the formatting of the source element prior to being set into the target objet
  • defaultValue (optional): The default value to use for the source element if it is missing in the source object. If the source element is missing in the source object and there is no default value specified in the transformation descriptor, the transformation will throw an exception

Example

Here is an example transformation map:

{
    "sourceLocale": "en-US",
    "targetLocale": "en-US",

    "transformMap": [{
        "sourcePtr": "/name",
        "targetPtr": "/target/name",
        "customFormatter": "formatName"
    }, {
        "sourcePtr": "/age",
        "targetPtr": "/target/age",
        "customFormatter": "formatAge",
        "defaultValue": "23"
    }, {
        "sourcePtr": "/jobLocations/1",
        "targetPtr": "/jobSiteOne/-"
    }]
}

The custom formatters fromatName and formatAge, used in the example map above, are defined and registered with the json-xformer as follows:

function formatName(sourceLocale, targetLocale, sourcePtr, targetPtr, sourceElement) {
            return sourceElement.toString().toUpperCase();
}
xformer.registerCustomTransformer("formatName", formatName);

function formatAge(sourceLocale, targetLocale, sourcePtr, targetPtr, sourceElement) {
    return Number(sourceElement);
}
xformer.registerCustomTransformer("formatAge", formatAge);

When the example transformation map above is used with the following source object:

{
    "name": "tester",
    "lastName": "one",
    "jobLocations": [
        "site 1",
        "site 2",
        "site 3"
    ]
}

it will produce the following results:

Json To Json Result

{
    target: {
        name: 'TESTER',
        age: 23
    },
    jobSiteOne: ['site 2']
}

Json To Properties Result

[  
    'target.name = "TESTER"',
    'target.age = 23',
    'jobSiteOne.- = "site 2"'
]

Please use the test cases in the test folder for further guidance and examples.