Skip to content

Commit bd5910b

Browse files
authored
feat: customizable delimiters via new options (#17)
- `elementDelimiter` - `modifierDelimiter`
1 parent e734bc6 commit bd5910b

File tree

3 files changed

+105
-21
lines changed

3 files changed

+105
-21
lines changed

README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![dependencies Status](https://david-dm.org/ybiquitous/bem-ts/status.svg)](https://david-dm.org/ybiquitous/bem-ts)
66
[![MIT License](https://img.shields.io/github/license/mashape/apistatus.svg)](LICENSE)
77

8-
BEM class names generator for TypeScript
8+
BEM class names generator for TypeScript.
99

1010
Inspired by [`bem-cn`](https://npm.im/bem-cn).
1111

@@ -40,6 +40,27 @@ b("element", { a: true, b: false, c: true });
4040
//=> "block__element--a block__element--c"
4141
```
4242

43+
### `elementDelimiter`
44+
45+
```ts
46+
const b = block("block", { elementDelimiter: "_" });
47+
48+
b("element");
49+
//=> "block_element"
50+
```
51+
52+
### `modifierDelimiter`
53+
54+
```ts
55+
const b = block("block", { modifierDelimiter: "-" });
56+
57+
b({ a: true });
58+
//=> "block-a"
59+
60+
b("element", { a: true });
61+
//=> "block__element-a"
62+
```
63+
4364
## Install
4465

4566
```sh

index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
type Modifiers = { [key: string]: boolean; };
22

3-
export default function bem(block: string) {
3+
export default function bem(
4+
block: string,
5+
{ elementDelimiter = "__", modifierDelimiter = "--" } = {},
6+
) {
47
return (elementOrModifiers?: string | Modifiers, modifiers?: Modifiers) => {
58
if (!elementOrModifiers) {
69
return block;
@@ -10,7 +13,7 @@ export default function bem(block: string) {
1013
let mods = modifiers;
1114

1215
if (typeof elementOrModifiers === "string") {
13-
base = `${base}__${elementOrModifiers}`;
16+
base = `${base}${elementDelimiter}${elementOrModifiers}`;
1417
} else {
1518
mods = elementOrModifiers;
1619
}
@@ -21,7 +24,7 @@ export default function bem(block: string) {
2124

2225
return Object.keys(mods)
2326
.filter((mod) => mods && mods[mod])
24-
.map((mod) => `${base}--${mod}`)
27+
.map((mod) => `${base}${modifierDelimiter}${mod}`)
2528
.join(" ");
2629
};
2730
}

test.ts

+77-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,88 @@
11
import block from ".";
22

3-
const b = block("block");
3+
describe("default", () => {
4+
const b = block("block");
45

5-
it("returns block", () => {
6-
expect(b()).toBe("block");
7-
});
6+
it("returns block", () => {
7+
expect(b()).toBe("block");
8+
});
89

9-
it("returns block with modifier", () => {
10-
expect(b({ a: true, b: false })).toBe("block--a");
11-
});
10+
it("returns block with modifier", () => {
11+
expect(b({ a: true, b: false })).toBe("block--a");
12+
});
1213

13-
it("returns block with multiple modifiers", () => {
14-
expect(b({ a: true, b: false, c: true })).toBe("block--a block--c");
15-
});
14+
it("returns block with multiple modifiers", () => {
15+
expect(b({ a: true, b: false, c: true })).toBe("block--a block--c");
16+
});
17+
18+
it("returns block with element", () => {
19+
expect(b("element")).toBe("block__element");
20+
});
21+
22+
it("returns block with element and modifier", () => {
23+
expect(b("element", { a: true, b: false })).toBe("block__element--a");
24+
});
1625

17-
it("returns block with element", () => {
18-
expect(b("element")).toBe("block__element");
26+
it("returns block with element and multiple modifiers", () => {
27+
expect(b("element", { a: true, b: false, c: true }))
28+
.toBe("block__element--a block__element--c");
29+
});
1930
});
2031

21-
it("returns block with element and modifier", () => {
22-
expect(b("element", { a: true, b: false })).toBe("block__element--a");
32+
describe("`elementDelimiter` option", () => {
33+
const b = block("block", { elementDelimiter: "_" });
34+
35+
it("returns block", () => {
36+
expect(b()).toBe("block");
37+
});
38+
39+
it("returns block with modifier", () => {
40+
expect(b({ a: true, b: false })).toBe("block--a");
41+
});
42+
43+
it("returns block with multiple modifiers", () => {
44+
expect(b({ a: true, b: false, c: true })).toBe("block--a block--c");
45+
});
46+
47+
it("returns block with element", () => {
48+
expect(b("element")).toBe("block_element");
49+
});
50+
51+
it("returns block with element and modifier", () => {
52+
expect(b("element", { a: true, b: false })).toBe("block_element--a");
53+
});
54+
55+
it("returns block with element and multiple modifiers", () => {
56+
expect(b("element", { a: true, b: false, c: true }))
57+
.toBe("block_element--a block_element--c");
58+
});
2359
});
2460

25-
it("returns block with element and multiple modifiers", () => {
26-
expect(b("element", { a: true, b: false, c: true }))
27-
.toBe("block__element--a block__element--c");
61+
describe("`modifierDelimiter` option", () => {
62+
const b = block("block", { modifierDelimiter: "-" });
63+
64+
it("returns block", () => {
65+
expect(b()).toBe("block");
66+
});
67+
68+
it("returns block with modifier", () => {
69+
expect(b({ a: true, b: false })).toBe("block-a");
70+
});
71+
72+
it("returns block with multiple modifiers", () => {
73+
expect(b({ a: true, b: false, c: true })).toBe("block-a block-c");
74+
});
75+
76+
it("returns block with element", () => {
77+
expect(b("element")).toBe("block__element");
78+
});
79+
80+
it("returns block with element and modifier", () => {
81+
expect(b("element", { a: true, b: false })).toBe("block__element-a");
82+
});
83+
84+
it("returns block with element and multiple modifiers", () => {
85+
expect(b("element", { a: true, b: false, c: true }))
86+
.toBe("block__element-a block__element-c");
87+
});
2888
});

0 commit comments

Comments
 (0)