Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@use-kit/functions",
"type": "module",
"version": "0.1.0",
"version": "0.1.2",
"packageManager": "pnpm@8.4.0",
"description": "",
"author": "Akashi Sai <akashi_sai@163.com>",
Expand Down
4 changes: 4 additions & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export * from './isBase64'
export * from './isEmpty'
export * from './isType'

export * from './mergeField'

export * from './sum'
export * from './sumPercent'

export * from './throttle'

export * from './uniqueList'

export * from './withDefaultObject'
16 changes: 16 additions & 0 deletions packages/core/mergeField/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
category: '@Core'
---

# mergeField

Merge object field. It returns the modified target object.

## Usage

```ts{4}
import { mergeField } from '@use-kit/functions'

const ret = mergeField({ a: 'a', d: 'd' }, { a: 'b', b: 'b', c: 'c' })
// ret: { a: 'b', d: 'd', b: 'b', c: 'c' }
```
15 changes: 14 additions & 1 deletion packages/core/mergeField/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { describe, expect, it } from 'vitest'
import { mergeField } from '.'

describe('merge field', () => {
it('should', () => expect(true).toBeTruthy())
it('should', () => {
const target = { a: 'a', d: 'd' }
const source = { a: 'b', b: 'b', c: 'c' }
const ret = mergeField(target, source)
expect(target).toMatchInlineSnapshot(`
{
"a": "b",
"b": "b",
"c": "c",
"d": "d",
}
`)
})
})
12 changes: 11 additions & 1 deletion packages/core/mergeField/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
// TODO:
interface objParams {
[key: string]: any
}

export const mergeField = (target: objParams, source: objParams) => {
Object.keys(source).forEach((sKey) => {
target[sKey] = source[sKey]
})

return target
}
15 changes: 13 additions & 2 deletions packages/core/sumPercent/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ Sum percentage.
## Usage

```ts
import { percentToNumber, sumPercent } from '@use-kit/functions'
import {
isPercent,
sumPercent,
toNumber,
withPercentCalculate,
} from '@use-kit/functions'

isPercent('15%') // true
toNumber('15%') // 0.15
toNumber('15%', false) // 15

const { x, y } = percentToNumber('20%', '30%') // { x: 20, y: 30 }
const ret = sumPercent('20%', '13%') // 33%
const ret = withPercentCalculate(20, '50%') // 10
const ret = withPercentCalculate('30', '50%') // 15
const ret = withPercentCalculate('20%', '50%') // 70%
```
42 changes: 41 additions & 1 deletion packages/core/sumPercent/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
export const percentToNumber = (target: string, source: string) => {
import { isNumber, isString } from '..'

const percentToNumber = (target: string, source: string) => {
const x = target.split('%')[0]
const y = source.split('%')[0]

return { x: Number(x), y: Number(y) }
}

export const isPercent = (target: string | number) => {
if (isNumber(target))
return false

return target.toString().includes('%')
}

export const toNumber = (target: string | number, keepSize = true) => {
if (isString(target)) {
if (isPercent(target)) {
const num = Number((target as string).split('%')[0])
return keepSize ? num / 100 : num
}

return Number(target)
}

return target as number
}

export const withPercentCalculate = (
target: string | number,
source: string | number,
) => {
if (isString(target) || isString(source)) {
const x = isNaN(Number(target)) ? toNumber(target) : Number(target)
const y = isNaN(Number(source)) ? toNumber(source) : Number(source)

// with percent
if (isNaN(Number(target)) || isNaN(Number(source)))
return x * y

return x + y
}

return Number(target) + Number(source)
}

export const sumPercent = (target: string, source: string) => {
const { x, y } = percentToNumber(target, source)

Expand Down
16 changes: 16 additions & 0 deletions packages/core/withDefaultObject/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
category: '@Core'
---

# withDefaultObject

Assignment with default object value.

## Usage

```ts{4}
import { withDefaultObject } from '@use-kit/functions'

const ret = withDefaultObject({ x: 1 }, { x: 0, y: 0 })
// ret: { x: 1, y: 0 }
```
68 changes: 68 additions & 0 deletions packages/core/withDefaultObject/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, expect, it } from 'vitest'
import { withDefaultObject } from '.'

describe('default object', () => {
it('default return', () => {
expect(
withDefaultObject({ x: 1 }, { x: 0, y: 0 }),
).toMatchInlineSnapshot(`
{
"x": 1,
"y": 0,
}
`)
})

it('with not return', () => {
const target = { x: 1 }
const source = { x: 0, y: 0 }
withDefaultObject(target, source)
expect(target).toMatchInlineSnapshot(`
{
"x": 1,
"y": 0,
}
`)
})

it('with {}', () => {
const target = {}
const source = { x: 0, y: 0 }
withDefaultObject(target, source)
expect(target).toMatchInlineSnapshot(`
{
"x": 0,
"y": 0,
}
`)
})

it('with undefined', () => {
let target
const source = { x: 0, y: 0 }
withDefaultObject((target || (target = {})), source)

expect(target).toMatchInlineSnapshot(`
{
"x": 0,
"y": 0,
}
`)

expect(withDefaultObject(target, source)).toMatchInlineSnapshot(`
{
"x": 0,
"y": 0,
}
`)
})

it('with default undefined', () => {
const target = undefined
const source = { x: 0, y: 0 }
withDefaultObject(target, source)

// TODO:
expect(target).toMatchInlineSnapshot('undefined')
})
})
20 changes: 20 additions & 0 deletions packages/core/withDefaultObject/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isUndefined } from '..'

