0.0.7 • Published 6 years ago

adenovo-utils v0.0.7

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

Adenovo utils

Util methods for Adenovo, maybe you can find what utils you want here.

Pid

中華民國身分證字號檢查與產生器, 基於wiki

const {checkPid, genPid} = require('adenovo-utils').pid;

const somePid = genPid(); //'B169298328'
const errorResult = checkPid('dsds'); // { result: false, msg: '身分證字號長度不正確' }

const okResult = checkPid('B169298328'); // { result: true }

Basic log

const {debugLog} = require('adenovo-utils').log;

debugLog('for debug usage');  // [debug] for debug usage

For ELK duration logs:

const {durationLog, startLog, endLog} = require('adenovo-utils').log;

const event = {foo: 'bar', hello: 'world'}; // your event object

durationLog('test')(event); // Lambda Event End: {"foo":"bar","hello":"world"}

startLog(event); // Lambda Event Start: {"foo":"bar","hello":"world"}

endLog(event); // Lambda Event End: {"foo":"bar","hello":"world"}

For ELK action logs (basic)

const {actionLog} = require('adenovo-utils').log;

const event = {foo: 'bar', hello: 'world'};
const payload = {group: 'mygroup', method: 'paySomething', data: event}; // optional: memberId

actionLog('handler')('debug')(payload); // DEV,debug,handler,mygroup,paySomething,{"foo":"bar","hello":"world"}

For ELK action logs (more specific)

Includes:

  • handlerLog(logLevel: string)(payload: object)
  • handlerLogDebug(payload)
  • handlerLogInfo
  • handlerLogWarning
  • handlerLogError
  • modelLog(logLevel: string)(payload: object)
  • modelLogDebug(payload)
  • modelLogInfo
  • modelLogWarning
  • modelLogError

For example:

const {handlerLog, modelLog, handlerLogDebug, modelLogWarning} = require('adenovo-utils').log;

const event = {foo: 'bar'};
const payload = {group: 'mygroup', method: 'paySomething', data: event};

handlerLog('hello')(payload); // DEV,hello,handler,mygroup,paySomething,{"foo":"bar"}
modelLog('world')(payload); // DEV,world,model,mygroup,paySomething,{"foo":"bar"}
handlerLogDebug(payload); // DEV,debug,handler,mygroup,paySomething,{"foo":"bar"}
modelLogWarning(payload); // DEV,warning,model,mygroup,paySomething,{"foo":"bar"}

ELK action log payload wrapping

const {handlerLogInfo, getLogPayload} = require('adenovo-utils').log;

const FUNC_TITLE = 'xxxxPayment';
const logFuncPayload = getLogPayload(FUNC_TITLE);

// ...

const methodName = 'xxxPaymentCallback';
const logPayload = logFuncPayload(methodName);

// ...

const orderInfo = {foo: 'bar'};
const member_id = 1234;
handlerLogInfo(logPayload(orderInfo)(memberId));

Functional utils

const {pipe, pipeAsync, compose, composeAsync} = require('adenovo-utils').functional;

const fn1 = a => a + 2;
const fn2 = a => a * 3;

const resp1 = pipe(fn1, fn2)(3); //  3 -> fn1 -> fn2 , result: 15

const resp2 = compose(fn1, fn2)(3); // 3 -> fn2 -> fn1, result: 11

pipeAsync and composeAsync are async pipe/compose version, you can chain your async functions !

Object utils

removeEmpty will remove the null and undefined entries from an object (will not remove empty string value).

const {isEmpty, removeEmpty} = require('adenovo-utils').obj;

const obj1 = {};
const obj2 = {a: 1, b:2};

const result1 = isEmpty(obj1); // true
const result2 = isEmpty(obj2); // false

const obj3 =  {a:1, b: undefined, c: null, d: 1, e: ''};
const result3 = removeEmpty(a); // { a: 1, d: 1, e: '' }

a {key: key} translator:

const {keyMirror} = require('adenovo-utils').obj;

const arr1 = ['a', 'b'];
keyMirror(arr1);  //{ a: 'a', b: 'b'}

const arr2 = [1, 2];
keyMirror(arr2);  //{ '1': 1, '2': 1}
0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago