JSlang.dev

JavaScript Deep Dives with Wolfram. Since 2015
Inclusive, Test-Driven, Spec-Focused, Collaborative.

September 18, 2018
TestsSource
Type coercion

Source Code

The source code we wrote during the event.

import assert from 'assert';

xit('weird', () => {
	  assert.strictEqual(undefined==false, true);
});
it('ToBoolean: undefined ', () => {
	  const undefinedAsBool = !!undefined;
	  assert.strictEqual(undefinedAsBool, false);
});
it('ToBoolean: undefined, using `Boolean`', () => {
	  const undefinedAsBool = Boolean(undefined);
	  assert.strictEqual(undefinedAsBool, false);
});
it('ToBoolean: null, using `Boolean`', () => {
	  const nullAsBool = Boolean(null);
	  assert.strictEqual(nullAsBool, false);
});
it('ToBoolean: +0, using `Boolean`', () => {
	  const asBool = Boolean(+0);
	  assert.strictEqual(asBool, false);
});
it('ToBoolean: -0, using `Boolean`', () => {
	  const asBool = Boolean(-0);
	  assert.strictEqual(asBool, false);
});
it('ToBoolean: NaN, using `Boolean`', () => {
	  const asBool = Boolean(NaN);
	  assert.strictEqual(asBool, false);
});
it('ToBoolean: Infinity, using `Boolean`', () => {
	  const asBool = Boolean(Infinity);
	  assert.strictEqual(asBool, true);
});
it('ToBoolean: -Infinity, using `Boolean`', () => {
	  const asBool = Boolean(-Infinity);
	  assert.strictEqual(asBool, true);
});
it('ToBoolean: Number.EPSILON, using `Boolean`', () => {
	  const asBool = Boolean(Number.EPSILON);
	  assert.strictEqual(asBool, true);
});
it('ToBoolean: Number.EPSILON/2, using `Boolean`', () => {
	  const asBool = Boolean(Number.EPSILON/2);
	  assert.strictEqual(asBool, true);
});
it('ToBoolean: empty string, using `Boolean`', () => {
	  const asBool = Boolean('');
	  assert.strictEqual(asBool, false);
});
it('ToBoolean: empty string, using `Boolean`', () => {
	  const asBool = Boolean('0');
	  assert.strictEqual(asBool, true);
});
it('ToBoolean: empty string, using `Boolean`', () => {
	  const asBool = Boolean(''+'');
	  assert.strictEqual(asBool, false);
});
it('ToBoolean: {}, using `Boolean`', () => {
	  const asBool = Boolean({});
	  assert.strictEqual(asBool, true);
});
it('ToBoolean: Boolean, using `Boolean`', () => {
	  const asBool = Boolean(Boolean);
	  assert.strictEqual(asBool, true);
});
it('ToBoolean: Number.MIN_VALUE, using `Boolean`', () => {
	  const asBool = Boolean(Number.MIN_VALUE);
	  assert.strictEqual(asBool, true);
});
it('ToBoolean: Number.MIN_VALUE/2, using `Boolean`', () => {
	  const asBool = Boolean(Number.MIN_VALUE/2);
	  assert.strictEqual(asBool, false);
});
it('ToBoolean: Number.MIN_VALUE-Number.MIN_VALUE, using `Boolean`', () => {
	  const asBool = Boolean(Number.MIN_VALUE-Number.MIN_VALUE);
	  assert.strictEqual(asBool, false);
});

import assert from 'assert';

describe('to number', () => {
  it('ToNumber: undefined, using `Number`', () => {
      const asNumber = Number(undefined);
      assert.strictEqual(Number.isNaN(asNumber), true);
  });
  it('ToNumber: whitespace, using `Number`', () => {
      const asNumber = Number(' ');
      assert.strictEqual(asNumber, 0);
  });
  it('ToNumber: TAB, using `Number`', () => {
      const asNumber = Number("\t");
      assert.strictEqual(asNumber, 0);
  });
  it('ToNumber: \\n, using `Number`', () => {
      const asNumber = Number("\n");
      assert.strictEqual(asNumber, 0);
  });
  it('ToNumber: TAB\\n, using `Number`', () => {
      const asNumber = Number("\t\n");
      assert.strictEqual(asNumber, 0);
  });
  it('ToNumber: +.TAB, using `Number`', () => {
      const asNumber = Number("+.\t");
      assert.strictEqual(isNaN(asNumber), true);
  });
  it('ToNumber: +.0TAB, using `Number`', () => {
      const asNumber = Number("+.0\t");
      assert.strictEqual(asNumber, 0);
  });
  it('ToNumber: 0a, using `Number`', () => {
      const asNumber = Number("0a");
      assert.strictEqual(isNaN(asNumber), true);
  });
  it('ToNumber: 0xa, using `Number`', () => {
      const asNumber = Number("0xa");
      assert.strictEqual(asNumber, 0xA);
  });
  it('ToNumber: `0 a`, using `Number`', () => {
      const asNumber = Number("0 a");
      assert.strictEqual(isNaN(asNumber), true);
  });
  it('ToNumber: 0xa, using `Number`, compared to 10', () => {
      const asNumber = Number("0xa");
      assert.strictEqual(asNumber, 10);
  });
  it('ToNumber: 012, using `Number`, compared to 10', () => {
      const asNumber = Number("012");
      assert.strictEqual(asNumber, 12);
  });
  it('ToNumber: 0000012, using `Number`, compared to 10', () => {
      const asNumber = Number("0000012");
      assert.strictEqual(asNumber, 12);
  });
  it('ToNumber: 00000.12, using `Number`, compared to 10', () => {
      const asNumber = Number("00000.12");
      assert.strictEqual(asNumber, .12);
  });
  
  
  // -!0
  it('ToBoolean: -!0', () => {
      const asBool = Boolean(-!0);
      assert.strictEqual(asBool, true);
  });
  it('ToBoolean: -true', () => {
      const minusTrue = -true;
      assert.strictEqual(minusTrue, -1);
  });
  
  it('isNaN works????', () => {
      assert.strictEqual(isNaN("JSLANG"), true);
  });
  it('isNaN works???? #', () => {
      assert.strictEqual(Number.isNaN("JSLANG"), false);
  });
  
  it('toString, 1', () => {
      const toS = '' + {
            toString: () => '1', 
            valueOf: () => '2'
          };
      assert.strictEqual(toS, '2');
  });
  it('toString, 2, `String`', () => {
      const toS = String({
            toString: () => '1', 
            valueOf: () => '2'
          });
      assert.strictEqual(toS, '1');
  });
  it('toString, 2, `Number`', () => {
      const toS = Number({
            toString: () => '1', 
            valueOf: () => '2'
          });
      assert.strictEqual(toS, 2);
  });
});