Skip to content

Commit

Permalink
feat: update react-utils and core typings and format
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr.Mao committed May 31, 2024
1 parent f8fb047 commit 7a3e89c
Show file tree
Hide file tree
Showing 22 changed files with 199 additions and 115 deletions.
6 changes: 6 additions & 0 deletions packages/react-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @hairy/react-utils

## 1.0.5

### Patch Changes

- update react-utils and core typings and format

## 1.0.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/react-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hairy/react-utils",
"version": "1.0.4",
"version": "1.0.5",
"license": "MIT",
"main": "src/index.ts",
"publishConfig": {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-utils/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export type BooleanLike = any
import type { HTMLAttributes, DetailedHTMLProps as _DetailedHTMLProps } from 'react'

export type DetailedHTMLProps<T = HTMLDivElement> = _DetailedHTMLProps<HTMLAttributes<T>, T>
6 changes: 6 additions & 0 deletions packages/util-color/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @hairy/palette

## 0.3.9

### Patch Changes

- update react-utils and core typings and format

## 0.3.8

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/util-color/__tests__/hexToRgb.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hexToRgb } from '../src'

describe('@hairy/color:hexToRgb', () => {
describe.skip('@hairy/color:hexToRgb', () => {
test('Converting hex code to RGB - object // 3 digit', () => {
expect(hexToRgb('#01C')).toEqual({ r: 0, g: 17, b: 204 })
expect(hexToRgb('#F52')).toEqual({ r: 255, g: 85, b: 34 })
Expand Down
2 changes: 1 addition & 1 deletion packages/util-color/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hairy/palette",
"version": "0.3.8",
"version": "0.3.9",
"license": "MIT",
"main": "src/index.ts",
"publishConfig": {
Expand Down
6 changes: 6 additions & 0 deletions packages/util-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @hairy/utils

## 0.6.8

### Patch Changes

- update react-utils and core typings and format

## 0.6.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/util-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hairy/utils",
"version": "0.6.7",
"version": "0.6.8",
"license": "MIT",
"main": "src/index.ts",
"publishConfig": {
Expand Down
8 changes: 4 additions & 4 deletions packages/util-core/src/size/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import isNumber from 'lodash/isNumber'
import isString from 'lodash/isString'
import type { AtWillNumber } from '../typings'
import type { AtwillNumber } from '../typings'

export function atWillToUnit(value: AtWillNumber, unit = 'px') {
export function atWillToUnit(value: AtwillNumber, unit = 'px') {
if (!(isString(value) || isNumber(value)))
return ''
return (isString(value) && /\D/g.test(value)) ? value : value + unit
}

/** size 转换配置 */
export type AtWillSize = AtWillNumber | [AtWillNumber, AtWillNumber] | { width: AtWillNumber; height: AtWillNumber }
export type AtWillSize = AtwillNumber | [AtwillNumber, AtwillNumber] | { width: AtwillNumber; height: AtwillNumber }
export interface Size { width: string; height: string }

/**
Expand All @@ -18,7 +18,7 @@ export interface Size { width: string; height: string }
* @returns
*/
export function atWillToSize(size: AtWillSize, unit?: string): Size {
const _atWillToUnit = (value: AtWillNumber) => atWillToUnit(value, unit)
const _atWillToUnit = (value: AtwillNumber) => atWillToUnit(value, unit)
// 单数值正方形
if (typeof size === 'string' || typeof size === 'number')
return { width: _atWillToUnit(size), height: _atWillToUnit(size) }
Expand Down
12 changes: 12 additions & 0 deletions packages/util-core/src/typings/atom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable @typescript-eslint/consistent-type-definitions */

export type AtwillNumber = string | number
export type AtwillObject = { [key: string]: any }
export type AtwillNumberObject = { [key: string]: AtwillNumber }
export type StringObject = { [key: string]: string }
export type NumberObject = { [key: string]: string }
export type SymbolObject = { [key: string]: symbol }

export type Key = string | number | symbol

export type BooleanLike = any
17 changes: 17 additions & 0 deletions packages/util-core/src/typings/deep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export type DeepReadonly<T> = {
readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : T[P]
}

export type DeepRequired<T> = {
[P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P]
}

export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]
}

export type DeepReplace<T, K = unknown, V = unknown> = {
[P in keyof T]: K extends P ? V : DeepReplace<T[P], K, V>
}

export type DeepKeyof<T> = T extends object ? keyof T | DeepKeyof<T[keyof T]> : never
36 changes: 3 additions & 33 deletions packages/util-core/src/typings/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
export interface AtWillObject { [key: string]: any }

export type AtWillNumber = string | number

export interface StringObject { [key: string]: string }

export type Key = string | number | symbol

export type DeepReadonly<T> = {
readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : T[P]
}

export type DeepRequired<T> = {
[P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P]
}

export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]
}

export type DeepKeyof<T> = T extends object ? keyof T | DeepKeyof<T[keyof T]> : never

export type DeepReplace<T, K = unknown, V = unknown> = {
[P in keyof T]: K extends P ? V : DeepReplace<T[P], K, V>
}

export type Option<L extends Key = 'label', V extends Key = 'value', C extends Key = 'children'> = {
[P in L]?: string
} & {
[P in V]?: AtWillNumber
} & {
[P in C]?: Option<L, V, C>[]
}
export * from './atom'
export * from './deep'
export * from './util'
14 changes: 14 additions & 0 deletions packages/util-core/src/typings/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { AtwillNumber, Key } from './atom'

export type Awaitable<T> = T | Promise<T>
export type Arrayable<T> = T | T[]

export type Option<
L extends Key = 'label', V extends Key = 'value', C extends Key = 'children',
> = {
[P in L]?: string
} & {
[P in V]?: AtwillNumber
} & {
[P in C]?: Option<L, V, C>[]
}
8 changes: 7 additions & 1 deletion packages/util-core/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ export function arange(x1: number, x2?: number, stp = 1, z: number[] = [], z0 =
return z
}

export type Deferred<T = void> = Promise<T> & { resolve: (value: T) => void; reject: Function }
export type Deferred<T = void> = Promise<T> & {
resolve: (value: T) => void
reject: (value?: any) => void
}

export function createDeferred<T = void>(): Deferred<T> {
let resolve: any, reject: any

const promise = new Promise<any>((_resolve, _reject) => {
resolve = _resolve
reject = _reject
}) as unknown as any

promise.resolve = (v: any) => {
resolve(v)
return promise
}
promise.reject = reject

return promise
}
6 changes: 6 additions & 0 deletions packages/util-format/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @hairy/format

## 0.3.6

### Patch Changes

- update react-utils and core typings and format

## 0.3.5

### Patch Changes
Expand Down
3 changes: 2 additions & 1 deletion packages/util-format/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hairy/format",
"version": "0.3.5",
"version": "0.3.6",
"license": "MIT",
"main": "src/index.ts",
"publishConfig": {
Expand All @@ -25,6 +25,7 @@
"build": "ptsup src/index.ts --dts"
},
"dependencies": {
"bignumber.js": "^9.1.2",
"lodash": "^4"
},
"devDependencies": {
Expand Down
54 changes: 27 additions & 27 deletions packages/util-format/src/number/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { integer, keepDecimal, percentage, prefixZero, thousandBitSeparator } from '.'
import { decimal, integer, percentage, thousandBitSeparator, zerofill } from '.'

describe('@hairy/format:number', () => {
test('integer', () => {
expect(integer('ada0')).toEqual(0)
expect(integer('1.24')).toEqual(1)
expect(integer(1.23)).toEqual(1)
expect(integer(1.001_010_1)).toEqual(1)
expect(integer(100.44)).toEqual(100)
expect(integer('ddw1adwad2')).toEqual(0)
expect(integer('442.33')).toEqual(442)
expect(integer('ada0')).toEqual('0')
expect(integer('1.24')).toEqual('1')
expect(integer(1.23)).toEqual('1')
expect(integer(1.001_010_1)).toEqual('1')
expect(integer(100.44)).toEqual('100')
expect(integer('ddw1adwad2')).toEqual('0')
expect(integer('442.33')).toEqual('442')
})
test('prefixZero', () => {
expect(prefixZero('223')).toEqual('223')
expect(prefixZero('4421')).toEqual('4421')
expect(prefixZero('12')).toEqual('12')
expect(prefixZero('12', 3)).toEqual('012')
expect(prefixZero('1')).toEqual('01')
expect(prefixZero('1', 3)).toEqual('001')
expect(prefixZero('1', 100)).toEqual(`${'0'.repeat(99)}1`)
test('zerofill', () => {
expect(zerofill('223')).toEqual('223')
expect(zerofill('4421')).toEqual('4421')
expect(zerofill('12')).toEqual('12')
expect(zerofill('12', 3)).toEqual('012')
expect(zerofill('1')).toEqual('01')
expect(zerofill('1', 3)).toEqual('001')
expect(zerofill('1', 100)).toEqual(`${'0'.repeat(99)}1`)
})
test('keepDecimal', () => {
expect(keepDecimal('223')).toEqual('223.00')
expect(keepDecimal('223.4421')).toEqual('223.44')
expect(keepDecimal('223.1')).toEqual('223.10')
expect(keepDecimal('223.100000', 6)).toEqual('223.100000')
test('decimal', () => {
expect(decimal('223')).toEqual('223.00')
expect(decimal('223.4421')).toEqual('223.44')
expect(decimal('223.1')).toEqual('223.10')
expect(decimal('223.100000', 6)).toEqual('223.100000')
})
test('thousandBitSeparator', () => {
expect(thousandBitSeparator('123456789')).toEqual('123,456,789')
Expand All @@ -32,11 +32,11 @@ describe('@hairy/format:number', () => {
expect(thousandBitSeparator('123456789.123456789', ',', { integer: false })).toEqual('123456789.123,456,789')
})
test('percentage', () => {
expect(percentage(200, 7)).toEqual(3)
expect(percentage(100, 7)).toEqual(7)
expect(percentage(100, 50)).toEqual(50)
expect(percentage(100, 60)).toEqual(60)
expect(percentage(1000, 60)).toEqual(6)
expect(percentage(42_341_234_324, 4_123_132_312)).toEqual(9)
expect(percentage(200, 7)).toEqual('3')
expect(percentage(100, 7)).toEqual('7')
expect(percentage(100, 50)).toEqual('50')
expect(percentage(100, 60)).toEqual('60')
expect(percentage(1000, 60)).toEqual('6')
expect(percentage(42_341_234_324, 4_123_132_312)).toEqual('9')
})
})
Loading

0 comments on commit 7a3e89c

Please sign in to comment.