Methods
exists(obj, propertyName) → {Boolean}
exists will return true when propertyName is a property of obj, else false is returned.
You can also check for the existence of a property contained in a nested object by chaining
the properties. Simply use a dot notation in the propertyName to specify the chain.
Example
const ub = require('@nuskin/uncle-buck')
let obj = { a: 1, b: { c: 0}}
ub.exists(obj,'a') //returns true
ub.exists(obj,'b.c') // returns true
Parameters:
| Name | Type | Description |
|---|---|---|
obj |
Object |
|
propertyName |
String | the name of the property to be checked for. |
Returns:
Returns true if propertyName is a property of obj.
- Type
- Boolean
isBoolean(variable) → {Boolean}
isBoolean tests if variable is of type Boolean
Example
const ub = require('@nuskin/uncle-buck')
ub.isBoolean(false) // returns true
Parameters:
| Name | Type | Description |
|---|---|---|
variable |
* | value to test |
Returns:
Returns true if variable is a boolean
- Type
- Boolean
isJSON(str) → {Boolean}
isJSON tests if str is valid JSON.
Example
const ub = require('@nuskin/uncle-buck')
ub.isJSON('{"abc":true}') // returns true
ub.isJSON('{"abc":true') // returns false
ub.isJSON({"abc":true}) // returns false
Parameters:
| Name | Type | Description |
|---|---|---|
str |
String | String to test |
Returns:
Returns true if string is valid JSON
- Type
- Boolean
isNull(n) → {Boolean}
isNull tests if n is null or undefined.
Example
const ub = require('@nuskin/uncle-buck')
ub.isNull(null) // returns true
ub.isNull(undefined) // returns true
ub.isNull({}) // returns false
Parameters:
| Name | Type | Description |
|---|---|---|
n |
* | value to test |
Returns:
Returns true if the value is null or undefined
- Type
- Boolean
isObject(variable) → {Boolean}
isObject tests if variable is an object
Example
const ub = require('@nuskin/uncle-buck')
ub.isObject('{"abc":true}') // returns false
ub.isObject({"abc":true}) // returns true
Parameters:
| Name | Type | Description |
|---|---|---|
variable |
* | value to test |
Returns:
Returns true if variable is an object
- Type
- Boolean
isString(variable) → {Boolean}
isString tests if variable is a String
Example
const ub = require('@nuskin/uncle-buck')
ub.isString('my string') // returns true
Parameters:
| Name | Type | Description |
|---|---|---|
variable |
* | value to test |
Returns:
Returns true if variable is a String
- Type
- Boolean
nvl(n, (*)) → {*}
nvl lets you replace null or undefined with a value of your choice. If n is
null or undefined, * then nvl returns val. If n is not null or undefined,
then nvl returns n.
Example
const ub = require('@nuskin/uncle-buck')
ub.nvl(null,0) // returns 0
ub.nvl('mystring','yourstring') // returns 'mystring'
Parameters:
| Name | Type | Description |
|---|---|---|
n |
* | The value you want to test for null or undefined |
(*) |
val - The value you want returned when |
Returns:
Returns val when n is null or undefined
- Type
- *
sleep(ms)
sleep will suspend execution for an interval amount of time that is set in milliseconds. The default is 1 second.
Example
const { sleep } = require('@nuskin/uncle-buck')
const myfunc = async () => {
await sleep(2000) // sleep for 2 seconds
// do something
}
myfunc()
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
ms |
Number |
1000
|
The number of milliseconds to sleep for |
stringToJSON(value) → {object}
stringToJSON will convert a JSON string to a JavaScript object and return it. If
value is already a JavaScript object, the object is simply returned. An error is thrown
if value is not parseable or a JavaScript object.
Example
const { stringToJSON } = require('@nuskin/uncle-buck')
const jsonStr = JSON.stringify({ a: 1, b: '2', c: null })
const obj = stringToJSON(jsonStr)
console.log(obj)
Parameters:
| Name | Type | Description |
|---|---|---|
value |
* | The value to evaluate |
Throws:
-
Thrown if
valueis not a valid JSON string or a JavaScript object. - Type
- Error
Returns:
The JavaScript object parsed from the JSON string.
- Type
- object