Skip to content

Commit

Permalink
Fix console util to support negative zero
Browse files Browse the repository at this point in the history
Fixes mdn#952
  • Loading branch information
kenrick95 committed Jun 4, 2018
1 parent d38e987 commit 6c5f090
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
44 changes: 44 additions & 0 deletions __tests__/console-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,50 @@ const consoleUtils = require('../js/editor-libs/console-utils');

describe('console utils', () => {
describe('formatOutput', () => {
test('String', function() {
expect(consoleUtils.formatOutput('lorem ipsum')).toBe(
'"lorem ipsum"'
);
});
test('undefined', function() {
expect(consoleUtils.formatOutput(undefined)).toBe('undefined');
});
test('null', function() {
expect(consoleUtils.formatOutput(null)).toBe('null');
});
test('NaN', function() {
expect(consoleUtils.formatOutput(NaN)).toBe('NaN');
});
test('Boolean: true', function() {
expect(consoleUtils.formatOutput(true)).toBe('true');
});
test('Boolean: false', function() {
expect(consoleUtils.formatOutput(false)).toBe('false');
});
test('Positive integer', function() {
expect(consoleUtils.formatOutput(42)).toBe('42');
});
test('Positive floating point', function() {
expect(consoleUtils.formatOutput(4.2)).toBe('4.2');
});
test('Negative integer', function() {
expect(consoleUtils.formatOutput(-42)).toBe('-42');
});
test('Negative floating point', function() {
expect(consoleUtils.formatOutput(-4.2)).toBe('-4.2');
});
test('Infinity', function() {
expect(consoleUtils.formatOutput(Infinity)).toBe('Infinity');
});
test('Negative Infinity', function() {
expect(consoleUtils.formatOutput(-Infinity)).toBe('-Infinity');
});
test('Positive zero', function() {
expect(consoleUtils.formatOutput(0)).toBe('0');
});
test('Negative zero', function() {
expect(consoleUtils.formatOutput(-0)).toBe('-0');
});
test('String object', function() {
expect(consoleUtils.formatOutput(new String('foo'))).toBe(
'String { "foo" }'
Expand Down
7 changes: 6 additions & 1 deletion js/editor-libs/console-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,15 @@ module.exports = {
if (
input === undefined ||
input === null ||
typeof input === 'number' ||
typeof input === 'boolean'
) {
return String(input);
} else if (typeof input === 'number') {
// Negative zero
if (Object.is(input, -0)) {
return '-0';
}
return String(input);
} else if (typeof input === 'string') {
// string literal
return '"' + input + '"';
Expand Down

0 comments on commit 6c5f090

Please sign in to comment.