mikroformat
v1.0.3
Published
MikroFormat helps you convert and format between different formats
Downloads
11
Maintainers
Readme
mikroformat
MikroFormat helps you convert and format between different formats.
MikroFormat helps you convert and format between different formats, for example to JSON, to various types of dates, to currencies and numbers, or different string casings.
- Simple API
- Tiny (~1.4 KB gzipped)
- Zero dependencies
- Has 100% test coverage
Usage
Basic importing and usage
// ES5 format
const { MikroFormat } = require('mikroformat');
// ES6 format
import { MikroFormat } from 'mikroformat';
const mikroformat = new MikroFormat();
const formatted = mikroformat.toBoolean(0);
console.log('Formatted value:', formatted);
Features
To boolean
Converts a value into a boolean true or false.
mikroformat.toBoolean(0); // false
To camel case
Formats a string into a camel-case representation, i.e. helloWorld
.
mikroformat.toCamelCase('Hello there!'); // "helloThere!"
To currency
Outputs a currency string that is accurately formatted to the locale and currency provided.
mikroformat.toCurrency({
value: 24837.731,
precision: 8,
locale: 'sv-SE',
currency: 'EUR'
}); // "24 837,731 €"
To date
Converts one type of date into another.
The available styles are:
date
: A basicYYYY-MM-DD
format, e.g.2024-02-29
iso
: ISO-8601 format, e.g.2024-03-13T13:02:40.000Z
unix
: A Unix timestamp, e.g.1710334960000
utc
: Universal Time Coordinated (RFC 7231) format, e.g.Wed, 13 Mar 2024 13:02:40 GMT
mikroformat.toDate(1710334960000, 'utc'); // "Wed, 13 Mar 2024 13:02:40 GMT"
To decimal
Formats a number or string as a decimal number.
You may provide a desired level of precision for the decimals.
mikroformat.toDecimal(123.129631586528, 8); // Result: 123.12963159
To integer
Formats a number or string as a whole integer.
mikroformat.toInteger(1672.47182); // 1672
To JSON
Formats a string representation into JSON.
mikroformat.toJSON('{"abc":123,"foo":{"bar":"qwerty"}}'); // { abc: 123, foo: { bar: 'qwerty' } }
To percent
Formats a number or string into a percentage representation (string).
You may provide a desired level of precision for the decimals.
mikroformat.toPercent(72.5); // "72.5%"
To slug
Formats a string into a slugified representation, i.e. hello-world
.
mikroformat.toSlug('New library simplifies formatting'); // "new-library-simplifies-formatting"
To snake case
Formats a string into a snake-case representation, i.e. hello_world
.
mikroformat.toSnakeCase('Some method name'); // "some_method_name"
To string
Formats a number, string, or object into a string.
Undefined or null values will become empty strings.
mikroformat.toString(123); // "123"
mikroformat.toString({ abc: 123, foo: { bar: 'qwerty' } }; // Result: '{"abc":123,"foo":{"bar":"qwerty"}}'
To title case
Formats a string into a title-case representation, i.e. Hello World
.
mikroformat.toTitleCase('formatting: a new theory'); // "Formatting: A New Theory"
To a normalized value
Returns an input as a normalized value, for example, mapping true
to Is employee
.
The mapping is provided as an array of values, which may be of string, boolean, number, or regular expression types.
const schema = {
'Is employee': ['true', true, 1, /^(yes|employee)$/],
'Contractor': ['false', false, 0, /^(no|consultant)$/],
};
mikroformat.toNormalized('yes', schema); // 'Is employee'
mikroformat.toNormalized(false, schema); // 'Contractor'
Optionally, the noMatchHandling
parameter may be set to either keep
(default) or drop
: keep
will return the input value if no match is found, while drop
will return an empty string.
Also optionally, a replacementValue
may be set. If noMatchHandling
is set to keep
, then any unmatched input will be replaced with the provided replacement value.
// Use explicit noMatchHandling params
mikroformat.toNormalized(123, schema, 'keep'); // 123
mikroformat.toNormalized(123, schema, 'drop'); // ''
// Setting a custom replacement value
mikroformat.toNormalized(123, schema, 'keep', 'Did not match'); // 'Did not match'
License
MIT. See LICENSE
file.