vigour-util
npm install vigour-util
This is a collection of small utility functions which can be required individually.
var valid = isEmail(val)
Checks whether provided parameter looks like a valid e-mail address
- param {string} val - the string to check
- returns {boolean} valid -
true
if val
is a valid e-mail address, false
otherwise
var isEmail = require('vigour-util/is/email')
isEmail('dev@vigour..io')
isEmail('dev@vigour.io')
var looksLikeHash = isHash(val)
Checks is a string looks like a hash generated by the hash
utility
- param {string} val - the string to check
- returns {boolean} looksLikeHash -
true
if val
looks like a hash, false
otherwise
var isHash = require('vigour-util/is/hash')
isHash('asd654')
var runningInNode = isNode()
- returns runningInNode -
true
if in node context, false
otherwise
var isNode = require('vigour-util/is/node')
isNode()
var finiteNumber = isNumber(value)
This just calls lodash.isfinite
internally. See those docs
- param {any} value - The value to check
- returns {boolean} finiteNumber - Returns
true
if value is a finite number, false
otherwise
var isNumber = require('vigour-util/is/number')
isNumber(2)
isNumber('2')
var looksLikeNumber = isNumberLike(val)
Checks whether provided parameter looks like a number
- param {any} val - the value to check
- returns {boolean} looksLikeNumber -
true
if val
looks like a number, false
otherwise
var isNumberLike = require('vigour-util/is/numberlike')
isNumberLike('2')
isNumberLike('a')
var stream = isStream(val)
Checks whether provided argument is a stream
- param {object} val - the object to check
- returns {boolean} stream -
true
if val
is a stream, false
otherwise
var stream = require('stream')
var rs = new stream.Readable()
var isStream = require('vigour-util/is/stream')
isStream(rs)
var readable = isStream.readable(val)
Checks whether provided argument is a readable stream
- param {object} val - the object to check
- returns {boolean} readable -
true
if val
is a readable stream, false
otherwise
var stream = require('stream')
var rs = new stream.Readable()
var isStream = require('vigour-util/is/stream')
isStream.readable(rs)
var writable = isStream.writable(val)
Checks whether provided argument is a writable stream
- param {object} val - the object to check
- returns {boolean} writable -
true
if val
is a writable stream, false
otherwise
var stream = require('stream')
var rs = new stream.Readable()
var isStream = require('vigour-util/is/stream')
isStream.writable(rs)
var touch = isTouch()
Checks if we're running in a touch-enabled context
- returns {boolean} touch -
true
if we're in a touch-enabled context, false
otherwise
var isTouch = require('vigour-util/is/touch')
isTouch()
var valid = isUrl(val)
Checks if a string is a valid url
- param {string} val - the string to check
- returns {boolean} valid -
true
if val is a valid url, false
otherwise
var isUrl = require('vigour-util/is/url')
isUrl('http://perdu.com')
isUrl('boom')
var plain = isPlainObj(obj)
Checks whether an object is a plain object (Compatible with vigour-base
)
- param {object} obj - the object to check
- returns {boolean} plain -
true
if obj is a plain object, false
otherwise
var isPlainObj = require('vigour/util/is/plainobj')
isPlainObj({})
isPlainObj(new Base({}))
var removed = isRemoved(base)
Checks if a property has been removed (Specific to vigour-base
)
- param {Base} base - the property to check
- returns {boolean} removed -
true
if base property has been removed, false
otherwise
var isRemoved = require('vigour-util/is/removed')
var Base = require('vigour-base')
var base = new Base({ bad: true })
base.bad.remove()
isRemoved(base.bad)
var emptyBase = isEmpty(obj)
Checks if a Base
object is empty (Specific to vigour-base
)
- param {object} obj - the object to check for emptiness
- returns {boolean} emptyBase -
true
if obj
is considered empty, false
otherwise
var isEmpty = require('vigour-util/is/empty')
var Base = require('vigour-base')
isEmpty(new Base({}))
isEmpty(new Base({ awesome: true }))
Checks whether a key is part of an array, allowing for prefixed keys
var found = pathContains(path, key)
Checks whether a key is part of an array, allowing for prefixed keys
- param {array} path - the array to look in
- param {string} key - the key to check for
- returns {boolean} found -
true
if key is found in array, false
otherwise
var pathContains = require('vigour-util/path/contains')
pathContains(['a','awesome:b','c'], 'b')
uuid
.val
A process-specific unique ID, generated on require
var uuid = require('vigour-util/uuid')
uuid.val
var id = uuid.generate()
Generates a unique ID
- returns {string} id - A unique ID
var uuid = require('vigour-util/uuid')
uuid.generate()
define(props)
Defines new (or modifies existing) properties (using Object.defineProperty
) on an object passed to define
as this
, setting configurable: true
by default
- param {object} props - Properties to set
var define = require('vigour-util/define')
var subject = {}
var props = [
{ one: true },
{ two: function () {
console.log('do nothing')
}
}
]
define.apply(subject, props)
descriptors(props)
Like Object.getOwnPropertyDescriptor
, but goes along the prototype chain and gets the descriptors for all properties.
- param {object} props - the properties to get the descriptors for
var descriptors = require('vigour-util/descriptors')
descriptors({ a: 'a', b: 'b' })
var object = Take a nested Javascript object and flatten it(subject)
Transforms a deep object into an object of single depth where keys are a path and values are leafs of the original object.
- param {object} subject - Object that needs to be flattened
- param {string} [seperator] - Optional seperator sign
- return {object} - Object with delimited keys
var flatten = require('vigour-util/flatten')
flatten({ a: { b: 'c', d: 'e' } })
flatten({ a: { b: 'c', d: 'e' } }, '/')
var hashOfKey = hash(key, seed)
Hashing utility optimized for speed, not collision avoidance. Produces alpha-numeric hashes between 5 and 7 characters long inclusively.
- param {string} key - the string to hash
- param {number} seed - a seed for hashing
- returns {string} hashOfKey - The created hash
var hash = require('vigour-util')
hash('Any sting in the world!!!')
include
Docs coming soon
merge
Deprecated: consider using lodash.merge
regenerator()
Like Babel's regenerator, but much more compact. Brought to you by Facebook, but bundled in vigour-util
for ease-of-use. See the docs
setwithpath
Deprecated: consider using lodash.set
var obj = unflatten(subject)
Opposite of flatten
. Unflattens an object with delimited keys
- param {object} subject - Object that needs to be unflattened
- param {string} [seperator] - Optional seperator sign
- return {object} obj - Nested Javascript object
var unflatten = require('vigour-util')
unflatten({
'a.b.c': 'd'
})
var ref = getReference(obj)
Get's the referenced object (Specific to vigour-base
)
- param {object} obj - the reference we want to follow
- returns {object} ref - The referenced object or
undefined
var Base = require('vigour-base')
var getReference = require('../../get/reference')
var a = new Base({})
var b = new Base(a)
getReference(b)