Skip to content

Commit

Permalink
fix: fixed type error
Browse files Browse the repository at this point in the history
  • Loading branch information
vtrbo committed Nov 30, 2023
1 parent f44b212 commit 467406d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 56 deletions.
4 changes: 2 additions & 2 deletions packages/arr/src/arr.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { isArray } from '@vtrbo/utils-is'

export function toArray<T>(data?: T | T[]): T[] {
export function toArray<T = any>(data?: T | T[]): T[] {
data = data ?? []
return isArray(data) ? data : [data]
}

export function groupBy<T>(list: T[], fn: (single: T) => any): Map<string, T[]> {
export function groupBy<T = any>(list: T[], fn: (single: T) => any): Map<string, T[]> {
const map = new Map<string, T[]>()
list.forEach((s: T) => {
const key = fn(s)
Expand Down
4 changes: 1 addition & 3 deletions packages/color/src/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export function hexToRgba(hex: string): string {
const getDouble = (start: number, end: number) => Number.parseInt(`0x${hex.slice(start, end)}`)
const getAlpha = (start: number, end: number, fn: typeof getSingle | typeof getDouble) => Math.round(fn(start, end) / 255 * 100) / 100

const hexMap: {
[key: number]: string
} = {
const hexMap: { [key: number]: string } = {
4: `rgb(${getSingle(1, 2)}, ${getSingle(2, 3)}, ${getSingle(3, 4)})`,
5: `rgba(${getSingle(1, 2)}, ${getSingle(2, 3)}, ${getSingle(3, 4)}, ${getAlpha(4, 5, getSingle)})`,
7: `rgb(${getDouble(1, 3)}, ${getDouble(3, 5)}, ${getDouble(5, 7)})`,
Expand Down
99 changes: 65 additions & 34 deletions packages/is/src/is.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,103 @@
import type { Recordable } from '@vtrbo/utils-tool'
import { toRawType } from '@vtrbo/utils-tool'

export function isType(data: any, type: string): boolean {
export function isType(data: unknown, type: string): boolean {
return toRawType(data).toLowerCase() === type.toLowerCase()
}

export function isString(data: any): data is string {
return isType(data, 'String')
export function isString(data: unknown): data is string {
return isType(data, 'string')
}

export function isNumber(data: any): data is number {
return isType(data, 'Number')
export function isNumber(data: unknown): data is number {
return isType(data, 'number')
}

export function isBoolean(data: any): data is boolean {
return isType(data, 'Boolean')
export function isNaN(data: unknown): data is number {
return Number.isNaN(data)
}

export function isObject(data: any): data is Record<any, any> {
return isType(data, 'Object')
export function isBoolean(data: unknown): data is boolean {
return isType(data, 'boolean')
}

export function isArray(data: any): data is any[] {
return isType(data, 'Array')
export function isTrue(data: unknown): data is true {
return data === true
}

export function isFunction<T extends Function>(data: any): data is T {
return isType(data, 'Function')
export function isFalse(data: unknown): data is false {
return data === false
}

export function isRegExp(data: any): data is RegExp {
return isType(data, 'RegExp')
export function isSymbol(data: unknown): data is symbol {
return isType(data, 'symbol')
}

export function isDate(data: any): data is Date {
return isType(data, 'Date')
export function isBigInt(data: unknown): data is bigint {
return isType(data, 'bigint')
}

export function isUndefined(data: any): data is undefined {
return isType(data, 'Undefined')
export function isObject<T extends Recordable = Recordable>(data: unknown): data is T {
return isType(data, 'object')
}

export function isNull(data: any): data is null {
return isType(data, 'Null')
export function isArray<T = any>(data: unknown): data is T[] {
return isType(data, 'array')
}

export function isSet(data: any): data is Set<any> {
return isType(data, 'Set')
export function isFunction<T extends Function = any>(data: unknown): data is T {
return isType(data, 'function')
}

export function isMap(data: any): data is Map<any, any> {
return isType(data, 'Map')
export function isPromise<T = any>(data: unknown): data is Promise<T> {
return (
!!data
&& isFunction((data as any).then)
&& isFunction((data as any).catch)
)
}

export function isRegExp(data: unknown): data is RegExp {
return isType(data, 'regexp')
}

export function isDate(data: unknown): data is Date {
return isType(data, 'date')
}

export function isUndefined(data: unknown): data is undefined {
return isType(data, 'undefined')
}

export function isNull(data: unknown): data is null {
return isType(data, 'null')
}

export function isSet<T = any>(data: unknown): data is Set<T> {
return isType(data, 'set')
}

export function isMap<K = any, V = any>(data: unknown): data is Map<K, V> {
return isType(data, 'map')
}

export const isClient = !isUndefined(window) && !isUndefined(document)

export function isHttp(url: string): boolean {
return url.startsWith('http://') || url.startsWith('https://')
}

export function isLowerCase(str: string) {
export function isLowerCase(str: string): boolean {
const reg = /^[a-z]+$/
return reg.test(str)
}

export function isUpperCase(str: string) {
export function isUpperCase(str: string): boolean {
const reg = /^[A-Z]+$/
return reg.test(str)
}

export function isMobile(mobile: string) {
export function isMobile(mobile: string): boolean {
const reg = /^1[3-9]\d{9}$/
return reg.test(mobile)
}
Expand All @@ -80,14 +111,14 @@ export function isColor(color: string, type: 'HEX' | 'RGB' | 'RGBA'): boolean {
return typeMap[type].test(color)
}

export function isEmptyObj(obj: unknown): boolean {
return isObject(obj) && !Object.keys(obj).length
export function isEmptyObj(object: unknown): boolean {
return isObject(object) && Reflect.ownKeys(object).length === 0
}

export function isEmptyArr(arr: unknown): boolean {
return isArray(arr) && !arr.length
export function isEmptyArr(array: unknown): boolean {
return isArray(array) && !array.length
}

export function isKeyOfObj<T extends object>(obj: T, k: keyof any): k is keyof T {
return k in obj
export function isKeyOfObj<T extends Recordable = Recordable>(object: T, k: keyof any): k is keyof T {
return k in object
}
24 changes: 12 additions & 12 deletions packages/obj/src/obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import type { Recordable } from '@vtrbo/utils-tool'
import { notNullish } from '@vtrbo/utils-tool'
import type { DeepMerge, ObjEntriesReturnType, ObjKeysReturnType } from '../types'

export function objKeys<T extends object>(obj: T): ObjKeysReturnType<T> {
return Object.keys(obj) as ObjKeysReturnType<T>
export function objKeys<T extends Recordable = Recordable>(object: T): ObjKeysReturnType<T> {
return Object.keys(object) as ObjKeysReturnType<T>
}

export function objEntries<T extends object>(obj: T): ObjEntriesReturnType<T> {
return Object.entries(obj) as ObjEntriesReturnType<T>
export function objEntries<T extends Recordable = Recordable>(object: T): ObjEntriesReturnType<T> {
return Object.entries(object) as ObjEntriesReturnType<T>
}

export function objMap<K extends string, V, NK = K, NV = V>(object: Record<K, V>, fn: (key: K, value: V) => [NK, NV] | undefined): Record<K, V> {
Expand All @@ -19,19 +19,19 @@ export function objMap<K extends string, V, NK = K, NV = V>(object: Record<K, V>
)
}

export function objPick<O extends object, T extends keyof O>(object: O, keys: T[]): Pick<O, T> {
export function objPick<O extends Recordable, T extends keyof O>(object: O, keys: T[]): Pick<O, T> {
return objKeys(object as object).reduce((acc, key) => {
return keys.includes(key) ? { ...acc, [key]: object[key] } : acc
}, {} as Pick<O, T>)
}

export function objOmit<O extends object, T extends keyof O>(object: O, keys: T[]): Omit<O, T> {
export function objOmit<O extends Recordable, T extends keyof O>(object: O, keys: T[]): Omit<O, T> {
return objKeys(object as object).reduce((acc, key) => {
return keys.includes(key) ? acc : { ...acc, [key]: object[key] }
}, {} as Omit<O, T>)
}

export function clearKeys<T extends object>(obj: T, conditions = [undefined, null, 'udf', 'nul', 'ept', 'epto', 'epta']): T {
export function clearKeys<T extends Recordable>(object: T, conditions = [undefined, null, 'udf', 'nul', 'ept', 'epto', 'epta']): T {
conditions = conditions.map(condition => isString(condition) ? condition.trim() : condition)

const clearUndefined = conditions.includes(undefined)
Expand All @@ -42,8 +42,8 @@ export function clearKeys<T extends object>(obj: T, conditions = [undefined, nul
const clearEpto = conditions.includes('epto')
const clearEpta = conditions.includes('epta')

for (const key of objKeys(obj)) {
const currentValue = (obj as Recordable)[key]
for (const key of objKeys(object)) {
const currentValue = (object as Recordable)[key]
if (
(clearUndefined && isUndefined(currentValue))
|| (clearNull && isNull(currentValue))
Expand All @@ -53,9 +53,9 @@ export function clearKeys<T extends object>(obj: T, conditions = [undefined, nul
|| (clearEpto && isEmptyObj(currentValue))
|| (clearEpta && isEmptyArr(currentValue))
)
delete (obj as Recordable)[key]
delete (object as Recordable)[key]
}
return obj
return object
}

export function deepClone<T>(target: T): T {
Expand All @@ -79,7 +79,7 @@ export function deepClone<T>(target: T): T {
return cloneTarget as T
}

export function deepMerge<T extends object = object, S extends object = T>(target: T, ...sources: S[]): DeepMerge<T, S> {
export function deepMerge<T extends Recordable = Recordable, S extends Recordable = T>(target: T, ...sources: S[]): DeepMerge<T, S> {
if (!sources.length)
return target as DeepMerge<T, S>

Expand Down
4 changes: 3 additions & 1 deletion packages/tool/src/tool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export function toRawType(data: any): string {
export function toRawType(data: unknown): string {
return Object.prototype.toString.call(data).slice(8, -1)
}

export function notNullish<T>(v: T | null | undefined): v is NonNullable<T> {
return v != null
}

export function noop() {}
8 changes: 4 additions & 4 deletions packages/tree/src/tree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function treeToList<T>(tree: T[], options?: {
export function treeToList<T = any>(tree: T[], options?: {
children?: string
}): T[] {
const tOption = Object.assign({
Expand All @@ -18,7 +18,7 @@ export function treeToList<T>(tree: T[], options?: {
return list
}

export function treeFilter<T>(tree: T[], callback: (node: T) => boolean, options?: {
export function treeFilter<T = any>(tree: T[], callback: (node: T) => boolean, options?: {
children?: string
}): T[] {
const tOption = Object.assign({
Expand All @@ -40,7 +40,7 @@ export function treeFilter<T>(tree: T[], callback: (node: T) => boolean, options
})
}

export function findPaths<T>(tree: T[], callback: (node: T) => boolean, options?: {
export function findPaths<T = any>(tree: T[], callback: (node: T) => boolean, options?: {
children?: string
findAll?: boolean
}): Array<T[]> {
Expand Down Expand Up @@ -77,7 +77,7 @@ export function findPaths<T>(tree: T[], callback: (node: T) => boolean, options?
return paths
}

export function findNodes<T>(tree: T[], callback: (node: T) => boolean, options?: {
export function findNodes<T = any>(tree: T[], callback: (node: T) => boolean, options?: {
children?: string
findAll?: boolean
}): T[] {
Expand Down

0 comments on commit 467406d

Please sign in to comment.