JSPM

morph.js

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q16297F
  • License GPL-3.0

Create JavaScript polymorphic functions using array-like syntax

Package Exports

  • morph.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (morph.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Morph.js

Create JavaScript polymorphic functions using array-like syntax.

Example:

//defining some simple validations

function isString(x) {
    return String(x) === x;
}

function isNumber(x) {
    return Number(x) === x;
}

//out morphic function

var f = Morph([
    [isString],
    function(myString) {
        console.log(myString, 'is a string.');
    }
], [
    [isNumber],
    function(myNumber) {
        console.log(myNumber, 'is a number');
    }
]);

f(3); //3 is a number
f("test"); //"test" is a string

Another example using multiple validations and parameters

function isArray(x) {
    return Array.isArray(x);
}

function notEmpty(x) {
    return x.length !== 0;
}

var f = Morph([
    [isArray, notEmpty],
    function(myNotEmptyArray) {
        console.log(myNotEmptyArray, 'is not an empty array');
    }
], [
    [isNumber],
    [isString],
    function(myNumber, myString) {
        console.log(myNumber, 'is a number');
        console.log(myString, 'is a string');
    }
]);

f(['x', 'y']);  //["x", "y"] is not an empty array
f(5, 'test');   //5 is a number
                //"test" is a string