Skip to content

Commit

Permalink
feat: support getUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitusun committed Sep 5, 2024
1 parent 3f70a10 commit 0ebe243
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 85 deletions.
148 changes: 148 additions & 0 deletions __tests__/getUnit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
const { default: orange, Lang } = require('../dist/cjs/orange');
const { default: getUnit, unitDict } = require('../dist/cjs/getUnit');

test('GetUnit', () => {
expect(getUnit(undefined)).toEqual(null);
expect(getUnit(90)).toEqual(null);
expect(getUnit(1100)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'K')
);

expect(getUnit(122000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'K')
);
expect(getUnit(122000, { lanType: Lang.ZH_CN })).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '万')
);
expect(getUnit(122000, { lanType: Lang.ZH_TW })).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '萬')
);
});

test('GetUnit By EN_US', () => {
orange.precision = 2;
expect(getUnit(1000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'K')
);
expect(getUnit(1000, { precision: 3, ignoreIntegerPrecision: false })).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'K')
);
expect(getUnit(12200)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'K')
);

orange.precision = 3;
expect(getUnit(122000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'K')
);
expect(getUnit(1220000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'M')
);
expect(getUnit(12200000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'M')
);
expect(getUnit(122000000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'M')
);
expect(getUnit(1220000000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'B')
);
expect(getUnit(12200000000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'B')
);
expect(getUnit(122000000000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'B')
);
expect(getUnit(1220000000000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'T')
);
expect(getUnit(12200000000000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'T')
);
expect(getUnit(122000000000000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'T')
);
expect(getUnit(1220000000000000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'T')
);
});

test('GetUnit By ZH_CN', () => {
orange.precision = 2;
orange.lang = Lang.ZH_CN;

expect(getUnit(1000)).toEqual(null);
expect(getUnit(122000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '万')
);
expect(getUnit(1220000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '万')
);
expect(getUnit(12200000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '万')
);
expect(getUnit(122000000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '亿')
);
expect(getUnit(1220000000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '亿')
);
expect(getUnit(12200000000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '亿')
);
expect(getUnit(122000000000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '亿')
);
expect(getUnit(1220000000000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '万亿')
);
expect(getUnit(12200000000000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '万亿')
);
expect(getUnit(122000000000000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '万亿')
);
expect(getUnit(1220000000000000)).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '万亿')
);
});

test('GetUnit By ZH_TW', () => {
orange.precision = 2;
orange.lang = Lang.ZH_TW;

expect(getUnit(1000)).toEqual(null);
expect(getUnit(122000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '萬')
);
expect(getUnit(1220000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '萬')
);
expect(getUnit(12200000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '萬')
);
expect(getUnit(122000000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '億')
);
expect(getUnit(1220000000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '億')
);
expect(getUnit(12200000000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '億')
);
expect(getUnit(122000000000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '億')
);
expect(getUnit(1220000000000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '萬億')
);
expect(getUnit(12200000000000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '萬億')
);
expect(getUnit(122000000000000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '萬億')
);
expect(getUnit(1220000000000000)).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '萬億')
);
});
16 changes: 10 additions & 6 deletions __tests__/toUnit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ test('ToUnit', () => {
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萬');

expect(toUnit(122000, { unit: { value: 1000, label: 'K' } })).toBe('122K');
});