interface objParams {
[key: string]: any
}

export const withDefaultObject = (
target: objParams | undefined,
source: objParams,
): objParams => {
if (isUndefined(target))
target = {}

Object.keys(source).forEach((sKey) => {
if (!Object.keys(target as objParams).includes(sKey))
(target as objParams)[sKey] = source[sKey]
})

return target as objParams
}
8 changes: 8 additions & 0 deletions packages/core/withDefaultObject/test.use.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { withDefaultObject } from '.'

const target = undefined
const source = { x: 0, y: 0 }
const ret = withDefaultObject(target, source)

console.log('target', target)
console.log('ret', ret)
16 changes: 16 additions & 0 deletions packages/docs/core/mergeField/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
category: '@Core'
---

# mergeField

Merge object field. It returns the modified target object.

## Usage

```ts{4}
import { mergeField } from '@use-kit/functions'

const ret = mergeField({ a: 'a', d: 'd' }, { a: 'b', b: 'b', c: 'c' })
// ret: { a: 'b', d: 'd', b: 'b', c: 'c' }
```
15 changes: 13 additions & 2 deletions packages/docs/core/sumPercent/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ Sum percentage.
## Usage

```ts
import { percentToNumber, sumPercent } from '@use-kit/functions'
import {
isPercent,
sumPercent,
toNumber,
withPercentCalculate,
} from '@use-kit/functions'

isPercent('15%') // true
toNumber('15%') // 0.15
toNumber('15%', false) // 15

const { x, y } = percentToNumber('20%', '30%') // { x: 20, y: 30 }
const ret = sumPercent('20%', '13%') // 33%
const ret = withPercentCalculate(20, '50%') // 10
const ret = withPercentCalculate('30', '50%') // 15
const ret = withPercentCalculate('20%', '50%') // 70%
```
16 changes: 16 additions & 0 deletions packages/docs/core/withDefaultObject/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
category: '@Core'
---

# withDefaultObject

Assignment with default object value.

## Usage

```ts{4}
import { withDefaultObject } from '@use-kit/functions'

const ret = withDefaultObject({ x: 1 }, { x: 0, y: 0 })
// ret: { x: 1, y: 0 }
```