Object Methods
Path:
/src/mod/object.coffee
compiled to/lib/mod/object.js
isEmpty()
This method will check if an object is empty. This is also true for undefined objects.
Usage:
isEmpty(object)
- Parameter
-
object
-Object
to be checked
- Return
Boolean
true
if object is empty
path()
This method allows you to access an element deep in an object’s structure by giving only the path to the element.
Example:
CoffeeScript Code util = require 'alinex-util'
test =
string: 'test'
object:
numbers:
one: 1
two: 2
list: [
one: 11
,
two: 12
]
result = util.object.path test, '/object/numbers'
This results to:
CoffeeScript Code result = { one: 1, two: 2 }
Usage:
path(object, path, separator)
- Parameter
-
object
-Object
to be searchedpath
-String|Array
specifying which element to referenceseparator
-String|RegExp
optional used as separator (default: /)
- Return
- element at the position of the path or
undefined
if not found
pathSearch()
Like path but here you may give a search pattern to find the element. It may also find multiple elements which will be returned in a list.
Example:
CoffeeScript Code util = require 'alinex-util'
test =
string: 'test'
object:
numbers:
one: 1
two: 2
list: [
one: 11
,
two: 12
]
result = util.object.pathSearch test, '**/one'
This results to:
CoffeeScript Code result = 1
Pattern
You may specify like in the following examples (using the default separator).
name - get first element with this name
group/sub/name - get element with path
You can search by using asterisk as directory placeholder or a double asterisk to go multiple level depth:
name/*/min - within any subelement
name/*/*/min - within any subelement (two level depth)
name/**/min - within any subelement in any depth
You may also use regexp notation to find the correct element:
name/test[AB]/min - pattern match with one missing character
name/test\d+/min - pattern match with multiple missing characters
SeeRegExp for the possible syntax but without modifier.
Usage:
pathSearch(object, path, separator)
- Parameter
-
object
-Object
to be searchedpath
-String|Array
specifying which element to referenceseparator
-String|RegExp
optional used as separator (default: /)
- Return
- element at the position of the path or
undefined
if not found
filter()
Like theArray.prototype.filter() method of arrays you may filter object entries here.
Example:
CoffeeScript Code util = require 'alinex-util'
test =
one: 1
two: 2
three: 3
four: 4
result = util.object.filter test, (value) -> value < 3
This results to:
CoffeeScript Code result =
one: 1
two: 2
Usage:
filter(obj, will)
- Parameter
-
-
obj
-Object
to be filtered -
will
-Function
be called for each entry with:value
- the current valuekey
-String
the current key nameobj
- the whole object
It should return a
Boolean
value. Iftrue
the entry will be copied to the resulting object.
-
- Return
Object
data object with all or some entries from the original
lcKeys()
Make the keys within a deep object all lowercase.
Example:
CoffeeScript Code util = require 'alinex-util'
test =
One: 1
TWO:
three: 3
fouR: 4
result = util.object.lcKeys test
Will result in:
CoffeeScript Code result =
one: 1
two:
three: 3
four: 4
Usage:
lcKeys(obj)
- Parameter
-
obj
-Object
to be optimized
- Return
Object
data object with all keys in lowercase
isCyclic()
This will check an object if it contains circular references.
Example:
CoffeeScript Code util = require 'alinex-util'
test = { eins: 1 }
util.object.isCyclic test # will be false
test.zwei = test.eins
util.object.isCyclic test # will be true
Usage:
isCyclic(obj)
- Parameter
-
obj
-Object
to be checked
- Return
Boolean
true
if the object contains circular references
getCyclic()
Detects circular references but instead of only checking it list all cyclic objects.
Example:
CoffeeScript Code util = require 'alinex-util'
test = { eins: 1 }
util.object.getCyclic test # empty list []
test.zwei = test.eins
util.object.getCyclic test # [{ eins: 1 }]
Usage:
getCyclic(obj)
- Parameter
-
obj
-Object
to be checked
- Return
Array
list of objects which are circular