cockpit.js: Utilities

## cockpit.js: Utilities cockpit.js: Utilities — Various utility functions |

## cockpit.format()

    string = cockpit.format(template, args)
    string = cockpit.format(template, [arg, ...])

Format a string interpolating `args` into `template` using shell like syntax. The `args` may be either an array or javascript object. The `template` can contain fields that look like `$name` or `${name}` or `$0`. Numeric fields are used with array `args` and start at zero.

In the second form, multiple `arg` arguments may be passed directly, and interpolated as as numeric fields in the `template`.

## cockpit.format_bytes()

    string = cockpit.format_bytes(number, [factor])
    array = cockpit.format_bytes(number, [factor, separate])

Formats `number` into a displayable `string` with a suffix, such as KB or MB. Returns an `array` of the formatted number and the suffix if `separate` is set to `true`.

If specifying 1000 or 1024 is specified as a `factor` then an appropriate suffix will be chosen. By default the `factor` is 1024. You can pass a string suffix as a `factor` in which case the resulting number will be formatted with the same suffix.

If the `number` is less than the `factor` or an unknown factor was passed in, then the formatted number is returned without a suffix. If `separate` is true, returns an array of `[formatted_number, suffix]` or `[formatted_number]` if returned without a suffix.

If `number` is `null` or `undefined` an empty string or an array without a suffix will be returned.

## cockpit.format_number()

    string = cockpit.format_number(number)

Formats `number` into a displayable `string`. If the number is not an integer, it is rounded to a single decimal place precision. If the number is near zero, but not quite zero it is rounded up or down to a single decimal place.

If `number` is `null` or `undefined` an empty string will be returned.

## cockpit.format_bytes_per_sec()

     string = cockpit.format_bytes_per_sec(number, [factor])
     array = cockpit.format_bytes_per_sec(number, [factor, separate])

Format `number` of bytes into a displayable speed `string`.

If specifying 1000 or 1024 is specified as a `factor` then an appropriate suffix will be chosen. By default the `factor` is 1024. You can pass a string suffix as a `factor` in which case the resulting number will be formatted with the same suffix.

If the `number` is less than the `factor` or an unknown factor was passed in, then the formatted number is returned without a suffix. If `separate` is true, returns an array of `[formatted_number, suffix]` or `[formatted_number]` if returned without a suffix.

If `number` is `null` or `undefined` an empty string or array will be returned.

## cockpit.format_bits_per_sec()

      string = cockpit.format_bits_per_sec(number, [factor])
     array = cockpit.format_bytes_per_sec(number, [factor, separate])

Format `number` of bits into a displayable speed `string`.

If specifying 1000 or 1024 is specified as a `factor` then an appropriate suffix will be chosen. By default the `factor` is 1024. You can pass a string suffix as a `factor` in which case the resulting number will be formatted with the same suffix.

If the `number` is less than the `factor` or an unknown factor was passed in, then the formatted number is returned without a suffix. If `separate` is true, returns an array of `[formatted_number, suffix]` or `[formatted_number]` if returned without a suffix.

If `number` is `null` or `undefined` an empty string or array will be returned.

## cockpit.info

    cockpit.info["version"]
    cockpit.info["build"]

This object contains information about cockpit itself. Note that when cockpit is running on multiple servers, this only reflects the server that was connected to. The following fields are defined:

`"build"` | A string containing build details.
`"version"` | A string containing the cockpit version number. It is almost always incorrect to use this to make a decision in code.

## cockpit.event_target

    cockpit.event_target(object, [handlers])

Adds an EventTarget implementation to the `object`. Optionally store the handlers in `handlers` if its specified.

## cockpit.extend

    cockpit.extend(target, src, ...)

This function is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. This is similar to `Object.assign()`.