All Projects β†’ vercel β†’ Ms

vercel / Ms

Licence: mit
Tiny millisecond conversion utility

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
shell
77523 projects

Projects that are alternatives of or similar to Ms

Util
A collection of useful utility functions
Stars: ✭ 201 (-94.66%)
Mutual labels:  utility, conversion
Length.js
πŸ“ JavaScript library for length units conversion.
Stars: ✭ 292 (-92.24%)
Mutual labels:  utility, conversion
embedded-time
Time(ing) library (Instant/Duration/Clock/Timer/Period/Frequency) for bare-metal embedded systems
Stars: ✭ 72 (-98.09%)
Mutual labels:  conversion, milliseconds
Remarshal
Convert between CBOR, JSON, MessagePack, TOML, and YAML
Stars: ✭ 421 (-88.81%)
Mutual labels:  utility, conversion
fastnumbers
Super-fast and clean conversions to numbers.
Stars: ✭ 85 (-97.74%)
Mutual labels:  utility, conversion
Senparc.co2net
ζ”―ζŒ .NET Framework & .NET Core ηš„ε…¬ε…±εŸΊη‘€ζ‰©ε±•εΊ“
Stars: ✭ 289 (-92.32%)
Mutual labels:  utility
React Lodash
βš›οΈ πŸ”§ Lodash as React components
Stars: ✭ 306 (-91.87%)
Mutual labels:  utility
Tooltip Sequence
A simple step by step tooltip helper for any site
Stars: ✭ 287 (-92.37%)
Mutual labels:  utility
Lobash
A modern, safe, powerful utility library for Bash script development.
Stars: ✭ 280 (-92.56%)
Mutual labels:  utility
Mbpmid2010 gpufix
MBPMid2010_GPUFix is an utility program that allows to fix MacBook Pro (15-inch, Mid 2010) intermittent black screen or loss of video. The algorithm is based on a solution provided by user fabioroberto on MacRumors forums.
Stars: ✭ 334 (-91.12%)
Mutual labels:  utility
Haxor News
Browse Hacker News like a haxor: A Hacker News command line interface (CLI).
Stars: ✭ 3,342 (-11.16%)
Mutual labels:  utility
Cryptocmd
Cryptocurrency historical price data library in Python. Data from https://coinmarketcap.com.
Stars: ✭ 299 (-92.05%)
Mutual labels:  utility
Encryptpad
Minimalist secure text editor and binary encryptor that implements RFC 4880 Open PGP format: symmetrically encrypted, compressed and integrity protected. The editor can protect files with passwords, key files or both.
Stars: ✭ 305 (-91.89%)
Mutual labels:  utility
Quick Error
A rust-macro which makes errors easy to write
Stars: ✭ 287 (-92.37%)
Mutual labels:  conversion
Tiny Utf8
Unicode (UTF-8) capable std::string
Stars: ✭ 322 (-91.44%)
Mutual labels:  conversion
Nomino
Batch rename utility for developers
Stars: ✭ 282 (-92.5%)
Mutual labels:  utility
Mysqlconvertertool
A MySQL Converter Tool
Stars: ✭ 299 (-92.05%)
Mutual labels:  conversion
Org Wiki
Wiki for Emacs org-mode built on top of Emacs org-mode.
Stars: ✭ 319 (-91.52%)
Mutual labels:  utility
Cypress Vue Unit Test
A little helper to unit test Vue components in the Cypress.io E2E test runner
Stars: ✭ 298 (-92.08%)
Mutual labels:  utility
Color Diff
Implemets the CIEDE2000 color difference algorithm, conversion between RGB and lab color and mapping all colors in palette X to the closest color in palette Y based on the CIEDE2000 difference.
Stars: ✭ 296 (-92.13%)
Mutual labels:  conversion

ms

CI

Use this package to easily convert various time formats to milliseconds.

Examples

ms('2 days')  // 172800000
ms('1d')      // 86400000
ms('10h')     // 36000000
ms('2.5 hrs') // 9000000
ms('2h')      // 7200000
ms('1m')      // 60000
ms('5s')      // 5000
ms('1y')      // 31557600000
ms('100')     // 100
ms('-3 days') // -259200000
ms('-1h')     // -3600000
ms('-200')    // -200

Convert from Milliseconds

ms(60000)             // "1m"
ms(2 * 60000)         // "2m"
ms(-3 * 60000)        // "-3m"
ms(ms('10 hours'))    // "10h"

Time Format Written-Out

ms(60000, { long: true })             // "1 minute"
ms(2 * 60000, { long: true })         // "2 minutes"
ms(-3 * 60000, { long: true })        // "-3 minutes"
ms(ms('10 hours'), { long: true })    // "10 hours"

Features

  • Works both in Node.js and in the browser
  • If a number is supplied to ms, a string with a unit is returned
  • If a string that contains the number is supplied, it returns it as a number (e.g.: it returns 100 for '100')
  • If you pass a string with a number and a valid unit, the number of equivalent milliseconds is returned

TypeScript support

As of v3.0, this package includes TypeScript definitions.

For added safety, we're using Template Literal Types (added in TypeScript 4.1). This ensures that you don't accidentally pass ms values that it can't process.

This won't require you to do anything special in most situations, but you can also import the StringValue type from ms if you need to use it.

import ms, { StringValue } from 'ms';

// Using the exported type.
function example(value: StringValue) {
  ms(value);
}

// This function will only accept a string compatible with `ms`.
example('1 h');

In this example, we use a Type Assertion to coerce a string.

import ms, { StringValue } from 'ms';

// Type assertion with the exported type.
function example(value: string) {
  try {
    // A string could be "wider" than the values accepted by `ms`, so we assert
    // that our `value` is a `StringValue`.
    //
    // It's important to note that this can be dangerous (see below).
    ms(value as StringValue);
  } catch (error: Error) {
    // Handle any errors from invalid vaues.
    console.error(error);
  }
}

// This function will accept any string, which may result in a bug.
example('any value');

You may also create a custom Template Literal Type.

import ms from 'ms';

type OnlyDaysAndWeeks = `${number} ${'days' | 'weeks'}`;

// Using a custom Template Literal Type.
function example(value: OnlyDaysAndWeeks) {
  // The type of `value` is narrower than the values `ms` accepts, which is
  // safe to use without coercion.
  ms(value);
}

// This function will accept "# days" or "# weeks" only.
example('5.2 days');

Related Packages

  • ms.macro - Run ms as a macro at build-time.

Caught a Bug?

  1. Fork this repository to your own GitHub account and then clone it to your local device
  2. Link the package to the global module directory: npm link
  3. Within the module you want to test your local development instance of ms, just link it to the dependencies: npm link ms. Instead of the default one from npm, Node.js will now use your clone of ms!

As always, you can run the tests using: npm test

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].