## Usage
npm install @allbin/output-helpersimport oh from output-helpers
Optionally use oh.config(config_object) to configure default languages and setup translation dictionaries.
oh.addDictionary(dictionary): undefinedAdd additional dictionaries for the translate function to use for lookups.
> See Dictionary definition.
oh.dateToMoment(date, utc): MomentConverts a javascript date to a Moment.
> date - The input javascript date to be converted.
> utc - Instruct moment to use utc parsing.
oh.format(value, options): string> value - The value to be formatted.
> options - An object with optional parameters:
>
>round
>ceil
>floor
>integer_padding
>decimal_padding
>padding
>trunc
>grouping
Ex: oh.format(1354.342, { round: 1, grouping: true }) > "1 350".
oh.formatDateAsString(date, output_format, input_format, utc): stringFormats a given date according to output_format. Formats are specified according to Moment standard.
> date - The date to be formatted.
> output_format - The output format.
> input_format - If input date is a string optionally a input format can be specified for Moments parser.
> utc - Instruct moment to use utc parsing.
oh.formatDateAsTimeString(date, input_format, utc): stringFormats given date as HH:mm. Equivalent to oh.formatDateAsString(date, "HH:mm").
> date - Date to be formatted.
> input_format - If input date is a string optionally a input format can be specified for Moments parser.
> utc - Instruct moment to use utc parsing.
oh.formatSecondsToMS(value, always_include, padding, colon): stringConverts a value (seconds) to a [m]m[s]s format: 1m2s.
> value - Number of seconds to be formatted.
> always_include <"none"|"minutes"|"seconds"|"both", default "none"> - Optional specifyer for which parts to always include.
> none - If not enough for a minute only returns seconds, if an exact minute the seconds will not be included.
> minutes - If value not above 60 "0m" will be prepended to the seconds.
> seconds - If value is exact minute "0s" will be appended after the minute.
> both - Will always prepend and append "0m" and "0s" respectively as needed.
> padding - Toggles if values should be padded so 4 becomes 04. Both minutes and seconds will be padded. Defaults to false.
> colon - Changes the output format to be "mm:ss" instead of "[m]m[s]s". Will always include both minutes and seconds, always padding. Defaults to false.
oh.getCurrentConfig(): OutputHelpers config objectoh.getDateLocale(language): Moment locale objectReturns a Moment locale object for the given language. This is used with Moments moment.defineLocale function to add locale support for calendars etc.
NOTE: OH will automatically add all available locales to moment! You only need to use this if you are running a separate moment instance or to change the definition and re-define the locale with moment manually.
> language <"sv-SE"|"en-US", defaults to configured language> - The language for which to return the locale specifications.
oh.getFallbackLang(): "sv-SE" | "en-US"Returns the currently configured fallback language used by oh.translate when primary language is not found.
oh.getLang(): "sv-SE" | "en-US"Returns the currently configured language used by oh.translate.
oh.roundDownTo(value, power): numberReturns value rounded down to the power.
> value - The value to be rounded.
> power - The power to round to.
Ex: roundDownTo(1345.341, 1) > 1340
oh.roundTo(value, ???): numberReturns value rounded to the power.
> value - The value to be rounded.
> power - The power to round to.
Ex: roundTo(1345.341, 1) > 1350
oh.roundUpTo(value, ???): numberReturns value rounded to the power.
> value - The value to be rounded.
> power - The power to round to.
Ex: roundUpTo(1345.341, 2) > 1400
oh.setConfig(config_options): undefinedSets the config options. Should be an object with any of the following settings.
date_locale <"sv-SE"|"en-US", default same as lang> - Specifies the locale used by Moment. fallback_language <"sv-SE"|"en-US", default "en-US"> - Fallback used by translate function if lang is not available. lang <"sv-SE"|"en-US", default "en-US"> - Default language used by translate function. extend_with - An object to extend the OH object with. Use this to attach additional dynamic functionality to OH.
oh.translate(key, capitalize, language, empty_on_error, dictionary): stringLooks up key in dictionary for language and returns specified value.
Will use the configured language and dictionaries unless specified.
>key - The key to look for in dictionary.
>capitalize
>language <"sv-SE"|"en-US", default to configured language> - Language to check for in dictionary.
>empty_on_error - When true an empty string will be returned when the requested key or language is not found. When false the key is returned.
>NOTE: Does not affect error logging.
>dictionary - See the oh.addDictionary() docs.
oh.translateTyped(key, capitalize, language, empty_on_error, dictionary): string Equivalent to oh.translate but takes in a Type T with which to match the key, eg: key keyof T. If the key is not present in T it will not allow compilation.
Optionally a prefix key can also be supplied, in which case it will be used to prefix every key in the dictionary to avoid collissions with other dictionaries keys.
NOTE: You will have to prefix all keys in your invokations of oh.translate with the same value. This can be done by wrapping the oh.translate function in a separate function which automatically adds the prefix.
{
sv-SE: {
email: "epost",
name: "namn",
...
},
en-US: {
email: "email",
name: "name",
...
},
prefix: "my-prefix"
}
`$3
`
function translate(str) {
return oh.translate(prefix + str);
}
``