All Projects → jonahsnider → convert

jonahsnider / convert

Licence: MIT license
The smallest & fastest library for really easy, totally type-safe unit conversions in TypeScript & JavaScript.

Programming Languages

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

Projects that are alternatives of or similar to convert

units
A run-time C++ library for working with units of measurement and conversions between them and with string representations of units and measurements
Stars: ✭ 114 (+142.55%)
Mutual labels:  conversion, unit-conversions, si-units
physikal
Mirror of Gitlab Repository
Stars: ✭ 33 (-29.79%)
Mutual labels:  conversion, uom
Length.js
📏 JavaScript library for length units conversion.
Stars: ✭ 292 (+521.28%)
Mutual labels:  convert, conversion
qTsConverter
A simple tool to convert qt translation file (ts) to other format (xlsx / csv) and vice versa
Stars: ✭ 26 (-44.68%)
Mutual labels:  convert, conversion
Js Quantities
JavaScript library for quantity calculation and unit conversion
Stars: ✭ 335 (+612.77%)
Mutual labels:  convert, conversion
Timezone Support
Lightweight time zone support for your applications or other date libraries.
Stars: ✭ 90 (+91.49%)
Mutual labels:  convert, conversion
php-unit-conversion
A library providing full PSR-4 compatible unit conversions
Stars: ✭ 47 (+0%)
Mutual labels:  conversion, metric
Convert Units
An elegant way to convert quantities between different units.
Stars: ✭ 480 (+921.28%)
Mutual labels:  convert, conversion
WSHModule
WSHModule is a JavaScript virtual machine built in WSH/JScript.
Stars: ✭ 28 (-40.43%)
Mutual labels:  commonjs, umd
Units Converter
A simple utility library to measure and convert between units
Stars: ✭ 31 (-34.04%)
Mutual labels:  commonjs, conversion
OpenConvert
Text conversion tool (from e.g. Word, HTML, txt) to corpus formats TEI or FoLiA)
Stars: ✭ 20 (-57.45%)
Mutual labels:  conversion
biguint-format
Node.js module to format big uint numbers from a byte array or a Buffer
Stars: ✭ 16 (-65.96%)
Mutual labels:  conversion
PTXQC
A Quality Control (QC) pipeline for Proteomics (PTX) results generated by MaxQuant
Stars: ✭ 34 (-27.66%)
Mutual labels:  metric
aushape
A library and a tool for converting audit logs to XML and JSON
Stars: ✭ 37 (-21.28%)
Mutual labels:  convert
embedded-time
Time(ing) library (Instant/Duration/Clock/Timer/Period/Frequency) for bare-metal embedded systems
Stars: ✭ 72 (+53.19%)
Mutual labels:  conversion
hcv-color
🌈 Color model HCV/HCG is an alternative to HSV and HSL, derived by Munsell color system, usable for Dark and Light themes... 🌈
Stars: ✭ 44 (-6.38%)
Mutual labels:  convert
podhd
PODHD Preset Tool -- Modify And Convert Line6 POD HD Presets, Setlists And Bundles.
Stars: ✭ 37 (-21.28%)
Mutual labels:  convert
BlocksConverter
A PocketMine-MP plugin allows you to convert Minecraft PC maps to MCPE/Bedrock maps or vice-versa.
Stars: ✭ 47 (+0%)
Mutual labels:  conversion
UnimelbSharedFiles
No description or website provided.
Stars: ✭ 16 (-65.96%)
Mutual labels:  uom
marc2bibframe2
Convert MARC records to BIBFRAME2 RDF
Stars: ✭ 72 (+53.19%)
Mutual labels:  conversion

Convert

The smallest & fastest library for really easy, totally type-safe unit conversions in TypeScript & JavaScript.

published npm version test coverage bundle size npm monthly downloads type definitions license CI

npm install convert
# or
yarn add convert

More installation examples below, including browser builds.

convert(5, 'miles').to('km');
convertMany('4d 16h').to('minutes');

