
Security News
GitHub Actions Supply Chain Attack Puts Thousands of Projects at Risk
A compromised GitHub Action exposed secrets in CI/CD logs, putting thousands of projects at risk and forcing developers to urgently secure their workflows.
This is Jituan Lin 's utils library, maybe is also helpful for you.
Almost function is curring for you.
Default support bluebird Promise.
berserk depend on ramda,ensure you install ramda correctly.
install berserk
npm install berserk
import or require the berserk, depend your environment.
const j =require('berserk')
// berserk include a whole Ramda.js in it
j.log('great: ',j.always('hello world'))
// will log:
// GREAT:
// hello world
import j from 'berserk'
//berserk also has it's own function
j.midFlow(
[
({a})=>({b:a+1}),
({b})=>({c:a+b}),
(obj)=>j.log(obj)
]
)({a:1})
//log: { a:1, b:2 , c:3 }
const {readdir} =require('fs')
const readdirP=toPromise(readdir)
readdir(Path)
.then(
files=>console.log(files),
(err)=>console.log(err)
)
npm run test
npm run doc
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.
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
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...
Judge a set is it another set's childSet
Better log function, will accept title and content as it's arguments, then console.log it
Same as ramda's cond,but more readable
function
the middleware must accept a object as 'context' and return a object to merge to context
The factory function to construct a single pair
any
A function return a unit what pass to it
boolean
Compare whether two set (es6: new Set(iterable)) is same as.
function
Wrap the callback style function to Promise style function, the callback style function must restrict by convention:
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
Param | Type | Description |
---|---|---|
res | function | the function which will call when no error throw |
rej | function | the function which will call when error occur |
cond
function, but support function that return Promise as its arguments(pairs)Kind: global function
Param | Type |
---|---|
pairs | Array |
Example
condP([
[
() => new Promise(
resolve => setTimeout(() => resolve(false), 200)
),
(result) => result + 1
],
[
() => new Promise(
resolve => setTimeout(() => resolve(true), 200)
),
(result) => result + 2
]
])(0)
// => 2
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
Param | Type |
---|---|
title | string |
Example
debug('the great')('hello world')
// will log '[The great]: hello world',and return
// => 'hello world'
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
Param | Type |
---|---|
data | any |
...funList | function |
Example
flow(
1,
R.inc,
R.inc
)
//=> 3
Judge a set is it another set's childSet
Kind: global function
Param | Type |
---|---|
childSet | Set |
supperSet | Set |
Example
isSupperSet(
new Set([1, {a: 2}, 3]),
new Set([1, {a: 2}, 3, 4])
)
// return false
Better log function, will accept title and content as it's arguments, then console.log it
Kind: global function
Param | Type |
---|---|
title | string |
content | string |
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'
Same as ramda's cond,but more readable
Kind: global function
Param | Type |
---|---|
...matcherList | array |
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
function
the middleware must accept a object as 'context' and return a object to merge to context
Kind: global function
Returns: function
- A function that accept a object as initial 'context' pass to first middleware
Param | Type | Description |
---|---|---|
mids | array | a array of middleware, |
Example
// A function use midFlow for create a element(browser) by option
const elm = j.midFlow(
[
({tag}) => ({el: document.createElement(tag)}),
({el, attr = {}}) => {
R.forEachObjIndexed(
(value, name) => el[name] = value,
attr
)
},
({children = [], el}) => {
j.midFlow(
[
({doc}) => {
R.forEach(
(child) => doc.appendChild(child),
children
)
},
({doc}) => {el.appendChild(doc)}
]
)({doc: new DocumentFragment})
},
({evtLir = [], el}) => {
R.forEach(
({handler, type, opt = {}}) => el.addEventListener(type, handler, opt),
evtLir
)
},
({el}) => el
]
)
The factory function to construct a single pair
Kind: global function
Param | Type |
---|---|
first | any |
second | any |
Example
const pair=Pair('name','jituanlin')
//=> ['name','jituanlin']
but wrap with Promise and concat root path with the return children path(path) Kind: global function
Param | Type |
---|---|
path | string |
Example
const {log,readDirP} =require('berserk')
readDirP('./src/').then(log('children path'))
// will output: [Children path]: ['./src/index.js','./src/utils']
any
A function return a unit what pass to it
Kind: global function
Params: any
Example
sameTo('hello world!')
// return 'hello world!'
boolean
Compare whether two set (es6: new Set(iterable)) is same as.
Kind: global function
Param | Type |
---|---|
set1 | Set |
set2 | Set |
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
Kind: global function
Param | Type | Description |
---|---|---|
type | string | the type of target, see more node.js document |
path | string | the target path |
Example
targetType('file', './doc/api.md').then(log('Is the target type of path file? '))
//will output: ['Is the target type of path file? ']: true/false
function
Wrap the callback style function to Promise style function, the callback style function must restrict by convention:
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
Param | Type | Description |
---|---|---|
fun | function | The 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)
)
FAQs
A library of convenient function by Jituan lin
The npm package berserk receives a total of 0 weekly downloads. As such, berserk popularity was classified as not popular.
We found that berserk demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A compromised GitHub Action exposed secrets in CI/CD logs, putting thousands of projects at risk and forcing developers to urgently secure their workflows.
Research
Security News
A malicious Maven package typosquatting a popular library is secretly stealing OAuth credentials on the 15th of each month, putting Java developers at risk.
Security News
Socket and Seal Security collaborate to fix a critical npm overrides bug, resolving a three-year security issue in the JavaScript ecosystem's most popular package manager.