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-transpose

v3.3.3

Published

JSON Transpose ====================================

Downloads

1,908

Readme

JSON Transpose

About

Transposes an object to another object using templated fields. Could not find a library that could safely reference fields from the source object that may not exist and compile the mapping functions for later use.

Build

The library is written using typescript, make sure to have to built the repository when testing as tests run off the final .js files

Usage

const { compile } = require('json-transpose');

const transform = compile({
  someValue: '${it.some.nested.value}'
});

const result = transform({
  some: {
    nested: {
      value: 'test'
    }
  }
});

console.log(result);

Output:
{
  someValue: 'test'
}

Interpolation

{
  ...
  [target field]: '${it.[path to source field]}',
  ...
}

Effectively you can do whatever JavaScript string templates can do.

{
  ...
  [target field 1]: 'This is a template ${it.[path to source field]}',
  [target field 2]: 'Some arithmatic ${it.[path to source field] + it.[path to source field]}',
  ...
}

Arrays

If the source will be an array and you don't want to transpose any of the fields just reference the array:

{
  ...
  target array: '${it.[path to source array]}'
  ...
}

To transpose the items within the array use an array for the definition which will take two items:

  1. The source
  2. The mappings
{
  ...,
  [target array]: {
    $array: {
      source: 'it.[path to source array]',
      item: {
        someValue: '${it.some.nested.value}'
      }
    }
  }
  ...
}

Coercion

If the expression is the only thing in the template, the type it evaluates to will be the value assigned to the field.

Input
{
  ...,
  string: 'Value: ${it.value}',
  integer: '${it.value + 10}',
  boolean: '${it.value > 10 && it.value < 20}'
  ...
}

Output:
{
  ...,
  string: 'Value: 5',
  integer: 15,
  boolean: false
  ...
}

Special Objects

Certain objects have been supplied as helpers for accessing data that would otherwise be inaccessible.

_root

The root object that was passed in. This is useful when needing to reference the root document within arrays.

{
  ...,
  [target array]: {
    $array: {
      source: 'it.[path to source array]',
      item: {
        rootValue: '${_root.value}',
        someValue: '${it.some.nested.value}'
      }
    }
  }
  ...
}

Custom Objects

Custom objects can be projected into expressions. json-transpose will attempt to filter out any non-field objects / operations. However, specific objects can be allowed for use.

const transform = compile({
  trucatedNumber: '${Math.trunc(it.number)}',
  formattedNumber: '${formatNumber(it.number)}'
}, {
  customObjects: {
    Math,
    formatNumber(number) {
      return `$${number.toFixed(2)}`;
    }
  }
});

transform({
  number: 15.2345
});

Output:
{
  truncatedNumber: 15,
  formattedNumber: '$15.23'
}

String only templates

It is okay to just have a string value for a template rather than a whole object.

const transform = compile('${it.some.nested.value}');

const result = transform({
  some: {
    nested: {
      value: 'test'
    }
  }
});

Output:
'test'

Missing Data Fields

If the value does not exist given the path for the field. The value will be set to null

const transform = compile({
  someValue: '${it.some.nested.value}'
});

const result = transform({
  some: { }
});

Output:
{
  someValue: null
}

Tests

npm run test