@dr/bem-helper
Advanced tools
| const bem = require("../src/index"); | ||
| describe("bem", () => { | ||
| test("block", () => { | ||
| expect(bem("block")).toBe("block"); | ||
| }); | ||
| test("block and element", () => { | ||
| expect(bem("block", "element")).toBe("element block__element"); | ||
| }); | ||
| test("block with modifier with a value of true", () => { | ||
| expect(bem("block", {modifier: true})).toBe("block block--modifier"); | ||
| }); | ||
| test("block with modifier with a value of false", () => { | ||
| expect(bem("block", {modifier: false})).toBe("block"); | ||
| }); | ||
| test("block with modifier with a value of null", () => { | ||
| expect(bem("block", {modifier: null})).toBe("block"); | ||
| }); | ||
| test("block with modifier with a value of undefined", () => { | ||
| var value; | ||
| expect(bem("block", {modifier: value})).toBe("block"); | ||
| }); | ||
| test("block with modifier with a value of a string", () => { | ||
| expect(bem("block", {modifier: "value"})).toBe("block block--modifier-value"); | ||
| }); | ||
| test("block with modifier with a value of an empty string", () => { | ||
| expect(bem("block", {modifier: ""})).toBe("block"); | ||
| }); | ||
| test("block with modifier with a number value", () => { | ||
| expect(bem("block", {modifier: 200})).toBe("block block--modifier-200"); | ||
| }); | ||
| test("block with modifier with a value of 0", () => { | ||
| expect(bem("block", {modifier: 0})).toBe("block block--modifier-0"); | ||
| }); | ||
| test("block and element with modifier", () => { | ||
| expect(bem("block", "element", {modifier: true})).toBe("element block__element element--modifier block__element--modifier"); | ||
| }); | ||
| test("block and element with multiple modifiers", () => { | ||
| expect(bem("block", "element", {modifier1: true, modifier2: "value"})).toBe("element block__element element--modifier1 block__element--modifier1 element--modifier2-value block__element--modifier2-value"); | ||
| }); | ||
| test("block with modifier and element", () => { | ||
| expect(bem("block", {modifier: true}, "element")).toBe("element block__element block--modifier__element"); | ||
| }); | ||
| test("block with modifier and element with modifier", () => { | ||
| expect(bem("block", {modifier: true}, "element", {modifier: true})).toBe("element block__element block--modifier__element element--modifier block__element--modifier block--modifier__element--modifier"); | ||
| }); | ||
| }); | ||
| describe("bem.single", () => { | ||
| test("block", () => { | ||
| expect(bem.single("block")).toBe("block"); | ||
| }); | ||
| test("block and element", () => { | ||
| expect(bem.single("block", "element")).toBe("block__element"); | ||
| }); | ||
| test("block with modifier with a value of true", () => { | ||
| expect(bem.single("block", {modifier: true})).toBe("block--modifier"); | ||
| }); | ||
| test("block with modifier with a value of false", () => { | ||
| expect(bem.single("block", {modifier: false})).toBe("block"); | ||
| }); | ||
| test("block with modifier with a value of null", () => { | ||
| expect(bem.single("block", {modifier: null})).toBe("block"); | ||
| }); | ||
| test("block with modifier with a value of undefined", () => { | ||
| var value; | ||
| expect(bem.single("block", {modifier: value})).toBe("block"); | ||
| }); | ||
| test("block with modifier with a value of a string", () => { | ||
| expect(bem.single("block", {modifier: "value"})).toBe("block--modifier-value"); | ||
| }); | ||
| test("block with modifier with a value of an empty string", () => { | ||
| expect(bem.single("block", {modifier: ""})).toBe("block"); | ||
| }); | ||
| test("block with modifier with a number value", () => { | ||
| expect(bem.single("block", {modifier: 200})).toBe("block--modifier-200"); | ||
| }); | ||
| test("block with modifier with a value of 0", () => { | ||
| expect(bem.single("block", {modifier: 0})).toBe("block--modifier-0"); | ||
| }); | ||
| test("block and element with modifier", () => { | ||
| expect(bem.single("block", "element", {modifier: true})).toBe("block__element--modifier"); | ||
| }); | ||
| test("block with modifier and element", () => { | ||
| expect(bem.single("block", {modifier: true}, "element")).toBe("block--modifier__element"); | ||
| }); | ||
| test("block with modifier and element with modifier", () => { | ||
| expect(bem.single("block", {modifier: true}, "element", {modifier: true})).toBe("block--modifier__element--modifier"); | ||
| }); | ||
| }); |
+3
| { | ||
| "presets": ["es2015"] | ||
| } |
+1
-1
@@ -45,3 +45,3 @@ "use strict"; | ||
| if (value !== false && value !== null && typeof value !== "undefined") { | ||
| if (value !== false && value !== "" && value !== null && typeof value !== "undefined") { | ||
| result.push("--" + modifier + (value !== true ? "-" + value : "")); | ||
@@ -48,0 +48,0 @@ } |
+8
-4
| { | ||
| "name": "@dr/bem-helper", | ||
| "version": "1.1.6", | ||
| "version": "1.2.0", | ||
| "description": "Helper to create BEM-style classnames", | ||
| "main": "lib/index.js", | ||
| "scripts": { | ||
| "build": "babel src -d lib --presets=es2015", | ||
| "prepublish": "npm run build" | ||
| "test": "jest", | ||
| "build": "babel src -d lib", | ||
| "prepublish": "npm test && npm run build" | ||
| }, | ||
@@ -27,4 +28,7 @@ "repository": { | ||
| "babel-cli": "^6.18.0", | ||
| "babel-preset-es2015": "^6.18.0" | ||
| "babel-jest": "^17.0.2", | ||
| "babel-polyfill": "^6.16.0", | ||
| "babel-preset-es2015": "^6.18.0", | ||
| "jest": "^17.0.2" | ||
| } | ||
| } |
+4
-4
@@ -17,3 +17,3 @@ # bem-helper | ||
| var className = bem("dr-module", "list"); | ||
| // className === "dr-module__list" | ||
| // className === "list dr-module__list" | ||
@@ -26,3 +26,3 @@ var className = bem("dr-module", { loaded: true }); | ||
| var className = bem("dr-module", "list", { expanded: true }); | ||
| // className === "dr-module__list dr-module__list--expanded" | ||
| // className === "list dr-module__list list--expanded dr-module__list--expanded" | ||
@@ -39,3 +39,3 @@ | ||
| var className = bem("dr-module", "list", { expanded: true, rated: 3 }); | ||
| // className === "dr-module__list dr-module__list--expanded dr-module__list--rated-3" | ||
| // className === "list dr-module__list list--expanded dr-module__list--expanded list--rated-3 dr-module__list--rated-3" | ||
@@ -56,3 +56,3 @@ | ||
| var className = boundBem("list", { expanded: true }); | ||
| // className === "dr-module__list dr-module__list--expanded" | ||
| // className === "list dr-module__list list--expanded dr-module__list--expanded" | ||
@@ -59,0 +59,0 @@ var boundSingle = bem.single.bind(null, block); |
+1
-1
@@ -37,3 +37,3 @@ function bem (...args) { | ||
| function modifierReducer (result, [modifier, value]) { | ||
| if (value !== false && value !== null && typeof value !== "undefined") { | ||
| if (value !== false && value !== "" && value !== null && typeof value !== "undefined") { | ||
| result.push(`--${modifier}${(value !== true) ? `-${value}` : "" }`); | ||
@@ -40,0 +40,0 @@ } |
11140
64.19%8
33.33%186
87.88%5
150%