JSPM

  • Created
  • Published
  • Downloads 1480
  • Score
    100M100P100Q110429F

Collection of Node.js / ECMAScript Gists

Package Exports

  • jinang
  • jinang/cloneObject
  • jinang/defineError
  • jinang/forInObject
  • jinang/jointString
  • jinang/modifyUrl
  • jinang/papply

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jinang) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

jinang

Collection of Node.js / ECMAScript Mini Modules

The name jinang is abbreviation of "Jin-Nang", which in chinese means a magic box. The modules in jinang are independent for each other, and are for different usage.

Table of contents

Get Started

// Modules are independent for each other and are suggested to be required independently.
const defineError = require('jinang/defineError');

const MyError = defineError('MyError', Error, function(message) {
    this.code = 'SOMETHING_IS_WRONG';
    this.message = message;
});

// ...
throw new MyError('Helo word!');

API

For your convenience, avaiable modules included in jinang are listed here,

cloneObject

  • Object cloneObject( object source )

  • Object cloneObject( object source, Array keynames )
    Item of keynames may be string or RegExp instance. If item is a string, key in source which strictly equals to the keyname will be cloned. However, if it is a RegExp instance, all keys in source will be iterated and those matched will be cloned.

  • Object cloneObject( object source, Function mapper )
    The mapper SHOULD be a function who accepts two arguments (key, value) and return an array [ newKey, newValue ].

currying

Transform a normal function to a curried one.

  • curried Function currying( Function fn, number paramsLength )
const currying = require('jinang/currying');

function plus(a, b, c) {
    return a + b + c;
}

// Create a curried function.
// 3 means that the original function plus() should be invoked with 3 arguments.
var curriedPlus = currying(plus, 3);

// Curried function accepts one and only one argument.
// It may return a new curried function, or the result returned by the original function.
curriedPlus(1);       // RETURN a new function.
curriedPlus(1)(2);    // RETURN a new function.
curriedPlus(1)(2)(3); // RETURN 6.

ATTENTION: Although Currying and Partial Application are mutually related, they are DIFFERENT. If you wanna create a new function which is based on an existing function and with some arguments predefined, use __papply.

Read more about currying:

defineError

  • Function defineError( string ErrorName, Function ParentErrorClass, Function constructor )

E.g.

// Modules are independent for each other and are suggested to be required independently.
const defineError = require('jinang/defineError');

const MyError = defineError('MyError', Error, function(message) {
    this.code = 'SOMETHING_IS_WRONG';
    this.message = message;
});

// ...
throw new MyError('Helo word!');

forInObject

  • void forInObject( object foo, Function iterator )
    Function iterator SHOULD accept two arguments.

E.g.

const forInObject - require('jinang/forInObject');
forInObject({ name: 'YounGoat' }, (name, value) {
    console.log(name);
    console.log(value);
});

jointString

  • string jointString( string joint, string str_1, string str_2, ...)
    Concatenate strings with joint. If the end of the front string or the beginning of the rear one equals to joint, it will be trimmed before being concatenated.

E.g.

const jointString = require('jinang/jointString');
jointString('/', 'a/', '/b');
jointString('/', 'a' , '/b');
jointString('/', 'a/',  'b');
jointString('/', 'a' ,  'b');
// All return 'a/b'.

modifyUrl

  • string modifyUrl( string urlname, Object options )
  • string modifyUrl.pathname( string urlname, string pathname, char flag )
  • string modifyUrl.protocol( string urlname, string protocol )
  • string modifyUrl.query( string urlname, string|Object query, char flag )

papply

Word "papply" is abbreviation of Partial Application, which means to apply a function to some of its arguments and return a new function with fewer parameters.

const papply = require('jinang/papply');

// Suppose a function with three parameters.
function add(a, b, c) {
    return a + b + c;
}

// Run partial application and return a new function.
const add2 = papply(add, 2);
add2(3, 4); // RETURN 2 + 3 + 4

const add2_3 = papply(add, 2, 3);
add2_3(4); // RETURN 2 + 3 + 4

Read more about partial application:

Why jinang

jinang is an incubator for creatives which will make programming with Node.js much easier.

Honorable Dependents

About

References