Skip to content

Commit

Permalink
change matching characters to just use asterix and bang
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Cuthbert authored and Jonathan Cuthbert committed Jan 15, 2017
1 parent aa52ad5 commit 2af4ec6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 60 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ Default:
- `propList` (Array) The properties that can change from px to rem.
- Values need to be exact matches.
- Use wildcard `*` to enable all properties. Example: `['*']`
- Use `~` to match any part of the property. (`['~position']` will match `background-position-y`)
- Use `^` to match the start of the property. (`['^font']` will match `font-weight`)
- Use `$` to match the end of the property. (`['$-radius']` will match `border-top-right-radius`)
- Use `*` at the start or end of a word. (`['*position*']` will match `background-position-y`)
- Use `!` to not match a property. Example: `['*', '!letter-spacing']`
- Combine the "not" prefix with the other prefixes. Example: `['*', '!~margin']`
- Combine the "not" prefix with the other prefixes. Example: `['*', '!font*']`
- `selectorBlackList` (Array) The selectors to ignore and leave as px.
- If value is string, it checks to see if selector contains the string.
- `['body']` will match `.body-class`
Expand Down
23 changes: 12 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ function createPxReplace (rootValue, unitPrecision, minPixelValue) {
if (!$1) return m;
var pixels = parseFloat($1);
if (pixels < minPixelValue) return m;
return toFixed((pixels / rootValue), unitPrecision) + 'rem';
var fixedVal = toFixed((pixels / rootValue), unitPrecision);
return (fixedVal === 0) ? '0' : fixedVal + 'rem';
};
}

Expand Down Expand Up @@ -121,12 +122,12 @@ function createPropListMatcher(propList) {
var lists = {
exact: filterPropList.exact(propList),
contain: filterPropList.contain(propList),
start: filterPropList.start(propList),
end: filterPropList.end(propList),
not: filterPropList.not(propList),
startWith: filterPropList.startWith(propList),
endWith: filterPropList.endWith(propList),
notExact: filterPropList.notExact(propList),
notContain: filterPropList.notContain(propList),
notStart: filterPropList.notStart(propList),
notEnd: filterPropList.notEnd(propList)
notStartWith: filterPropList.notStartWith(propList),
notEndWith: filterPropList.notEndWith(propList)
};
return function (prop) {
if (matchAll) return true;
Expand All @@ -137,22 +138,22 @@ function createPropListMatcher(propList) {
lists.contain.some(function (m) {
return prop.indexOf(m) > -1;
}) ||
lists.start.some(function (m) {
lists.startWith.some(function (m) {
return prop.indexOf(m) === 0;
}) ||
lists.end.some(function (m) {
lists.endWith.some(function (m) {
return prop.indexOf(m) === prop.length - m.length;
})
) &&
!(
lists.not.indexOf(prop) > -1 ||
lists.notExact.indexOf(prop) > -1 ||
lists.notContain.some(function (m) {
return prop.indexOf(m) > -1;
}) ||
lists.notStart.some(function (m) {
lists.notStartWith.some(function (m) {
return prop.indexOf(m) === 0;
}) ||
lists.notEnd.some(function (m) {
lists.notEndWith.some(function (m) {
return prop.indexOf(m) === prop.length - m.length;
})
)
Expand Down
62 changes: 34 additions & 28 deletions lib/filter-prop-list.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
module.exports = {
exact: function (list) {
return list.filter(function (m) {
return m.match(/^[^\*\~\^\$\!]+/);
return m.match(/^[^\*\!]+$/);
});
},
contain: function (list) {
return list.filter(function (m) {
return m.indexOf('~') === 0;
}).map(trimFirstCharacter);
return m.match(/^\*.+\*$/);
}).map(function (m) {
return m.substr(1, m.length - 2);
});
},
start: function (list) {
endWith: function (list) {
return list.filter(function (m) {
return m.indexOf('^') === 0;
}).map(trimFirstCharacter);
return m.match(/^\*[^\*]+$/);
}).map(function (m) {
return m.substr(1);
});
},
end: function (list) {
startWith: function (list) {
return list.filter(function (m) {
return m.indexOf('$') === 0;
}).map(trimFirstCharacter);
return m.match(/^[^\*\!]+\*$/);
}).map(function (m) {
return m.substr(0, m.length - 1);
});
},
not: function (list) {
notExact: function (list) {
return list.filter(function (m) {
return m.match(/^\![^\~\^\$]+/);
}).map(trimFirstCharacter);
return m.match(/^\![^\*].*$/);
}).map(function (m) {
return m.substr(1);
});
},
notContain: function (list) {
return list.filter(function (m) {
return m.indexOf('!~') === 0;
}).map(trimFirstTwoCharacters);
return m.match(/^\!\*.+\*$/);
}).map(function (m) {
return m.substr(2, m.length - 3);
});
},
notStart: function (list) {
notEndWith: function (list) {
return list.filter(function (m) {
return m.indexOf('!^') === 0;
}).map(trimFirstTwoCharacters);
return m.match(/^\!\*[^\*]+$/);
}).map(function (m) {
return m.substr(2);
});
},
notEnd: function (list) {
notStartWith: function (list) {
return list.filter(function (m) {
return m.indexOf('!$') === 0;
}).map(trimFirstTwoCharacters);
return m.match(/^\![^\*]+\*$/);
}).map(function (m) {
return m.substr(1, m.length - 2);
});
}
};

function trimFirstCharacter(str) {
return str.substring(1);
}

function trimFirstTwoCharacters(str) {
return str.substring(2);
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"postcss-plugin"
],
"dependencies": {
"object-assign": "^4.0.1",
"postcss": "^5.0.2"
"object-assign": "^4.1.0",
"postcss": "^5.2.10"
}
}
37 changes: 22 additions & 15 deletions spec/pxtorem-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ describe('pxtorem', function () {

expect(processed).toBe(expected);
});

it('should remain unitless if 0', function () {
var expected = '.rule { font-size: 0px; font-size: 0; }';
var processed = postcss(pxtorem()).process(expected).css;

expect(processed).toBe(expected);
});
});

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

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

Expand Down Expand Up @@ -321,50 +328,50 @@ describe('minPixelValue', function () {

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

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

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

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

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

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

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

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

0 comments on commit 2af4ec6

Please sign in to comment.