generated from citrus327/ts-lib-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
67 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@citrus327/bem-namer": major | ||
--- | ||
|
||
feat: initial commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
# @citrus327/array-partition | ||
# @citrus327/bem-namer | ||
|
||
 | ||
 | ||
|
||
partition an array into two. | ||
 | ||
 | ||
|
||
A simplified version of BEM name generator | ||
|
||
## Installation | ||
|
||
```sh | ||
pnpm install @citrus327/array-partition | ||
pnpm install @citrus327/bem-namer | ||
``` | ||
|
||
## Usage | ||
|
||
```ts | ||
const arr = [1, 2, 3, 4, 5, 6] | ||
const [target, rest] = partition(arr, (o) => o % 2 === 0) | ||
import { bem } from '@citrus327/bem-namer'; | ||
|
||
const { b, m } = bem("user-card") | ||
|
||
expect(b()).toBe("user-card") | ||
expect(b("container")).toBe("user-card-container") | ||
expect(m("primary")).toBe("user-card--primary") | ||
expect(m("container", "primary")).toBe("user-card-container--primary") | ||
|
||
expect(target).toEqual([2, 4, 6]) | ||
expect(rest).toEqual([1, 3, 5]) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,28 @@ | ||
export const foo = 'foo' | ||
const join = (a: string, b: string, seperator: string) => { | ||
return [a, b].filter(Boolean).join(seperator); | ||
}; | ||
|
||
|
||
const bem = (baseClassName = '') => ({ | ||
b: (block?: string) => { | ||
if (!block) return baseClassName; | ||
return `${baseClassName}-${block}`; | ||
}, | ||
m: (block = '', modifier = '') => { | ||
let _modifier = modifier; | ||
let _block = block; | ||
|
||
if (!_modifier && _block) { | ||
_modifier = _block; | ||
_block = ''; | ||
} | ||
|
||
if (!_block && _modifier) { | ||
return join(baseClassName, _modifier, '--'); | ||
} | ||
|
||
return join(join(baseClassName, block, '-'), modifier, '--'); | ||
}, | ||
}); | ||
|
||
export { bem }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
import { it, describe, expect } from "vitest" | ||
import { foo } from "../src" | ||
import { bem } from "../src" | ||
|
||
describe("simple", () => { | ||
it("should equal", () => { | ||
expect(foo).toBe("foo") | ||
it("should equal with initials", () => { | ||
const { b } = bem("user-card") | ||
expect(b()).toBe("user-card") | ||
}) | ||
|
||
it("should add block", () => { | ||
const { b } = bem("user-card") | ||
expect(b("container")).toBe("user-card-container") | ||
}) | ||
|
||
it("should add modifier", () => { | ||
const { m } = bem("user-card") | ||
expect(m("primary")).toBe("user-card--primary") | ||
}) | ||
|
||
it("should add modifier with block", () => { | ||
const { m } = bem("user-card") | ||
expect(m("container", "primary")).toBe("user-card-container--primary") | ||
}) | ||
}) |