Features

  • Full build time and runtime validation of conversions
  • Using a web framework like Next.js or Nuxt.js? You get 0-cost build-time conversions. Convert is totally side-effect free, so conversions will be precalculated at build-time, so absolutely zero conversion code is sent to clients!
  • Works in browsers and Node.js (UMD and ESM builds will work anywhere)
  • Out of the box ES3 backwards-compatibility (CI tests on Node.js v0.9.1)
  • Absolutely tiny bundle size and 0 dependencies
  • Supports bigints without breaking on old engines

Usage

Generated API documentation for the latest version is available online.

View docs.

// ESM:
import convert from 'convert';
// CJS:
const {convert} = require('convert');

// 360 seconds into minutes
convert(360, 'seconds').to('minutes');
// -> 6

// BigInt support
convert(20n, 'hours').to('minutes');
// -> 1200n

// Format to the best unit automatically
convert(5500, 'meters').to('best');
// -> { quantity: 5.5, unit: 'km', toString: () => '5.5km' }

// We also do length, data, volume, mass, temperature, and more
convert(5, 'kilometers').to('nautical miles');
convert(12, 'pounds').to('ounces');
convert(8192, 'bytes').to('KiB');
convert(10, 'atmospheres').to('kPa');
convert(451, 'fahrenheit').to('celsius');

Converting many units

import {convertMany} from 'convert';
const {convertMany} = require('convert');

// Convert 1 day and 8 hours into ms
convertMany('1d8h').to('ms');

Converting to best unit

import convert from 'convert';
const {convert} = require('convert');

// Convert into the best unit
const duration = convert(36, 'h').to('best');
// -> { quantity: 1.5, unit: 'd', toString: () => '1.5d' }

// The toString() method means you can automatically cast the object to a string without any issues
'duration is ' + duration;
// -> duration is 1.5d

// You can also specify to use a specific kind of units (metric or imperial, metric is default)
convert(3.5, 'km').to('best'); // -> { quantity: 3.5, unit: 'km', toString: () => '3.5km' }
convert(3.5, 'km').to('best', 'metric'); // -> { quantity: 3.5, unit: 'km', toString: () => '3.5km' }
convert(3.5, 'km').to('best', 'imperial'); // -> { quantity: 2.17, unit: 'mi', toString: () => '3.5mi' }

ms shorthand

import {ms} from 'convert';
const {ms} = require('convert');

// Convert a duration into milliseconds
ms('1d 2h 30min');
// -> 95400000

// Convert milliseconds to a string
ms(86400000);
// -> '1d'

Installation

Package manager

Convert is published as convert on npm.

npm install convert
# or
yarn add convert

CommonJS

// This chooses which build to use depending on NODE_ENV
const {convert} = require('convert');

// You can also specify which build to use
const {convert} = require('convert/dev');
const {convert} = require('convert/prod');

ES Modules

// The production build is the default
import convert from 'convert';

// ESM does not have automatic build switching, you must explicitly import the dev build
import convert from 'convert/dev';
import convert from 'convert/prod';

Browsers

Pick your favorite CDN:

Modules

<script type="module">
	import convert from 'https://cdn.skypack.dev/convert@4';
	import convert from 'https://esm.run/convert@4';
	import convert from 'https://cdn.jsdelivr.net/npm/convert@4';
	import convert from 'https://unpkg.com/convert@4';
</script>

UMD (global)

<script src="https://cdn.jsdelivr.net/npm/convert@4/dist/convert.prod.js"></script>
<script src="https://unpkg.com/convert@4/dist/convert.prod.js"></script>

Alternatives

Convert is better than other unit conversion libraries because it's faster and smaller than them, while having the same features. Benchmarks of popular unit conversion libraries, including Convert are available here.

Convert is the fastest, taking less than a microsecond for all functions. That's a little over 3 million convert() calls per second.

Bundle size comparison

npm bundle size of convert

npm bundle size of safe-units

npm bundle size of convert-units

npm bundle size of js-quantities

npm bundle size of uom + npm bundle size of uom-units

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].