all files / src/detector/ rgb-alpha-detector.js

100% Statements 44/44
93.75% Branches 15/16
100% Functions 12/12
100% Lines 14/14
3 statements, 5 branches Ignored     
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                   188308× 564924×     188308×                 107620× 107620× 107620×                 80688× 80688× 80688× 80688×        
import { NumberExpression } from "./number-expression";
 
/**
 * アルファ値を含むRGB表現の検出処理を提供します。
 */
class RgbAlphaDetector {
    /**
     * 色表現の正規表現を取得します。
     * @returns {String} 正規表現を返します。
     */
    _getColorExpression() {
        const values = [
            [...Array(3)].map(x => NumberExpression.numericWithMargins),
            NumberExpression.questionablePercentWithMargins
        ].flat();
        return `rgb\\(${values.join(",")}\\)`;
    }
 
    /**
     * 表現を検査します。
     * @param {String} expression 検査対象の表現。
     * @returns {Boolean} 検証結果(true : 一致、false : 不一致)を返します。
     */
    match(expression) {
        const color = this._getColorExpression();
        const regExp = new RegExp(`^\\s*${color}\\s*$`, "i");
        return regExp.test(expression);
    }
 
    /**
     * 文字列中の該当表現を検出します。
     * @param {String} expression 検査対象の表現。
     * @returns {Array<String>} 検出した表現を返します。
     */
    detect(expression) {
        const color = this._getColorExpression();
        const regExp = new RegExp(color, "gi");
        const results = (expression || "").match(regExp) || [];
        return results.map(x => x.trim());
    }
}
 
export { RgbAlphaDetector };