Qwiery Utils
The Qwiery Utils package source contains a cornucopia of utility functions which are used across Qwiery but also have on their owm great value.
The following (static) classes are available:
Utils
import { Utils } from "@orbifold/utils";amountToMoneyFormat
Returns a money format or an empty string if the conversion failed. If no currency is specified, USD will be used as a default.
Utils.amountToMoneyFormat(12, "EUR") // €12.00
Utils.amountToMoneyFormat(10) // US$10.00The full list of currency codes can be found in various places, see e.g. this ISO-4217.
boolToYesNo
Turns the given input into "Yes" or "No". It also accepts things like 'yep', 'nah', 'nope' and so on.
Utils.boolToYesNo(true); // Yes
Utils.boolToYesNo(false); // No
Utils.boolToYesNo("nope"); // No
Utils.boolToYesNo("duh"); // YescamelTypeToTitle
Turns a camel-cased string into a title.
Utils.camelTypeToTitle("itWorks"); // It Works
Utils.camelTypeToTitle("thisTime"); // This TimeDate
DateString
Durations
Predecessors
Strings
Time
Colors
Part of the Qwiery Utils package, the Colors class provides a set of color-related utilities.
Install with:
npm install @orbifold/utilsand import in your module with:
import {Colors} from "@orbifold/utils";NamedColor
This is a dictionary of the classic predefined (Web) CSS colors and returns the hex value for a given name.
Colors.NamedColor["red"]; // #FF0000
Colors.NamedColor["papayawhip"]; // #FFEFD5randomKnownColor
Returns a random color name from the NamedColor set.
Colors.randomKnownColor(); // e.g. 'papayawhip'You can turn this into a hex value by passing true as the first argument.
Colors.randomKnownColor(true); // e.g. '#FFEFD5'Parameters
hexValue(optional, defaultfalse)
Returns string;
randomKnownColors
Returns an array of random colors from the NamedColor set.
Colors.randomKnownColors(3); // e.g. ['papayawhip', 'red', 'blue']You can turn this into an array of hex values by passing true as the first argument.
Colors.randomKnownColors(3, true); // e.g. ['#FFEFD5', '#FF0000', '#0000FF']Parameters
count(optional, default10) How many colors to return.hexValue(optional, defaultfalse) Whether to return hex values.
Returns string[];
getKnownColorFromHex
Returns the name of the given hex value if it matches a named color.
Colors.getKnownColorFromHex("#FF0000"); // red
Colors.getKnownColorFromHex("#FFEFD5"); // papayawhipParameters
hexstring {string} An hex color.
Returns string | null
hex2rgb
Converts the hex color to rgb (as a 3-array).
Colors.hex2rgb("#FF0000"); // [255, 0, 0]Parameters
hex{string} A hex color.
Returns number[] | null
rgb2hex
Converts the given RGB to a hex color.
Colors.rgb2hex(255, 0, 0); // #FF0000Parameters
argsnumber[] A 3-array representing RGB.
Returns string
rgb2hsl
Converts an RGB color value to HSL. Conversion formula adapted from this reference. Assumes r, g, and b are contained in the set [0, 255] and returns h, s, and l in the set [0, 1].
Colors.rgb2hsl(255, 0, 0); // [0, 1, 0.5]Parameters
args...number
hsl2rgb
Converts an HSL color value to RGB. Conversion formula adapted from this reference. Assumes h, s, and l are contained in the set [0, 1] and returns r, g, and b in the set [0, 255].
Parameters
args...any
