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

rearg

v0.0.2

Published

JavaScript library for managing your functions arguments

Downloads

4

Readme

rearg

This library helps you to make your methods more flexible by managing arguments list. Just use one of wrappers to define your method. Wrapper prepares arguments list due to described pattern and calls your original method.

Wrappers

rearg.reorder(pattern, func)

If you define method, which takes list of arguments of different types, you can let caller to pass them in accidental order. Use wrapper reorder to define such method. It reorders arguments for you before call original method.

###E.g. You have to get three arguments of different types: number, string and boolean.

var myMethod = rearg.reorder('nsb', function(num, str, b) {
  console.log(num, str, b);
});

Now you can call your method in any way. Original method will be called with arguments list ordered in right way.

myMethod('aa', false, 5);
    // [5, "aa", false]

myMethod(true, 'aa', 7);
    // [7, "aa", true]

myMethod('456');
    // [undefined, "456", undefined]

###Notice If you use type "O" (any Object), you can't use types "d" (Date), "r" (RegExp), "a" (Array) or "o" (other Object) in pattern, because they might be also interpreted as "O" (any Object). But you may use them with "o" (other Object) there. Anyway, you caught the error if you try to define method with collisions in pattern. (see Symbols in patterns)

rearg.expand(pattern, func)

Extend wrapper is used when you want to keep original order of arguments, but you want let caller to miss some of them. Just use question mark for optional params in pattern. This wrapper throws error, if passed arguments couldn't be interpreted due to pattern.

###E.g.

var myMethod = rearg.expand('nss?s?f', function(num, s1, s2, s3, f) {
  console.log(arguments);
});

In this example third and fourth arguments (s2 and s3) aren't required and caller might miss them. Wrapper will replace them by undefined in that case.

myMethod(2, 'a', 'b', 'c', function() {});
    // [2, "a", "b", "c", function]

myMethod(2, 'a', 'b', function() {});
    // [2, "a", "b", undefined, function]

myMethod(2, 'a', function() {});
    // [2, "a", undefined, undefined, function]

myMethod(2, function() {});
    // throws error as second argument is required by pattern and should be string

###Notice You can define optional arguments in sequence with required arguments of the same type only when optional arguments placed in the end of this sequence. It means you can't define pattern like "s?ssn" or "ss?sn". You can define it like "sss?n" and let only last string argument be optional. The same story with type "O" (any Object) in conjunction with any of types "d" (Date), "r" (RegExp), "a" (Array) or "o" (other Object), if you use them in sequence. But it's ok with type "o" (other Object). (see Symbols in patterns)

Symbols in patterns

n - number
s - string
b - boolean
f - function
O - any Object
a - Array
d - Date
r - RegExp
o - other Object (object which isn't Array, Date or RegExp)

The most interesting is type "O". Because any javaScript value (except undefined) might be strongly recognized as one of the other types. But arrays, dates, regular expressions and other objects are also recognized as "O" (any Object). That's why there are some restrictions to use type "O", described in notices for reorder and expand methods.

How start?

npm install rearg
var rearg = require('rearg');

License

MIT