test('ToUnit By EN_US', () => {
orange.precision = 2;
expect(toUnit(1000)).toBe("1K");
expect(toUnit(1000, { precision: 3, ignoreIntegerPrecision: false })).toBe("1.000K");
expect(toUnit(1000)).toBe('1K');
expect(toUnit(1000, { precision: 3, ignoreIntegerPrecision: false })).toBe(
'1.000K'
);
expect(toUnit(12200)).toBe('12.20K');

orange.precision = 3;
Expand All @@ -34,7 +38,7 @@ test('toUnit By ZH_CN', () => {
orange.precision = 2;
orange.lang = Lang.ZH_CN;

expect(toUnit(1000)).toBe("1000");
expect(toUnit(1000)).toBe('1000');
expect(toUnit(122000)).toBe('12.20万');
expect(toUnit(1220000)).toBe('122万');
expect(toUnit(12200000)).toBe('1220万');
Expand All @@ -46,13 +50,13 @@ test('toUnit By ZH_CN', () => {
expect(toUnit(12200000000000)).toBe('12.20万亿');
expect(toUnit(122000000000000)).toBe('122万亿');
expect(toUnit(1220000000000000)).toBe('1220万亿');
})
});

test('toUnit By ZH_TW', () => {
orange.precision = 2;
orange.lang = Lang.ZH_TW;

expect(toUnit(1000)).toBe("1000");
expect(toUnit(1000)).toBe('1000');
expect(toUnit(122000)).toBe('12.20萬');
expect(toUnit(1220000)).toBe('122萬');
expect(toUnit(12200000)).toBe('1220萬');
Expand All @@ -64,4 +68,4 @@ test('toUnit By ZH_TW', () => {
expect(toUnit(12200000000000)).toBe('12.20萬億');
expect(toUnit(122000000000000)).toBe('122萬億');
expect(toUnit(1220000000000000)).toBe('1220萬億');
})
});
6 changes: 6 additions & 0 deletions docs/CHANGELOG/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ nav:

## v4

### v4.4.0

`2024-09-05`

- feat: add `getUnit`

### v4.3.0

`2024-07-01`
Expand Down
38 changes: 24 additions & 14 deletions docs/methods/getUnit.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,47 @@ order: 8

## Since

`2.1.0`
`4.4.0`

## Arguments

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

## Returns

- `Returns: string`: 转换后的值
- `Returns: null | Unit`: 转换后的值

## 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萬');
import { getUnit, Lang, orange } from '@dz-web/o-orange';

test('GetUnit', () => {
expect(getUnit(undefined)).toEqual(null);
expect(getUnit(90)).toEqual(null);
expect(getUnit(1100)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'K')
);

expect(getUnit(122000)).toEqual(
unitDict[Lang.EN_US].find((unit) => unit.label === 'K')
);
expect(getUnit(122000, { lanType: Lang.ZH_CN })).toEqual(
unitDict[Lang.ZH_CN].find((unit) => unit.label === '')
);
expect(getUnit(122000, { lanType: Lang.ZH_TW })).toEqual(
unitDict[Lang.ZH_TW].find((unit) => unit.label === '')
);
});

```

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

orange.precision = 2;
orange.lang = Lang.ZH_CN;

const unit = getUnit(122000);
console.log(unit);
Expand Down
1 change: 1 addition & 0 deletions docs/methods/toUnit.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ order: 7
- `num: string | number`: 待转换值
- `[options = {}]: Object`
- `[options.lanType = Lang.EN_US]: Lang` 语言类型
- `[options.unit]: Unit` 自定义Unit(来自于[getUnit](./getUnit.md)的返回值,如果非undefined就会跳过检测unit步骤)
- `[...rest]` 支持`toFixed`方法的所有参数

## Returns
Expand Down
30 changes: 12 additions & 18 deletions src/getUnit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export interface Unit {
value: number;
}

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

const unitDict: Record<Lang, Unit[]> = {
export const unitDict: Record<Lang, Unit[]> = {
[Lang.EN_US]: [
{ value: Math.pow(10, 12), label: 'T' },
{ value: Math.pow(10, 9), label: 'B' },
Expand All @@ -32,33 +32,27 @@ const unitDict: Record<Lang, Unit[]> = {

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

const {
placeholder = orange.placeholder,
ignoreIntegerPrecision = true,
} = rest;
function getUnit(
num: number | string,
options: GetUnitOptions = {}
): Unit | null {
const { lanType = orange.lang } = options;

const pureNum: number = toNumber(num);

if (isNaN(pureNum)) return placeholder;
if (isNaN(pureNum)) return null;

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,
};
let result: Unit | null = null;

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

for (let i = 0; i < unitLen; i++) {
const { label, value } = unit[i];
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +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 getUnit, unitDict } from './getUnit';
export { default as getSignByComparison } from './getSignByComparison';
export { csvArrayToObjects, csvArrayWithoutKeysToObjects } from './csv-array';
export {
Expand Down
Loading

0 comments on commit 0ebe243

Please sign in to comment.