Skip to content

Commit 775d3c1

Browse files
committed
feat: add a space separating quantity from unit when converting best conversion to string
Closes #665
1 parent 8f7eb2d commit 775d3c1

File tree

9 files changed

+29
-33
lines changed

9 files changed

+29
-33
lines changed

docs/api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type Area = UnitsByMeasure<MeasureKind.Area>;
1414
export type BestConversion<Q extends number | bigint, U extends BestUnits> = {
1515
quantity: _LiteralToPrimitive<Q>;
1616
unit: U;
17-
toString(toFixed?: number): `${_LiteralToPrimitive<Q>}${U}`;
17+
toString(toFixed?: number): `${_LiteralToPrimitive<Q>} ${U}`;
1818
};
1919

2020
// @public

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
},
9595
{
9696
"brotli": true,
97-
"limit": "5.22 KB",
97+
"limit": "5.23 KB",
9898
"path": "./dist/index.mjs"
9999
},
100100
{

src/converters/conversions/time.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ describe('special case for m', () => {
1414
convert(1, 'm').to('s');
1515
convert(60, 's').to('m');
1616

17-
const best1m: `${number}${Length}` = convert(1, 'm').to('best').toString();
18-
const best60s: `${number}${Time}` = convert(1, 'min').to('best').toString();
17+
const best1m: `${number} ${Length}` = convert(1, 'm').to('best').toString();
18+
const best60s: `${number} ${Time}` = convert(1, 'min').to('best').toString();
1919

20-
expect(best1m).toBe('1m');
21-
expect(best60s).toBe('60s');
20+
expect(best1m).toBe('1 m');
21+
expect(best60s).toBe('60 s');
2222
});

src/converters/convert-many.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('conversions', () => {
4444
expect(result.quantity).toBe(to[0]);
4545
expect(result.unit).toBe(to[1]);
4646

47-
expect(result.toString()).toBe(to[0].toString() + to[1]);
47+
expect(result.toString()).toBe(`${to[0].toString()} ${to[1]}`);
4848
});
4949
});
5050
});

src/converters/convert.test.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,34 @@ describe('best conversions', () => {
1717
expect(result.unit).toBe('d');
1818

1919
expect(result.quantity).toMatchInlineSnapshot('1.5');
20-
expect(result.toString()).toMatchInlineSnapshot(`"1.5d"`);
20+
expect(result.toString()).toMatchInlineSnapshot(`"1.5 d"`);
2121
});
2222

2323
describe('with rounding', () => {
2424
test('works when removing decimal places', () => {
25-
expect(convert(123_456, 'm').to('best').toString(2)).toMatchInlineSnapshot(`"123.46km"`);
26-
expect(convert(123_456, 'm').to('best').toString(0)).toMatchInlineSnapshot(`"123km"`);
27-
expect(convert(1000, 'micrometer').to('best').toString(1)).toMatchInlineSnapshot(`"1.0mm"`);
28-
expect(convert(1000, 'micrometer').to('best').toString(0)).toMatchInlineSnapshot(`"1mm"`);
25+
expect(convert(123_456, 'm').to('best').toString(2)).toMatchInlineSnapshot(`"123.46 km"`);
26+
expect(convert(123_456, 'm').to('best').toString(0)).toMatchInlineSnapshot(`"123 km"`);
27+
expect(convert(1000, 'micrometer').to('best').toString(1)).toMatchInlineSnapshot(`"1.0 mm"`);
28+
expect(convert(1000, 'micrometer').to('best').toString(0)).toMatchInlineSnapshot(`"1 mm"`);
2929
});
3030

3131
test('works when adding decimal places', () => {
32-
expect(convert(1000, 'm').to('best').toString(4)).toMatchInlineSnapshot(`"1.0000km"`);
33-
expect(convert(1000, 'm').to('best').toString(0)).toMatchInlineSnapshot(`"1km"`);
34-
expect(convert(1000, 'micrometer').to('best').toString(4)).toMatchInlineSnapshot(`"1.0000mm"`);
35-
expect(convert(1000, 'micrometer').to('best').toString(0)).toMatchInlineSnapshot(`"1mm"`);
32+
expect(convert(1000, 'm').to('best').toString(4)).toMatchInlineSnapshot(`"1.0000 km"`);
33+
expect(convert(1000, 'm').to('best').toString(0)).toMatchInlineSnapshot(`"1 km"`);
34+
expect(convert(1000, 'micrometer').to('best').toString(4)).toMatchInlineSnapshot(`"1.0000 mm"`);
35+
expect(convert(1000, 'micrometer').to('best').toString(0)).toMatchInlineSnapshot(`"1 mm"`);
3636
});
3737

3838
test('does nothing when omitted', () => {
39-
expect(convert(123_456, 'm').to('best').toString()).toMatchInlineSnapshot(`"123.456km"`);
40-
expect(convert(123_456, 'm').to('best').toString(undefined)).toMatchInlineSnapshot(`"123.456km"`);
41-
expect(convert(1000, 'micrometer').to('best').toString()).toMatchInlineSnapshot(`"1mm"`);
42-
expect(convert(1000, 'micrometer').to('best').toString(undefined)).toMatchInlineSnapshot(`"1mm"`);
39+
expect(convert(123_456, 'm').to('best').toString()).toMatchInlineSnapshot(`"123.456 km"`);
40+
expect(convert(123_456, 'm').to('best').toString(undefined)).toMatchInlineSnapshot(`"123.456 km"`);
41+
expect(convert(1000, 'micrometer').to('best').toString()).toMatchInlineSnapshot(`"1 mm"`);
42+
expect(convert(1000, 'micrometer').to('best').toString(undefined)).toMatchInlineSnapshot(`"1 mm"`);
4343
});
4444
});
4545

