Skip to content

convert

toArray

Signature

js
convert.toArray (obj)

Description

Converts a plain object into an array of its values, discarding the keys. Thin wrapper around _.toArray.

Parameters

NameTypeRequiredDescription
objobjectyesThe plain object to convert

Returns

TypeDescription
ArrayAn array of the object's own enumerable values, in insertion order

Examples

js
convert.toArray({ a: 1, b: 2, c: 3 })
// => [1, 2, 3]
js
// throws TypeError: 'obj must be an object'
convert.toArray([1, 2, 3])

toObjects

Signature

js
convert.toObjects (obj, keys)

Description

Converts an array of arrays into an array of plain objects by zipping each inner array with the provided keys. Each inner array becomes an object whose properties are named after the corresponding key.

Parameters

NameTypeRequiredDescription
objArrayyesAn array of arrays, where each inner array holds the values for one object
keysArrayyesA non-empty array of strings used as property names

Returns

TypeDescription
ArrayAn array of plain objects, one per inner array

Examples

js
convert.toObjects([['Alice', 25], ['Bob', 17]], ['name', 'age'])
// => [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 17 }]
js
// throws TypeError: 'obj must be an array'
convert.toObjects({ name: 'Alice' }, ['name'])

// throws TypeError: 'keys must be a non empty array'
convert.toObjects([['Alice', 25]], [])

toValue

Signature

js
convert.toValue (value, units)

Description

Converts a scalar value according to a units descriptor. Supports four modes, selected by the properties present on units:

  • Date conversion (units.asDate) — parses value as a date using moment, optionally from a source format (units.from), and returns either a formatted string (units.to) or a native Date object.
  • String conversion (units.asString) — calls value.toString(), optionally with a radix when units.asString is a number.
  • Number conversion (units.asNumber) — coerces value to a number, removing spaces first when the value is a string.
  • Physical unit conversion (default) — converts value from the unit units.from to the unit units.to using mathjs.

Parameters

NameTypeRequiredDescription
value*yesThe value to convert
unitsobjectyesDescriptor object controlling the conversion mode
units.asDatestringnoSet to 'utc' for UTC parsing, or any truthy string for local time parsing
units.asStringboolean | numbernoSet to true to convert to string, or a number to use as radix
units.asNumberbooleannoSet to true to coerce value to a number
units.fromstringnoSource unit or date format
units.tostringnoTarget unit or date output format

Returns

TypeDescription
Date | string | numberThe converted value, whose type depends on the active conversion mode

Examples

js
// Physical unit conversion
convert.toValue(1, { from: 'km', to: 'm' })
// => 1000

// Date conversion to a formatted string
convert.toValue('2024-01-15', { asDate: true, to: 'DD/MM/YYYY' })
// => '15/01/2024'

// Date conversion in UTC to a native Date object
convert.toValue('2024-01-15T00:00:00Z', { asDate: 'utc' })
// => Date object

// Date conversion from a custom input format
convert.toValue('15/01/2024', { asDate: true, from: 'DD/MM/YYYY', to: 'YYYY-MM-DD' })
// => '2024-01-15'

// Number to binary string
convert.toValue(255, { asString: 2 })
// => 'ff'

// String with spaces to number
convert.toValue('1 234', { asNumber: true })
// => 1234