GithubHelp home page GithubHelp logo

isabella232 / over.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stretchr/over.js

0.0 0.0 0.0 247 KB

Elegant function overloading in JavaScript.

License: Other

JavaScript 94.53% CSS 5.47%

over.js's Introduction

over.js

Elegant function overloading in JavaScript.

Example

Just wrap your functions with the Over() method, and use special argument names to denote types:

var obj = {

  /**
   * Says something in the console.
   *
   * say(msg) - Says something once.
   * say(msg, times) - Says something many times.
   */
  say: Over(

    // one string
    function(msg$string){

      // say it in the console
      console.info(msg$string);

    },

    // a string and a number
    function(msg$string, times$number){

      // say the message times$number times
      for (var i = 0; i < times$number; i++) this.say(msg$string);

    },

    // then everything else
    function(msg$string, everything$etc) {

      // say the string
      this.say(msg$string);

      // now say all remaining arguments
      for (var i in everything$etc) {
        this.say(everything$etc[i])
      }

    }
  )

};

Argument names

[name]${test}

The argument name contains any name (optional), followed by a $ literal, followed by the type of test that will take place when checking the signature of the function.

Built in tests:

  • $string - Checks whether the argument is a string or not.
  • $number - Checks whether the argument is a number or not.
  • $object - Checks whether the argument is an object or not (not an array).
  • $array - Checks whether the argument is an array or not.
  • $bool | $boolean - Checks whether the argument is a boolean or not (i.e. true or false).
  • $function - Checks whether the argument is a function or not.
  • $nothing - Checks whether the argument is null or undefined.
  • $null - Checks whether the argument is null or not.
  • $undefined - Checks whether the argument is undefined or not.

The special $etc kind

If you use anything$etc, it will allow any kind of argument (or no argument at all), and any arguments following it. All remaining arguments will be collected into an array, which will become the value for anything$etc. So you can check for the length of additional arguments by doing anything$etc.length.

Adding your own

You can add your own tests by simply adding a function to the Over.is object. For example, to add support for checking if an argument is a positive number, you could do:

Over.is.positiveNumber = function(v){ return Over.is.number(v) && v > 0; };

over.js's People

Contributors

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