Skip to content

Commit 253c788

Browse files
committed
Add node version
1 parent 574295d commit 253c788

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

topx.js renamed to browser.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ function getSizeBrutal(unit, element) {
2323
}
2424

2525
function toPX(str, element) {
26+
if (!str) return null
27+
2628
element = element || document.body
27-
str = (str || 'px').trim().toLowerCase()
29+
str = (str + '' || 'px').trim().toLowerCase()
2830
if(element === window || element === document) {
2931
element = document.body
3032
}
@@ -63,7 +65,10 @@ function toPX(str, element) {
6365

6466
// detect number of units
6567
var parts = parseUnit(str)
66-
if (!isNaN(parts[0])) return parts[0] * toPX(parts[1], element)
68+
if (!isNaN(parts[0]) && parts[1]) {
69+
var px = toPX(parts[1], element)
70+
return typeof px === 'number' ? parts[0] * px : null
71+
}
6772

68-
return 1
73+
return null
6974
}

index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
var parseUnit = require('parse-unit')
4+
5+
module.exports = toPX
6+
7+
var PIXELS_PER_INCH = 96
8+
9+
var defaults = {
10+
'ch': 8,
11+
'ex': 7.15625,
12+
'em': 16,
13+
'rem': 16,
14+
'in': PIXELS_PER_INCH,
15+
'cm': PIXELS_PER_INCH / 2.54,
16+
'mm': PIXELS_PER_INCH / 25.4,
17+
'pt': PIXELS_PER_INCH / 72,
18+
'pc': PIXELS_PER_INCH / 6,
19+
'px': 1
20+
}
21+
22+
function toPX(str) {
23+
if (!str) return null
24+
25+
if (defaults[str]) return defaults[str]
26+
27+
// detect number of units
28+
var parts = parseUnit(str)
29+
if (!isNaN(parts[0]) && parts[1]) {
30+
var px = toPX(parts[1])
31+
return typeof px === 'number' ? parts[0] * px : null
32+
}
33+
34+
return null
35+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "to-px",
33
"version": "1.0.1",
44
"description": "Convert any CSS unit to logical pixels (\"px\")",
5-
"main": "topx.js",
5+
"main": "index.js",
6+
"browser": "browser.js",
67
"directories": {
78
"test": "test"
89
},

test/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
var tape = require('tape')
4-
var toPX = require('../topx')
4+
var toPX = require('../')
55
var parseUnit = require('parse-unit')
66
var almostEqual = require('almost-equal')
77

@@ -10,6 +10,7 @@ var units = ['em', 'ch', 'ex', 'rem', 'px', 'vw', 'vh', 'vmin', 'vmax', 'in', 'c
1010
var fontSizes = ['20px', '10px', '1em', '3in']
1111

1212
tape('test to-px', function(t) {
13+
if (typeof document === 'undefined') return t.end()
1314

1415
function testUnitsEmpirically(element) {
1516
var testDiv = document.createElement('div')
@@ -30,9 +31,9 @@ tape('test to-px', function(t) {
3031
t.ok(almostEqual(actual, expected, 0.005, almostEqual.FLT_EPSILON),
3132
'testing: ' + value + ' ' + actual + ' ~ ' + expected)
3233

33-
value = '.14' + units[i]
34+
value = Math.PI + units[i]
3435
actual = toPX(value, element)
35-
expected *= .14
36+
expected *= Math.PI
3637
t.ok(almostEqual(actual, expected, 0.005, almostEqual.FLT_EPSILON),
3738
'testing: ' + value + ' ' + actual + ' ~ ' + expected)
3839
}
@@ -54,3 +55,15 @@ tape('test to-px', function(t) {
5455

5556
t.end()
5657
})
58+
59+
tape('edge cases', function (t) {
60+
t.equal(toPX(), null, 'no value')
61+
t.equal(toPX(''), null, 'empty string')
62+
t.equal(toPX(null), null, 'null value')
63+
t.equal(toPX('abc'), null, 'unknown units')
64+
t.equal(toPX('5def'), null, 'wrong units')
65+
t.equal(toPX('10'), null, 'number no units')
66+
t.equal(toPX(10), null, 'number value')
67+
68+
t.end()
69+
})

0 commit comments

Comments
 (0)