Skip to content

Commit 2af4ec6

Browse files
Jonathan CuthbertJonathan Cuthbert
Jonathan Cuthbert
authored and
Jonathan Cuthbert
committed
change matching characters to just use asterix and bang
1 parent aa52ad5 commit 2af4ec6

File tree

5 files changed

+72
-60
lines changed

5 files changed

+72
-60
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,9 @@ Default:
7171
- `propList` (Array) The properties that can change from px to rem.
7272
- Values need to be exact matches.
7373
- Use wildcard `*` to enable all properties. Example: `['*']`
74-
- Use `~` to match any part of the property. (`['~position']` will match `background-position-y`)
75-
- Use `^` to match the start of the property. (`['^font']` will match `font-weight`)
76-
- Use `$` to match the end of the property. (`['$-radius']` will match `border-top-right-radius`)
74+
- Use `*` at the start or end of a word. (`['*position*']` will match `background-position-y`)
7775
- Use `!` to not match a property. Example: `['*', '!letter-spacing']`
78-
- Combine the "not" prefix with the other prefixes. Example: `['*', '!~margin']`
76+
- Combine the "not" prefix with the other prefixes. Example: `['*', '!font*']`
7977
- `selectorBlackList` (Array) The selectors to ignore and leave as px.
8078
- If value is string, it checks to see if selector contains the string.
8179
- `['body']` will match `.body-class`

index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ function createPxReplace (rootValue, unitPrecision, minPixelValue) {
9191
if (!$1) return m;
9292
var pixels = parseFloat($1);
9393
if (pixels < minPixelValue) return m;
94-
return toFixed((pixels / rootValue), unitPrecision) + 'rem';
94+
var fixedVal = toFixed((pixels / rootValue), unitPrecision);
95+
return (fixedVal === 0) ? '0' : fixedVal + 'rem';
9596
};
9697
}
9798

