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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@use-kit/functions",
"type": "module",
"version": "0.0.8",
"version": "0.0.9",
"packageManager": "pnpm@8.4.0",
"description": "",
"author": "Akashi Sai <akashi_sai@163.com>",
Expand Down Expand Up @@ -39,17 +39,19 @@
"scripts": {
"build": "unbuild",
"build:rollup": "rollup --config rollup.config.ts --configPlugin typescript",
"clean": "rimraf dist",
"dev": "unbuild --stub",
"lint": "eslint .",
"prepublishOnly": "nr build",
"release": "bumpp && npm publish",
"start": "esno packages/index.ts",
"test": "vitest",
"typecheck": "tsc --noEmit",
"docs:clean": "rimraf 'packages/docs/browser' 'packages/docs/core' 'packages/docs/integrated' 'packages/docs/node'",
"docs:dev": "vitepress dev packages/docs",
"docs:build": "vitepress build packages/docs",
"docs:preview": "vitepress preview packages/docs",
"ci-docs": "tsno run scripts/ci-docs.ts"
"ci-docs": "nr docs:clean && tsno run scripts/ci-docs.ts"
},
"dependencies": {
"dayjs": "^1.11.7",
Expand Down
5 changes: 4 additions & 1 deletion packages/core/getObjectField/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ Filter object field.
## Usage

```ts
import { getObjectField } from '@use-kit/functions'
import { getObjectField, objectFilter } from '@use-kit/functions'

const obj = { name: 'asuka', age: 23, height: 158 }
const ret = getObjectField(obj, ['name']) // { name: 'asuka' }

// The same way you can do
const ret = objectFilter(obj, (key, value) => key === 'name')
```
6 changes: 6 additions & 0 deletions packages/core/getObjectField/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('should', () => {
"name": "asuka",
}
`)

expect(objectFilter(obj, (key: any) => key === 'name')).toMatchInlineSnapshot(`
{
"name": "asuka",
}
`)
})
})

Expand Down
3 changes: 2 additions & 1 deletion packages/core/getTreeNode/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Get tree node.
## Usage

```ts
import { getTreeNode, getTreeNodes } from '@use-kit/functions'
import { getParentTree, getTreeNode, getTreeNodes } from '@use-kit/functions'

const tree = [{
id: 'nogi',
Expand All @@ -20,4 +20,5 @@ const tree = [{

const node = getTreeNode(tree, node => node.id === 'asuka')
const nodes = getTreeNodes(tree, node => node.parent === 'nogi')
const parent = getParentTree(tree, node => node.id === 'shiori') // The result is consistent with the previous example
```
17 changes: 16 additions & 1 deletion packages/core/getTreeNode/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { getTreeNode, getTreeNodes } from '.'
import { getParentTree, getTreeNode, getTreeNodes } from '.'

const tree = [{
id: 'nogi',
Expand Down Expand Up @@ -34,4 +34,19 @@ describe('should', () => {
]
`)
})

it('getParentTree', () => {
expect(getParentTree(tree, (node: any) => node.id === 'shiori')).toMatchInlineSnapshot(`
[
{
"id": "asuka",
"parent": "nogi",
},
{
"id": "shiori",
"parent": "nogi",
},
]
`)
})
})
14 changes: 14 additions & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export * from './composeTree'

export * from './debounce'

export * from './flattenArray'
export * from './flattenTree'
export * from './fuzzyQuery'

export * from './generateArray'
export * from './generateTree'
Expand All @@ -11,4 +14,15 @@ export * from './getFileType'
export * from './getObjectField'
export * from './getTreeNode'

export * from './hideMobile'

export * from './isBase64'
export * from './isEmpty'
export * from './isType'

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

export * from './throttle'

export * from './uniqueList'
Empty file.
5 changes: 5 additions & 0 deletions packages/core/mergeField/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { describe, expect, it } from 'vitest'

describe('merge field', () => {
it('should', () => expect(true).toBeTruthy())
})
1 change: 1 addition & 0 deletions packages/core/mergeField/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO:
Empty file.
5 changes: 5 additions & 0 deletions packages/core/mergeList/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { describe, expect, it } from 'vitest'

describe('merge field', () => {
it('should', () => expect(true).toBeTruthy())
})
1 change: 1 addition & 0 deletions packages/core/mergeList/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO:
19 changes: 19 additions & 0 deletions packages/core/sum/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
category: '@Core'
---

# sum

Return numbers as possible whether sum a number or string.

## Usage

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

sum(1, 2) // 3
sum(1, '2') // 3
sum('1', '2') // 3
sum(1, 'a') // 1a
sum('b', 'c') // bc
```
24 changes: 24 additions & 0 deletions packages/core/sum/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, it } from 'vitest'
import { sum } from '.'

describe('sum', () => {
it('n & n', () => {
expect(sum(1, 2)).toBe(3)
})

it('n & sn', () => {
expect(sum(1, '3')).toBe(4)
})

it('sn & sn', () => {
expect(sum('2', '3')).toBe(5)
})

it('n & s', () => {
expect(sum(3, 'a')).toBe('3a')
})

it('s & s', () => {
expect(sum('b', 'c')).toBe('bc')
})
})
18 changes: 18 additions & 0 deletions packages/core/sum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { isNumber, isString } from '..'

type sumProps = number | string

export const sum = (target: sumProps, source: sumProps) => {
if (isNumber(target) && isNumber(source))
return (target as number) + (source as number)

if (isString(target) || isString(source)) {
if (!isNaN(Number(target)) && !isNaN(Number(source)))
return Number(target) + Number(source)

else
return (target as string) + (source as string)
}

return (target as string) + (source as string)
}
16 changes: 16 additions & 0 deletions packages/core/sumPercent/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
category: '@Core'
---

# sumPercent

Sum percentage.

## Usage

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

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

describe('sum percent', () => {
it('add', () => {
expect(sumPercent('20%', '13%')).toMatchInlineSnapshot('"33%"')
})
})
12 changes: 12 additions & 0 deletions packages/core/sumPercent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export 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 sumPercent = (target: string, source: string) => {
const { x, y } = percentToNumber(target, source)

return `${x + y}%`
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
category: '@Core'
---

# uniqueArray
# uniqueList

Unique array.

## Usage

```ts{6}
import { uniqueArray } from '@use-kit/functions'
import { uniqueList } from '@use-kit/functions'
const arr = [1, 2, 2, '1', 'ab', 'ac', 'ab']
const ret = uniqueArray(arr)
const ret = uniqueList(arr)
// expect: [1, 2, '1', 'ab', 'ac']
Expand All @@ -22,7 +22,7 @@ const arr = [
{ id: 3, name: 'akashi' },
{ id: 4, name: 'shiori' },
]
const ret = uniqueArray(arr)
const ret = uniqueList(arr, 'name')
// expect: [
// {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, expect, it } from 'vitest'
import { uniqueArray } from '.'
import { uniqueList } from '.'

describe('unique array', () => {
it('number string', () => {
const arr = [1, 2, 2, 3, '1', 'akashi', 1, 'asuka', 'akashi', 3, 4]
expect(uniqueArray(arr)).toMatchInlineSnapshot(`
expect(uniqueList(arr)).toMatchInlineSnapshot(`
[
1,
2,
Expand All @@ -24,7 +24,7 @@ describe('unique array', () => {
{ id: 3, name: 'akashi' },
{ id: 4, name: 'shiori' },
]
expect(uniqueArray(arr, 'name')).toMatchInlineSnapshot(`
expect(uniqueList(arr, 'name')).toMatchInlineSnapshot(`
[
{
"id": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { typeOf } from '../isType'

export const uniqueArray = (
export const uniqueList = (
arr: (number | string)[] | Record<string, any>[] = [],
key = 'id',
): (number | string)[] | Record<string, any>[] => {
Expand Down
5 changes: 4 additions & 1 deletion packages/docs/core/getObjectField/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ Filter object field.
## Usage

```ts
import { getObjectField } from '@use-kit/functions'
import { getObjectField, objectFilter } from '@use-kit/functions'

const obj = { name: 'asuka', age: 23, height: 158 }
const ret = getObjectField(obj, ['name']) // { name: 'asuka' }

// The same way you can do
const ret = objectFilter(obj, (key, value) => key === 'name')
```
3 changes: 2 additions & 1 deletion packages/docs/core/getTreeNode/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Get tree node.
## Usage

```ts
import { getTreeNode, getTreeNodes } from '@use-kit/functions'
import { getParentTree, getTreeNode, getTreeNodes } from '@use-kit/functions'

const tree = [{
id: 'nogi',
Expand All @@ -20,4 +20,5 @@ const tree = [{

const node = getTreeNode(tree, node => node.id === 'asuka')
const nodes = getTreeNodes(tree, node => node.parent === 'nogi')
const parent = getParentTree(tree, node => node.id === 'shiori') // The result is consistent with the previous example
```
Empty file.
Empty file.
19 changes: 19 additions & 0 deletions packages/docs/core/sum/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
category: '@Core'
---

# sum

Return numbers as possible whether sum a number or string.

## Usage

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

sum(1, 2) // 3
sum(1, '2') // 3
sum('1', '2') // 3
sum(1, 'a') // 1a
sum('b', 'c') // bc
```
16 changes: 16 additions & 0 deletions packages/docs/core/sumPercent/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
category: '@Core'
---

# sumPercent

Sum percentage.

## Usage

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

const { x, y } = percentToNumber('20%', '30%') // { x: 20, y: 30 }
const ret = sumPercent('20%', '13%') // 33%
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
category: '@Core'
---

# uniqueArray
# uniqueList

Unique array.

## Usage

```ts{6}
import { uniqueArray } from '@use-kit/functions'
import { uniqueList } from '@use-kit/functions'
const arr = [1, 2, 2, '1', 'ab', 'ac', 'ab']
const ret = uniqueArray(arr)
const ret = uniqueList(arr)
// expect: [1, 2, '1', 'ab', 'ac']
Expand All @@ -22,7 +22,7 @@ const arr = [
{ id: 3, name: 'akashi' },
{ id: 4, name: 'shiori' },
]
const ret = uniqueArray(arr)
const ret = uniqueList(arr, 'name')
// expect: [
// {
Expand Down