Giter Site home page Giter Site logo

duckface's Introduction

#duckface NPM version Code Climate Build Status devDependency Status Coverage Status

Duck typing interfaces

duckface gives you the power of interfaces from languages like java. But since JavaScript uses duck typing we won't call it an interface but a duckface.

var duckface = require('duckface');

/**
 * This is a function that requires
 * an object as a parameter. This object
 * must have a `set` and a `get` function.
 */
function myApiFunction(objectWithGetAndSet) {
  duckface.ensureHasFunctions(objectWithGetAndSet, ["get","set"],
    duckface.IS_MISSING_THE_FUNCTIONS(
      "The parameter for the function myApiFunction"
    )); // if we don't get all the required functions: throw an error
}

duckface

There are two important functions in duckface: hasFunctions and ensureHasFunctions.

duckface.hasFunctions(object, functions)

object is the object to be checked. functions is an array of names of functions that must be 'implemented' by the object to be valid.

The function returns true if object contains all functions defined in the functions array. Otherwise it returns false.

duckface.ensureHasFunctions(object, functions, errorHandler)

This function works the same as hasFunctions except that it will throw an Error instead of returning true or false. object and functions are the same as in hasFunctions. The additional errorHandler is a function that gets executed if object does not have all the functions in functions. The return value of errorHandler is used for the text of the thrown Error.

errorHandler has one parameter. This parameter is a plain object that looks like this:

{
  // the functions array passed to ensureHasFunctions
  functions: ["function1","function2","function3"], 
  // all functions missing in the given object
  missingFunctions: ["function2"]
};

Release History

  • 2014-09-12   v0.1.0   Remove lodash dependency
  • 2014-09-05   v0.0.1   Initial version.

duckface's People

Contributors

tillarnold avatar

Stargazers

 avatar

Watchers

 avatar  avatar  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.