├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── convert-length.js ├── package-lock.json ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | test 7 | test.js 8 | demo/ 9 | .npmignore 10 | LICENSE.md -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2017 Matt DesLauriers 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 20 | OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # convert-length 2 | 3 | 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: 4 | 5 | - `mm` (millimeters) 6 | - `cm` (centimeters) 7 | - `m` (meters) 8 | - `pc` (pica, or 1 / 6 of an inch) 9 | - `pt` (point, or 1 / 72 of an inch) 10 | - `in` (inch) 11 | - `ft` (feet, or 12 inches) 12 | - `px` (pixels) 13 | 14 | Example: 15 | 16 | ```js 17 | const convert = require('convert-length'); 18 | 19 | // Convert 10 inches to meters 20 | const result = convert(10, 'in', 'm') 21 | // -> 0.254 22 | 23 | // Convert A4 print (210 x 297 mm) to pixels @ 300 PPI 24 | const result = [ 210, 297 ].map(n => { 25 | return convert(n, 'mm', 'px', { pixelsPerInch: 300 }) 26 | }); 27 | // -> [ 2480, 3508 ] 28 | ``` 29 | 30 | Pixels are computed based on the specified `pixelsPerInch` setting (default 96), as per the CSS spec. 31 | 32 | ## Install 33 | 34 | Use [npm](https://npmjs.com/) to install and use this. Should work with browserify, Webpack, etc. 35 | 36 | ```sh 37 | npm install convert-length 38 | ``` 39 | 40 | ## Usage 41 | 42 | ### `result = convert(value, fromUnit, toUnit, [options])` 43 | 44 | Converts the `value` number from the `fromUnit` unit string (e.g. `"in"`) to the `toUnit` unit string. The unit strings are case insensitive. 45 | 46 | Options can be: 47 | 48 | - `pixelsPerInch` (default 96) the number of pixels in one inch, used when converting to and from `"px"` units 49 | - `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. 50 | - `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). 51 | 52 | ### `convert.units` 53 | 54 | The list of supported units for this module, equivalent to: 55 | 56 | ```js 57 | [ 'mm', 'cm', 'm', 'pc', 'pt', 'in', 'ft', 'px' ] 58 | ``` 59 | 60 | ## See Also 61 | 62 | This module was inspired by [measures](https://www.npmjs.com/package/measures) and [convert-units](https://www.npmjs.com/package/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. 63 | 64 | ## License 65 | 66 | MIT, see [LICENSE.md](http://github.com/mattdesl/convert-length/blob/master/LICENSE.md) for details. 67 | -------------------------------------------------------------------------------- /convert-length.js: -------------------------------------------------------------------------------- 1 | var defined = require('defined'); 2 | var units = [ 'mm', 'cm', 'm', 'pc', 'pt', 'in', 'ft', 'px' ]; 3 | 4 | var conversions = { 5 | // metric 6 | m: { 7 | system: 'metric', 8 | factor: 1 9 | }, 10 | cm: { 11 | system: 'metric', 12 | factor: 1 / 100 13 | }, 14 | mm: { 15 | system: 'metric', 16 | factor: 1 / 1000 17 | }, 18 | // imperial 19 | pt: { 20 | system: 'imperial', 21 | factor: 1 / 72 22 | }, 23 | pc: { 24 | system: 'imperial', 25 | factor: 1 / 6 26 | }, 27 | in: { 28 | system: 'imperial', 29 | factor: 1 30 | }, 31 | ft: { 32 | system: 'imperial', 33 | factor: 12 34 | } 35 | }; 36 | 37 | const anchors = { 38 | metric: { 39 | unit: 'm', 40 | ratio: 1 / 0.0254 41 | }, 42 | imperial: { 43 | unit: 'in', 44 | ratio: 0.0254 45 | } 46 | }; 47 | 48 | function round (value, decimals) { 49 | return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals); 50 | } 51 | 52 | function convertDistance (value, fromUnit, toUnit, opts) { 53 | if (typeof value !== 'number' || !isFinite(value)) throw new Error('Value must be a finite number'); 54 | if (!fromUnit || !toUnit) throw new Error('Must specify from and to units'); 55 | 56 | opts = opts || {}; 57 | var pixelsPerInch = defined(opts.pixelsPerInch, 96); 58 | var precision = opts.precision; 59 | var roundPixel = opts.roundPixel !== false; 60 | 61 | fromUnit = fromUnit.toLowerCase(); 62 | toUnit = toUnit.toLowerCase(); 63 | 64 | if (units.indexOf(fromUnit) === -1) throw new Error('Invalid from unit "' + fromUnit + '", must be one of: ' + units.join(', ')); 65 | if (units.indexOf(toUnit) === -1) throw new Error('Invalid from unit "' + toUnit + '", must be one of: ' + units.join(', ')); 66 | 67 | if (fromUnit === toUnit) { 68 | // We don't need to convert from A to B since they are the same already 69 | return value; 70 | } 71 | 72 | var toFactor = 1; 73 | var fromFactor = 1; 74 | var isToPixel = false; 75 | 76 | if (fromUnit === 'px') { 77 | fromFactor = 1 / pixelsPerInch; 78 | fromUnit = 'in'; 79 | } 80 | if (toUnit === 'px') { 81 | isToPixel = true; 82 | toFactor = pixelsPerInch; 83 | toUnit = 'in'; 84 | } 85 | 86 | var fromUnitData = conversions[fromUnit]; 87 | var toUnitData = conversions[toUnit]; 88 | 89 | // source to anchor inside source's system 90 | var anchor = value * fromUnitData.factor * fromFactor; 91 | 92 | // if systems differ, convert one to another 93 | if (fromUnitData.system !== toUnitData.system) { 94 | // regular 'm' to 'in' and so forth 95 | anchor *= anchors[fromUnitData.system].ratio; 96 | } 97 | 98 | var result = anchor / toUnitData.factor * toFactor; 99 | if (isToPixel && roundPixel) { 100 | result = Math.round(result); 101 | } else if (typeof precision === 'number' && isFinite(precision)) { 102 | result = round(result, precision); 103 | } 104 | return result; 105 | } 106 | 107 | module.exports = convertDistance; 108 | module.exports.units = units; 109 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "convert-length", 3 | "version": "1.0.1", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "balanced-match": { 8 | "version": "1.0.0", 9 | "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", 10 | "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", 11 | "dev": true 12 | }, 13 | "brace-expansion": { 14 | "version": "1.1.11", 15 | "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", 16 | "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", 17 | "dev": true, 18 | "requires": { 19 | "balanced-match": "^1.0.0", 20 | "concat-map": "0.0.1" 21 | } 22 | }, 23 | "concat-map": { 24 | "version": "0.0.1", 25 | "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", 26 | "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", 27 | "dev": true 28 | }, 29 | "deep-equal": { 30 | "version": "1.0.1", 31 | "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", 32 | "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=", 33 | "dev": true 34 | }, 35 | "define-properties": { 36 | "version": "1.1.2", 37 | "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.2.tgz", 38 | "integrity": "sha1-g6c/L+pWmJj7c3GTyPhzyvbUXJQ=", 39 | "dev": true, 40 | "requires": { 41 | "foreach": "^2.0.5", 42 | "object-keys": "^1.0.8" 43 | } 44 | }, 45 | "defined": { 46 | "version": "1.0.0", 47 | "resolved": "https://registry.npmjs.org/defined/-/defined-1.0.0.tgz", 48 | "integrity": "sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=" 49 | }, 50 | "es-abstract": { 51 | "version": "1.12.0", 52 | "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.12.0.tgz", 53 | "integrity": "sha512-C8Fx/0jFmV5IPoMOFPA9P9G5NtqW+4cOPit3MIuvR2t7Ag2K15EJTpxnHAYTzL+aYQJIESYeXZmDBfOBE1HcpA==", 54 | "dev": true, 55 | "requires": { 56 | "es-to-primitive": "^1.1.1", 57 | "function-bind": "^1.1.1", 58 | "has": "^1.0.1", 59 | "is-callable": "^1.1.3", 60 | "is-regex": "^1.0.4" 61 | } 62 | }, 63 | "es-to-primitive": { 64 | "version": "1.1.1", 65 | "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.1.1.tgz", 66 | "integrity": "sha1-RTVSSKiJeQNLZ5Lhm7gfK3l13Q0=", 67 | "dev": true, 68 | "requires": { 69 | "is-callable": "^1.1.1", 70 | "is-date-object": "^1.0.1", 71 | "is-symbol": "^1.0.1" 72 | } 73 | }, 74 | "for-each": { 75 | "version": "0.3.3", 76 | "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", 77 | "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", 78 | "dev": true, 79 | "requires": { 80 | "is-callable": "^1.1.3" 81 | } 82 | }, 83 | "foreach": { 84 | "version": "2.0.5", 85 | "resolved": "https://registry.npmjs.org/foreach/-/foreach-2.0.5.tgz", 86 | "integrity": "sha1-C+4AUBiusmDQo6865ljdATbsG5k=", 87 | "dev": true 88 | }, 89 | "fs.realpath": { 90 | "version": "1.0.0", 91 | "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", 92 | "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", 93 | "dev": true 94 | }, 95 | "function-bind": { 96 | "version": "1.1.1", 97 | "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", 98 | "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", 99 | "dev": true 100 | }, 101 | "glob": { 102 | "version": "7.1.2", 103 | "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", 104 | "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", 105 | "dev": true, 106 | "requires": { 107 | "fs.realpath": "^1.0.0", 108 | "inflight": "^1.0.4", 109 | "inherits": "2", 110 | "minimatch": "^3.0.4", 111 | "once": "^1.3.0", 112 | "path-is-absolute": "^1.0.0" 113 | } 114 | }, 115 | "has": { 116 | "version": "1.0.3", 117 | "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", 118 | "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", 119 | "dev": true, 120 | "requires": { 121 | "function-bind": "^1.1.1" 122 | } 123 | }, 124 | "inflight": { 125 | "version": "1.0.6", 126 | "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", 127 | "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", 128 | "dev": true, 129 | "requires": { 130 | "once": "^1.3.0", 131 | "wrappy": "1" 132 | } 133 | }, 134 | "inherits": { 135 | "version": "2.0.3", 136 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", 137 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", 138 | "dev": true 139 | }, 140 | "is-callable": { 141 | "version": "1.1.4", 142 | "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz", 143 | "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==", 144 | "dev": true 145 | }, 146 | "is-date-object": { 147 | "version": "1.0.1", 148 | "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", 149 | "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", 150 | "dev": true 151 | }, 152 | "is-regex": { 153 | "version": "1.0.4", 154 | "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", 155 | "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", 156 | "dev": true, 157 | "requires": { 158 | "has": "^1.0.1" 159 | } 160 | }, 161 | "is-symbol": { 162 | "version": "1.0.1", 163 | "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.1.tgz", 164 | "integrity": "sha1-PMWfAAJRlLarLjjbrmaJJWtmBXI=", 165 | "dev": true 166 | }, 167 | "minimatch": { 168 | "version": "3.0.4", 169 | "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", 170 | "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", 171 | "dev": true, 172 | "requires": { 173 | "brace-expansion": "^1.1.7" 174 | } 175 | }, 176 | "minimist": { 177 | "version": "1.2.0", 178 | "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", 179 | "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", 180 | "dev": true 181 | }, 182 | "object-inspect": { 183 | "version": "1.6.0", 184 | "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.6.0.tgz", 185 | "integrity": "sha512-GJzfBZ6DgDAmnuaM3104jR4s1Myxr3Y3zfIyN4z3UdqN69oSRacNK8UhnobDdC+7J2AHCjGwxQubNJfE70SXXQ==", 186 | "dev": true 187 | }, 188 | "object-keys": { 189 | "version": "1.0.12", 190 | "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz", 191 | "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==", 192 | "dev": true 193 | }, 194 | "once": { 195 | "version": "1.4.0", 196 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", 197 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", 198 | "dev": true, 199 | "requires": { 200 | "wrappy": "1" 201 | } 202 | }, 203 | "path-is-absolute": { 204 | "version": "1.0.1", 205 | "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", 206 | "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", 207 | "dev": true 208 | }, 209 | "path-parse": { 210 | "version": "1.0.5", 211 | "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz", 212 | "integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=", 213 | "dev": true 214 | }, 215 | "resolve": { 216 | "version": "1.7.1", 217 | "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.7.1.tgz", 218 | "integrity": "sha512-c7rwLofp8g1U+h1KNyHL/jicrKg1Ek4q+Lr33AL65uZTinUZHe30D5HlyN5V9NW0JX1D5dXQ4jqW5l7Sy/kGfw==", 219 | "dev": true, 220 | "requires": { 221 | "path-parse": "^1.0.5" 222 | } 223 | }, 224 | "resumer": { 225 | "version": "0.0.0", 226 | "resolved": "https://registry.npmjs.org/resumer/-/resumer-0.0.0.tgz", 227 | "integrity": "sha1-8ej0YeQGS6Oegq883CqMiT0HZ1k=", 228 | "dev": true, 229 | "requires": { 230 | "through": "~2.3.4" 231 | } 232 | }, 233 | "string.prototype.trim": { 234 | "version": "1.1.2", 235 | "resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.1.2.tgz", 236 | "integrity": "sha1-0E3iyJ4Tf019IG8Ia17S+ua+jOo=", 237 | "dev": true, 238 | "requires": { 239 | "define-properties": "^1.1.2", 240 | "es-abstract": "^1.5.0", 241 | "function-bind": "^1.0.2" 242 | } 243 | }, 244 | "tape": { 245 | "version": "4.9.1", 246 | "resolved": "https://registry.npmjs.org/tape/-/tape-4.9.1.tgz", 247 | "integrity": "sha512-6fKIXknLpoe/Jp4rzHKFPpJUHDHDqn8jus99IfPnHIjyz78HYlefTGD3b5EkbQzuLfaEvmfPK3IolLgq2xT3kw==", 248 | "dev": true, 249 | "requires": { 250 | "deep-equal": "~1.0.1", 251 | "defined": "~1.0.0", 252 | "for-each": "~0.3.3", 253 | "function-bind": "~1.1.1", 254 | "glob": "~7.1.2", 255 | "has": "~1.0.3", 256 | "inherits": "~2.0.3", 257 | "minimist": "~1.2.0", 258 | "object-inspect": "~1.6.0", 259 | "resolve": "~1.7.1", 260 | "resumer": "~0.0.0", 261 | "string.prototype.trim": "~1.1.2", 262 | "through": "~2.3.8" 263 | } 264 | }, 265 | "through": { 266 | "version": "2.3.8", 267 | "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", 268 | "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", 269 | "dev": true 270 | }, 271 | "wrappy": { 272 | "version": "1.0.2", 273 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", 274 | "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", 275 | "dev": true 276 | } 277 | } 278 | } 279 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "convert-length", 3 | "version": "1.0.1", 4 | "description": "Converts a distance unit (e.g. m) to another (e.g. cm)", 5 | "main": "./convert-length.js", 6 | "license": "MIT", 7 | "author": { 8 | "name": "Matt DesLauriers", 9 | "email": "dave.des@gmail.com", 10 | "url": "https://github.com/mattdesl" 11 | }, 12 | "dependencies": { 13 | "defined": "^1.0.0" 14 | }, 15 | "devDependencies": { 16 | "tape": "^4.9.1" 17 | }, 18 | "scripts": { 19 | "test": "node test.js" 20 | }, 21 | "keywords": [ 22 | "distance", 23 | "length", 24 | "px", 25 | "conversion", 26 | "convert", 27 | "converts", 28 | "from", 29 | "to", 30 | "unit", 31 | "units", 32 | "m", 33 | "cm", 34 | "mm", 35 | "meter", 36 | "meters", 37 | "inches", 38 | "in", 39 | "centimeter", 40 | "millimetre", 41 | "centimetre", 42 | "metre", 43 | "metres", 44 | "inch", 45 | "millimeter" 46 | ], 47 | "repository": { 48 | "type": "git", 49 | "url": "git://github.com/mattdesl/convert-length.git" 50 | }, 51 | "homepage": "https://github.com/mattdesl/convert-length", 52 | "bugs": { 53 | "url": "https://github.com/mattdesl/convert-length/issues" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const tape = require('tape'); 2 | const convert = require('./'); 3 | 4 | const convertWithPrecision = (a, b, c, precision = 4, opt = {}) => { 5 | return convert(a, b, c, Object.assign({}, opt, { precision })); 6 | }; 7 | 8 | tape('test px to physical', t => { 9 | t.equal(convert(1, 'in', 'px'), 96); 10 | t.equal(convert(1, 'px', 'in'), 1 / 96); 11 | t.equal(convert(4, 'in', 'px'), 384); 12 | t.equal(convert(1, 'in', 'px', { pixelsPerInch: 72 }), 72); 13 | t.equal(convert(6, 'cm', 'px'), 227); 14 | t.equal(convert(10, 'mm', 'px'), 38); 15 | t.equal(convert(10, 'mm', 'px', { pixelsPerInch: 72 }), 28); 16 | t.equal(convert(10, 'px', 'mm', { precision: 2 }), 2.65); 17 | t.equal(convert(11, 'px', 'in', { precision: 3 }), 0.115); 18 | t.end(); 19 | }); 20 | 21 | tape('test basic conversion functions', t => { 22 | t.equal(convert(1, 'm', 'm'), 1); 23 | t.equal(convert(1, 'cm', 'm'), 0.01); 24 | t.equal(convert(1, 'mm', 'm'), 0.001); 25 | t.equal(convert(1, 'm', 'cm'), 100); 26 | t.equal(convert(1, 'm', 'mm'), 1000); 27 | t.equal(convertWithPrecision(1, 'm', 'ft'), 3.2808); 28 | t.equal(convert(1, 'ft', 'in'), 12); 29 | t.equal(convert(1, 'in', 'ft'), 1 / 12); 30 | t.equal(convertWithPrecision(1, 'm', 'ft'), 3.2808); 31 | t.equal(convertWithPrecision(1, 'm', 'ft'), 3.2808); 32 | t.equal(convertWithPrecision(1, 'm', 'in'), 39.3701); 33 | t.equal(convert(1, 'in', 'm'), 0.0254); 34 | t.equal(convert(1, 'cm', 'm'), 0.01); 35 | t.equal(convert(1, 'm', 'cm'), 100); 36 | t.equal(convert(72, 'pt', 'in'), 1); 37 | t.equal(convert(1, 'in', 'pt'), 72); 38 | t.equal(convert(1, 'in', 'pc'), 6); 39 | t.equal(convert(6, 'pc', 'in'), 1); 40 | t.equal(convert(6, 'pc', 'pc'), 6); 41 | t.equal(convert(6, 'in', 'in'), 6); 42 | t.end(); 43 | }); 44 | 45 | tape('test API and errors', t => { 46 | t.throws(() => convert(2, 'gold', 'foo')); 47 | t.throws(() => convert(NaN, 'px', 'in')); 48 | t.throws(() => convert(25)); 49 | t.deepEqual(convert.units, [ 'mm', 'cm', 'm', 'pc', 'pt', 'in', 'ft', 'px' ]); 50 | t.end(); 51 | }); 52 | --------------------------------------------------------------------------------