@@ -121,12 +122,12 @@ function createPropListMatcher(propList) {
121122
var lists = {
122123
exact: filterPropList.exact(propList),
123124
contain: filterPropList.contain(propList),
124-
start: filterPropList.start(propList),
125-
end: filterPropList.end(propList),
126-
not: filterPropList.not(propList),
125+
startWith: filterPropList.startWith(propList),
126+
endWith: filterPropList.endWith(propList),
127+
notExact: filterPropList.notExact(propList),
127128
notContain: filterPropList.notContain(propList),
128-
notStart: filterPropList.notStart(propList),
129-
notEnd: filterPropList.notEnd(propList)
129+
notStartWith: filterPropList.notStartWith(propList),
130+
notEndWith: filterPropList.notEndWith(propList)
130131
};
131132
return function (prop) {
132133
if (matchAll) return true;
@@ -137,22 +138,22 @@ function createPropListMatcher(propList) {
137138
lists.contain.some(function (m) {
138139
return prop.indexOf(m) > -1;
139140
}) ||
140-
lists.start.some(function (m) {
141+
lists.startWith.some(function (m) {
141142
return prop.indexOf(m) === 0;
142143
}) ||
143-
lists.end.some(function (m) {
144+
lists.endWith.some(function (m) {
144145
return prop.indexOf(m) === prop.length - m.length;
145146
})
146147
) &&
147148
!(
148-
lists.not.indexOf(prop) > -1 ||
149+
lists.notExact.indexOf(prop) > -1 ||
149150
lists.notContain.some(function (m) {
150151
return prop.indexOf(m) > -1;
151152
}) ||
152-
lists.notStart.some(function (m) {
153+
lists.notStartWith.some(function (m) {
153154
return prop.indexOf(m) === 0;
154155
}) ||
155-
lists.notEnd.some(function (m) {
156+
lists.notEndWith.some(function (m) {
156157
return prop.indexOf(m) === prop.length - m.length;
157158
})
158159
)

lib/filter-prop-list.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,56 @@
11
module.exports = {
22
exact: function (list) {
33
return list.filter(function (m) {
4-
return m.match(/^[^\*\~\^\$\!]+/);
4+
return m.match(/^[^\*\!]+$/);
55
});
66
},
77
contain: function (list) {
88
return list.filter(function (m) {
9-
return m.indexOf('~') === 0;
10-
}).map(trimFirstCharacter);
9+
return m.match(/^\*.+\*$/);
10+
}).map(function (m) {
11+
return m.substr(1, m.length - 2);
12+
});
1113
},
12-
start: function (list) {
14+
endWith: function (list) {
1315
return list.filter(function (m) {
14-
return m.indexOf('^') === 0;
15-
}).map(trimFirstCharacter);
16+
return m.match(/^\*[^\*]+$/);
17+
}).map(function (m) {
18+
return m.substr(1);
19+
});
1620
},
17-
end: function (list) {
21+
startWith: function (list) {
1822
return list.filter(function (m) {
19-
return m.indexOf('$') === 0;
20-
}).map(trimFirstCharacter);
23+
return m.match(/^[^\*\!]+\*$/);
24+
}).map(function (m) {
25+
return m.substr(0, m.length - 1);
26+
});
2127
},
22-
not: function (list) {
28+
notExact: function (list) {
2329
return list.filter(function (m) {
24-
return m.match(/^\![^\~\^\$]+/);
25-
}).map(trimFirstCharacter);
30+
return m.match(/^\![^\*].*$/);
31+
}).map(function (m) {
32+
return m.substr(1);
33+
});
2634
},
2735
notContain: function (list) {
2836
return list.filter(function (m) {
29-
return m.indexOf('!~') === 0;
30-
}).map(trimFirstTwoCharacters);
37+
return m.match(/^\!\*.+\*$/);
38+
}).map(function (m) {
39+
return m.substr(2, m.length - 3);
40+
});
3141
},
32-
notStart: function (list) {
42+
notEndWith: function (list) {
3343
return list.filter(function (m) {
34-
return m.indexOf('!^') === 0;
35-
}).map(trimFirstTwoCharacters);
44+
return m.match(/^\!\*[^\*]+$/);
45+
}).map(function (m) {
46+
return m.substr(2);
47+
});
3648
},
37-
notEnd: function (list) {
49+
notStartWith: function (list) {
3850
return list.filter(function (m) {
39-
return m.indexOf('!$') === 0;
40-
}).map(trimFirstTwoCharacters);
51+
return m.match(/^\![^\*]+\*$/);
52+
}).map(function (m) {
53+
return m.substr(1, m.length - 2);
54+
});
4155
}
4256
};
43-
44-
function trimFirstCharacter(str) {
45-
return str.substring(1);
46-
}
47-
48-
function trimFirstTwoCharacters(str) {
49-
return str.substring(2);
50-
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"postcss-plugin"
2626
],
2727
"dependencies": {
28-
"object-assign": "^4.0.1",
29-
"postcss": "^5.0.2"
28+
"object-assign": "^4.1.0",
29+
"postcss": "^5.2.10"
3030
}
3131
}

spec/pxtorem-spec.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ describe('pxtorem', function () {
6262

6363
expect(processed).toBe(expected);
6464
});
65+
66+
it('should remain unitless if 0', function () {
67+
var expected = '.rule { font-size: 0px; font-size: 0; }';
68+
var processed = postcss(pxtorem()).process(expected).css;
69+
70+
expect(processed).toBe(expected);
71+
});
6572
});
6673

