@svizzle/utils/any-any

Methods

(static) makeEmptyArrayIfUndefined(any) → {*|array}

Source:
Since:
  • 0.1.0

Return an empty array if the input is undefined or identity otherwise.

Example
> makeEmptyArrayIfUndefined(undefined)
[]
> makeEmptyArrayIfUndefined([1, 2, 3])
[1, 2, 3]
> makeEmptyArrayIfUndefined({a: 1})
{a: 1}
> makeEmptyArrayIfUndefined('a')
'a'
Parameters:
Name Type Description
any *
Returns:
Type
* | array

(static) sanitize(any) → {*}

Source:
Since:
  • 0.17.0

Return a copy of the input after stringifying it and parsing it. This can be useful to check equality of json variables with json objects obtained from a file, because e.g. stringifying strips undefined values.

Example
> actual = {a: 1, b: undefined}
{a: 1, b: undefined}
>
> // note how `JSON.stringify` works
> JSON.stringify(actual)
'{"a":1}'
>
> expectedFromFile = {a: 1}
> assert.deepStrictEqual(actual, expectedFromFile)
Uncaught AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected

  {
    a: 1,
+   b: undefined
  }
>
> sanitizedObj = sanitize(actual)
> assert.deepStrictEqual(sanitizedObj, expectedFromFile)
undefined

> array = [1, undefined]
> sanitize(array)
[1, null]
Parameters:
Name Type Description
any *
Returns:

any

Type
*

(static) toFloatOrIdentity(any) → {number|*}

Source:
Since:
  • 0.1.0

Return a number if the input can be converted to float, identity otherwise

Example
> toFloatOrIdentity('2')
2
> toFloatOrIdentity('2px')
2
> toFloatOrIdentity('')
''
> toFloatOrIdentity('h2o')
'h2o'
> toFloatOrIdentity([1.1])
1.1
> toFloatOrIdentity([1.1, 2])
1.1
> toFloatOrIdentity([1.1, 2, 3])
1.1
> toFloatOrIdentity([])
[]
> toFloatOrIdentity({a: 1})
{a: 1}
> toFloatOrIdentity(true)
true
> toFloatOrIdentity(null)
null
> toFloatOrIdentity(undefined)
undefined
Parameters:
Name Type Description
any *
Returns:
Type
number | *