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

acdc

v2.0.0

Published

JavaScript object transformation

Downloads

95

Readme

Build Status Code Style

AC/DC

AC/DC is a library for transforming one JavaScript object into another. We wrote it because we needed to transform an horribly complicated legacy feed into something sane. It might also be useful for generating view models.

The Concepts

AC/DC works on the concept of flows of asynchronous tasks. The output from one task becomes the input for the next. The first task in a flow is implicitly set by AC/DC and will wrap the remaining tasks in a domain. The first explicit task in a flow will usually be a 'sequence', which will execute a collection of other tasks. For example...

Given a customer

{
    "details": {
        "title": "Mr",
        "firstName": "Fred",
        "lastName": "Bloggs",
        "dateOfBirth": "2015-07-15T00:00:00.000+01:00"
    }
}
domain()
└── sequence()
    ├── output(customer)           // Yields the customer object
    ├── get('details.dateOfBirth') // Yields '2015-07-15T00:00:00.000+01:00'
    └── parseDate()                // Yields a JavaScript date object representing '2015-07-15T00:00:00.000+01:00'

To operate on multiple properties of an object use the fork task

domain()
└── sequence()
    ├── output(customer)             // Yields the customer object
    ├── fork()
    │   ├── get('details.title')     // Yields 'Mr'
    │   ├── get('details.firstName') // Yields 'Fred'
    │   └── get('details.lastName')  // Yields 'Bloggs'
    └── format('%s. %s %s')          // Yields 'Mr. Fred Bloggs'

For richer transformations use combinations of sequences, forks, get and set

domain()
└── sequence()
    ├── output(customer)
    ├── fork()
    │   ├── sequence()
    │   │   ├── get('details.dateOfBirth')
    │   │   ├── parseDate()
    │   │   └── set('dob')
    │   ├── sequence()
    │   │   ├── get('details.title')
    │   │   └── set('title')
    │   ├── sequence()
    │   │   ├── get('details.firstName')
    │   │   └── set('firstName')
    │   ├── sequence()
    │   │   ├── get('details.lastName')
    │   │   └── set('lastName')
    │   └── sequence()
    │       ├── render('{{details.title}}. {{details.firstName}} {{details.lastName')
    │       └── set('fullName')
    └── merge()

Yields

{
    title: 'Mr',
    firstName: 'Fred',
    lastName: 'Bloggs',
    fullname: 'Mr. Fred Bloggs',
    dob: Date<2015-07-15T00:00:00.000+01:00>
}

All of this gets a bit verbose, and could be more tersely written as

domain()
└── sequence()
    ├── output(customer)
    ├── fork()
    │   ├── transform('details.dateOfBirth', parseDate(), 'dob')
    │   ├── copy('details.title', 'title')
    │   ├── copy('details.firstName', 'firstName')
    │   ├── copy('details.lastName', 'lastName')
    │   └── sequence()
    │       ├── render('{{details.title}}. {{details.firstName}} {{details.lastName')
    │       └── set('fullName')
    └── merge()

The Code

var acdc = require('acdc')
var tasks = require('acdc/lib/tasks')
var sequence = tasks.flow.sequence
var output = tasks.flow.output
var fork = tasks.flow.fork
var copy = tasks.property.copy
var transform = tasks.property.transform
var parseDate = tasks.date.parseDate
var merge = tasks.array.merge
var render = tasks.hogan.render

acdc()
    .run((dsl, cb) => {
        cb(sequence([
            output(customer),
            fork([
                transform('details.dateOfBirth', parseDate(), 'dob'),
                copy('details.title', 'title'),
                copy('details.firstName', 'firstName'),
                copy('details.lastName', 'lastName'),
                sequence([
                    render('{{details.title}}. {{details.firstName}} {{details.lastName}}'),
                    set('fullName')
                ])
            ]),
            merge()
        ]))
    })
    .done(done)

The DSL

If you're not opposed to using with you can reduce the number of imports as follows

var acdc = require('acdc')
var tasks = require('acdc/lib/tasks')

acdc()
    .bind(tasks.flow),
    .bind(tasks.property),
    .bind(tasks.date),
    .bind(tasks.array),
    .bind(tasks.hogan)
    .run((dsl, cb) => {
        with(dsl) {
            cb(sequence([
                output(customer),
                fork([
                    transform('details.dateOfBirth', parseDate(), 'dob'),
                    copy('details.title', 'title'),
                    copy('details.firstName', 'firstName'),
                    copy('details.lastName', 'lastName'),
                    sequence([
                        render('{{details.title}}. {{details.firstName}} {{details.lastName}}'),
                        set('fullName')
                    ])
                ]),
                merge()
            ]))
        }
    })
    .done(done)

Provided Tasks

AC/DC provides scores of tasks for transformation and flow control. The most important ones are in the flow, logic and property folders. For further information please consult the Task API

Custom Tasks

A simple task is an object with a 'fn' property of type function. We refer to this object as the task definition.

module.exports = {
    fn: function uppercase(input, ctx, cb) {
        cb(null, input ? input.toUpperCase() : input)
    }
}

You can validate the input by adding a schema property

var Joi = require('joi')
var schemas = require('acdc/lib/schemas')

module.exports = {
    fn: function uppercase(input, ctx, cb) {
        cb(null, input ? input.toUpperCase() : input)
    },
    schema: schemas.context.keys({
        input: Joi.string().allow('').optional()
    })
}

Before using your custom task in a flow you need to shorthand it. This will convert the task it into an executable function and copy function arguments to ctx.params. If your task uses parameters, you must include a schema which validates the parameters since this will also determine argument position to parameter mapping.

Flow Definition

var customTaskDef = require('../my/custom/task')
var shorthand = require('acdc/lib/utils/shorthand')
var customTask = shorthand(customTaskDef)

acdc()
    .bind(tasks.flow),
    .bind(tasks.property)
    .run((dsl, cb) => {
        with(dsl) {
            cb(sequence([
                output(customer),
                get('details.title'),
                customTask('Fred', 'Bloggs'),
                set('title')
            ]))
        }
    })
    .done(done)

Task Definition

var Joi = require('joi')
var _ = require('lodash')
var schemas = require('acdc/lib/schemas')
var R = require('ramda')

module.exports = {
    fn: function customTask(input, ctx, cb) {
        cb(null, ctx.params.firstName + ' ' + ctx.parmas.lastName)
    },
    schema: schemas.context.keys({
        params: Joi.object().keys({
            firstName: Joi.string(),
            lastName: Joi.string(),
        })
    })
}

Custom Task Libraries

If you write lots of custom tasks you may want to organise them into libraries and require them en masse. See acdc/utils/requireTasks and index.js in any of the provided tasks folders for how to do this.

Inline Tasks

You can add tasks inline as follows...

acdc()
    .bind(tasks.flow),
    .bind(tasks.property)
    .run((dsl, cb) => {
        with(dsl) {
            cb(sequence([
                output(customer),
                get('details.title'),
                {
                    task: {
                        fn: function uppercase(input, ctx, cb) {
                            cb(null, input ? input.toUpperCase() : input)
                        }
                    }
                },
                set('title')
            ]))
        }
    })
    .done(done)