npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

arguejs

v0.2.3

Published

ArgueJS is a library that allows you to delightfully extend your methods's signatures with optional parameters, default values and type-checking.

Downloads

49,749

Readme

#ArgueJS

ArgueJS is a library that allows you to delightfully extend your methods's signatures with optional parameters, default values and type-checking.

example

Let's suppose we want to rewrite the well known method range from underscorejs.

Note that documentation says its method signature is range([start], stop, [step]). With ArgueJS we could type just this way:

function range(){ 
  arguments = __({start: [Number, 0], stop: Number, step: [Number, 1]})

  for(var i = arguments.start; i < arguments.stop; i += arguments.step)
    console.log(i);
}
>>> range(3)
 0
 1
 2
>>> range(3, 5)
 3
 4
>>> range(0, 5, 2)
 0
 2
 4

Installation

ArgueJS is available for both node.js and the browser.

###Node.js

Package is available through npm:

$ npm install arguejs

Browser

Include the ArgueJS browser build in your pages.

<script src="argue.js" type="text/javascript"></script>

This will provide __ as a global object, or define it if you are using AMD.

The latest version will be available for hot-linking at http://raw.github.com/zvictor/ArgueJS/master/argue.js. If you prefer to host yourself, use the argue.js file from the root of the github project.

Getting started

When writing your JavaScript methods with ArgueJS, have in mind that you will not use conventional parameters definition as you used before. Actually, all your methods should be defined without them.

Just at the beggining of your method scope, you should pass an object defining your method signature into a call to __ and save its reference for later.

function person(){
  var paramDefinition = {name: String, age: Number};
  arguments = __(paramDefinition);
  // String name is now referenced by arguments.name
  // Number age is now referenced by arguments.age
  return arguments;
}
>>> Person('John', 27).name
 'John'
>>> Person('John', 27).age
 27

Type-checking

Type-checking ensures your arguments are what you expected, and throws errors when not.

example:

function age(){
  arguments = __({born: Date})
  // ...
>>> age('01/10/1988')
 Error: parameter 'born' waiting for a Date argument but received a String

Avoid type-checking

The special data type undefined can be used to allow the parameter to be of any type.

example:

function Book(){
  arguments = __({title: undefined})
  // ...
  return arguments.title;
}
>>> book('Animal Farm: a Fairy Story')
 'Animal Farm: a Fairy Story'
>>> book(1984)
 1984

Data types

  • String
  • Number
  • Boolean
  • Array
  • Function
  • Object
  • Date
  • RegExp

Special data types:

  • undefined

Optional parameters

Optional parameters are great to avoid a mess of conditional clauses at the beggining of your method. To make a parameter optional, declare its type inside of an Array, like this: {name: [String]}

example:

function unique(){
  arguments = __({array: Array, isSorted: [Boolean], iterator: [Function])
  // Array array is required
  // Boolean isSorted is optional
  // Function iterator is optional
  
  // ...

If no value is passed to an optional parameter, then its argument value will be undefined. To set a default value for your parameter, take a look at default values.

Default values

When writing methods, sometimes you want to override the value of an undefined argument by a default value. The sintax to do this is similiar to optional parameters. That is because a parameter with default value is an optional parameter by itself.

To set a default value for a parameter declare its type and its default value inside of an Array, like this: {name: [String, 'unknown']}

example:

function unique(){
  arguments = __({array: Array, isSorted: [Boolean, false], iterator: [Function, function(element){
    return element;
  }])
  // Array array is required
  // Boolean isSorted is optional and its default value is false
  // Function iterator is optional and its default value is the function declared above
  
  // ...

If you do not care about its type, but just want it to have a default value, you should type your parameter as undefined

example:

  arguments = __({name: [undefined, 'unknown']);

Contributing

This project is in its very early stages and any help, suggestion or posted issue will be very appreciated.