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

starscream

v0.1.14

Published

JSON transformer with asynchronous converters

Downloads

45

Readme

Deprecated in favour of AC/DC

starscream

Asynchronous JSON transformation

starscream

Installation

npm install starscream --save

Usage

var starscream = require('starscream')
var original = {
  source: {
     path: "robots in disguise"
  }
}
var options = {
  mapping: [{
    reader: {
      type: "jsonPointer",
      path: "/source/path"
    },
    transformer: {
      type: "uppercase"
    },
    writer: {
      type: "jsonPointer",
      path: "destination/path"
    }
  }]
}
starscream(options, original, function(err, transformed) {
   assert.equal(transformed.destination.path, 'ROBOTS IN DISGUISE')
})

Mapping Shorthand (Array Based)

var options = {
  mapping: [
    "/source/path/a",
    "/source/path/b",
  ]
}

Reads the value at /source/path/a in the original document, and writes it to /source/path/a in the transformed document

var options = {
  mapping: [{
    "/source/path/a": "/destination/path/a"
  }]
}

Reads the value at /source/path/a in the original document, and writes it to /destination/path/a in the transformed document

var options = {
  mapping: [{
    reader: "/source/path/a",
    transformer: "uppercase",
    writer: "/destination/path/a"
  }, {
    reader: "/source/path/b",
    transformer: "uppercase",
    writer: "/destination/path/b"
  }
}]

Reads the value at /source/path/a in the original document, transforms it to uppercase, and writes it to /destination/path/a in the transformed document

Mapping Shorthand (Object Based)

var options = {
  mapping: {
    "/source/path/a": "/destination/path/a",
    "/source/path/b": "/destination/path/b"
  }
}

Reads the value at /source/path/a in the original document, and writes it to /destination/path/a in the transformed document

var options = {
  mapping: {
    "/source/path/a": {
      transformer: "uppercase"
    },
    "/source/path/b": {
      transformer: "lowercase"
    }
  }
}

Reads the value at /source/path/a in the original document, transforms it to uppercase, and writes it to /source/path/a in the transformed document

var options = {
  mapping: {
    "/source/path/a": {
      transformer: "uppercase",
      writer: "/destination/path/a"
    },
    "/source/path/b": {
      transformer: "lowercase",
      writer: "/destination/path/b"
    },
  }
}

Reads the value at /source/path/a in the original document, transforms it to uppercase, and writes it to /destination/path/b in the transformed document

Using multiple sources for a single mapping

var options = {
  mapping: [{
    reader: {
      type: "serial",
      reader": [{
        type: "jsonPointer",
        path: "/source/path/a"
      }, {
        type: "jsonPointer",
        path: "/source/path/b"
      }],
    },
    writer": {
      type: "jsonPointer",
      path: "/destination/path"
    }
  }]
}

Reads the values at /source/path/a and /source/path/b in the original document, and inserts them as an array into the transformed document at '/desination/path'

Shorthand

var options = {
  mapping: [{
    reade": {
      type: "serial",
      reader: [
        "/source/path/a",
        "/source/path/b"
      }]
    },
    writer: {
      type: "jsonPointer",
      path: "/destination/path"
    }
  }]
}

The same shorthand rules apply

Aggregating Transformers

var options = {
  mapping: [{
    reader: {
      type: "serial",
      readers: [{
        type: "jsonPointer",
        path: "/source/path/a"
      }, {
        type: "jsonPointer",
        path: "/source/path/b"
      }],
    },
    transformer: {
      type: "concatenate",
      separator: "_"
    },
    writer: {
      type: "jsonPointer",
      path: "/destination/path"
    }
  }]
}

Reads the values at /source/path/a and /source/path/b from the original document, combines them with a underscore (_) and writes them to /destination/path in the transformed document

Chaining Transformers

