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

declepticon

v1.3.0

Published

declarative data transformations

Downloads

8

Readme

declepticon

package version build status

declarative validation and transformation of inconsistently structured data

(the name is a truly terrible pun, because I had to come up with something)

Usage

$ npm install declepticon
let { transformation, optional, eager, validators } = require("declepticon");

let { nonBlankString, integer } = validators;
let transform = transformation({
    name: "Item",
    fields: {
        id: integer,
        type: value => value === "item" || value === "entity",
        name: nonBlankString
    },
    slots: {
        id: eager,
        type: true,
        designation: eager("name")
    },
    stringify: ({ id }) => `#${id}`
});

let data = require("/path/to/data.json");
let record = transform(data);

This will turn the incoming JSON data into a record <Item #123> with the properties described by slots, reporting discrepancies with regard to the expectations expressed within fields.

Note that transformed records can be turned back into JSON via JSON.stringify(record), provided slots' values can be serialized.

See tests for more elaborate examples.

API

Anything described here can be imported directly from the declepticon package (e.g. let { transformation, eager } = require("declepticon")).

  • transformation(descriptor) is the primary entry point, returning a function transform(data, context) to validate and transform data objects (optionally supplying an arbitrary context object for slot transformations) as described by descriptor properties (see below).

  • struct is used to describe nested structures within descriptor fields.

  • optional is a function used to indicate non-essential fields within descriptors. It accepts one more validators.

  • eager is used to indicate that a slot should be populated before validation, for identification within error reporting (i.e. slots used within stringify). eager can be used by iteself or as a function with the name of the respective original field.

  • skipSlot, if returned from a slot transformation, indicates that the respective slot should be omitted from an individual record.

  • validators is a set of common type validators provided for convenience; see validators.js for details.

  • repr(value, jsonify) is a utility function to return a value's technical representation (i.e. wrapping it in backticks), optionally converting it to JSON.

Descriptors

Any descriptor object has the following properties:

  • name is a string identifier used for error reporting:

    name: "Item"
  • stringify is an optional function which is passed the respective record and returns a string for use in error reporting:

    stringify: ({ id, name }) => `${repr(id)} "${name}"`

    This will automatically be combined with name to result in <Item `123` "foo">.

  • onError is an optional function which is invoked with the respective message if a validation error occurs.

    onError(message) {
        throw new Error(message);
    }
  • fields is an object describing the expected shape of data, both with regard to its properties and their respective values:

    fields: {
        id: value => Number.isInteger(value),
        type: "item",
        label: optional(value => value === true || value === false),
        details: struct("ItemDetails", {
            url: value => true
        })
    }

    The right-hand side consists of a validator or an array thereof (OR conjunction). A validator is typically a function which is passed the original value and returns false if that value is considered invalid. As a shortcut, the expected value may be used as a validator instead (strict equality comparison).

    Non-essential fields are flagged by passing the respective validator(s) to the optional function (see above).

    Nested structures are described by passing a name and fields object to the struct function (see above).

  • slots is an object describing the shape of the transformed record:

    slots: {
        id: eager,
        name: eager("name"),
        caption: "label",
        url: ({ details }, context) => details ? details.url + context : skipSlot
    }

    The right-hand side consists of a transformer. This is typically a function which is passed the original data object and the context object passed to the transform function (if any). It returns either the desired value (e.g. converting the original value into a number) or skipSlot to conditionally omit that slot.

    As a shortcut, true can be used to copy over the corresponding original value. If a string is used, the respective field's value is copied over instead.

Contributing

  • ensure Node is installed
  • npm install downloads dependencies
  • npm test checks code for stylistic consistency

Alternatives