Skip to content

Commit

Permalink
feat: support ensureSuffix, ensurePrefix, refactor project structrue
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Nov 5, 2024
1 parent c7a8773 commit 475cdde
Show file tree
Hide file tree
Showing 127 changed files with 1,234 additions and 953 deletions.
3 changes: 2 additions & 1 deletion docs/.vitepress/items/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const utilItems = [
{ text: 'mitt', link: '/util/mitt' },
{ text: 'copyText', link: '/util/copy-text' },
{ text: 'storage', link: '/util/storage' },
{ text: 'classes', link: '/util/classes' },
{ text: 'createNamespaceFn', link: '/util/create-namespace-fn' },
{ text: 'raf', link: '/util/raf' },
Expand All @@ -16,5 +18,4 @@ export const utilItems = [
{ text: 'getAllParentScroller', link: '/util/get-all-parent-scroller' },
{ text: 'prettyJSONObject', link: '/util/pretty-JSON-object' },
{ text: 'tryParseJSON', link: '/util/try-parse-JSON' },
{ text: 'copyText', link: '/util/copy-text' },
]
25 changes: 25 additions & 0 deletions docs/string/ensure-prefix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ensurePrefix

Ensure that a prefix exists in the `string`, and add it if it does not exist

### Usage

```ts
import { ensurePrefix } from 'rattail'

ensurePrefix('prefix-hello-world', 'prefix-') // return 'prefix-hello-world'
ensurePrefix('hello-world', 'prefix-') // return 'prefix-hello-world'
```

### Arguments

| Arg | Type | Defaults |
| -------- | :------: | -------: |
| `value` | `string` | |
| `prefix` | `string` | |

### Return

| Type |
| :------: |
| `string` |
25 changes: 25 additions & 0 deletions docs/string/ensure-suffix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ensureSuffix

Ensure that a suffix exists in the `string`, and add it if it does not exist

### Usage

```ts
import { ensureSuffix } from 'rattail'

ensureSuffix('hello-world-suffix', '-suffix') // return 'hello-world-suffix'
ensureSuffix('hello-world', '-suffix') // return 'hello-world-suffix'
```

### Arguments

| Arg | Type | Defaults |
| -------- | :------: | -------: |
| `value` | `string` | |
| `suffix` | `string` | |

### Return

| Type |
| :------: |
| `string` |
25 changes: 25 additions & 0 deletions docs/util/storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# storage

Enhance `localStorage` and `sessionStorage`, support automatic json `stringify` and `parse` of data, and retain all native APIs.

### Usage

#### localStorage

```js
import { localStorage } from 'rattail'

localStorage.set('key', { a: 1 }) // automatic json stringify
localStorage.get('key') // return { a: 1 }, automatic json parse
localStorage.remove('key') // same with localStorage.removeItem('key')
```

#### sessionStorage

```js
import { sessionStorage } from 'rattail'

sessionStorage.set('key', { a: 1 }) // automatic json stringify
sessionStorage.get('key') // return { a: 1 }, automatic json parse
sessionStorage.remove('key') // same with sessionStorage.removeItem('key')
```
25 changes: 25 additions & 0 deletions docs/zh/string/ensure-prefix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ensurePrefix

确保`字符串`存在某个前缀,如果不存在则添加该前缀。

### 使用

```ts
import { ensurePrefix } from 'rattail'

ensurePrefix('prefix-hello-world', 'prefix-') // return 'prefix-hello-world'
ensurePrefix('hello-world', 'prefix-') // return 'prefix-hello-world'
```

### 参数列表

| 参数 | 类型 | 默认值 |
| -------- | :------: | -----: |
| `value` | `string` | |
| `prefix` | `string` | |

### 返回值

| 类型 |
| :------: |
| `string` |
25 changes: 25 additions & 0 deletions docs/zh/string/ensure-suffix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ensureSuffix

确保`字符串`存在某个后缀,如果不存在则添加该后缀。

### 使用

```ts
import { ensureSuffix } from 'rattail'

ensureSuffix('hello-world-suffix', '-suffix') // return 'hello-world-suffix'
ensureSuffix('hello-world', '-suffix') // return 'hello-world-suffix'
```

### 参数列表

| 参数 | 类型 | 默认值 |
| -------- | :------: | -----: |
| `value` | `string` | |
| `suffix` | `string` | |

### 返回值

| 类型 |
| :------: |
| `string` |
25 changes: 25 additions & 0 deletions docs/zh/util/storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# storage

增强 `localStorage``sessionStorage`,支持数据的自动 json `stringify``parse`,并保留所有原生 API。

### 使用

#### localStorage

```js
import { localStorage } from 'rattail'

localStorage.set('key', { a: 1 }) // 自动 json stringify
localStorage.get('key') // return { a: 1 },自动 json parse
localStorage.remove('key') // 等价于 localStorage.removeItem('key')
```

#### sessionStorage

```js
import { sessionStorage } from 'rattail'

sessionStorage.set('key', { a: 1 }) // 自动 json stringify
sessionStorage.get('key') // return { a: 1 },自动 json parse
sessionStorage.remove('key') // 等价于 sessionStorage.removeItem('key')
```
97 changes: 0 additions & 97 deletions src/array.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/array/at.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function at<T>(arr: T[], index: number): T | undefined {
if (!arr.length) {
return
}

if (index < 0) {
index += arr.length
}

return arr[index]
}
15 changes: 15 additions & 0 deletions src/array/chunk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { clamp } from '../number'

export function chunk<T>(arr: T[], size = 1): T[][] {
size = clamp(size, 1, arr.length)

const result: T[][] = []
let index = 0

while (index < arr.length) {
result.push(arr.slice(index, index + size))
index += size
}

return result
}
19 changes: 19 additions & 0 deletions src/array/find.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function find<T>(
arr: Array<T>,
fn: (item: T, index: number, array: Array<T>) => any,
from: 'start' | 'end' = 'start',
): [T, number] | [null, -1] {
let i = from === 'start' ? 0 : arr.length - 1

while (arr.length > 0 && i >= 0 && i <= arr.length - 1) {
const flag = fn(arr[i], i, arr)

if (flag) {
return [arr[i], i]
}

from === 'start' ? i++ : i--
}

return [null, -1]
}
11 changes: 11 additions & 0 deletions src/array/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export * from './at'
export * from './chunk'
export * from './removeItem'
export * from './toggleItem'
export * from './uniq'
export * from './uniqBy'
export * from './find'
export * from './shuffle'
export * from './removeArrayBlank'
export * from './removeArrayEmpty'
export * from './normalizeToArray'
5 changes: 5 additions & 0 deletions src/array/normalizeToArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { isArray } from '../general'

export function normalizeToArray<T>(value: T | T[]) {
return isArray(value) ? value : [value]
}
3 changes: 3 additions & 0 deletions src/array/removeArrayBlank.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function removeArrayBlank<T>(arr: Array<T | null | undefined>) {
return arr.filter((item) => item != null) as T[]
}
3 changes: 3 additions & 0 deletions src/array/removeArrayEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function removeArrayEmpty<T>(arr: Array<T | null | undefined | ''>) {
return arr.filter((item) => item != null && item !== '') as T[]
}
8 changes: 8 additions & 0 deletions src/array/removeItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function removeItem<T>(arr: Array<T>, item: T) {
if (arr.length) {
const index: number = arr.indexOf(item)
if (index > -1) {
return arr.splice(index, 1)
}
}
}
7 changes: 7 additions & 0 deletions src/array/shuffle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function shuffle<T>(arr: T[]): T[] {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[arr[i], arr[j]] = [arr[j], arr[i]]
}
return arr
}
6 changes: 6 additions & 0 deletions src/array/toggleItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { removeItem } from './removeItem'

export function toggleItem<T>(arr: Array<T>, item: T) {
arr.includes(item) ? removeItem(arr, item) : arr.push(item)
return arr
}
3 changes: 3 additions & 0 deletions src/array/uniq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function uniq<T>(arr: T[]) {
return [...new Set(arr)]
}
11 changes: 11 additions & 0 deletions src/array/uniqBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function uniqBy<T>(arr: T[], fn: (a: T, b: T) => boolean): T[] {
return arr.reduce((ret: T[], i: T) => {
const index = ret.findIndex((j: T) => fn(i, j))

if (index === -1) {
ret.push(i)
}

return ret
}, [])
}
5 changes: 5 additions & 0 deletions src/collection/cloneDeep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cloneDeepWith } from './cloneDeepWith'

export function cloneDeep<T>(value: T): T {
return cloneDeepWith(value, () => undefined)
}
Loading

0 comments on commit 475cdde

Please sign in to comment.