-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92fdbbb
commit 3f70a10
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters