Skip to content

Commit

Permalink
feat: add getUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitusun committed Sep 5, 2024
1 parent 92fdbbb commit 3f70a10
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/methods/getUnit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
group:
title: 数字处理
order: 8
---

# getUnit

获取数字的单位。

## Since

`2.1.0`

## Arguments

- `num: string | number`: 待转换值
- `[options = {}]: Object`
- `[options.lanType = Lang.EN_US]: Lang` 语言类型
- `[...rest]` 支持`toFixed`方法的所有参数

## Returns

- `Returns: string`: 转换后的值

## Examples

```js
import { toUnit, Lang, orange } from '@dz-web/o-orange';

test('ToUnit', () => {
expect(toUnit(undefined)).toBe('--');
expect(toUnit(1100)).toBe('1.10K');

expect(toUnit(122000)).toBe('122K');
expect(toUnit(122000, { lanType: Lang.ZH_CN })).toBe('12.20万');
expect(toUnit(122000, { lanType: Lang.ZH_TW })).toBe('12.20萬');
});

```

```tsx
import { getUnit } from '@dz-web/o-orange';

const unit = getUnit(122000);
console.log(unit);

export default function App() {
return <div>Hello World</div>
}
```
75 changes: 75 additions & 0 deletions src/getUnit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import orange, { Lang } from './orange';
import toFixed, { ToFixedOption } from './toFixed';
import { toNumber } from './utils';

export interface Unit {
label: string;
value: number;
}

export interface toUnitOptions extends ToFixedOption {
lanType?: Lang;
}

const unitDict: Record<Lang, Unit[]> = {
[Lang.EN_US]: [
{ value: Math.pow(10, 12), label: 'T' },
{ value: Math.pow(10, 9), label: 'B' },
{ value: Math.pow(10, 6), label: 'M' },
{ value: Math.pow(10, 3), label: 'K' },
],
[Lang.ZH_CN]: [
{ value: Math.pow(10, 12), label: '万亿' },
{ value: Math.pow(10, 8), label: '亿' },
{ value: Math.pow(10, 4), label: '万' },
],
[Lang.ZH_TW]: [
{ value: Math.pow(10, 12), label: '萬億' },
{ value: Math.pow(10, 8), label: '億' },
{ value: Math.pow(10, 4), label: '萬' },
],
};

/**
*
* Convert value to English units, like 1B 1M 1K
*
* @since 2.1.0
*
*/
function getUnit(num: number | string, options: toUnitOptions = {}): string {
const { lanType = orange.lang, ...rest } = options;

const {
placeholder = orange.placeholder,
ignoreIntegerPrecision = true,
} = rest;

const pureNum: number = toNumber(num);

if (isNaN(pureNum)) return placeholder;

const unit: Unit[] = unitDict[lanType] || unitDict[Lang.EN_US];
const unitLen: number = unit.length;
const numAbs: number = Math.abs(+num);
let result = '';
const toFixedParams = {
ignoreIntegerPrecision,
...rest,
};

if (numAbs < unit[unitLen - 1].value) return toFixed(num, toFixedParams);

for (let i = 0; i < unitLen; i++) {
const { label, value } = unit[i];

if (numAbs >= value) {
result = { label, value };
break;
}
}

return result;
}

export default getUnit;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { default as toPercent } from './toPercent';
export { default as toSlice } from './toSlice';
export { default as toThousand } from './toThousand';
export { default as toUnit } from './toUnit';
export { default as getUnit } from './getUnit';
export { default as getSignByComparison } from './getSignByComparison';
export { csvArrayToObjects, csvArrayWithoutKeysToObjects } from './csv-array';
export {
Expand Down

0 comments on commit 3f70a10

Please sign in to comment.