Namespace: api.common
in Impress Application Server
$ npm install metasync
common.splitAt(index, array)
index:number
- index defining end of first part and start of secondarray:array
- to be splitted
Returns: tuple with two parts of the array
common.isScalar(value)
value
- scalar value or Object
Returns: boolean
common.copy(ds)
ds:array of objects
- source dataset
Returns: array of objects
common.clone(obj)
obj:object or array
Returns: object or array
common.duplucate(obj)
obj:object or array
Returns: object or array
common.getByPath(data, dataPath)
data:hash
dataPath:string
- dot-separated path
Returns: value
common.setByPath(data, dataPath, value)
data:hash
dataPath:string
- dot-separated pathvalue
- new value
common.deleteByPath(data, dataPath)
data:object
dataPath:string
- dot-separated path
Returns: boolean
common.merge(...args)
args:array of array
Returns: array
common.subst(tpl, data, dataPath, escapeHtml)
tpl:string
- template bodydata:hash
- data structure to visualizedataPath:string
- current position in data structureescapeHtml:boolean
- escape html special characters if true
Returns: string
common.htmlEscape(content)
content:string
- to escape
Returns: string
Example: htmlEscape('5>=5') = '5<=5'
common.fileExt(fileName, Result)
fileName:string
- file name
Returns: string
Example: fileExt('/dir/file.txt')
Result: 'txt'
common.removeExt(fileName, Result)
fileName:string
- file name
Returns: string
Example: fileExt('file.txt')
Result: 'file'
common.spinalToCamel(name)
name:string
Returns: string
common.escapeRegExp(s)
s:string
Returns: string
Example: escapeRegExp('/path/to/res?search=this.that')`
common.newEscapedRegExp(s)
s:string
Returns: RegExp, instance
common.addTrailingSlash(s)
s:string
Returns: string
common.stripTrailingSlash(s)
s:string
Returns: string
common.dirname(filePath)
filePath:string
Returns: string
common.capitalize(s)
s:string
common.between(s, prefix, suffix)
s:string
- sourceprefix:string
- before needed fragmentsuffix:string
- after needed fragment
Returns: string
common.removeBOM(s)
s:string
- possibly starts with BOM
Returns: string
common.arrayRegExp(items)
items:array of string
Returns: RegExp, instance
Example: ['/css/*', '/index.html']
common.isTimeEqual(time1, time2)
time1:string
- time or millisecondstime2:string
- time or milliseconds
Returns: boolean
Example: isTimeEqual(sinceTime, buffer.stats.mtime);
common.nowDate(now)
now:Date
(optional)
Returns: string
common.nowDateTime(now)
now:Date
(optional)
Returns: string
common.omap(mapFn, obj)
mapFn:funtion
- to appy to every field valueobj:object
- which fields used for mapping
Returns: object, with same reference but with transformed fields
common.compose(...fns)
fns:array of function
Returns: function, composed
common.maybe(fn, defVal, value)
fn:function
defVal
- default valuevalue
(optional)
Returns: result of fn
or defVal
common.zip(...arrays)
arrays
- array of array
Returns: array, length is minimal of input arrays length
Hint: Element with index i of resulting array is array with elements with index i from input arrays
common.replicate(count, elem)
count:number
- new array lengthelem
- value to replicate
Returns: array, replicated
common.zipWith(fn, ...arrays)
fn:function
- for zipping elements with index iarrays:array of array
Returns: array
Hint: Element with index i of resulting array is result of fn called with arguments from arrays
common.curryUntil(condition, fn, ...args)
condition:function
- (argsI, argsParts) returns booleanfn:function
- which will be curriedargs:array
- arguments for fn
Returns: function, curried
common.curryN(fn, count, ...args)
fn:function
- curriedcount:number
- of times function should be curriedargs:array
- arguments for first currying
Returns: function, curried given times count
common.curryTwice(fn)
fn:function
- to be curried
Returns: function, to pass arguments that returns curried fn
common.curry(fn, ...args)
fn:function
args:array
- arguments
Returns: function, curried
common.applyArgs(...args)
args:array
- arguments to save in closure
Returns: function, to pass (fn) arguments will be applied
common.either(fn)
fn:function
- to be called
Returns: result of fn
common.falseness()
Returns: boolean, always false
common.trueness()
Returns: boolean, always true
common.emptiness()
Returns: always undefined
common.nop(callback)
callback:function
- callback to be called with (null)
common.noop(empty, callback)
empty
- incoming value to be ignoredcallback:function
- callback to be called with (null, null)
common.once(fn)
fn:function (optional)
Returns: function, wrapped callback
Hint: previous name: common.cb
(deprecated)
It's unsafe: may return null, allow multiple calls
common.unsafeCallback(args)
args:array
- arguments
Returns: function, callback or null
Hint: previous name: common.cbUnsafe
(deprecated)
Hint: another alias: common.extractCallback
(deprecated)
Wrap collback with once and return common.emptiness if no callback
common.safeCallback(args)
args:array
- arguments
Returns: function, wrapped callback
Hint: previous name: cbExtract
(deprecated)
common.requiredCallback(args)
args:array
- arguments
Returns: function
common.onceCallback(args)
Wrap collback with once()
and return common.emptiness if no callback
args:array
- arguments
Returns: function
common.override(obj, fn, Hint)
obj:object
- containing method to overridefn:function
- name will be used to find method
Hint: Previous function will be accessible by obj.fnName.inherited
common.range(from, to)
from:naumber
- range startto:naumber
- range end
Returns: array
Example: range(1, 5) = [1, 2, 3, 4, 5]
common.sequence(seq, max, list, range from..to, range from..count, range from..max-to)
seq:array
max:number
- optional max
Returns: array
Example:
- list: sequence([81, 82, 83]) = [81, 82, 83]
- range from..to: sequence([81,,83]) = [81, 82, 83]
- range from..count: sequence([81, [3]]) = [81, 82, 83]
- range from..max-to: sequence([81, [-2]], 5) = [81, 82, 83]
common.last(arr)
arr:array
Returns: element
common.safe(fn)
fn:function
Returns: function, wrapped with try/catch interception
common.random(min, max)
min:number
- range startmax:number
- range end
Returns: number
common.shuffle(arr)
arr:array
Returns: array
common.emitter()
Returns: EventEmitter, instance
common.restLeft(fn)
fn:function
- (args, arg1..argN, callback)
Returns: function, (arg1..argN, ...args, callback)
common.duration(s)
s:string
- duration syntax
Returns: number, milliseconds
Example: duration('1d 10h 7m 13s')
common.bytesToSize(bytes)
bytes:number
- size
Returns: string
common.sizeToBytes(size)
size:string
- size
Returns: number
common.ip2int(ip)
ip:string
- IP address
Returns: number
common.localIPs()
Returns: srray of strings
common.parseHost(host)
host:string
- host or empty string, may contain:port
Returns: string, host without port but not empty
(i.e., not longer than a machine word) in-place and return the remainder
common.longDivModBE(buffer, divisor)
buffer:Buffer
- containing a dividentdivisor:a divisor as a Number
Returns: number, the remainder
common.generateKey(length, possible)
length:number
- key lengthpossible:string
- with possible characters
Returns: string, key
common.generateGUID()
Returns: string, GUID
common.generateSID(config)
config:record
- { length, characters, secret }
Returns: string, SID
common.crcSID(config, key)
config:record
- { length, characters, secret }key:string
- SID key
Returns: string, CRC
common.validateSID(config, sid)
config:record
- { length, characters, secret }sid:string
- session id
Returns: boolean
common.hash(password, salt)
password:string
salt:string
Returns: string, hash
common.validateHash(hash, password, salt)
hash:string
password:string
salt:string
Returns: boolean
common.sortComparePriority(priority, s1, s2)
priority:array of strings with priority
s1, s2:string
- to compare
Returns: number
Example: files.sort(common.sortComparePriority)
common.sortCompareDirectories(a, b)
a, b:string
- to compare
Returns: number
Example: files.sort(sortCompareDirectories);
common.sortCompareByName(a, b)
a, b:object
- { name } to compare
Returns: number
Example: files.sort(sortCompareByName)
common.cache()
Returns: object, cache instance
cache.allocated
- total allocated sizecache.add(key, val)
- add recordcache.del(key)
- delete recordcache.clr(prefix, fn)
- delete all ifkey.startsWith(prefix)
common.section(s, separator)
s:string
separator:string
- or char
Returns: ['All you need ', ' JavaScript']
Example: rsection('All you need is JavaScript', 'is')
common.rsection(s, separator)
s:string
separator:string
- or char
Returns: ['All you need is Jav', 'Script']
Example: rsection('All you need is JavaScript', 'a')
common.split(s, separator, limit)
s:string
separator:string (optional)
- default: ','limit:number (optional)
- max length of result array
Example: split('a,b,c,d')
Result: ['a', 'b', 'c', 'd']
Example: split('a,b,c,d', ',', 2)
Result: ['a', 'b']
common.rsplit(s, separator, limit)
s:string
separator:string (optional)
- default: ','limit:number (optional)
- max length of result array
Example: split('a,b,c,d', ',', 2)
Result: ['c', 'd']
common.rsplit(s, separator, limit)
s:string
separator:string (optional)
- default: ','limit:number (optional)
- max length of result array
Example: split('a,b,c,d', ',', 2)
Result: ['c', 'd']
common.mixin(target, source)
target
- mixin to targetsource
- source methods
common.forwardEvents(from, to, events)
from:EventEmitter
- to listen for eventto:EventEmitter
- to emit event onevents:array of string
- event names
Example: common.forwardEvent(from, to);
Example: common.forwardEvent(from, to, 'eventName');
Example: common.forwardEvent(from, to, { eventName: 'newEventName' });
Example: common.forwardEvent(from, to, ['eventName1', 'eventName2']);
common.methods(iface)
iface:object
- to be introspected
Returns: array of string, method names
common.properties(iface)
iface:object
- to be introspected
Returns: array of string, property names
- Timur Shemsedinov (marcusaurelius) timur.shemsedinov@gmail.com
- Vlad Dziuba (DzyubSpirit) dzyubavlad@gmail.com
- See github for full contributors list