Skip to content

Commit 126bd2a

Browse files
authored
✨ feat: withPercentCalculate withDefaultObject #8 from use-kit/feat/fns
2 parents c7c3838 + 8357ef5 commit 126bd2a

File tree

14 files changed

+257
-8
lines changed

14 files changed

+257
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@use-kit/functions",
33
"type": "module",
4-
"version": "0.1.0",
4+
"version": "0.1.2",
55
"packageManager": "pnpm@8.4.0",
66
"description": "",
77
"author": "Akashi Sai <akashi_sai@163.com>",

packages/core/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ export * from './isBase64'
2020
export * from './isEmpty'
2121
export * from './isType'
2222

23+
export * from './mergeField'
24+
2325
export * from './sum'
2426
export * from './sumPercent'
2527

2628
export * from './throttle'
2729

2830
export * from './uniqueList'
31+
32+
export * from './withDefaultObject'

packages/core/mergeField/index.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
category: '@Core'
3+
---
4+
5+
# mergeField
6+
7+
Merge object field. It returns the modified target object.
8+
9+
## Usage
10+
11+
```ts{4}
12+
import { mergeField } from '@use-kit/functions'
13+
14+
const ret = mergeField({ a: 'a', d: 'd' }, { a: 'b', b: 'b', c: 'c' })
15+
// ret: { a: 'b', d: 'd', b: 'b', c: 'c' }
16+
```
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import { describe, expect, it } from 'vitest'
2+
import { mergeField } from '.'
23

34
describe('merge field', () => {
4-
it('should', () => expect(true).toBeTruthy())
5+
it('should', () => {
6+
const target = { a: 'a', d: 'd' }
7+
const source = { a: 'b', b: 'b', c: 'c' }
8+
const ret = mergeField(target, source)
9+
expect(target).toMatchInlineSnapshot(`
10+
{
11+
"a": "b",
12+
"b": "b",
13+
"c": "c",
14+
"d": "d",
15+
}
16+
`)
17+
})
518
})

packages/core/mergeField/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
// TODO:
1+
interface objParams {
2+
[key: string]: any
3+
}
4+
5+
export const mergeField = (target: objParams, source: objParams) => {
6+
Object.keys(source).forEach((sKey) => {
7+
target[sKey] = source[sKey]
8+
})
9+
10+
return target
11+
}

packages/core/sumPercent/index.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@ Sum percentage.
99
## Usage
1010

1111
```ts
12-
import { percentToNumber, sumPercent } from '@use-kit/functions'
12+
import {
13+
isPercent,
14+
sumPercent,
15+
toNumber,
16+
withPercentCalculate,
17+
} from '@use-kit/functions'
18+
19+
isPercent('15%') // true
20+
toNumber('15%') // 0.15
21+
toNumber('15%', false) // 15
1322

14-
const { x, y } = percentToNumber('20%', '30%') // { x: 20, y: 30 }
1523
const ret = sumPercent('20%', '13%') // 33%
24+
const ret = withPercentCalculate(20, '50%') // 10
25+
const ret = withPercentCalculate('30', '50%') // 15
26+
const ret = withPercentCalculate('20%', '50%') // 70%
1627
```

packages/core/sumPercent/index.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
1-
export const percentToNumber = (target: string, source: string) => {
1+
import { isNumber, isString } from '..'
2+
3+
const percentToNumber = (target: string, source: string) => {
24
const x = target.split('%')[0]
35
const y = source.split('%')[0]
46

57
return { x: Number(x), y: Number(y) }
68
}
79

10+
export const isPercent = (target: string | number) => {
11+
if (isNumber(target))
12+
return false
13+
14+
return target.toString().includes('%')
15+
}
16+
17+
export const toNumber = (target: string | number, keepSize = true) => {
18+
if (isString(target)) {
19+
if (isPercent(target)) {
20+
const num = Number((target as string).split('%')[0])
21+
return keepSize ? num / 100 : num
22+
}
23+
24+
return Number(target)
25+
}
26+
27+
return target as number
28+
}
29+
30+
export const withPercentCalculate = (
31+
target: string | number,
32+
source: string | number,
33+
) => {
34+
if (isString(target) || isString(source)) {
35+
const x = isNaN(Number(target)) ? toNumber(target) : Number(target)
36+
const y = isNaN(Number(source)) ? toNumber(source) : Number(source)
37+
38+
// with percent
39+
if (isNaN(Number(target)) || isNaN(Number(source)))
40+
return x * y
41+
42+
return x + y
43+
}
44+
45+
return Number(target) + Number(source)
46+
}
47+
848
export const sumPercent = (target: string, source: string) => {
949
const { x, y } = percentToNumber(target, source)
1050

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
category: '@Core'
3+
---
4+
5+
# withDefaultObject
6+
7+
Assignment with default object value.
8+
9+
## Usage
10+
11+
```ts{4}
12+
import { withDefaultObject } from '@use-kit/functions'
13+
14+
const ret = withDefaultObject({ x: 1 }, { x: 0, y: 0 })
15+
// ret: { x: 1, y: 0 }
16+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { withDefaultObject } from '.'
3+
4+
describe('default object', () => {
5+
it('default return', () => {
6+
expect(
7+
withDefaultObject({ x: 1 }, { x: 0, y: 0 }),
8+
).toMatchInlineSnapshot(`
9+
{
10+
"x": 1,
11+
"y": 0,
12+
}
13+
`)
14+
})
15+
16+
it('with not return', () => {
17+
const target = { x: 1 }
18+
const source = { x: 0, y: 0 }
19+
withDefaultObject(target, source)
20+
expect(target).toMatchInlineSnapshot(`
21+
{
22+
"x": 1,
23+
"y": 0,
24+
}
25+
`)
26+
})
27+
28+
it('with {}', () => {
29+
const target = {}
30+
const source = { x: 0, y: 0 }
31+
withDefaultObject(target, source)
32+
expect(target).toMatchInlineSnapshot(`
33+
{
34+
"x": 0,
35+
"y": 0,
36+
}
37+
`)
38+
})
39+
40+
it('with undefined', () => {
41+
let target
42+
const source = { x: 0, y: 0 }
43+
withDefaultObject((target || (target = {})), source)
44+
45+
expect(target).toMatchInlineSnapshot(`
46+
{
47+
"x": 0,
48+
"y": 0,
49+
}
50+
`)
51+
52+
expect(withDefaultObject(target, source)).toMatchInlineSnapshot(`
53+
{
54+
"x": 0,
55+
"y": 0,
56+
}
57+
`)
58+
})
59+
60+
it('with default undefined', () => {
61+
const target = undefined
62+
const source = { x: 0, y: 0 }
63+
withDefaultObject(target, source)
64+
65+
// TODO:
66+
expect(target).toMatchInlineSnapshot('undefined')
67+
})
68+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { isUndefined } from '..'
2+
3+
interface objParams {
4+
[key: string]: any
5+
}
6+
7+
export const withDefaultObject = (
8+
target: objParams | undefined,
9+
source: objParams,
10+
): objParams => {
11+
if (isUndefined(target))
12+
target = {}
13+
14+
Object.keys(source).forEach((sKey) => {
15+
if (!Object.keys(target as objParams).includes(sKey))
16+
(target as objParams)[sKey] = source[sKey]
17+
})
18+
19+
return target as objParams
20+
}

0 commit comments

Comments
 (0)