Skip to content

Commit d73708c

Browse files
committed
omit leading zero for numbers between -1 and 1
1 parent 6dad3c3 commit d73708c

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ function stringifyPrimitive(thing: any) {
205205
if (typeof thing === 'string') return JSON.stringify(thing).replace(unsafe, escape);
206206
if (thing === void 0) return 'void 0';
207207
if (thing === 0 && 1 / thing < 0) return '-0';
208-
return String(thing);
208+
const str = String(thing);
209+
if (typeof thing === 'number') return str.replace(/^(-)?0\./, '$1.');
210+
return str;
209211
}
210212

211213
function getType(thing: any) {

test/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ describe('devalue', () => {
1414
test('number', 42, '42');
1515
test('negative number', -42, '-42');
1616
test('negative zero', -0, '-0');
17+
test('positive decimal', 0.1, '.1');
18+
test('negative decimal', -0.1, '-.1');
1719
test('string', 'woo!!!', '"woo!!!"');
1820
test('boolean', true, 'true');
1921
test('Number', new Number(42), 'Object(42)');

0 commit comments

Comments
 (0)