Skip to content

Commit

Permalink
feat: toFixedWithoutZeros return string value
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed Feb 22, 2023
1 parent 2a1591b commit 7b8699e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pnpm add @utopia-utils/tree
### 字符串

* randomString: 随机生成指定长度、指定字符集的字符串。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/randomString.ts)

* capitalize: 首字母大写。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/capitalize.ts)
### Dom

```bash
Expand All @@ -62,12 +62,10 @@ pnpm add @utopia-utils/dom

* waitForSelector: 等待指定的选择器匹配的元素出现在页面中,如果调用此方法时已经有匹配的元素,那么此方法立即返回。 如果指定的选择器在超时时间后扔不出现,返回 `null`[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/waitForSelector.ts)
* panzoom: 为指定的元素添加拖拽缩放功能。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/panzoom/core.ts)
* compressImg: 压缩图片。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/compressImg.ts)
### 杂项
* [defineDictionary](#defineDictionary): 定义业务字典。 **typesafe** [source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/defineDictionary.ts)
* ~~[createEnumFromOptions](#createEnumFromOptions): 通过 `options` 自动生成对应的 `enum`, 后期只需要维护 `options`**typesafe**[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/createEnumFromOptions.ts)~~
* sleep: 等待指定的时间。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/sleep.ts)
* capitalize: 首字母大写。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/capitalize.ts)
* [retry](#retry): 重试函数(如果函数抛出错误)直到成功或者达到最大重试次数。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/retry.ts)
* objectKeys: 带类型的 `Object.keys()`[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/objectKeys.ts)
* omit: 删除 `object` 对象的指定属性。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/omit.ts)
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { average, toFixedWithoutZeros } from './math'

describe('math functions', () => {
it('toFixedWithoutZeros', () => {
expect(toFixedWithoutZeros(0.615, 2)).toBe(0.61)
expect(toFixedWithoutZeros(10, 2)).toBe(10)
expect(toFixedWithoutZeros(1.005, 2)).toBe(1)
expect(toFixedWithoutZeros(0.615, 2)).toBe('0.61')
expect(toFixedWithoutZeros(10, 2)).toBe('10')
expect(toFixedWithoutZeros(1.005, 2)).toBe('1')
})
it('average', () => {
expect(average(1, 2, 3)).toBe(2)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* @param precision
* @returns
*/
export function toFixedWithoutZeros(num: number, precision: number): number {
return Number(num.toFixed(precision))
export function toFixedWithoutZeros(num: number, precision: number): string {
return `${Number(num.toFixed(precision))}`
}

/**
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ export interface MemoizedFn<T extends (this: any, ...args: any[]) => any> {
addMemoized(1, 2) // => 3 from cache
// clear cache
fn.clear()
addMemoized.clear()
// Modify the result cache.
fn.cache.set(object, ['a', 'b'])
values(object)
// => ['a', 'b']
addMemoized.cache.set(JSON.stringify([1, 2]), 4)
addMemoized(1, 2) // => 4 from cache
* ```
*/
export function memoize<T extends (...args: any[]) => any>(fn: T, options?: MemoizeOptions<T>): MemoizedFn<T> {
Expand Down

0 comments on commit 7b8699e

Please sign in to comment.