Skip to content

Commit

Permalink
omit leading zero for numbers between -1 and 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jul 31, 2018
1 parent 6dad3c3 commit d73708c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ function stringifyPrimitive(thing: any) {
if (typeof thing === 'string') return JSON.stringify(thing).replace(unsafe, escape);
if (thing === void 0) return 'void 0';
if (thing === 0 && 1 / thing < 0) return '-0';
return String(thing);
const str = String(thing);
if (typeof thing === 'number') return str.replace(/^(-)?0\./, '$1.');
return str;
}

function getType(thing: any) {
Expand Down
2 changes: 2 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('devalue', () => {
test('number', 42, '42');
test('negative number', -42, '-42');
test('negative zero', -0, '-0');
test('positive decimal', 0.1, '.1');
test('negative decimal', -0.1, '-.1');
test('string', 'woo!!!', '"woo!!!"');
test('boolean', true, 'true');
test('Number', new Number(42), 'Object(42)');
Expand Down

0 comments on commit d73708c

Please sign in to comment.