forked from mahirshah/css-property-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getShorthandsForProperty.js
45 lines (42 loc) · 1.5 KB
/
getShorthandsForProperty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const shortHandProperties = require('./formatted-data/shorthand-properties.json');
const properties = require('./formatted-data/properties.json');
/**
* @type {Object}
*/
const inverseMapping = {};
function computeInverseMapping() {
const shorthands = Object.keys(shortHandProperties);
shorthands.forEach((shorthand) => {
const longhands = shortHandProperties[shorthand].computed;
longhands.forEach((longhand) => {
if (inverseMapping[longhand]) {
inverseMapping[longhand].push(shorthand);
} else {
inverseMapping[longhand] = [shorthand];
}
});
});
const longhands = Object.keys(inverseMapping);
longhands.forEach((longhand) => {
inverseMapping[longhand].forEach((shorthand) => {
if (inverseMapping[shorthand]) {
inverseMapping[longhand].push(...inverseMapping[shorthand]);
}
});
});
}
/**
* Return a list of all properties that set the given property.
* Includes at least the value provided, plus any other shorthands that can
* set it.
* @param {string} property - the property name
* @return {Array<string>} all properties that set this property.
* @example
* console.log(getShorthandsForProperty('border-left-width'));
* // => [ 'border-left-width', 'border-left', 'border-width', 'border' ]
*/
module.exports = function getShorthandsForProperty(property) {
if (!inverseMapping['background-image']) computeInverseMapping();
if (!properties[property]) return [];
return [property, ...(inverseMapping[property] || [])];
};