Mirror: CSS prefixing helpers in less than 1KB ๐ŸŒˆ

Update prefixProperty to use binary flag

Changed files
+31 -15
scripts
src
+23 -2
README.md
···
The API is fairly straightforward and only consists of two functions, `prefixProperty` and `prefixValue`.
```js
-
prefixProperty('margin'); // ['margin']
-
prefixProperty('appearance'); // ['appearance', '-moz-appearance', '-webkit-appearance']
+
prefixProperty('margin'); // 0b000
+
prefixProperty('appearance'); // 0b110
prefixValue('color', 'palevioletred'); // 'palevioletred'
prefixValue('position', 'sticky'); // '-webkit-sticky, sticky'
```
+
+
`prefixProperty` returns a bitmap depending on which prefix should be
+
applied:
+
+
- `0b001` stands for `-ms-`
+
- `0b010` stands for `-moz-`
+
- `0b100` stands for `-webkit`
+
+
These are combined using a binary OR, so an example usage of the
+
`pefixProperty` helper may look like the following:
+
+
```js
+
const prefix = (prop, value) => {
+
const flag = prefixProperty(prop);
+
let css = `${prop}: ${value};\n`;
+
if (flag & 0b001) css += `-ms-${css}`;
+
if (flag & 0b010) css += `-moz-${css}`;
+
if (flag & 0b100) css += `-webkit-${css}`;
+
return css;
+
};
+
```
-2
scripts/generate-prefix-map.js
···
#!/usr/bin/env node
-
const fs = require('fs');
-
const path = require('path');
const prefixMap = require('inline-style-prefixer/lib/data').default.prefixMap;
const mdnProperties = require('mdn-data/css/properties.json');
+1 -1
src/index.d.ts
···
export as namespace CSSPrefixer;
declare namespace CSSPrefixer {
-
function prefixProperty(prop: string): string[];
+
function prefixProperty(prop: string): 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
function prefixValue(prop: string, value: string): string;
}
+7 -10
src/index.js
···
import /* codegen */ '../scripts/generate-prefix-map';
+
const ms = 0b001;
+
const moz = 0b010;
+
const webkit = 0b100;
+
export const prefixProperty = prop => {
-
const props = [prop];
-
if (msPrefixRe.test(prop)) {
-
props.push(`-ms-${prop}`);
-
} else if (mozPrefixRe.test(prop)) {
-
props.push(`-moz-${prop}`);
-
} else if (webkitPrefixRe.test(prop)) {
-
props.push(`-webkit-${prop}`);
-
}
-
-
return props;
+
return (msPrefixRe.test(prop) ? ms : 0)
+
| (mozPrefixRe.test(prop) ? moz : 0)
+
| (webkitPrefixRe.test(prop) ? webkit : 0)
};
export const prefixValue = (prop, value) => {