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
ckl1
committed
Jul 27, 2022
1 parent
622f25a
commit dd683d7
Showing
11 changed files
with
172 additions
and
57 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,30 @@ | ||
interface Fn<T = any> { | ||
(...arg: T[]): T | ||
} | ||
|
||
/** | ||
* 查找数组对象的某个下标 | ||
* @category Array | ||
* @param ary 查找的数组 | ||
* @param fn 判断的方法 | ||
* @example | ||
* ``` typescript | ||
* findIndex([1, 2, 3], (item, i, ary) => { | ||
* return item === 2 | ||
* }) | ||
* ``` | ||
*/ | ||
export const findIndex = <T = any>(ary: Array<T>, fn: Fn): number => { | ||
if (ary.findIndex) { | ||
return ary.findIndex(fn) | ||
} | ||
let index = -1 | ||
ary.some((item: T, i: number, ary: Array<T>) => { | ||
const ret: T = fn(item, i, ary) | ||
if (ret) { | ||
index = i | ||
return ret | ||
} | ||
}) | ||
return 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,14 @@ | ||
/** | ||
* 设置Css变量 | ||
* @category Attribute | ||
* @param prop 需要设置的属性 | ||
* @param val 需要设置的值 | ||
* @param dom 需要设置的dom | ||
* @example | ||
* ``` typescript | ||
* setCssVar('--color', 'red') | ||
* ``` | ||
*/ | ||
export const setCssVar = (prop: string, val: any, dom = document.documentElement) => { | ||
dom.style.setProperty(prop, val) | ||
} |
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,37 @@ | ||
/** | ||
* 日期格式转换 | ||
* @category Date | ||
* @param time 需要转换的时间 | ||
* @param fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss | ||
* @example | ||
* ``` typescript | ||
* formatTime(new Date(), 'yyyy-MM-dd') | ||
* ``` | ||
*/ | ||
export function formatTime(time: Date | number | string, fmt: string) { | ||
if (!time) return '' | ||
else { | ||
const date = new Date(time) | ||
const o: any = { | ||
'M+': date.getMonth() + 1, | ||
'd+': date.getDate(), | ||
'H+': date.getHours(), | ||
'm+': date.getMinutes(), | ||
's+': date.getSeconds(), | ||
'q+': Math.floor((date.getMonth() + 3) / 3), | ||
S: date.getMilliseconds() | ||
} | ||
if (/(y+)/.test(fmt)) { | ||
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) | ||
} | ||
for (const k in o) { | ||
if (new RegExp('(' + k + ')').test(fmt)) { | ||
fmt = fmt.replace( | ||
RegExp.$1, | ||
RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length) | ||
) | ||
} | ||
} | ||
return fmt | ||
} | ||
} |
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
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
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,18 @@ | ||
import { expect, it } from 'vitest' | ||
import { trim, underlineToHump } from './index' | ||
|
||
it('public/trim', () => { | ||
expect(trim(' ')).equal('') | ||
expect(trim('123 ')).equal('123') | ||
expect(trim(' 123')).equal('123') | ||
expect(trim(' 123 ')).equal('123') | ||
expect(trim(' 1 2 3 ')).equal('1 2 3') | ||
}) | ||
|
||
it('public/underlineToHump', () => { | ||
expect(underlineToHump('testTest')).equal('testTest') | ||
expect(underlineToHump('test-test')).equal('testTest') | ||
expect(underlineToHump('test_test')).equal('testTest') | ||
expect(underlineToHump('Test_Test')).equal('TestTest') | ||
expect(underlineToHump('TestTest')).equal('TestTest') | ||
}) |
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,62 @@ | ||
const SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g | ||
const MOZ_HACK_REGEXP = /^moz([A-Z])/ | ||
|
||
/** | ||
* 去除两边空格 | ||
* @category String | ||
* @param s 去除空格的字符串 | ||
* @example | ||
* ``` typescript | ||
* trim(' 123 ') | ||
* ``` | ||
*/ | ||
export const trim = (s: string): string => { | ||
return (s || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '') | ||
} | ||
|
||
/** | ||
* 字符串转驼峰 | ||
* @category String | ||
* @param name 需要转换的字符串 | ||
* @example | ||
* ``` typescript | ||
* underlineToHump('test-test') | ||
* ``` | ||
*/ | ||
export const underlineToHump = (name: string): string => { | ||
return name | ||
.replace(SPECIAL_CHARS_REGEXP, function (_, __, letter, offset) { | ||
return offset ? letter.toUpperCase() : letter | ||
}) | ||
.replace(MOZ_HACK_REGEXP, 'Moz$1') | ||
} | ||
|
||
/** | ||
* 驼峰字符串转下划线 | ||
* @category String | ||
* @param str 驼峰字符串 | ||
* @example | ||
* ``` typescript | ||
* humpToUnderline('TestTest') | ||
* ``` | ||
*/ | ||
export const humpToUnderline = (str: string): string => { | ||
return str.replace(/([A-Z])/g, '-$1').toLowerCase() | ||
} | ||
|
||
/** | ||
* 随机字符串 | ||
* @category String | ||
* @example | ||
* ``` typescript | ||
* toAnyString() | ||
* ``` | ||
*/ | ||
export function toAnyString() { | ||
const str: string = 'xxxxx-xxxxx-4xxxx-yxxxx-xxxxx'.replace(/[xy]/g, (c: string) => { | ||
const r: number = (Math.random() * 16) | 0 | ||
const v: number = c === 'x' ? r : (r & 0x3) | 0x8 | ||
return v.toString() | ||
}) | ||
return str | ||
} |
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