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

@dmitra/transform-data

v0.1.3

Published

Utility to transform data based on instructions.

Downloads

4

Readme

Purpose

Utility to transform data based on instructions.

Description

Input data may be of any type as well as output data.
Transformation is performed on data structure and via provided functions on the values too.

Internally it is a single function called recursively on the same source and rules params. They are accompanied by optionally provided or set at root call target object. And the only variable showing the current depth of recursion is absolute path.
Current values of source and rules are retrieved by this absolute path and are written to the target.

convert(source, rules, target = {}, path = '/') {
    ...
    return target['/']
}

Usage

There is a single function which can transform input value to the output by rules provided. In its simplest case with no rules provided this function will return the input value as is:

convert(1) ==> 1

Rules of Transformation

String - path

path starting with '/' is absolute
relative path is prepended by '../' by default

convert(1, 'a') ==> { a: 1 }

equivalent to

convert(1, '../a') 

as there is no parent beyond root object - parent prefix is replaced by current path: './a'

Func

convert(1, v => ++v) ==> 2

Array - [vFunc | [vFuncs] | new value, pathFunc | [pathFuncs] new path]

vFunc - value transformation func

path is implied to be current if not provided

convert(1, [v => ++v]) ==> 2
// equivalent to
convert(1, [v => ++v, '.'])

path - relative path to write value to target

convert(1, [v => ++v, 'a']) ==> {a: 2}

path starting with '/' is treated as absolute

convert(1, [v => ++v, '/.a']) ==> {a: 2}

pathFunc - path transformation func

convert({ a: 1 },  [v => v, path => '_' + path] ) ==> { _a: 1 }

Objects

Rules as an object should match the structure of the source object. They should have identical hierarchy where only leaves are rules instead of values.

Paths in rules missing in source object will be ignored. Missing paths in rules means, that source values will be copied to the target as is. There might be a conflict between new paths and "copied by default". The is no guaranteed order of keys iteration for objects, so define unique rules to avoid overwriting.

convert({ a: 1}, { a: 'b'}) ==> { b: 1}

is equivalent to:

convert({ a: 1 }, { a: '../b' }) ==> { b: 1 }

which reads as:
Copy "values in a root source object with a key "a" to the parent object
(relative to the value path) with a key "b".

Nested objects

Augment rules object with special key '.' to specify the rule for the parent and child simultaneously:

convert(
  {
    a: {
      b: 2,
    },
  }, {
    a: {
      '.': '_a',
      b: '_b',
    },
  },
)

Array source

Operate on each array item
convert([1, 2], { '*': v => ++v }) ==> [2, 3]

Special cases

Omit value

path is defined as "put the value nowhere"

convert(1, '') ==> undefined'

Glob pattern

convert({ a: 1, b: 2 }, { '*': v => ++v }) ==> { a: 2, b: 3 }

Examples

For more use cases see tests.

Competition

There are multiple projects serving the same goal with different approaches:

node-object-mapper

This library is rules-centric.
Rules end up being verbose and redundant if attributes are just copied:

{ 'root.level1.level2': [['root.level1.CHANGED', v => ++v]]
{ 'root.level1.unchanged': [['root.level1.unchanged', v => ++v]]

node-json-transform

This library is target-centric defining rules as template of target object:
with many special keys:

item: {
  propNameInTarget: 'propNameInSource',  
},
remove: ["keyToRemove"],
defaults: {
  "missingData": true
},
operate: [{
  run: value => 'result of transformation function',
  on: 'path.to.prop'
}]

This makes sense when you are parsing incoming data to make specific format you want to use. But it depends on the point of view: you might want to focus on the data you have.

Jsonata

This is a full-blown turing complete query language. So instead of using javascript you have to learn custom DSL.