Skip to content

Commit

Permalink
feat: new function - fenToYuan
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed Jan 17, 2024
1 parent 0ca3ff8 commit dc71791
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pnpm add @utopia-utils/dom
* base64ToFile: base64 转 File, 如图片裁剪时,我们获取到的是 base64,但上传接口一般需要 formData 上传。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/base64ToFile.ts)
* toBase64: 将 File | Blob | imgUrl 转 base64。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/toBase64.ts)
* yuanToFen: 人民币:元转分。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/yuanToFen.ts)
* fenToYuan: 人民币:分转元。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/fenToYuan.ts)
### 类型判断

```bash
Expand Down
37 changes: 37 additions & 0 deletions packages/core/src/fenToYuan.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { afterEach, describe, expect, it, vi } from 'vitest'

import { fenToYuan } from './fenToYuan'

describe('fenToYuan', () => {
afterEach(() => {
vi.resetAllMocks()
})
it('should return undefined when input is not number', () => {
expect(fenToYuan(null as any)).toBe(undefined)
expect(fenToYuan(undefined as any)).toBe(undefined)
expect(fenToYuan('' as any)).toBe(undefined)
expect(fenToYuan('abc' as any)).toBe(undefined)
expect(fenToYuan({} as any)).toBe(undefined)
expect(fenToYuan([] as any)).toBe(undefined)
expect(fenToYuan(true as any)).toBe(undefined)
expect(fenToYuan(false as any)).toBe(undefined)
expect(fenToYuan(NaN as any)).toBe(undefined)
})
it('happy path', () => {
expect(fenToYuan(0)).toBe(0)
expect(fenToYuan(1)).toBe(0.01)
expect(fenToYuan(100)).toBe(1)
expect(fenToYuan(1000)).toBe(10)
expect(fenToYuan(99)).toBe(0.99)
expect(fenToYuan(10000)).toBe(100)
expect(fenToYuan(100000)).toBe(1000)
expect(fenToYuan(1000000000000000000)).toBe(10000000000000000)
expect(fenToYuan(10000000000000000000)).toBe(100000000000000000)
})
it('should return null if throw error', () => {
vi.spyOn(Number, 'isNaN').mockImplementation(() => {
throw new Error('mock error')
})
expect(fenToYuan(100)).toBe(undefined)
})
})
19 changes: 19 additions & 0 deletions packages/core/src/fenToYuan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { divide } from 'number-precision'
import { isNumber } from '@utopia-utils/share'

/**
* 金额¥ 分转元
* @param fen 分
* @returns 元, 如果输入不是数字,则返回 undefined
* */
export function fenToYuan(fen: number | undefined): number | undefined {
try {
if (!isNumber(fen) || Number.isNaN(fen))
return undefined
return divide(fen, 100)
}
catch (err) {
console.error(err)
return undefined
}
}

0 comments on commit dc71791

Please sign in to comment.