-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support ensureSuffix, ensurePrefix, refactor project structrue
- Loading branch information
Showing
127 changed files
with
1,234 additions
and
953 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
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,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` | |
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,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` | |
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,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') | ||
``` |
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,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` | |
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,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` | |
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,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') | ||
``` |
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
export function at<T>(arr: T[], index: number): T | undefined { | ||
if (!arr.length) { | ||
return | ||
} | ||
|
||
if (index < 0) { | ||
index += arr.length | ||
} | ||
|
||
return arr[index] | ||
} |
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,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 | ||
} |
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,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] | ||
} |
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,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' |
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 @@ | ||
import { isArray } from '../general' | ||
|
||
export function normalizeToArray<T>(value: T | T[]) { | ||
return isArray(value) ? value : [value] | ||
} |
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,3 @@ | ||
export function removeArrayBlank<T>(arr: Array<T | null | undefined>) { | ||
return arr.filter((item) => item != null) as T[] | ||
} |
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,3 @@ | ||
export function removeArrayEmpty<T>(arr: Array<T | null | undefined | ''>) { | ||
return arr.filter((item) => item != null && item !== '') as T[] | ||
} |
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,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) | ||
} | ||
} | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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,3 @@ | ||
export function uniq<T>(arr: T[]) { | ||
return [...new Set(arr)] | ||
} |
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,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 | ||
}, []) | ||
} |
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 @@ | ||
import { cloneDeepWith } from './cloneDeepWith' | ||
|
||
export function cloneDeep<T>(value: T): T { | ||
return cloneDeepWith(value, () => undefined) | ||
} |
Oops, something went wrong.