All Projects → serkanyersen → Kwargsjs

serkanyersen / Kwargsjs

Licence: mit
Smart python like argument management for javascript

Programming Languages

javascript
184084 projects - #8 most used programming language
python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to Kwargsjs

Intellij Swagger
A plugin to help you easily edit Swagger and OpenAPI specification files inside IntelliJ IDEA
Stars: ✭ 1,073 (+858.04%)
Mutual labels:  utilities
Nps Utils
Utilities for http://npm.im/nps (npm-package-scripts)
Stars: ✭ 83 (-25.89%)
Mutual labels:  utilities
Rearmed Js
A collection of helpful methods and monkey patches for Arrays, Objects, Numbers, and Strings in Javascript
Stars: ✭ 102 (-8.93%)
Mutual labels:  utilities
Gitfu.fish
All-purpose git utility functions in Fish.
Stars: ✭ 64 (-42.86%)
Mutual labels:  utilities
Useful Tools
A list of useful tools and programs for developers, DevOps and SysAdmins
Stars: ✭ 74 (-33.93%)
Mutual labels:  utilities
Alfonz
Mr. Alfonz is here to help you build your Android app, make the development process easier and avoid boilerplate code.
Stars: ✭ 90 (-19.64%)
Mutual labels:  utilities
Sassyfication
💅Library with sass mixins to speed up your css workflow.
Stars: ✭ 51 (-54.46%)
Mutual labels:  utilities
Elect
The collection of utilities, best practice and fluent method for .NET Core
Stars: ✭ 107 (-4.46%)
Mutual labels:  utilities
Sequelize Test Helpers
A collection of utilities to help with unit-testing Sequelize models
Stars: ✭ 83 (-25.89%)
Mutual labels:  utilities
Node Mysql Utilities
Query builder for node-mysql with introspection, etc.
Stars: ✭ 98 (-12.5%)
Mutual labels:  utilities
Utils
🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
Stars: ✭ 1,158 (+933.93%)
Mutual labels:  utilities
Awesome Vuetify
🎉 The best resources related to Vuetify
Stars: ✭ 1,189 (+961.61%)
Mutual labels:  utilities
Raisincss
An Utility CSS only library. It supports css grid and many more useful css properties.
Stars: ✭ 93 (-16.96%)
Mutual labels:  utilities
Android Templates And Utilities
Collection of source codes, utilities, templates and snippets for Android development.
Stars: ✭ 1,099 (+881.25%)
Mutual labels:  utilities
Datofu
there's a :db/fn for that
Stars: ✭ 104 (-7.14%)
Mutual labels:  utilities
Utils
Docker image with tools like curl, wget, ping, nslookup, dig, psql etc.
Stars: ✭ 49 (-56.25%)
Mutual labels:  utilities
Bufferutil
WebSocket buffer utils
Stars: ✭ 83 (-25.89%)
Mutual labels:  utilities
Office Js Helpers
[ARCHIVED] A collection of helpers to simplify development of Office Add-ins & Microsoft Teams Tabs
Stars: ✭ 111 (-0.89%)
Mutual labels:  utilities
Bootstrap 4 Utilities
Bootstrap 4 utility classes in LESS CSS for Bootstrap 3 or any other projects.
Stars: ✭ 105 (-6.25%)
Mutual labels:  utilities
Glom
☄️ Python's nested data operator (and CLI), for all your declarative restructuring needs. Got data? Glom it! ☄️
Stars: ✭ 1,341 (+1097.32%)
Mutual labels:  utilities

Keyword arguments for Javascript. Similar to python's kwargs

This little tool gives you the ability to use keyword arguments support for your functions. So you can either specify each argument as you wish or use the arguments regularly. In fact you can do both at the same time.

Another feature is to have the ability to set default values for your function arguments without changing or adding any code to your function.

Build Status

Usage

Just include the script on your site. That's it. When included it will add a new method called kwargs to Function prototype and you can use it like this:

var functionName = function(arg1, arg2){
    // code
}.kwargs([defaults]);

With node.js

You can install it with npm

npm install kwargsjs

and include it on your scripts

var kwargs = require('kwargsjs');
var functionName = kwargs(function(arg1, arg2){
    // code
}, [defaults]);
// or using function prototype
var functionName = function(arg1, arg2){
    // code
}.kwargs([defaults]);

Examples

Just write your function as you would normally, and don't worry about the arguments size. just call .kwargs() and rest will be handled.

var test = function(arg1, arg2, arg3){
    // Your code
}.kwargs();

Now, if you want you can pass all arguments in a single object and they all will be mapped to their correct places

test({
    arg3: 'val3',
    arg1: 'val1',
    arg2: 'val2'
});

You can also use your function like you would normally use

test('val1', 'val2', 'val3');

the best part is that you can do both

test('val1', {
    arg3: 'val3',
    arg1: 'val1',
});

Using Default values for arguments

Let's say we have this function that says Hello to a given name.

var greeting = function(name){
    return "Hello " + name;
};
greeting('Frank'); // -> Hello Frank

If no name is given, we want it to return "Hello World", usually you would have to add conditions to your function and check for existence of name argument. kwargs automatically handles that for you.

var greeting = function(name){
    return "Hello " + name;
}.kwargs({name: 'World'}); // Set a default value for your argument and
                           // it will be used when this argument is empty
// Here are the results
greeting('Frank'); // -> Hello Frank
greeting(); // -> Hello World

A real example

Let's say we have a function that receives a lot of arguments and generates a name with prefixes and suffixes when provided.

var name = function(firstName, lastName, middleName, prefix, suffix){
    var name = [];
    if(prefix){
        name.push(prefix);
    }
    name.push(firstName);
    if(middleName){
        name.push(middleName);
    }
    name.push(lastName);
    if(suffix){
        name.push(suffix);
    }
    return name.join(' ');
}.kwargs();

Now, when we want create a name with only a suffix, all we have to do is to provide the name and suffix. You can only pass required arguments without changing anything on your function code.

name('John', 'Doe', { suffix:'Ph.D.' });
// -> John Doe Ph.D.
name('Max', 'Fightmaster', { prefix: 'Staff Sgt.' })
// -> Staff Sgt. Max Fightmaster
name('Isaac', 'Newton', { prefix: 'Sir', suffix: 'PRS MP'});
// -> Sir Isaac Newton PRS MP

Warnings

Name Mangling

Some javascript minifiers such as UglifyJS are using a technique called mangling which shortens the code by changing the argument and variable names in your function, this feature might break kwargs because it relies on these names. You can either disable this feature on your minifier or define your argument names as reserved words in mangle options


Kwargs argument and reguler object arguments

If last argument passed is an object, code assumes it's a kwargs object, if your function accepts objects as arguments you should be careful about this, here is an example.

// in both cases, `anObject` argument will be interpreted as `kwargs` object and be ignored
myFunc(anObject);
myFunc('val', anObject);

to avoid this problem you have two solutions

myFunc(anObject, {}); // passing last argument as an empty object
// or using the options method and passing your object in kwargs
myFunc({
  arg1: anObject
});

LICENSE

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].