-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(comments): implements control directives through comments (#18)
* feat(comments): implements control directives through comments * Polyfill for Object.values and replace Array.prototype.includes by Array.prototype.indexOf * Solve Eslint style errors
- Loading branch information
1 parent
86999d6
commit 0394fb1
Showing
5 changed files
with
205 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Object.getValues polyfill | ||
* @param {Object} obj object | ||
* @return {Array} | ||
*/ | ||
function getValues(obj) { | ||
return Object.keys(obj).map(function (key) { | ||
return obj[key] | ||
}) | ||
} | ||
|
||
const CONTROL_DIRECTIVE_REG_EXP = /^\/\*!? *byebye:?(begin|end)?:(\w+) *\*\/$/ | ||
|
||
const CONTROL_DIRECTIVES = { | ||
IGNORE: 'ignore', | ||
} | ||
|
||
const CONTROL_DIRECTIVES_BLOCKS = { | ||
BEGIN: 'begin', | ||
END: 'end', | ||
} | ||
|
||
const controlDirectivesValues = getValues(CONTROL_DIRECTIVES) | ||
const controlDirectivesBlockValues = getValues(CONTROL_DIRECTIVES_BLOCKS) | ||
|
||
/** | ||
* Check if a directive match is a valid one | ||
* @param {Mixed Array} match string match | ||
* @return {Boolean} | ||
*/ | ||
function isValidMatchDirective(match) { | ||
if (Array.isArray(match)) { | ||
return ( | ||
controlDirectivesValues.indexOf(match[2]) >= 0 && | ||
(typeof match[1] === 'undefined' || controlDirectivesBlockValues.indexOf(match[1]) >= 0) | ||
) | ||
} | ||
return false | ||
} | ||
|
||
/** | ||
* Extract a control directive from a comment | ||
* @param {Comment} comment postcss comment | ||
* @return {Directive object} | ||
*/ | ||
function getControlDirective(comment) { | ||
const commentStr = comment.toString() | ||
const match = commentStr.match(CONTROL_DIRECTIVE_REG_EXP) | ||
if (match && isValidMatchDirective(match)) { | ||
const controlDirective = {directive: match[2]} | ||
if (match[1]) { | ||
controlDirective.block = match[1] | ||
} | ||
return controlDirective | ||
} | ||
return null | ||
} | ||
|
||
module.exports = { | ||
CONTROL_DIRECTIVES, | ||
CONTROL_DIRECTIVES_BLOCKS, | ||
getControlDirective, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Escape a string so that it can be turned into a regex | ||
* @param {String} str String to transform | ||
* @return {String} Escaped string | ||
*/ | ||
function escapeRegExp(str) { | ||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&') | ||
} | ||
|
||
/** | ||
* Turn strings from rules to remove into a regexp to concat them later | ||
* @param {Mixed Array} rulesToRemove | ||
* @return {RegExp Array} | ||
*/ | ||
function regexize(rulesToRemove) { | ||
const rulesRegexes = [] | ||
for (let i = 0, l = rulesToRemove.length; i < l; i++) { | ||
if (typeof rulesToRemove[i] === 'string') { | ||
rulesRegexes.push(new RegExp('^\\s*' + escapeRegExp(rulesToRemove[i]) + '\\s*$')) | ||
} else { | ||
rulesRegexes.push(rulesToRemove[i]) | ||
} | ||
} | ||
return rulesRegexes | ||
} | ||
|
||
/** | ||
* Concat various regular expressions into one | ||
* @param {RegExp Array} regexes | ||
* @return {RegExp} concatanated regexp | ||
*/ | ||
function concatRegexes(regexes) { | ||
let rconcat = '' | ||
|
||
if (Array.isArray(regexes)) { | ||
for (let i = 0, l = regexes.length; i < l; i++) { | ||
rconcat += regexes[i].source + '|' | ||
} | ||
|
||
rconcat = rconcat.substr(0, rconcat.length - 1) | ||
|
||
return new RegExp(rconcat) | ||
} | ||
} | ||
|
||
module.exports = { | ||
regexize, | ||
concatRegexes, | ||
} |