Skip to content

Commit f47ce2c

Browse files
author
Andrii Kirmas
committed
#39 Change bem query shape to be without element
1 parent fdaa48e commit f47ce2c

File tree

8 files changed

+41
-101
lines changed

8 files changed

+41
-101
lines changed

__recipes__/index.test.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@ type CssModule = Record<
2424
ClassHash
2525
>
2626

27-
describe("go", () => {
27+
it("go", () => {
2828
const bem = classBeming<ClassNamesProperty<CssModule> & ClassNamed>()
2929
, classNamed = bem(true, {
3030
block: "m",
3131
block_el: {"m": "X"}
3232
})
33-
it("TBD", () => expect(classNamed).not.toStrictEqual({
34-
className: "block block-m block_el block_el_m-X"
35-
}))
36-
it("Now", () => expect(classNamed).toStrictEqual({
37-
className: "block block-m block_el_m block_el_m-X"
38-
}))
33+
expect(classNamed).toStrictEqual({
34+
className: "block block-m block_el block_el-m-X"
35+
})
3936
})

__recipes__/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"pretest": "npm run setup",
88
"ci": "npm ci --prefer-offline --no-audit --silent --quiet --progress=false",
99
"setup": "npm uninstall --prefer-offline --no-audit --silent --quiet --progress=false react-classnaming && npm install --prefer-offline --no-audit --silent --quiet --progress=false ../",
10-
"jest": "jest --"
10+
"jest": "jest"
1111
},
1212
"keywords": [],
1313
"author": "",

__tests__/readme.spec.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ describe("bem", () => {
180180
button: {status: "danger"},
181181
button__icon: {hover: true}
182182
})}/>
183-
).not.toSame(
183+
).toSame(
184184
<div className="${props.className} form__item button button--status--danger button__icon button__icon--hover" />
185185
)
186186
})

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
"compile": "tsc --project tsconfig.compile.json",
1717
"precompile": "rm -rf dist",
1818
"postcompile": "mkdir -p dist/types && find src -name '*.d.ts' -exec cp {} dist \\;",
19-
"spec": "cd __recipes__ && npm run test",
19+
"spec": "cd __recipes__ && npm run test --",
2020
"setup": "git config include.path ../.gitconfig && git-hooks-wrapper init",
21-
"jest": "jest --runInBand --"
21+
"jest": "jest --runInBand"
2222
},
2323
"repository": {
2424
"type": "git",

src/bem.core.test.ts

+13-49
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,21 @@ import {
88

99
describe(bem2arr.name, () => {
1010
describe("singletons", () => {
11-
const mod = undefined
1211
const suites: Record<string, [BemInGeneral, string][]> = {
13-
"block singleton": [
14-
[{block: false }, ""],
15-
[{block: true }, "block"],
16-
[{block: "mod" }, "block block--mod"],
17-
18-
[{block: {$: false }}, "block"],
19-
[{block: {$: true }}, "block"],
20-
[{block: {$: "mod" }}, "block block--mod"],
21-
[{block: {$: {} }}, "block"],
22-
23-
[{block: {$: {mod: false}}}, "block"],
24-
[{block: {$: {mod: true }}}, "block block--mod"],
25-
[{block: {$: {mod: "val"}}}, "block block--mod--val"],
26-
],
27-
"element singleton": [
28-
[{block: {el: false }}, ""],
29-
[{block: {el: true }}, "block__el"],
30-
[{block: {el: "mod" }}, "block__el block__el--mod"],
31-
[{block: {el: {} }}, "block__el"],
32-
[{block: {el: {mod: false}}}, "block__el"],
33-
[{block: {el: {mod: true }}}, "block__el block__el--mod"],
34-
[{block: {el: {mod: "val"}}}, "block__el block__el--mod--val"],
35-
],
36-
"block and el combine": [
37-
[{block: {
38-
$: {mod: true},
39-
el: true }}, "block block--mod block__el"]
40-
],
41-
"wrong shape": [
42-
//@ts-expect-error
43-
[{block: {$: {mod: {} }}}, "block block--mod"],
44-
//@ts-expect-error
45-
[{block: {el: {mod: {}} }}, "block__el block__el--mod"],
46-
],
47-
"weird stuff": [
48-
[{block: "" }, "" /* or "block" */],
49-
[{block: {} }, "" /* or "block" */],
50-
[{block: {$: "" }}, "block"],
51-
[{block: {$: {mod: "" }}}, "block"],
52-
[{block: {el: "" }}, "" /* or "block__el" */],
53-
[{block: {el: {mod: "" }}}, "block__el"],
12+
"singletons": [
13+
[{base: undefined }, "base"],
14+
[{base: false }, "base"],
15+
[{base: true }, "base"],
16+
[{base: "mod" }, "base base--mod"],
17+
//@ts-expect-error //TODO #40
18+
[{base: ["mod"] }, "base base--0--mod" /* TODO #40 "base base--mod"*/],
19+
//@ts-expect-error //TODO #40
20+
[{base: [false] }, "base"],
21+
[{base: {} }, "base"],
22+
[{base: {mod: false}}, "base"],
23+
[{base: {mod: true }}, "base base--mod"],
24+
[{base: {mod: "val"}}, "base base--mod--val"],
5425
],
55-
"undefineds": [
56-
[{block: undefined }, ""],
57-
[{block: {$: undefined }}, "block"],
58-
[{block: {$: {mod }}}, "block"],
59-
[{block: {el: undefined }}, ""],
60-
[{block: {el: {mod }}}, "block__el"],
61-
]
6226
}
6327

6428
Object.entries(suites).forEach(([title, launches]) => describe(title, () => launches

src/bem.core.ts

+15-34
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,28 @@ export {
1919
function bem2arr(query: BemInGeneral) {
2020
const $return: string[] = []
2121

22-
for (const block in query) {
23-
const blockQ = query[block]
22+
for (const base in query) {
23+
const baseQ = query[base]
24+
$return.push(base)
2425

25-
if (!blockQ)
26+
if (!baseQ)
2627
continue
27-
if (typeof blockQ !== "object") {
28-
$return.push(block)
29-
if (typeof blockQ === "string")
30-
$return.push(`${block}${modDelimiter}${blockQ}`)
28+
if (typeof baseQ !== "object") {
29+
if (typeof baseQ === "string")
30+
$return.push(`${base}${modDelimiter}${baseQ}`)
3131
continue
3232
}
3333

34-
for (const el in blockQ) {
35-
const elementQ = blockQ[el]
36-
, isBlockMod = el === blockModKey
37-
, element = isBlockMod ? block : `${block}${elementDelimiter}${el}`
38-
39-
if (!elementQ) {
40-
isBlockMod && $return.push(element)
34+
for (const mod in baseQ) {
35+
const modValue = baseQ[mod]
36+
if (!modValue)
4137
continue
42-
}
43-
44-
$return.push(element)
4538

46-
if (typeof elementQ !== "object") {
47-
if (typeof elementQ === "string")
48-
$return.push(`${element}${modDelimiter}${elementQ}`)
49-
continue
50-
}
51-
52-
for (const mod in elementQ) {
53-
const modValue = elementQ[mod]
54-
if (!modValue)
55-
continue
56-
57-
$return.push(`${element}${modDelimiter}${mod}${
58-
typeof modValue !== "string"
59-
? ""
60-
: `${modDelimiter}${modValue}`
61-
}`)
62-
}
39+
$return.push(`${base}${modDelimiter}${mod}${
40+
typeof modValue !== "string"
41+
? ""
42+
: `${modDelimiter}${modValue}`
43+
}`)
6344
}
6445
}
6546

src/bem.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("contexting", () => {
88

99
it("1", () => expect(bem({
1010
block1: true,
11-
block2: { el: true }
11+
block2__el: true
1212
})).toStrictEqual({
1313
className: "block1 block2__el"
1414
}))

src/bem.types.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,9 @@ type MVs<
7979
> = classes extends `${b}${delM}${infer MV}` ? MV : never
8080

8181
export type BemInGeneral = {
82-
[block: string]: undefined | boolean | string | {
83-
[el: string]: undefined | boolean | string
84-
// TODO #40 | (false|string)[]
85-
| {
86-
[mod: string]: undefined | boolean | string
87-
}
82+
[base: string]: undefined | boolean | string
83+
// TODO #40 | (false|string)[]
84+
| {
85+
[mod: string]: undefined | boolean | string
8886
}
8987
}

0 commit comments

Comments
 (0)