Skip to content

Commit 986c5b1

Browse files
committed
Merge pull request #81 from bem/issue-68
Presets
2 parents f20e9f7 + beaabbe commit 986c5b1

17 files changed

+50
-42
lines changed

README.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,13 @@ Read more in the [Guidelines](http://cssguidelin.es/#bem-like-naming).
394394
Example:
395395

396396
```js
397-
var csswizardry = bemNaming({
398-
elem: '__',
399-
mod: { name: '--', val: '_' }
400-
});
397+
var twoDashes = bemNaming('two-dashes');
401398

402-
csswizardry.parse('block__elem'); // { block: 'block', elem: 'elem' }
403-
csswizardry.parse('block--mod_val'); // { block: 'block',
404-
// modName: 'mod', modVal: 'val' }
399+
twoDashes.parse('block__elem'); // { block: 'block', elem: 'elem' }
400+
twoDashes.parse('block--mod_val'); // { block: 'block',
401+
// modName: 'mod', modVal: 'val' }
405402

406-
csswizardry.stringify({ // 'block__elem--mod'
403+
twoDashes.stringify({ // 'block__elem--mod'
407404
block: 'block',
408405
elem: 'elem',
409406
modName: 'mod'

README.ru.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,13 @@ myNaming.stringify({ // 'blockName-elemName--boolElemMod'
392392
Пример:
393393

394394
```js
395-
var csswizardry = bemNaming({
396-
elem: '__',
397-
mod: { name: '--', val: '_' }
398-
});
395+
var twoDashes = bemNaming('two-dashes');
399396

400-
csswizardry.parse('block__elem'); // { block: 'block', elem: 'elem' }
401-
csswizardry.parse('block--mod_val'); // { block: 'block',
402-
// modName: 'mod', modVal: `val` }
397+
twoDashes.parse('block__elem'); // { block: 'block', elem: 'elem' }
398+
twoDashes.parse('block--mod_val'); // { block: 'block',
399+
// modName: 'mod', modVal: `val` }
403400

404-
csswizardry.stringify({ // 'block__elem--mod'
401+
twoDashes.stringify({ // 'block__elem--mod'
405402
block: 'block',
406403
elem: 'elem',
407404
modName: 'mod'

index.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,28 +205,42 @@ var defineAsGlobal = true,
205205
'isBlock', 'isElem', 'isBlockMod', 'isElemMod'
206206
],
207207
fields = ['elemDelim', 'modDelim', 'modValDelim'],
208+
presets = {
209+
'two-dashes': {
210+
elem: '__',
211+
modName: '--',
212+
modVal: '_',
213+
wordPattern: '[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*'
214+
}
215+
},
208216
bemNaming = function (options) {
209217
options || (options = {});
210218

211-
var mod = options.mod || '_',
212-
modName, modVal;
219+
var naming;
213220

214-
if (typeof mod === 'string') {
215-
modName = mod;
216-
modVal = mod;
221+
if (options === 'two-dashes') {
222+
naming = presets['two-dashes'];
217223
} else {
218-
modName = mod.name || '_';
219-
modVal = mod.val || modName;
220-
}
221-
222-
var naming = {
224+
var mod = options.mod || '_',
225+
modName, modVal;
226+
227+
if (typeof mod === 'string') {
228+
modName = mod;
229+
modVal = mod;
230+
} else {
231+
modName = mod.name || '_';
232+
modVal = mod.val || modName;
233+
}
234+
235+
naming = {
223236
elem: options.elem || '__',
224237
modName: modName,
225238
modVal: modVal,
226239
wordPattern: options.wordPattern || '[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*'
227-
},
228-
id = JSON.stringify(naming);
240+
}
241+
}
229242

243+
var id = JSON.stringify(naming);
230244
if (cache[id]) {
231245
return cache[id];
232246
}

test/presets/original/is-block-mod.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const test = require('ava');
4-
const naming = require('../../../index');
4+
const naming = require('../../../index')('origin');
55

66
test('should detect mod of block', t => {
77
t.true(naming.isBlockMod('block_mod_val'));

test/presets/original/is-elem-mod.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const test = require('ava');
4-
const naming = require('../../../index');
4+
const naming = require('../../../index')('origin');
55

66
test('should detect mod of elem', t => {
77
t.true(naming.isElemMod('block__elem_mod_val'));

test/presets/original/is-elem.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const test = require('ava');
4-
const naming = require('../../../index');
4+
const naming = require('../../../index')('origin');
55

66
test('should detect elem', t => {
77
t.true(naming.isElem('block__elem'));

test/presets/original/parse.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const test = require('ava');
4-
const naming = require('../../../index');
4+
const naming = require('../../../index')('origin');
55

66
test('should not parse not valid string', t => {
77
const obj = naming.parse('(*)_(*)');

test/presets/original/stringify.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const test = require('ava');
4-
const naming = require('../../../index');
4+
const naming = require('../../../index')('origin');
55

66
test('should stringify block', t => {
77
const str = naming.stringify({ block: 'block' });

test/presets/original/type-of.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const test = require('ava');
4-
const naming = require('../../../index');
4+
const naming = require('../../../index')('origin');
55

66
test('should not determine not valid string', t => {
77
const type = naming.typeOf('(*)_(*)');

test/presets/original/validate.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const test = require('ava');
4-
const naming = require('../../../index');
4+
const naming = require('../../../index')('origin');
55

66
test('should validate block', t => {
77
t.true(naming.validate('block'));

0 commit comments

Comments
 (0)