GithubHelp home page GithubHelp logo

rlugojr / smolder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joelotter/smolder

0.0 1.0 0.0 13 KB

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

License: MIT License

JavaScript 100.00%

smolder's Introduction

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.

smolder's People

Contributors

joelotter avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.