Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
citrus327 committed Jan 23, 2024
1 parent 87cbdc5 commit 96ca468
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-otters-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@citrus327/bem-namer": major
---

feat: initial commit
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
# @citrus327/array-partition
# @citrus327/bem-namer

![Download](https://img.shields.io/npm/dw/@citrus327/array-partition)
![Version](https://img.shields.io/npm/v/@citrus327/array-partition)

partition an array into two.
![Download](https://img.shields.io/npm/dw/@citrus327/bem-namer)
![Version](https://img.shields.io/npm/v/@citrus327/bem-namer)

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])
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@citrus327/my-ts-lib",
"name": "@citrus327/bem-namer",
"version": "0.0.0",
"description": "My awesome typescript library",
"description": "A simplified version of BEM name generator",
"publishConfig": {
"access": "public"
},
Expand Down
29 changes: 28 additions & 1 deletion src/index.ts
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 };
22 changes: 19 additions & 3 deletions test/index.test.ts
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")
})
})

0 comments on commit 96ca468

Please sign in to comment.