gather sometimes need.
var common = require("common-a2dev")
let v = []
if (common.isArray(v)) {
console.log("it's array")
} else {
console.log("it's not array")
}
check args isExists
common.existsArg(undefined) // false
common.existsArg(1) // true
common.existsArg(null) // true
check value it's can change number
common.canNumber (1) // true
common.canNumber ("1") // true
common.canNumber ("a") // false
common.canNumber ("1a") // false
check value it's false or when value is object not have data
common.hasDataObject ({}) // false
common.hasDataObject ([]) // false
common.hasDataObject ([0]) // true
common.hasDataObject () // false
common.hasDataObject (0) // false
get value from object
common.valueObject ({a: 1}, "a", 2) // 1
common.valueObject ({a: 1}, "b", 2) // 2
value copy object
var obj1 = {a: 1}
var obj2 = common.copyObject (obj1) // 1
obj2.a = 2
console.log(obj1) // {a: 1}
console.log(obj2) // {a: 2}
get urls by string
common.matchUrl ("http://exsample.com is url. second url is http://exsample2.com") // ["http://exsample.com", "http://exsample2.com"]
- isDeleteEscape: boolean
- if it's true. delete escape string.
fill data by sample
var obj = {a: 1}
var options = {
isPrune: false
}
var obj2 = common.fillObject (obj, {b: 2}, options) // {a: 1, b: 2}
- isPrune: boolean
- if it's true. run prneOjbect after fillObject
prune data from obj not exists sample value
var obj = {a: 1, b: 2}
var options = {
isPrune: false
}
var obj2 = common.pruneObject (obj, {b: 2}, options) // {b: 2}
get randomInt
common.randomInt(1, 5) // 1-5
get value type from class
common.typeString(1) // '[object Number]'
common.typeString("1") // '[object String]'
common.typeString([]) // '[object Array]'
common.typeString({}) // '[object Object]'
check value is object corectly
common.isObject(1) // false
common.isObject("1") // false
common.isObject(null) // false
common.isObject([]) // false
common.isObject({}) // true
check value is object corectly
common.isArray(1) // false
common.isArray("1") // false
common.isArray(null) // false
common.isArray([]) // true
common.isArray({}) // false
check value is object or array. if it's null return is false.
common.isObjectArray(1) // false
common.isObjectArray("1") // false
common.isObjectArray(null) // false
common.isObjectArray([]) // true
common.isObjectArray({}) // true
get node binary path currentry run.
common.getArgumentNode() // '/usr/local/Cellar/node/8.1.2/bin/node'
NOTE: please use only when cli.
get node script path currentry run.
common.getArgumentCurrent() // '/path/to/current/${script.js}'
NOTE: please use only when cli.
get value from argument.
node sample.js 1 2 3
common.getArgumentValue(0) // 1
common.getArgumentValue(2) // 3
add node path. for Environment
common.addNodePathEnv("/path/to/node") // NODE_PATH: "${NODE_PATH};/path/to/node"
get match string from multi string by pattern.
common.getMatches(["abcdefgabcxyz", "hijxyz"], /cd/) // [ 'cd' ]
common.getMatches(["abcdefgabcxyz", "hijxyz"], /xy/) // [ 'xy', 'xy' ]
common.getMatches(["abcdefgabcxyz", "hijxyz"], /[a-z]{2}/) // [ 'ab', 'hi' ]
common.getMatches(["abcdefgabcxyz", "hijxyz"], /[a-z]{2}/g) // [ 'ab', 'cd', 'ef', 'ga', 'bc', 'xy', 'hi', 'jx', 'yz' ]
get max length from strings
common.getMaxLengthStr(["1", "333", "22"]) // 3
fill string fill
common.fillStr(123, 6) // '00123'
common.fillStr(123, 2) // '23'
common.fillStr(123, 6, "ZY") // 'ZYZ123'
common.fillStr("abc", 7, "ZY") // 'ZYZYabc'
NOTE: when fill's value is multi. stacking from the front.
get string btween prefix and suffix
common.betweenStr("abcdefghijklmn", "cd", "kl") // 'efghij'
common.betweenStr("abcdefghijklmn", "ZZ", "ZZ") // ''
common.betweenStr("abcdefghijklmn", "cd", "kl", {isDetail: true}) // [ 'cdefghijkl', 'efghij', index: 2, input: 'abcdefghijklmn' ]
- isDetail: boolean
- if it's true. return match object.
escape string for regExp
get disallow strings for filename.
common.disallowStringFileName () // [ '\\', '/', ':', '*', '?', '"', '<', '>', '|' ]
replace disallow string from string for filename of filesystem.
common.disallowStringFileName () // [ '\\', '/', ':', '*', '?', '"', '<', '>', '|' ]
var d = new Date
d.format('YYYY-MM-DDTHH:mm:SS') // '2018-03-17T13:54:38'
NOTE: automaticaly set prototype