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
// 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);
Converts a value into a boolean true or false.
mikroformat.toBoolean(0); // false
Formats a string into a camel-case representation, i.e. helloWorld
.
mikroformat.toCamelCase('Hello there!'); // "helloThere!"
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 €"
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"
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
Formats a number or string as a whole integer.
mikroformat.toInteger(1672.47182); // 1672
Formats a string representation into JSON.
mikroformat.toJSON('{"abc":123,"foo":{"bar":"qwerty"}}'); // { abc: 123, foo: { bar: 'qwerty' } }
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%"
Formats a string into a slugified representation, i.e. hello-world
.
mikroformat.toSlug('New library simplifies formatting'); // "new-library-simplifies-formatting"
Formats a string into a snake-case representation, i.e. hello_world
.
mikroformat.toSnakeCase('Some method name'); // "some_method_name"
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"}}'
Formats a string into a title-case representation, i.e. Hello World
.
mikroformat.toTitleCase('formatting: a new theory'); // "Formatting: A New Theory"
MIT. See LICENSE
file.