- 提供JavaScript常用的功能,類似helper或是utility,讓javascript專案使用
- 可用於Server-side專案,如node.js,或是Client-side專案,如vue,react中
- 發佈於npm中,使用時當作一個package使用
- 可用於es6與commonjs中
- Validator
- Parser
- time
- array
- hostname
- Checker
- Utils
-
check the input validity with specified rules
-
only the validated attribute(s) are returned
-
convert data type on validating, e.g '100' to 100 for 'type:number'
-
rules
- required
- type:[string|date|datetime|isoDatetime|number|email|bool|boolean|null|nonEmptyString|object]
- same:attributeName
-
validator:: obj -> obj -> obj
-
examples
const rules = { foo: 'required|type:number', bar: 'required|same:foo', koo: 'type:datetime', loo: 'validValues:[v1,v2,v3]', moo: 'required|type:nonEmptyString', aoo: 'type:array', soo: 'type:[number,null,nonEmptyString]', }; const validatedData = { foo: 123, bar: 123, koo: '2000-01-01', loo: 'invalidValue', moo: '', aoo:'notArray'}; let result = validator(rules)(validatedData); // => { validated: true , values:{foo:123, bar:123,koo:'2000-01-01'}} // => { validated: false , // errors: { // foo: ['foo should be a number.'], // koo: ['koo should be a string.'] // loo: ['loo should be in one of the values: v1,v2,v3.'] // moo: ['moo should be an non empty string.'] // aoo: ['aoo should be in array format.'] // } // }
- shuffle :: array -> array
- re-order the array items randomly
- isValidHostname :: String -> Boolean
- To validate a give hostname
-
isTrue :: a -> boolean
- To check if a given value is true
-
isEmpty :: a -> boolean
- To check is a given value is empty
-
isIntBetween:: int a -> int b -> int c -> boolean
- To check is a given int or value is between the specified range
-
isNumber:: a -> boolean
- To check is given value is a number
-
isString:: a -> boolean
- To check is given value is a string
-
isObject:: a -> boolean
- To check is given value is an object
- null and array are excluded
-
isValidEmail:: a -> boolean
- To check is given value is a valid email
-
isValidBool:: a -> boolean
- To check is given value is a valid boolean value
-
isFunction:: a -> boolean
- To check is given value a Function
-
isValidHostname:: a -> boolean
- To check if a given value a valid hostname
-
isDate:: str a -> boolean
- To check is given string is in date format
- valid date format: yyyy-mm-dd (time is not included)
-
isDateTime:: str a -> boolean
- To check is given string is in datetime format
- valid datetime format includes: unix time (integer), date+time, UTC
-
isIsoFormat:: a -> boolean
- To check is given value is in ISO 8601 format
-
isIsoFormatWithZeroOffset:: a -> boolean
- To check is given value is in ISO 8601 format with zero offset
-
useRetry
-
useLock
-
useWait
- To make an action execution wait for a given time before execution
useWait(1000, action); //=> wait for 1000ms, and then run action - ```
-
useRedisCache
- To get value from redis cache, otherwise get the value and cache it
const foo = await useCache('someCacheKey', getter, {keyLife:100}); //=> keyLife: key有效時間 (in second) - ```
-
now :: null -> DateTime
now(); //=> current time
-
isoStrToUtcStr:: isoDatetimeString -> utcDatetimeString
isoStrToUtcStr('1997-07-16T19:20:35+03:00'); //=> '1997-07-16 16:20:35'
-
isoStrToTaipeiStr :: isoDatetimeString -> taipeiDatetimeString
isoStrToTaipeiStr('1997-07-16T19:20:35+03:00'); // utc: 1997-07-16T16:20:35 //=> '1997-07-17 00:20:35'
-
clearSpace:: string -> string
- To clear or remove space in a string
-
trim
- To trim the string(s) in object properties and values
- To trim string(s) in list
- Can trim string(s) in nested objects or nested array
trim({ 'p1 ': 'foo ', p2: [' abc', ' def '], p3: { p3_1: ['p31 '] }, p4: { p4_1: ['p41 ', ' p42'], 'p4_2': {} }, p5: 100, }); //=> { p1: 'foo', p2: ['abc', 'def'], p3: { p3_1: ['p31'] }, p4: { p4_1: ['p41', 'p42'], 'p4_2' : {} }, p5: 100, }
-
extractByPath
-
To extract a value from a structured collection object
-
Object structure
{ k1: { k1a: {} k1b: {} }, k2: { k2a: {} k2b: {} } }
- example:
extractByPath(['info', 'age'])({'person1':{info:{age:10}}, 'person2':{info:{age:20}}}); //=> {'person1':10, 'person2':20}
-
-
getDirty
-
To get the values in new object which differ from that in the original object
-
The function is auto-curry
-
example:
oriObj = {a:1, b:3} newObj = {a:1, b:5, c:7} getDirty(oriObj)(newObj); //=> {b:5, c:7}
-
-
renameKey
-
To change a key name
-
example:
oriObj = {foo:1} renameKey('foo','bar')(oriObj); //=> {bar:1}
-
-
createEventEmitter
-
To create an event emitter
-
example:
const em = createEventEmitter(); // register event handler em.on('someEvent', someHandler); // remove event handler // A. remove specific handler em.remove('someEvent', someHandler); // B. remove all handlers em.remove('someEvent'); // send out event em.emit('someEvent', payload);
-
-
pluckObjProp
-
To pluck a nested obj prop
-
example:
// Functor f => k -> {k:v} -> {k:v} const obj = { prop1: { foo: 123, bar: 'abc' }, prop2: { foo: 999, bar: 'abc' } } pluckObjProp('foo')(obj) //=> {prop1: 123, prop2: 999}
-
-
clean
- To remove obj props if value is undefined/null/emptyString
- Applicable to nested object
clean({ foo: 123, bar: undefined, koo: { k1: 'abc', k2: undefined, }, poo: null, moo: '', }) //=> { foo: 123, koo: { k1:'abc'} }
-
cleanNilUndefined
- To remove obj props if value is undefined/null
- Applicable to nested object
clean({ foo: 123, bar: undefined, koo: { k1: 'abc', k2: undefined, }, poo: null, moo: '', }) //=> { foo: 123, koo: { k1:'abc'} moo: '' }
-
hasValue
- To check if an object has specified value
- Applicable to simple object
hasValue(123, { foo: 123 }) //=> true hasValue('abc', { foo: 123 }) //=> false
-
rmArrSquareInReqParamsKey
harmArrSquareInReqParamsKeys( { from: 0, 'ids[]': [ 'abc1705548556993', 'abc1705558802725'] }); //=> { from: 0, 'ids' : [ 'abc1705548556993' , 'abc1705558802725'] };
-
getObjectDifference
const obj1 = { a: 1, b: { c: 2, d: { e: 3, }, }, f: { g: 5, }, }; const obj2 = { a: 1, b: { c: 3, d: { e: 4, }, }, }; getObjectDifference(obj1, obj2); //=> { { 'b.c': [2, 3], 'b.d.e': [3, 4], f: [{ g: 5 }, undefined] }
-
parseObj :: obj -> QueryString
- convert an object to query string
- example:
parsrStr({a:'foo',b:'bar'}) //=> a=foo&b=bar
-
parseStr :: QueryString -> obj
- convert a query string to an object
- example:
parsrObj('a=foo&b=bar') //=> {a:'foo',b:'bar'}
-
convertKeyValue :: [a] -> obj (a: 'str=str')
- convert a key-value array to an object
convertKeyValue(['foo=bar', 'fiz=fuz']); //=> {foo:'bar', fiz:'fuz'}
-
convertObjToArr :: obj -> [a] (a: 'str=str')
- convert an object to a key-value array
convertKeyValue({ foo: 'bar', fiz: 'fuz' }); //=> ['foo=bar', 'fiz=fuz']
-
snakeToCamel :: str -> str
- convert a snake-string to camel-case string
snakeToCamel('abc_def_ghi') //=> 'abcDefGhi'
-
objKeyToCamel :: obj -> obj
- convert all keys in an object from snake to camel string
- nested object is supported
objKeyToCamel({this_is_key: val}) //=> {thisIsKey: val}
-
camelToSnake :: str -> str
- convert a camel-case string to snake-case string
camelToSnake('abcDefGhi') //=> 'abc_def_ghi'
-
objKeyToSnake :: obj -> obj
- convert all keys in an object from camel to snake-case string
- nested object is supported
objKeyToSnake({ thisIsKey: 'someVal' }) //=> { this_is_key: 'someVal' }
-
parseCookie :: string -> obj
- parse a cookie string to an object
parseCookie('foo=bar ; equation=E%3Dmc%5E2; asd='); //=> { foo: 'bar', equation: 'E=mc^2', asd: '' }
-
serializeCookie :: obj -> string
- serialize an object to cookie string
serializeCookie({ foo: 'bar', equation: 'E=mc^2', asd: '' }); //=> 'foo=bar;equation=E%3Dmc%5E2;asd='