Skip to content

Commit 6efd3b5

Browse files
committed
Merge pull request #8 from pbevin/master
Support uppercase letters in units
2 parents 15d7496 + f881251 commit 6efd3b5

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function reduceCSSCalc(value, decimalPrecision) {
6666
}
6767

6868
// Remove units in expression:
69-
var toEvaluate = expression.replace(new RegExp(unit, "g"), "")
69+
var toEvaluate = expression.replace(new RegExp(unit, "gi"), "")
7070
var result
7171

7272
try {
@@ -134,16 +134,18 @@ function reduceCSSCalc(value, decimalPrecision) {
134134

135135
function getUnitsInExpression(expression) {
136136
var uniqueUnits = []
137-
var unitRegEx = /[\.0-9]([%a-z]+)/g
137+
var uniqueLowerCaseUnits = []
138+
var unitRegEx = /[\.0-9]([%a-z]+)/gi
138139
var matches = unitRegEx.exec(expression)
139140

140141
while (matches) {
141142
if (!matches || !matches[1]) {
142143
continue
143144
}
144145

145-
if (uniqueUnits.indexOf(matches[1]) === -1) {
146+
if (uniqueLowerCaseUnits.indexOf(matches[1].toLowerCase()) === -1) {
146147
uniqueUnits.push(matches[1])
148+
uniqueLowerCaseUnits.push(matches[1].toLowerCase())
147149
}
148150

149151
matches = unitRegEx.exec(expression)

test/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,12 @@ test("ignore unrecognized values", function(t) {
104104

105105
t.end()
106106
})
107+
108+
test("non-lowercase units", function(t) {
109+
t.equal(reduceCSSCalc("calc(1PX)"), "1PX", "all uppercase");
110+
t.equal(reduceCSSCalc("calc(1Px)"), "1Px", "first letter uppercase");
111+
t.equal(reduceCSSCalc("calc(50% - 42Px)"), "calc(50% - 42Px)", "preserves percentage");
112+
t.equal(reduceCSSCalc("calc(1Px + 1pX)"), "2Px", "combines same units mixed case");
113+
114+
t.end()
115+
})

0 commit comments

Comments
 (0)