var options = {
  mapping: [{
    reader: "/source/path"
    transformer: {
      type: "serial",
      transformers: [{
        type: "uppercase"
      }, {
        type: "prefix",
        text: "foo-"
      }]
    },
    writer": {
      type: "jsonPointer",
      path: "/destination/path"
    }
  }]
}

Reads the values at /source/pathin the original document, transforms it to uppercase, adds the foo- prefix and writes it to /destination/path in the transformed document

Iteration

You can iterate over arrays using the mapSeries transformer

    [
        {
            "reader": "/source/path",
            "transformer": {
                "type": "mapSeries",
                "transformer": "uppercase"
            },
            "writer": "/destination/path"
        }
    ]

Reads the array at /source/path and transforms each element to upper case before writing the array to /destination/path

Out of the box readers

jsonPointer (default)

var options = {
  mapping: [{
    reader: {
      "path": "/source/path"
      "ignoreMissing": "true"
    }
    writer: "/destination/path"
  }]
}

Reads the value at /source/path. If the path is missing and ignoreMissing is false (the defult) return undefined instead of erroring

property

var options = {
  mapping: [{
    reader: {
      "type": "property",
      "path": "source.path",
      "ignoreMissing": "true"
    }
    writer: "/destination/path"
  }]
}

Reads the value at source.path. If the path is missing and ignoreMissing is false (the defult) return undefined instead of erroring

Writers of the box readers

jsonPointer

var options = {
  mapping: [{
    reader: {
      "/source/path"
    writer: {
      "path": "/destination/path",
      "ignoreMissing": "true"
    }
  }]
}

Writes the value to /destination/path. If the value is undefined and ignoreMissing is true (the default) will not write anything, otherwise writes the value as undefined

property

var options = {
  mapping: [{
    reader: "/source/path"
    writer: {
      "type": "property",
      "path": "destination.path",
      "ignoreMissing": "true"
    }
  }]
}

Writes the value to /destination/path. If the value is undefined and ignoreMissing is true (the default) will not write anything, otherwise writes the value as undefined

Out of the box transformers

mapSeries

var options = {
  mapping: [{
    reader: "/source/path"
    transformer: {
      "type": "mapSeries",
      "transformer": "uppercase"
    }
    "toggle",
    writer: "/destination/path"
  }]
}

toggle

var options = {
  mapping: [{
    reader: "/source/path"
    transformer: "toggle",
    writer: "/destination/path"
  }]
}

uppercase

var options = {
  mapping: [{
    reader: "/source/path"
    transformer: "uppercase",
    writer: "/destination/path"
  }]
}

lowercase

var options = {
  mapping: [{
    reader: "/source/path"
    transformer: "lowercase",
    writer: "/destination/path"
  }]
}

mutualExclusion

var options = {
  mapping: [{
    readers: [
      "/source/path/a",
      "/source/path/b"
    ],
    transformer: "mutualExclusion",
    writer: "/destination/path"
  }]
}

Writes either /source/path/a or /source/path/b (with preference for a)

conditional

var options = {
  mapping: [{
    readers: [
      "/source/path/a",
      "/source/path/b"
    ],
    transformer: "mutualExclusion",
    writer: "/destination/path"
  }]
}

Writes /source/path/b if /source/path/a is truthy

guard

var options = {
  mapping: [{
    readers: [
      "/source/path/a",
      "/source/path/b"
    ],
    transformer: "mutualExclusion",
    writer: "/destination/path"
  }]
}

Writes /source/path/b if /source/path/a is falsey

Custom Transformers

var options = {
  mapping: [{
    reader: "/source/path"
    transformer: {
      type: "dbLookup",
      collection: "refdata"
    },
    writer: {
      type: "jsonPointer",
      path: "/destination/path"
    }
  }],
  transformers: {
    dbLookup: function(config, value, cb) {
      db.collections(config.collection).findOne({ code: value }, cb)
    })
  }
}

Reads the value at /source/path in the original document, and cross references it an item of refdata. The same mechanism can be used for writing your own readers and writers too.