4646
test('works with metric', () => {
47-
expect(convert(0.001, 'days').to('best', 'metric').toString()).toMatchInlineSnapshot(`"1.44min"`);
47+
expect(convert(0.001, 'days').to('best', 'metric').toString()).toMatchInlineSnapshot(`"1.44 min"`);
4848
});
4949
});
5050

src/converters/convert.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,8 @@ function convertToBest<Q extends number | bigint, U extends Unit>(
9595
return {
9696
quantity: result,
9797
unit: bestUnit,
98-
toString:
99-
typeof quantity === 'bigint'
100-
? () => ((result as bigint) + bestUnit) as `${LiteralToPrimitive<Q>}${BestUnitsForUnit<U>}`
101-
: (toFixed?: number) =>
102-
((toFixed === undefined ? result : (result as number).toFixed(toFixed)) +
103-
bestUnit) as `${LiteralToPrimitive<Q>}${BestUnitsForUnit<U>}`,
98+
toString: (toFixed?: number) =>
99+
`${typeof result === 'bigint' || toFixed === undefined ? result : result.toFixed(toFixed)} ${bestUnit}` as `${LiteralToPrimitive<Q>} ${BestUnitsForUnit<U>}`,
104100
};
105101
}
106102

src/converters/ms.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ describe('conversions', () => {
1515

1616
describe('ms to duration string', () => {
1717
test.each([
18-
[60_000, '1min'],
19-
[2 * 60_000, '2min'],
20-
[-3 * 60_000, '-3min'],
21-
[10 * 60_000 * 60, '10h'],
18+
[60_000, '1 min'],
19+
[2 * 60_000, '2 min'],
20+
[-3 * 60_000, '-3 min'],
21+
[10 * 60_000 * 60, '10 h'],
2222
])('%p -> %p', (from, to) => {
2323
expect(ms(from)).toBe(to);
2424
});

src/converters/ms.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function ms<Q extends number | bigint>(
4242
export function ms(value: string): number;
4343
export function ms<Q extends number | bigint>(
4444
value: Q | string,
45-
): number | `${LiteralToPrimitive<Q>}${BestUnitsForMeasure<MeasureKind.Time>}` {
45+
): number | `${LiteralToPrimitive<Q>} ${BestUnitsForMeasure<MeasureKind.Time>}` {
4646
if (typeof value === 'string') {
4747
return convertMany(value).to('ms');
4848
}

src/types/converter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type BestConversion<Q extends number | bigint, U extends BestUnits> = {
2323
* Providing `0` will round the number to the nearest integer.
2424
* This option is ignored when converting `bigint`s.
2525
*/
26-
toString(toFixed?: number): `${LiteralToPrimitive<Q>}${U}`;
26+
toString(toFixed?: number): `${LiteralToPrimitive<Q>} ${U}`;
2727
};
2828

2929
/**

0 commit comments

Comments
 (0)