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

convert-length

v1.0.1

Published

Converts a distance unit (e.g. m) to another (e.g. cm)

Downloads

4,609

Readme

convert-length

A simple utility to convert from physical lengths (meters, inches, etc) to pixels and back, based on the CSS spec. Supports converting to and from the following units:

  • mm (millimeters)
  • cm (centimeters)
  • m (meters)
  • pc (pica, or 1 / 6 of an inch)
  • pt (point, or 1 / 72 of an inch)
  • in (inch)
  • ft (feet, or 12 inches)
  • px (pixels)

Example:

const convert = require('convert-length');

// Convert 10 inches to meters
const result = convert(10, 'in', 'm')
// -> 0.254

// Convert A4 print (210 x 297 mm) to pixels @ 300 PPI
const result = [ 210, 297 ].map(n => {
  return convert(n, 'mm', 'px', { pixelsPerInch: 300 })
});
// -> [ 2480, 3508 ]

Pixels are computed based on the specified pixelsPerInch setting (default 96), as per the CSS spec.

Install

Use npm to install and use this. Should work with browserify, Webpack, etc.

npm install convert-length

Usage

result = convert(value, fromUnit, toUnit, [options])

Converts the value number from the fromUnit unit string (e.g. "in") to the toUnit unit string. The unit strings are case insensitive.

Options can be:

  • pixelsPerInch (default 96) the number of pixels in one inch, used when converting to and from "px" units
  • precision if specified, the value will be rounded to the Nth decimal. e.g. A precision of 3 will round to 0.001. If not specified, the result will not be rounded.
  • roundPixel (default true) If enabled, when converting to a "px" unit the return value will be rounded to a whole pixel. If disabled, the conversion will instead round to the specified Nth precision decimal (or no rounding if precision is not specified).

convert.units

The list of supported units for this module, equivalent to:

[ 'mm', 'cm', 'm', 'pc', 'pt', 'in', 'ft', 'px' ]

See Also

This module was inspired by measures and convert-units, but I wanted something dead-simple for the browser, without all the extra features, and that supports pixels in the same way CSS and Photoshop handle their conversions.

License

MIT, see LICENSE.md for details.