• Stars
    star
    147
  • Rank 242,369 (Top 5 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 10 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Elegant function overloading in JavaScript.

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; };

More Repositories

1

testify

A toolkit with common assertions and mocks that plays nicely with the standard library
Go
22,028
star
2

arg.js

Lightweight URL argument and parameter parser
JavaScript
1,060
star
3

objx

Go package for dealing with maps, slices, JSON and other data.
Go
670
star
4

gomniauth

Authentication framework for Go applications.
Go
637
star
5

goweb

A lightweight RESTful web framework for Go
Go
632
star
6

gorc

Recursive go testing, done better.
Go
141
star
7

powerwalk

Package for concurrently walking files
Go
104
star
8

stew

Stew is a very high performance package that extends common Go objects providing better alternatives or wrappers.
Go
67
star
9

hoard

A fast, smart caching package for Go
Go
59
star
10

github-stars

Returns the aggregate number of stars for a user/org or single repo
JavaScript
52
star
11

signature

URL signing package for Go
Go
38
star
12

codecs

Provides interfaces, functions and codecs that can be used to encode/decode data to/from various formats.
Go
31
star
13

filetypes.js

A complete list of file types, extensions and mime types in JavaScript.
JavaScript
18
star
14

pat

Go package for patterns and best practices
Go
15
star
15

readcaster

Go package for elegantly broadcasting one io.Reader source to many io.Readers
Go
14
star
16

commander

A Go package that makes it easy to create a command line interface.
Go
12
star
17

todo

A Go package that helps you remember the DO in TODO
Go
12
star
18

ottox

Plugins for the Otto Go JavaScript parser and interpreter
Go
12
star
19

thru

The worlds simplest web server
Go
11
star
20

slog

Logging package for Go
Go
10
star
21

jsonblend

A command line tool providing extensive capabilities for combining json objects.
Go
9
star
22

piglatin

English -> Pig Latin translator package for Go - TDD tutorial code
Go
9
star
23

respond

Go package for building data APIs
Go
7
star
24

sdk-go

Go SDK for Stretchr platform
Go
7
star
25

pools

pools is a go package for managing a suite of differently sized slices of objects backed by sync.Pool
5
star
26

tracer

A quick and dirty tracing package for Go
Go
5
star
27

bits

A simple bit manipulation package for Go.
Go
4
star
28

tdd-present

TDD Presentation code
Go
4
star
29

sdk-cocoa

Cocoa and iOS SDK for the Stretchr Platform
Objective-C
3
star
30

fanjs

JavaScript code for fanning elements in a circular pattern
JavaScript
3
star
31

sdk-ruby

Ruby SDK for the Stretchr Platform
Ruby
2
star
32

excerpt-search

Returns a highlighted excerpt from a block of text based on search terms
JavaScript
2
star
33

stretchr.github.io

The Stretchr.com homepage
JavaScript
2
star
34

jbaas

jsonblend as a service.
Go
2
star
35

sdk-js

HTML5 and JavaScript SDK for Stretchr
JavaScript
2
star
36

jsonblend-website

jsonblend website
JavaScript
2
star
37

config

Package that provides easy configuration management for Go programs
Go
2
star
38

sandiego

Repo for San Diego crime data workshop projects
2
star
39

cmds

Elegant command line and external tool management package for Go
Go
2
star
40

issues

Bug tracking for Stretchr platform
1
star
41

TagSpot

iOS application and HTML page showing example integrations into Stretchr
Objective-C
1
star
42

stretchr-backbone

Backbone.js adapter for Stretchr
JavaScript
1
star