Namespace: object

object

Methods

(static) collectionContains(collection, userFilters, detailedopt, ignoreCaseopt) → {Boolean|Object}

Checks if an array of objetcs contain another object (even partially)
Parameters:
Name Type Attributes Default Description
collection Array.<Object>
userFilters Object object to find
detailed Boolean <optional>
false if true will return an object with deails about the searc
ignoreCase Boolean <optional>
false if true, will ignore case when matching strings
Source:
To Do:
  • don't support nested objects for now
Returns:
Type
Boolean | Object
Examples
single filter
const fruits = [{ name: 'banana', weight: 200, color: 'yellow' }, { name: 'tomato', weight: 150, color: 'red' }]
collectionContains(fruits, { name: 'banana' }); // will return true
multiple filters
const fruits = [{ name: 'banana', weight: 200, color: 'yellow' }, { name: 'tomato', weight: 150, color: 'red' }]
collectionContains(fruits, { name: 'banana', color: 'red' }); // will return false
custom comparison logic
const fruits = [{ name: 'banana', weight: 200, color: 'yellow' }, { name: 'tomato', weight: 150, color: 'red' }]
collectionContains(fruits, { weight: v => v > 100 }); // will return true

(static) firstKey(obj) → {any}

Get first key of an object or null if it doesn't have keys
Parameters:
Name Type Description
obj Object
Source:
Returns:
Type
any
Example
firstKey({ a: 2, b: 2 }) // 'a'

(static) flatten(obj, separatoropt, prefixopt) → {Object}

Transforms an nested object into an flat object with dotted notation
Parameters:
Name Type Attributes Default Description
obj Object | Array the source object or array
separator string <optional>
"."
prefix string <optional>
"" All keys will be prefixed with this arg
Source:
Returns:
Type
Object
Example
flatten({ a: { b: 2 } } // { "a.b": 2 }

(static) isCyclic(obj) → {boolean}

Check if any given object has some kind of cyclic reference.
Parameters:
Name Type Description
obj Object the source to be checked
Source:
Returns:
Type
boolean
Example
const obj { a: 1, b: 2 }
obj.c = obj;

isCyclic(obj) // true

(static) module.exports(paths, obj, defaultValueopt) → {any}

Receives a list of paths and use getFlattened to get the first existent value
Parameters:
Name Type Attributes Description
paths Array.<String>
obj Object
defaultValue any <optional>
Source:
See:
  • getFlattened
Returns:
Type
any
Example
const obj = {
  a: 1,
  b: {
    c: 2
  }
}

getFirstFlattened(['a.c', 'a.b.c'], obj) // 2

(static) module.exports(path, obj, defaultValue) → {any}

Get element from obj by string path
Parameters:
Name Type Description
path string specify the key of the object you want
obj Object reference object
defaultValue any value to return if key was not found. Default is null
Source:
Returns:
Type
any
Examples
const a = {a: {b: 1}}
getFlattened("a.b", a) // 1
with default value
const a = { a: { b: 1 } }
getFlattened('a.c', a, 2020) // 2020

(static) module.exports(paths, obj) → {object}

Extracts a fragment of an object, including nested properties and array support. Note: it doen´t work when the keys have '.'.
Parameters:
Name Type Description
paths Array.<string> to extract a value from every object in a collection use '*'. eg: ['a.*.name']
obj object
Source:
Returns:
Type
object
Example
const input = {
  name: "Xpto",
  age: 77,
  address: {
    street: "first",
    number: 88,
  },
  phones: [
    { number: 1111111, code: 11 },
    { number: 2222222, code: 22 },
  ],
};

const res = getFlattenedArray(["name", "address.number", "phones.*.code"], input)

// result
{
  name: "Xpto",
  address: { number: 88 },
  phones: [
    { code: 11 },
    { code: 22 },
  ],
};

(static) module.exports(path, newValue, objopt) → {*}

Parameters:
Name Type Attributes Description
path String
newValue *
obj Object | Array <optional>
Source:
See:
  • getFlattened Similar to getFlattened, but it set the value instead of return it. [version 2.0+] To create an array the path fragment must be wrapped on brackets (eg: path.[0].other) Note: for array on the top level, obj must be an array
Returns:
Type
*
Examples
setFlattened("a.b", 2, {}) // { a: { b: 2 } }
setFlattened("a.[0].c", 2, {}) // { a: [{ c: 2 } ]}