|
| 1 | +const CSSdata = require('mdn-data'); |
| 2 | +const PropsByPopularity = require('./lib/props-by-popularity.js'); |
| 3 | + |
| 4 | +const propNames = Object.keys(CSSdata.css.properties); |
| 5 | + |
| 6 | +const propRegexps = propNames.map(prop => { |
| 7 | + let prefix = ''; |
| 8 | + let property = prop; |
| 9 | + if (prop[0] === '-') { |
| 10 | + let matchPrefix = prop.match(/^(-[^-]*-)(.+)$/i) |
| 11 | + if (matchPrefix) { |
| 12 | + prefix = matchPrefix[1]; |
| 13 | + property = matchPrefix[2]; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + property = property.replace(/([\*])/g, '\\$1'); |
| 18 | + property = property.replace(/(.)/g, '$1?').replace(/^(.)\?/g, '$1'); |
| 19 | + |
| 20 | + // TODO & FIXME: handle prefix, now its fakey-fakey |
| 21 | + return { |
| 22 | + strictProp: new RegExp(`^${prefix + property.replace(/(\-\?.)\?/g, '$1')}$`, 'i'), |
| 23 | + looseProp: new RegExp(`^${prefix + property}$`, 'i') |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +const expandProps = function(abbr) { |
| 28 | + const filteredProps = propNames.filter((prop, i) => abbr.match(propRegexps[i].looseProp)); |
| 29 | + const rankedProps = filteredProps.map(prop => { return { |
| 30 | + name: prop, |
| 31 | + weights: { |
| 32 | + hasProperDashes: ( |
| 33 | + prop.indexOf('-') === -1 || abbr.match(propRegexps[propNames.indexOf(prop)].strictProp) |
| 34 | + ) ? 1 : 0, |
| 35 | + startsFromAbbr: prop.indexOf(abbr) === 0 ? 1 : 0, |
| 36 | + hasCommonPairStart: ( |
| 37 | + (prop.indexOf('border') === 0 && abbr.indexOf('bd') === 0) || |
| 38 | + (prop.indexOf('background') === 0 && abbr.indexOf('bg') === 0) |
| 39 | + ) ? 1 : 0, |
| 40 | + hasDirections: prop.match(/top|right|bottom|left/) ? 1 : 0, |
| 41 | + popularity: PropsByPopularity.indexOf(prop) === -1 ? PropsByPopularity.length : PropsByPopularity.indexOf(prop), |
| 42 | + strLength: prop.length |
| 43 | + } |
| 44 | + }}) |
| 45 | + |
| 46 | + rankedProps.sort((a, b) => { |
| 47 | + return (b.weights.hasProperDashes - a.weights.hasProperDashes) || |
| 48 | + (b.weights.startsFromAbbr - a.weights.startsFromAbbr) || |
| 49 | + (b.weights.hasCommonPairStart - a.weights.hasCommonPairStart) || |
| 50 | + (b.weights.hasDirections - a.weights.hasDirections) || |
| 51 | + (a.weights.popularity - b.weights.popularity) || |
| 52 | + (a.weights.strLength - b.weights.strLength); |
| 53 | + }) |
| 54 | + |
| 55 | + return rankedProps; |
| 56 | +}; |
| 57 | + |
| 58 | +const Hayaku = { |
| 59 | + expandProps: expandProps |
| 60 | +} |
| 61 | + |
| 62 | +module.exports = Hayaku; |
0 commit comments