Symbol.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const assert = require('assert');
  2. exports.test_has_instance = function(sym) {
  3. class Array1 {
  4. static [sym](instance) {
  5. return Array.isArray(instance);
  6. }
  7. }
  8. assert.equal(typeof sym, "symbol");
  9. assert.ok([] instanceof Array1);
  10. };
  11. exports.test_is_concat_spreadable = function(sym) {
  12. const alpha = ['a', 'b', 'c'];
  13. const numeric = [1, 2, 3];
  14. let alphaNumeric = alpha.concat(numeric);
  15. assert.deepEqual(alphaNumeric, ["a", "b", "c", 1, 2, 3]);
  16. numeric[sym] = false;
  17. alphaNumeric = alpha.concat(numeric);
  18. assert.deepEqual(alphaNumeric, ["a", "b", "c", numeric]);
  19. };
  20. exports.test_iterator = function(sym) {
  21. const iterable1 = new Object();
  22. iterable1[sym] = function* () {
  23. yield 1;
  24. yield 2;
  25. yield 3;
  26. };
  27. assert.deepEqual([...iterable1], [1, 2, 3]);
  28. };
  29. exports.test_async_iterator = async function(sym) {
  30. const iterable1 = new Object();
  31. iterable1[sym] = function () {
  32. let done = false;
  33. return {
  34. next() {
  35. if (done) {
  36. return Promise.resolve({
  37. done: true,
  38. value: 1
  39. });
  40. } else {
  41. done = true;
  42. return Promise.resolve({
  43. done: false,
  44. value: 0
  45. });
  46. }
  47. }
  48. };
  49. };
  50. const values = [];
  51. for await (let value of iterable1) {
  52. values.push(value);
  53. }
  54. assert.deepEqual(values, [0]);
  55. };
  56. exports.test_match = function(sym) {
  57. const regexp1 = /foo/;
  58. assert.throws(() => '/foo/'.startsWith(regexp1));
  59. regexp1[sym] = false;
  60. assert.ok('/foo/'.startsWith(regexp1));
  61. assert.equal('/baz/'.endsWith(regexp1), false);
  62. };
  63. exports.test_replace = function(sym) {
  64. class Replace1 {
  65. constructor(value) {
  66. this.value = value;
  67. }
  68. [sym](string) {
  69. return `s/${string}/${this.value}/g`;
  70. }
  71. }
  72. assert.equal('foo'.replace(new Replace1('bar')), 's/foo/bar/g');
  73. };
  74. exports.test_search = function(sym) {
  75. class Search1 {
  76. constructor(value) {
  77. this.value = value;
  78. }
  79. [sym](string) {
  80. return string.indexOf(this.value);
  81. }
  82. }
  83. assert.equal('foobar'.search(new Search1('bar')), 3);
  84. };
  85. exports.test_species = function(sym) {
  86. class Array1 extends Array {
  87. static get [sym]() { return Array; }
  88. }
  89. const a = new Array1(1, 2, 3);
  90. const mapped = a.map(x => x * x);
  91. assert.equal(mapped instanceof Array1, false);
  92. assert.ok(mapped instanceof Array);
  93. };
  94. exports.test_split = function(sym) {
  95. class Split1 {
  96. constructor(value) {
  97. this.value = value;
  98. }
  99. [sym](string) {
  100. var index = string.indexOf(this.value);
  101. return this.value + string.substr(0, index) + "/"
  102. + string.substr(index + this.value.length);
  103. }
  104. }
  105. assert.equal('foobar'.split(new Split1('foo')), 'foo/bar');
  106. };
  107. exports.test_to_primitive = function(sym) {
  108. const object1 = {
  109. [sym](hint) {
  110. if (hint == 'number') {
  111. return 42;
  112. }
  113. return null;
  114. }
  115. };
  116. assert.equal(+object1, 42);
  117. };
  118. exports.test_to_string_tag = function(sym) {
  119. class ValidatorClass {
  120. get [sym]() {
  121. return 'Validator';
  122. }
  123. }
  124. assert.equal(Object.prototype.toString.call(new ValidatorClass()), '[object Validator]');
  125. };