@bem-react/classname 1.6.0 → 1.6.1-dev.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BEM className configure function.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ``` ts
|
|
6
|
+
*
|
|
7
|
+
* import { withNaming } from '@bem-react/classname';
|
|
8
|
+
*
|
|
9
|
+
* const cn = withNaming({ n: 'ns-', e: '__', m: '_' });
|
|
10
|
+
*
|
|
11
|
+
* cn('block', 'elem'); // 'ns-block__elem'
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @param preset settings for the naming convention
|
|
15
|
+
*/
|
|
16
|
+
function withNaming(preset) {
|
|
17
|
+
var nameSpace = preset.n || '';
|
|
18
|
+
var modValueDelimiter = preset.v || preset.m;
|
|
19
|
+
function stringify(b, e, m, mix) {
|
|
20
|
+
var entityName = e ? nameSpace + b + preset.e + e : nameSpace + b;
|
|
21
|
+
var className = entityName;
|
|
22
|
+
if (m) {
|
|
23
|
+
var modPrefix = ' ' + className + preset.m;
|
|
24
|
+
for (var k in m) {
|
|
25
|
+
if (m.hasOwnProperty(k)) {
|
|
26
|
+
var modVal = m[k];
|
|
27
|
+
if (modVal === true) {
|
|
28
|
+
className += modPrefix + k;
|
|
29
|
+
}
|
|
30
|
+
else if (modVal) {
|
|
31
|
+
className += modPrefix + k + modValueDelimiter + modVal;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (mix !== undefined) {
|
|
37
|
+
mix = Array.isArray(mix) ? mix : [mix];
|
|
38
|
+
for (var i = 0, len = mix.length; i < len; i++) {
|
|
39
|
+
var value = mix[i];
|
|
40
|
+
// Skipping non-string values and empty strings
|
|
41
|
+
if (!value || typeof value.valueOf() !== 'string')
|
|
42
|
+
continue;
|
|
43
|
+
var mixes = value.valueOf().split(' ');
|
|
44
|
+
for (var j = 0; j < mixes.length; j++) {
|
|
45
|
+
var val = mixes[j];
|
|
46
|
+
if (val !== entityName) {
|
|
47
|
+
className += ' ' + val;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return className;
|
|
53
|
+
}
|
|
54
|
+
return function cnGenerator(b, e) {
|
|
55
|
+
return function (elemOrMods, elemModsOrBlockMix, elemMix) {
|
|
56
|
+
if (typeof elemOrMods === 'string') {
|
|
57
|
+
if (typeof elemModsOrBlockMix === 'string' || Array.isArray(elemModsOrBlockMix)) {
|
|
58
|
+
return stringify(b, elemOrMods, undefined, elemModsOrBlockMix);
|
|
59
|
+
}
|
|
60
|
+
return stringify(b, elemOrMods, elemModsOrBlockMix, elemMix);
|
|
61
|
+
}
|
|
62
|
+
return stringify(b, e, elemOrMods, elemModsOrBlockMix);
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* BEM Entity className initializer with React naming preset.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ``` ts
|
|
71
|
+
*
|
|
72
|
+
* import { cn } from '@bem-react/classname';
|
|
73
|
+
*
|
|
74
|
+
* const cat = cn('Cat');
|
|
75
|
+
*
|
|
76
|
+
* cat(); // Cat
|
|
77
|
+
* cat({ size: 'm' }); // Cat_size_m
|
|
78
|
+
* cat('Tail'); // Cat-Tail
|
|
79
|
+
* cat('Tail', { length: 'small' }); // Cat-Tail_length_small
|
|
80
|
+
*
|
|
81
|
+
* const dogPaw = cn('Dog', 'Paw');
|
|
82
|
+
*
|
|
83
|
+
* dogPaw(); // Dog-Paw
|
|
84
|
+
* dogPaw({ color: 'black', exists: true }); // Dog-Paw_color_black Dog-Paw_exists
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @see https://en.bem.info/methodology/naming-convention/#react-style
|
|
88
|
+
*/
|
|
89
|
+
var cn = withNaming({
|
|
90
|
+
e: '-',
|
|
91
|
+
m: '_',
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
export { cn, withNaming };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bem-react/classname",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.1-dev.4+591cfee",
|
|
4
4
|
"description": "BEM React ClassName",
|
|
5
5
|
"homepage": "https://github.com/bem/bem-react/tree/master/packages/classname",
|
|
6
6
|
"repository": "https://github.com/bem/bem-react",
|
|
@@ -11,20 +11,28 @@
|
|
|
11
11
|
"notation",
|
|
12
12
|
"core"
|
|
13
13
|
],
|
|
14
|
-
"main": "index.js",
|
|
15
|
-
"typings": "classname.d.ts",
|
|
16
14
|
"publishConfig": {
|
|
17
15
|
"access": "public"
|
|
18
16
|
},
|
|
19
|
-
"files": [
|
|
20
|
-
"build",
|
|
21
|
-
"classname.d.ts"
|
|
22
|
-
],
|
|
23
17
|
"license": "MPL-2.0",
|
|
24
18
|
"scripts": {
|
|
25
19
|
"prepublishOnly": "npm run build",
|
|
26
20
|
"build": "node ../../scripts/rollup/build.js",
|
|
27
21
|
"unit": "../../node_modules/.bin/jest --config ../../.config/jest/jest.config.js"
|
|
28
22
|
},
|
|
29
|
-
"
|
|
23
|
+
"files": [
|
|
24
|
+
"build",
|
|
25
|
+
"classname.d.ts"
|
|
26
|
+
],
|
|
27
|
+
"main": "./build/classname.cjs",
|
|
28
|
+
"module": "./build/classname.mjs",
|
|
29
|
+
"types": "./classname.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./classname.d.ts",
|
|
33
|
+
"import": "./build/classname.mjs",
|
|
34
|
+
"require": "./build/classname.cjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "591cfee41cfc0dce2f099f2e3d590fdf8bfbf51e"
|
|
30
38
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
'use strict';function r(r){function t(t,i,a,o){var f=i?e+t+r.e+i:e+t,v=f;if(a){var s=' '+v+r.m;for(var u in a)if(a.hasOwnProperty(u)){var p=a[u];!0===p?v+=s+u:p&&(v+=s+u+n+p)}}if(void 0!==o)for(var y=0,c=(o=Array.isArray(o)?o:[o]).length;y<c;y++){var l=o[y];if(l&&'string'==typeof l.valueOf())for(var g=l.valueOf().split(' '),d=0;d<g.length;d++){var h=g[d];h!==f&&(v+=' '+h)}}return v}var e=r.n||'',n=r.v||r.m;return function(r,e){return function(n,i,a){return'string'==typeof n?'string'==typeof i||Array.isArray(i)?t(r,n,void 0,i):t(r,n,i,a):t(r,e,n,i)}}}Object.defineProperty(exports,'__esModule',{value:!0});var t=r({e:'-',m:'_'});exports.cn=t,exports.withNaming=r;
|
package/index.js
DELETED
|
File without changes
|