Skip to content

Commit

Permalink
style: format by prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Sep 9, 2019
1 parent f25c4db commit fad7445
Show file tree
Hide file tree
Showing 34 changed files with 1,806 additions and 1,690 deletions.
6 changes: 3 additions & 3 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": [
"@commitlint/config-conventional"
]
"extends": [
"@commitlint/config-conventional"
]
}
3 changes: 1 addition & 2 deletions docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ A set of tools for developing user interfaces using the [BEM methodology](https:

## Packages


| Package | Version | Size |
|------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`@bem-react/classname`](packages/classname) | [![npm (scoped)](https://img.shields.io/npm/v/@bem-react/classname.svg)](https://www.npmjs.com/package/@bem-react/classname) | [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@bem-react/classname.svg)](https://bundlephobia.com/result?p=@bem-react/classname) |
| [`@bem-react/classnames`](packages/classnames) | [![npm (scoped)](https://img.shields.io/npm/v/@bem-react/classnames.svg)](https://www.npmjs.com/package/@bem-react/classnames) | [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@bem-react/classnames.svg)](https://bundlephobia.com/result?p=@bem-react/classnames) |
| [`@bem-react/core`](packages/core) | [![npm (scoped)](https://img.shields.io/npm/v/@bem-react/core.svg)](https://www.npmjs.com/package/@bem-react/core) | [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@bem-react/core.svg)](https://bundlephobia.com/result?p=@bem-react/core) |
Expand Down
2 changes: 1 addition & 1 deletion environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* Eenvironment flag for development.
* @internal
*/
declare const __DEV__: boolean;
declare const __DEV__: boolean
43 changes: 23 additions & 20 deletions enzyme.config.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
'use strict';
'use strict'

const { JSDOM } = require('jsdom');
const { configure } = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
const { JSDOM } = require('jsdom')
const { configure } = require('enzyme')
const Adapter = require('enzyme-adapter-react-16')

configure({ adapter: new Adapter() });
configure({ adapter: new Adapter() })

const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;
const jsdom = new JSDOM('<!doctype html><html><body></body></html>')
const { window } = jsdom

function copyProps(src, target) {
const props = Object.getOwnPropertyNames(src)
.filter(prop => typeof target[prop] === 'undefined')
.reduce((result, prop) => ({
...result,
[prop]: Object.getOwnPropertyDescriptor(src, prop),
}), {});
Object.defineProperties(target, props);
const props = Object.getOwnPropertyNames(src)
.filter((prop) => typeof target[prop] === 'undefined')
.reduce(
(result, prop) => ({
...result,
[prop]: Object.getOwnPropertyDescriptor(src, prop),
}),
{},
)
Object.defineProperties(target, props)
}

global.window = window;
global.document = window.document;
global.window = window
global.document = window.document
global.navigator = {
userAgent: 'node.js',
};
userAgent: 'node.js',
}

copyProps(window, global);
copyProps(window, global)

global.__DEV__ = true;
global.__DEV__ = true
6 changes: 3 additions & 3 deletions mocha.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
'use strict'

// Set this flag so that ts-node can load the declarations
process.env.TS_NODE_FILES = true;
process.env.TS_NODE_PROJECT = '../../tsconfig.test.json';
process.env.TS_NODE_FILES = true
process.env.TS_NODE_PROJECT = '../../tsconfig.test.json'
28 changes: 14 additions & 14 deletions packages/classname/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ npm i -S @bem-react/classname

## Usage

``` js
import { cn } from '@bem-react/classname';
```js
import { cn } from '@bem-react/classname'

const cat = cn('Cat');
const cat = cn('Cat')

cat(); // Cat
cat({ size: 'm' }); // Cat Cat_size_m
cat('Tail'); // Cat-Tail
cat('Tail', { length: 'small' }); // Cat-Tail Cat-Tail_length_small
cat() // Cat
cat({ size: 'm' }) // Cat Cat_size_m
cat('Tail') // Cat-Tail
cat('Tail', { length: 'small' }) // Cat-Tail Cat-Tail_length_small

const dogPaw = cn('Dog', 'Paw');
const dogPaw = cn('Dog', 'Paw')

dogPaw(); // Dog-Paw
dogPaw({ color: 'black', exists: true }); // Dog-Paw Dog-Paw_color_black Dog-Paw_exists
dogPaw() // Dog-Paw
dogPaw({ color: 'black', exists: true }) // Dog-Paw Dog-Paw_color_black Dog-Paw_exists
```

## Configure

By default `classname` uses React naming preset. But it's possible to use any.

``` js
import { withNaming } from '@bem-react/classname';
```js
import { withNaming } from '@bem-react/classname'

const cn = withNaming({ n: 'ns-', e: '__', m: '_', v: '_' });
const cn = withNaming({ n: 'ns-', e: '__', m: '_', v: '_' })

cn('block', 'elem')({ theme: 'default' }); // ns-block__elem_theme_default
cn('block', 'elem')({ theme: 'default' }) // ns-block__elem_theme_default
```
154 changes: 77 additions & 77 deletions packages/classname/classname.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @@bem-react/classname
*/
export type ClassNameList = Array<string | undefined>;
export type ClassNameList = Array<string | undefined>

/**
* Allowed modifiers format.
Expand All @@ -12,49 +12,49 @@ export type ClassNameList = Array<string | undefined>;
*
* @@bem-react/classname
*/
export type NoStrictEntityMods = Record<string, string | boolean | number | undefined>;
export type NoStrictEntityMods = Record<string, string | boolean | number | undefined>

/**
* BEM Entity className initializer.
*
* @@bem-react/classname
*/
export type ClassNameInitilizer = (blockName: string, elemName?: string) => ClassNameFormatter;
export type ClassNameInitilizer = (blockName: string, elemName?: string) => ClassNameFormatter

/**
* BEM Entity className formatter.
*
* @@bem-react/classname
*/
export type ClassNameFormatter = (
elemNameOrBlockMods?: NoStrictEntityMods | string | null,
elemModsOrBlockMix?: NoStrictEntityMods | ClassNameList | null,
elemMix?: ClassNameList,
) => string;
elemNameOrBlockMods?: NoStrictEntityMods | string | null,
elemModsOrBlockMix?: NoStrictEntityMods | ClassNameList | null,
elemMix?: ClassNameList,
) => string

/**
* Settings for the naming convention.
* @@bem-react/classname
*/
export interface IPreset {
/**
* Global namespace.
*
* @example `omg-Bem-Elem_mod_val`
*/
n?: string;
/**
* Elem's delimiter.
*/
e?: string;
/**
* Modifier's delimiter.
*/
m?: string;
/**
* Modifier value delimiter.
*/
v?: string;
/**
* Global namespace.
*
* @example `omg-Bem-Elem_mod_val`
*/
n?: string
/**
* Elem's delimiter.
*/
e?: string
/**
* Modifier's delimiter.
*/
m?: string
/**
* Modifier value delimiter.
*/
v?: string
}

/**
Expand All @@ -75,66 +75,66 @@ export interface IPreset {
* @@bem-react/classname
*/
export function withNaming(preset: IPreset): ClassNameInitilizer {
const nameSpace = preset.n || '';
const modValueDelimiter = preset.v || preset.m;
const nameSpace = preset.n || ''
const modValueDelimiter = preset.v || preset.m

function stringify(b: string, e?: string, m?: NoStrictEntityMods | null, mix?: ClassNameList) {
const entityName = e ? (nameSpace + b + preset.e + e) : (nameSpace + b);
let className = entityName;
function stringify(b: string, e?: string, m?: NoStrictEntityMods | null, mix?: ClassNameList) {
const entityName = e ? nameSpace + b + preset.e + e : nameSpace + b
let className = entityName

if (m) {
const modPrefix = ' ' + className + preset.m;
if (m) {
const modPrefix = ' ' + className + preset.m

for (let k in m) {
if (m.hasOwnProperty(k)) {
const modVal = m[k];
for (let k in m) {
if (m.hasOwnProperty(k)) {
const modVal = m[k]

if (modVal === true) {
className += modPrefix + k;
} else if (modVal) {
className += modPrefix + k + modValueDelimiter + modVal;
}
}
}
if (modVal === true) {
className += modPrefix + k
} else if (modVal) {
className += modPrefix + k + modValueDelimiter + modVal
}
}
}
}

if (mix !== undefined) {
for (let i = 0, len = mix.length; i < len; i++) {
const value = mix[i];
if (mix !== undefined) {
for (let i = 0, len = mix.length; i < len; i++) {
const value = mix[i]

// Skipping non-string values and empty strings
if (typeof value !== 'string' || !value) continue;
// Skipping non-string values and empty strings
if (typeof value !== 'string' || !value) continue

const mixes = value.split(' ');
for (let j = 0; j < mixes.length; j++) {
const val = mixes[j];
if (val !== entityName) {
className += ' ' + val;
}
}
}
const mixes = value.split(' ')
for (let j = 0; j < mixes.length; j++) {
const val = mixes[j]
if (val !== entityName) {
className += ' ' + val
}
}

return className;
}
}

return function cnGenerator(b: string, e?: string): ClassNameFormatter {
return (
elemOrMods?: NoStrictEntityMods | string | null,
elemModsOrBlockMix?: NoStrictEntityMods | ClassNameList | null,
elemMix?: ClassNameList,
) => {
if (typeof elemOrMods === 'string') {
if (Array.isArray(elemModsOrBlockMix)) {
return stringify(b, elemOrMods, undefined, elemModsOrBlockMix);
} else {
return stringify(b, elemOrMods, elemModsOrBlockMix, elemMix);
}
} else {
return stringify(b, e, elemOrMods, elemModsOrBlockMix as ClassNameList);
}
};
};
return className
}

return function cnGenerator(b: string, e?: string): ClassNameFormatter {
return (
elemOrMods?: NoStrictEntityMods | string | null,
elemModsOrBlockMix?: NoStrictEntityMods | ClassNameList | null,
elemMix?: ClassNameList,
) => {
if (typeof elemOrMods === 'string') {
if (Array.isArray(elemModsOrBlockMix)) {
return stringify(b, elemOrMods, undefined, elemModsOrBlockMix)
} else {
return stringify(b, elemOrMods, elemModsOrBlockMix, elemMix)
}
} else {
return stringify(b, e, elemOrMods, elemModsOrBlockMix as ClassNameList)
}
}
}
}

/**
Expand Down Expand Up @@ -163,6 +163,6 @@ export function withNaming(preset: IPreset): ClassNameInitilizer {
* @@bem-react/classname
*/
export const cn = withNaming({
e: '-',
m: '_',
});
e: '-',
m: '_',
})
6 changes: 3 additions & 3 deletions packages/classname/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
'use strict'

if (process.env.NODE_ENV === 'production') {
module.exports = require('./build/classname.production.min.js');
module.exports = require('./build/classname.production.min.js')
} else {
module.exports = require('./build/classname.development.js');
module.exports = require('./build/classname.development.js')
}
Loading

0 comments on commit fad7445

Please sign in to comment.