6774
describe('value parsing', function () {
@@ -193,7 +200,7 @@ describe('propWhiteList', function () {
193200
var css = '.rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }';
194201
var expected = '.rule { font-size: 1rem; margin: 1rem; margin-left: 5px; padding: 5px; padding-right: 1rem }';
195202
var options = {
196-
propWhiteList: ['~font', '^margin', '!margin-left', '$-right', 'pad']
203+
propWhiteList: ['*font*', 'margin*', '!margin-left', '*-right', 'pad']
197204
};
198205
var processed = postcss(pxtorem(options)).process(css).css;
199206

@@ -204,7 +211,7 @@ describe('propWhiteList', function () {
204211
var css = '.rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }';
205212
var expected = '.rule { font-size: 16px; margin: 1rem; margin-left: 5px; padding: 5px; padding-right: 16px }';
206213
var options = {
207-
propWhiteList: ['*', '!margin-left', '!~padding', '!^font']
214+
propWhiteList: ['*', '!margin-left', '!*padding*', '!font*']
208215
};
209216
var processed = postcss(pxtorem(options)).process(css).css;
210217

@@ -321,50 +328,50 @@ describe('minPixelValue', function () {
321328

322329
describe('filter-prop-list', function () {
323330
it('should find "exact" matches from propList', function () {
324-
var propList = ['font-size', 'margin', '!padding', '~border', '*', '$y', '!~font'];
331+
var propList = ['font-size', 'margin', '!padding', '*border*', '*', '*y', '!*font*'];
325332
var expected = 'font-size,margin';
326333
expect(filterPropList.exact(propList).join()).toBe(expected);
327334
});
328335

329336
it('should find "contain" matches from propList and reduce to string', function () {
330-
var propList = ['font-size', '~margin', '!padding', '~border', '*', '$y', '!~font'];
337+
var propList = ['font-size', '*margin*', '!padding', '*border*', '*', '*y', '!*font*'];
331338
var expected = 'margin,border';
332339
expect(filterPropList.contain(propList).join()).toBe(expected);
333340
});
334341

335342
it('should find "start" matches from propList and reduce to string', function () {
336-
var propList = ['font-size', '~margin', '!padding', '^border', '*', '$y', '!~font'];
343+
var propList = ['font-size', '*margin*', '!padding', 'border*', '*', '*y', '!*font*'];
337344
var expected = 'border';
338-
expect(filterPropList.start(propList).join()).toBe(expected);
345+
expect(filterPropList.startWith(propList).join()).toBe(expected);
339346
});
340347

341348
it('should find "end" matches from propList and reduce to string', function () {
342-
var propList = ['font-size', '~margin', '!padding', '^border', '*', '$y', '!~font'];
349+
var propList = ['font-size', '*margin*', '!padding', 'border*', '*', '*y', '!*font*'];
343350
var expected = 'y';
344-
expect(filterPropList.end(propList).join()).toBe(expected);
351+
expect(filterPropList.endWith(propList).join()).toBe(expected);
345352
});
346353

347354
it('should find "not" matches from propList and reduce to string', function () {
348-
var propList = ['font-size', '~margin', '!padding', '^border', '*', '$y', '!~font'];
355+
var propList = ['font-size', '*margin*', '!padding', 'border*', '*', '*y', '!*font*'];
349356
var expected = 'padding';
350-
expect(filterPropList.not(propList).join()).toBe(expected);
357+
expect(filterPropList.notExact(propList).join()).toBe(expected);
351358
});
352359

353360
it('should find "not contain" matches from propList and reduce to string', function () {
354-
var propList = ['font-size', '~margin', '!padding', '!^border', '*', '$y', '!~font'];
361+
var propList = ['font-size', '*margin*', '!padding', '!border*', '*', '*y', '!*font*'];
355362
var expected = 'font';
356363
expect(filterPropList.notContain(propList).join()).toBe(expected);
357364
});
358365

359366
it('should find "not start" matches from propList and reduce to string', function () {
360-
var propList = ['font-size', '~margin', '!padding', '!^border', '*', '$y', '!~font'];
367+
var propList = ['font-size', '*margin*', '!padding', '!border*', '*', '*y', '!*font*'];
361368
var expected = 'border';
362-
expect(filterPropList.notStart(propList).join()).toBe(expected);
369+
expect(filterPropList.notStartWith(propList).join()).toBe(expected);
363370
});
364371

365372
it('should find "not end" matches from propList and reduce to string', function () {
366-
var propList = ['font-size', '~margin', '!padding', '!^border', '*', '!$y', '!~font'];
373+
var propList = ['font-size', '*margin*', '!padding', '!border*', '*', '!*y', '!*font*'];
367374
var expected = 'y';
368-
expect(filterPropList.notEnd(propList).join()).toBe(expected);
375+
expect(filterPropList.notEndWith(propList).join()).toBe(expected);
369376
});
370377
});

0 commit comments

Comments
 (0)