npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mikroformat

v1.0.3

Published

MikroFormat helps you convert and format between different formats

Downloads

11

Readme

mikroformat

MikroFormat helps you convert and format between different formats.

Build Status

Quality Gate Status

codecov

Maintainability


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 basic YYYY-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.