Skip to content

Commit

Permalink
Опция ignore принимает регулярное выражение
Browse files Browse the repository at this point in the history
  • Loading branch information
tenorok committed Oct 29, 2013
2 parents d083ef1 + 84bdc01 commit f4a78e2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 19 deletions.
76 changes: 60 additions & 16 deletions autoclasscss.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* @file AutoclassCSS - Generator CSS skeleton {@link https://github.com/tenorok/autoclassCSS}
* @copyright 2012–2013 Artem Kurbatov, tenorok.ru
* @license MIT license
* @version 0.0.1
* @version 0.0.2
*/

(function(window) {
(function(global) {

/**
* Конструктор
Expand Down Expand Up @@ -41,6 +41,22 @@ function duplicateStr(string, count) {
return new Array(count + 1).join(string);
}

function isString(string) {
return typeof string === 'string';
}

function isBoolean(boolean) {
return typeof boolean === 'boolean';
}

function isArray(array) {
return array instanceof Array;
}

function isRegexp(regexp) {
return regexp instanceof RegExp;
}

Autoclasscss.prototype = {

/**
Expand Down Expand Up @@ -75,24 +91,36 @@ Autoclasscss.prototype = {
/**
* Добавление игнорируемых классов
* @memberof Autoclasscss#
* @param {string|Array|boolean} classes Класс, массив классов или false для отмены игнорирования
* @param {string|Array|boolean|RegExp} classes Класс, массив классов, регулярное выражение или false для отмены игнорирования
* @returns {this}
*/
ignore: function(classes) {

switch(typeof classes) {
// Если false
if(isBoolean(classes) && !classes) {
this.params.ignore = [];
return this;
}

if(isRegexp(classes)) {
this.params.ignore = classes;
return this;
}

case 'string':
this.params.ignore.push(classes);
return this;
// Если в ignore не массив, а регулярное выражение
if(!isArray(this.params.ignore)) {
// Сброс ignore в пустой массив, чтобы не было ошибок при добавлении
this.params.ignore = [];
}

case 'object':
this.params.ignore = this.params.ignore.concat(classes);
return this;
if(isString(classes)) {
this.params.ignore.push(classes);
return this;
}

case 'boolean':
this.params.ignore = [];
return this;
if(isArray(classes)) {
this.params.ignore = this.params.ignore.concat(classes);
return this;
}
},

Expand Down Expand Up @@ -319,6 +347,22 @@ Autoclasscss.prototype = {
].indexOf(tag);
}

/**
* Является ли класс игнорируемым
* @private
* @param {string} cls Имя класса
* @returns {boolean}
*/
function isIgnoringClass(cls) {

if(isArray(that.params.ignore)) {
return ~that.params.ignore.indexOf(cls);
}

// Иначе в ignore регулярное выражение
return that.params.ignore.test(cls);
}

/**
* Получить массив тегов с их классами
* @private
Expand All @@ -343,7 +387,7 @@ Autoclasscss.prototype = {
break;

case 'class':
~that.params.ignore.indexOf(element.val) || tags[tags.length - 1].classes.push(element.val);
isIgnoringClass(element.val) || tags[tags.length - 1].classes.push(element.val);
break;

case 'tag-close':
Expand Down Expand Up @@ -494,6 +538,6 @@ Autoclasscss.prototype = {
}
};

window.Autoclasscss = Autoclasscss;
global.Autoclasscss = Autoclasscss;

})(window, undefined);
})(this, undefined);
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "autoclasscss",
"version": "0.0.1",
"version": "0.0.2",
"license": [
"MIT",
"GPL-3.0"
Expand All @@ -19,4 +19,4 @@
"ace": "git@github.com:ajaxorg/ace-builds.git",
"jsdoc": "git@github.com:jsdoc3/jsdoc.git"
}
}
}
40 changes: 39 additions & 1 deletion test/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,44 @@ describe('Тестирование опции: ignore', function() {
' .c {\n' +
' \n' +
' }'
],
[
[
[],
/[aci]/
],

'.b {\n' +
' \n' +
'}\n' +
' .d {\n' +
' \n' +
' }'
],
[
[
['a', 'b'],
/i[34]/
],

'.a {\n' +
' \n' +
'}\n' +
'.i {\n' +
' \n' +
'}\n' +
'.i2 {\n' +
' \n' +
'}\n' +
' .b {\n' +
' \n' +
' }\n' +
' .d {\n' +
' \n' +
' }\n' +
' .c {\n' +
' \n' +
' }'
]
];

Expand All @@ -98,4 +136,4 @@ describe('Тестирование опции: ignore', function() {
).toBe(test[1]);
});
});
});
});

0 comments on commit f4a78e2

Please sign in to comment.