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

smolder

v0.3.1

Published

A library wrapper that attempts to reshape data going into your functions.

Downloads

3

Readme

Smolder

Build Status Coverage Status

Smolder (it's-a-mold-er - sorry) is a JavaScript library wrapper which aims to reshape the input to functions to match a provided schema. It's kind of like casting on speed.

The actual reshaping is done using Reshaper.

Installation

npm install smolder

Usage

Smolder(library, [schema])

  • library: The JavaScript library to be wrapped. This should be imported - you can do Smolder(require('your library')), for example.
  • [schema]: The schema for the library's functions. This can also be included within the library itself, as shown in the examples.

Examples

If we have a library, arrays.js, which looks like this:

module.exports = {
    sumArray: function (array) {
        return arr.reduce(function(a, b) {
            return a + b;
        });
    }
};

And we have some data on people:

var peopleData = [
    {
        name: 'Joel',
        info: {
            age: 22,
            height: 1.9,
            middleName: 'Robert',
            lastName: 'Auterson'
        }
    },
    {
        name: 'Jake',
        info: {
            age: 24,
            height: 1.85,
            middleName: 'Wild',
            lastName: 'Hall'
        }
    }
];

We can use Smolder to apply the sumArray function to the data above, and still get the result we want.

// First let's create a schema for the arrays library.
var schema = {
    sumArray: {
        array: ['Number'] // Parameter 'array' must be an array of numbers.
    }
}

var Smolder = require('smolder');
// Smolder will wrap the arrays library for us
var arrays = Smolder(require('arrays'), schema);

arrays.sumArray(peopleData);
// => 46

The reshaping process will by default use the first match it finds. In the above case, it creates an array using the age values.

If we want to use something other than the default, we can provide Smolder with 'hints', like so:

var hints = {
    array: 'height' // We want to sum the heights, not the ages.
};

arrays.sumArray(peopleData, hints);
// => 3.75

It's also possible to just provide Smolder with a string or array of strings as the hint. In this case, it'll use this hint for all parameters.

arrays.sumArray(peopleData, 'height');
// => 3.75

If we're using a library we created, we can build it with support for Smolder out-of-the-box. We just need to add the schema to the library's exports, like so:

module.exports = {

    __SMOLDER_SCHEMA: {
        sumArray: {
            array: ['Number']
        }
    },

    sumArray: function (array) {
        var result = 0;
        for (var i = 0; i < array.length; i++) {
            result += array[i];
        }
        return result;
    }
};

We can then require through Smolder without specifying a schema:

var arrays = Smolder(require('arrays'));

For any function or parameter where a schema is not specified, it won't be changed by the Smolder wrapper.