3.0.1 • Published 6 years ago

berserk v3.0.1

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

A library of convenient function by Jituan Lin

This is Jituan Lin 's utils library, maybe is also helpful for you.

This project is power by Ramda.js

Note:.

  • Almost function is curring for you.

  • berserk depend on ramda,ensure you install ramda correctly.

How to use

  1. install berserk npm install berserk

  2. import or require the berserk, depend your environment.

    1. in node.js
    	const j =require('berserk')
        // berserk include a whole Ramda.js in it
    	j.log('great: ',j.always('hello world'))
        // will log:
        //  GREAT:
        //      hello world
    1. with webpack
        import j from 'berserk'
        //berserk also has it's own function
          do_(
             (initValue)=>Promise.resolve({a:initValue}),
             ({a})=>({b:a+1}),
             (result)=>console.log(result)
          )(0)
        //log: { a : 0 , b : 1 }

How to test

npm run test

Generate API doc

npm run doc

Develop a new function

npm run new, then will create a template in ./src ,./test, and add new function in ./index.js

API doc

Functions

checkErr(res, rej) ⇒ function

A convenient function for handle error in callback function. Accept two function res(resolve) and rej(reject) , return a wrap function that accept a list arguments, the first argument as error, if error is null, the res function will call,else the rej function.

Kind: global function
Returns: function - return a function that accept a list arguments, the first argument as error, if error is null, the res function will call,else the rej function

ParamTypeDescription
resfunctionthe function which will call when no error throw
rejfunctionthe function which will call when error occur

condP(pairs) ⇒ Promise

Same as Ramda 's cond function, but support function that return Promise as it's arguments

Kind: global function

ParamType
pairsArray

Example

condP([
 [
 () => new Promise(
 resolve => setTimeout(() => resolve(false), 200)
 ),
 (result) => result + 1
 ],
 [
 () => new Promise(
 resolve => setTimeout(() => resolve(true), 200)
 ),
 (result) => result + 2
 ]
 ])(0)
 // => 2

debug(title) ⇒ function

accept a title, return a function, the return function accept any data then log it with title(for better debug), then return the data back

Kind: global function

ParamType
titlestring

Example

debug('the great')('hello world')
// will log '[The great]: hello world',and return
// => 'hello world'

do_(...actions) ⇒ function

The action must accept a object as 'context' and return a object to merge to context, support promise. Similar do in Haskell.

Kind: global function
Returns: function - A function that accept a object as initial 'context' pass to first action

ParamTypeDescription
...actionsarraya array of function to chain,

Example

do_(
 (initValue)=>Promise.resolve({a:initValue}),
 ({a})=>({b:a+1}),
 (result)=>console.log(result)
 )(0)

I() ⇒ any

A function return a unit what pass to it. Know as I combinator.

Kind: global function
Params: any
Example

sameTo('hello world!')
// return 'hello world!'

isSuperSet(childSet, supperSet)

Judge a set is it another set's childSet

Kind: global function

ParamType
childSetSet
supperSetSet

Example

isSupperSet(
       new Set([1, {a: 2}, 3]),
       new Set([1, {a: 2}, 3, 4])
)
// return false

log(title, content)

Better log function, will accept title and content as it's arguments, then console.log it

Kind: global function

ParamType
titlestring
contentstring

Example

const j =require('berserk')
const fs =require('fs')
fs.readdir(
   'path',
   j.ifErr(j.log('success'),j.log(error occur))
)
// will print '[Success]: ['file1','file2'...]',if not error thrown
// otherwise print '[Error occur]: error message'

Matcher(...matcherList)

Same as ramda's cond,but more readable

Kind: global function

ParamType
...matcherListarray

Example

const j=require('berserk')
const Pattern=Pair
const matcher=Matcher(
       Pattern(R.is(Array),R.head),
       Pattern(R.T,R.always('no match'))
)
mather([1,2,3])
//=> 1

Pair(first, second)

The factory function to construct a single pair

Kind: global function

ParamType
firstany
secondany

Example

const pair=Pair('name','jituanlin')
//=> ['name','jituanlin']

run(data, ...funList)

Same as ramda's pipe, but will accept a data and some function as it's arguments.Then call first function with data and past the result as arguments of next function, then call the second function as previous function result chain by chain...

Kind: global function

ParamType
dataany
...funListfunction

Example

run(
   1,
   R.inc,
   R.inc
)
//=> 3

setEq(set1, set2) ⇒ boolean

Compare whether two set (es6: new Set(iterable)) is same as.

Kind: global function

ParamType
set1Set
set2Set

Example

j.setEq(
   new Set([1, 2, 3]),
   new Set([3, 2, 1])
) // return true

j.setEq(
   new Set([1, 2]),
     new Set([1, 2, 3])
)// return false

toPromise(fun) ⇒ function

Wrap the callback style function to Promise style function, the callback style function must restrict by convention: 1. the function must put the callback function where the last of arguments, such as (arg1,arg2,arg3,arg...,callback) 2. the callback function must call as callback(err,arg1,arg2,arg...) note: berserk support bluebird Promise

Kind: global function
Returns: function - Return the new function that will return a Promise, while the origin function throw a error, the Promise will be Promise.reject(error), while the origin function work fine, the Promise will be Promise.resolve(args: array), the args is which callback function accept

ParamTypeDescription
funfunctionThe callback style function to transform

Example

const {readdir} =require('fs')
   const readdirP=toPromise(readdir)
   readdir(Path)
     .then(
       files=>console.log(files),
       (err)=>console.log(err)
       )
3.0.1

6 years ago

3.0.0

6 years ago

2.1.5

7 years ago

2.1.4

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.0

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago