function-com-utils

0.0.5 • Public • Published

function-com-utils

Some utils of function , like Log、event module 、async module and so on .


SingleEvent

SingleEvent is a pub/sub module based on self defination event . It use document.createEvent function to create a event object . So it could run in IE 、Chrome and other browser .

Why this module has a single attribute , because you can only used one instance , created by SingleEvent() , to listen one EVENT topic name .

But remeber this , SingleEvent can only handle NAMED function .

How to use

	import { SingleEvent } from "function-com-utils" ;
	let ev = SingleEvent("myEvent") ;
	let a = function(){console.log('a');} ;
	let b = function(){console.log('b');} ;
	// on
	ev.on(a) ;
	ev.on(b) ;
	// emit
	ev.emit() ; // a b
	// remove
	ev.remove(a) ;
	ev.emit() ; // b

MultEvent

MultEvent is a pub/sub module based on self defination event . It use document.createEvent function to create a event object . So it could run in IE 、Chrome and other browser .

How to use

	import {MultEvent} from "function-com-utils" ;

	let a = function(){console.log('a');} ;
	let b = function(){console.log('b');} ;

	MultEvent.on("topic1" , a) ;
	MultEvent.on("topic1" , b) ;
	MultEvent.remove("topic1" , a) ;
	MultEvent.emit("topic1") ;

	MultEvent.on("topic2" , a) ;
	MultEvent.on("topic2" , b) ;
	MultEvent.emit("topic2") ;

MultAsync

MultAsync could handle given number async process . With this moulde , you could have a final result through binding a final function .

How to use

	import {MultAsync} from "function-com-utils" ;
	let getFinalData = (res) => { console.log(res);}
	let m = MultAsync(3 , getFinalData) ;
	let t1 = setTimeout(() => { 
		let obj = {a : 1} ; m.registerProcess("key1" , obj) ;
		clearTimeout(t1) ; 
	} , 1000) ;
	let t2 = setTimeout(() => { 
		let obj = {a : 2} ; m.registerProcess("key2" , obj) ;
		clearTimeout(t2) ; 
	} , 2000) ;
	let t3 = setTimeout(() => { 
		let obj = {a : 3} ; m.registerProcess("key3" , obj) ;
		clearTimeout(t3) ; 
	} , 3000) ;

Curry

Curry module has offered a currying process .

How to use

	import { Curry } from "function-com-utils" ;
	var cost = (function () {
	  var money = 0;
	  return function () {
		    for (var i = 0, l = arguments.length; i < l; i++) {
		      money += arguments[i];
		    }
		    return money;
		  }
	})();
	let caf = Curry(cost) ;
	caf(100) ;
	caf(1000) ;
	console.log(caf()) // 1100 ;

CurryProcess

CurryProcess could help you to curry some process . Especially , when you want to depart your complex process .

But remeber , this function would no suport async process , if you want to do it , you should use async/await or Promise .

How to use

	import { CurryProcess } from "function-com-utils" 
	let initPrams = { a : 1 } ;
	let process1 = (data) => { return Object.assign({b : 1} , data) } ;
	let process2 = (data) => { return Object.assign({c : 1} , data) } ;
	let res = CurryProcess(initPrams)(process1)(process2)() ;
	console.log(res) ; // { a : 1 , b : 1 , c : 1 } 

MultSync

Use Pattern

MultSync would help you to handle some sync process . And the same params would be given in every process .

But remeber , you should use next function to run next process .

How to use

	import { MultSync } from "function-com-utils" ;
	let ms = new MultSync() ;
	ms.use(function(a , b){ console.log("====" , a) ; ms.next() ; }) ;
	ms.use(function(a , b){ console.log("====" , b) ; ms.next() ; }) ;
	ms.use(function(a , b){ console.log("====" , a + b) ;ms.next() ; }) ;
	ms.emit(1 , 2) ;

filterXSS

Use Pattern

	filterXSS(dangerStr , <replacePatten>)	
filterXSS module offer you a XSS filter function ,
which would filter dangerous script 、console and alert tag and content .

How To Use

	import { filterXSS } from "function-com-utils" ;
	let dangerStr = "<script>alert(111);</script>wqe" ;
	console.log(filterXSS(dangerStr)) ; // wqe

md5

	md5(string)
md5 module offer a basic md5 counting function .

How To Use

	import { md5 } from "function-com-utils" ;
	let str = "this is a md5 test" ;
	console.log(md5(str)) ; 

LINCESE

MIT

Readme

Keywords

Package Sidebar

Install

npm i function-com-utils

Weekly Downloads

2

Version

0.0.5

License

ISC

Unpacked Size

47.2 kB

Total Files

15

Last publish

Collaborators

  • chenchao17