string.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. var runStringTests = function (it) {
  2. 'use strict';
  3. var functionsHaveNames = (function foo() {}).name === 'foo';
  4. var ifFunctionsHaveNamesIt = functionsHaveNames ? it : it.skip;
  5. var ifShimIt = (typeof process !== 'undefined' && process.env.NO_ES6_SHIM) ? it.skip : it;
  6. var hasSymbols = typeof Symbol === 'function' && typeof Symbol['for'] === 'function' && typeof Symbol.iterator === 'symbol';
  7. var ifSymbolsDescribe = hasSymbols ? describe : describe.skip;
  8. describe('String', function () {
  9. var hasStrictMode = (function () { return this === null; }.call(null));
  10. var testObjectCoercible = function (methodName) {
  11. var fn = String.prototype[methodName];
  12. if (!hasStrictMode) { return; } // skip these tests on IE <= 10
  13. expect(function () { return fn.call(undefined); }).to['throw'](TypeError);
  14. expect(function () { return fn.call(null); }).to['throw'](TypeError);
  15. expect(function () { return fn.apply(undefined); }).to['throw'](TypeError);
  16. expect(function () { return fn.apply(null); }).to['throw'](TypeError);
  17. };
  18. ifShimIt('is on the exported object', function () {
  19. var exported = require('../');
  20. expect(exported.String).to.equal(String);
  21. });
  22. describe('#repeat()', function () {
  23. if (!Object.prototype.hasOwnProperty.call(String.prototype, 'repeat')) {
  24. return it('exists', function () {
  25. expect(String.prototype).to.have.property('repeat');
  26. });
  27. }
  28. ifFunctionsHaveNamesIt('has the right name', function () {
  29. expect(String.prototype.repeat).to.have.property('name', 'repeat');
  30. });
  31. it('is not enumerable', function () {
  32. expect(String.prototype).ownPropertyDescriptor('repeat').to.have.property('enumerable', false);
  33. });
  34. it('has the right arity', function () {
  35. expect(String.prototype.repeat).to.have.property('length', 1);
  36. });
  37. it('should throw a TypeError when called on null or undefined', function () {
  38. testObjectCoercible('repeat');
  39. });
  40. it('should throw a RangeError when negative or infinite', function () {
  41. expect(function negativeOne() { return 'test'.repeat(-1); }).to['throw'](RangeError);
  42. expect(function infinite() { return 'test'.repeat(Infinity); }).to['throw'](RangeError);
  43. });
  44. it('should coerce to an integer', function () {
  45. expect('test'.repeat(null)).to.eql('');
  46. expect('test'.repeat(false)).to.eql('');
  47. expect('test'.repeat('')).to.eql('');
  48. expect('test'.repeat(NaN)).to.eql('');
  49. expect('test'.repeat({})).to.eql('');
  50. expect('test'.repeat([])).to.eql('');
  51. expect('test'.repeat({
  52. valueOf: function () { return 2; }
  53. })).to.eql('testtest');
  54. });
  55. it('should work', function () {
  56. expect('test'.repeat(3)).to.eql('testtesttest');
  57. });
  58. it('should work on integers', function () {
  59. expect(String.prototype.repeat.call(2, 3)).to.eql('222');
  60. });
  61. it('should work on booleans', function () {
  62. expect(String.prototype.repeat.call(true, 3)).to.eql('truetruetrue');
  63. });
  64. it('should work on dates', function () {
  65. var d = new Date();
  66. expect(String.prototype.repeat.call(d, 3)).to.eql([d, d, d].join(''));
  67. });
  68. });
  69. describe('#startsWith()', function () {
  70. if (!Object.prototype.hasOwnProperty.call(String.prototype, 'startsWith')) {
  71. return it('exists', function () {
  72. expect(String.prototype).to.have.property('startsWith');
  73. });
  74. }
  75. ifFunctionsHaveNamesIt('has the right name', function () {
  76. expect(String.prototype.startsWith).to.have.property('name', 'startsWith');
  77. });
  78. it('is not enumerable', function () {
  79. expect(String.prototype).ownPropertyDescriptor('startsWith').to.have.property('enumerable', false);
  80. });
  81. it('has the right arity', function () {
  82. // WebKit nightly had this bug, fixed in https://bugs.webkit.org/show_bug.cgi?id=143659
  83. expect(String.prototype.startsWith).to.have.property('length', 1);
  84. });
  85. it('should throw a TypeError when called on null or undefined', function () {
  86. testObjectCoercible('startsWith');
  87. });
  88. it('should throw a TypeError when called on null or undefined', function () {
  89. testObjectCoercible('startsWith');
  90. });
  91. it('should be truthy on correct results', function () {
  92. expect('test'.startsWith('te')).to.equal(true);
  93. expect('test'.startsWith('st')).to.equal(false);
  94. expect(''.startsWith('/')).to.equal(false);
  95. expect('#'.startsWith('/')).to.equal(false);
  96. expect('##'.startsWith('///')).to.equal(false);
  97. expect('abc'.startsWith('abc')).to.equal(true);
  98. expect('abcd'.startsWith('abc')).to.equal(true);
  99. expect('abc'.startsWith('a')).to.equal(true);
  100. expect('abc'.startsWith('abcd')).to.equal(false);
  101. expect('abc'.startsWith('bcde')).to.equal(false);
  102. expect('abc'.startsWith('b')).to.equal(false);
  103. expect('abc'.startsWith('abc', 0)).to.equal(true);
  104. expect('abc'.startsWith('bc', 0)).to.equal(false);
  105. expect('abc'.startsWith('bc', 1)).to.equal(true);
  106. expect('abc'.startsWith('c', 1)).to.equal(false);
  107. expect('abc'.startsWith('abc', 1)).to.equal(false);
  108. expect('abc'.startsWith('c', 2)).to.equal(true);
  109. expect('abc'.startsWith('d', 2)).to.equal(false);
  110. expect('abc'.startsWith('dcd', 2)).to.equal(false);
  111. expect('abc'.startsWith('a', NaN)).to.equal(true);
  112. expect('abc'.startsWith('b', NaN)).to.equal(false);
  113. expect('abc'.startsWith('ab', -43)).to.equal(true);
  114. expect('abc'.startsWith('ab', -Infinity)).to.equal(true);
  115. expect('abc'.startsWith('bc', -42)).to.equal(false);
  116. expect('abc'.startsWith('bc', -Infinity)).to.equal(false);
  117. if (hasStrictMode) {
  118. expect(function () {
  119. return ''.startsWith.call(null, 'nu');
  120. }).to['throw'](TypeError);
  121. expect(function () {
  122. return ''.startsWith.call(undefined, 'un');
  123. }).to['throw'](TypeError);
  124. }
  125. var myobj = {
  126. toString: function () { return 'abc'; },
  127. startsWith: String.prototype.startsWith
  128. };
  129. expect(myobj.startsWith('abc')).to.equal(true);
  130. expect(myobj.startsWith('bc')).to.equal(false);
  131. var gotStr = false;
  132. var gotPos = false;
  133. myobj = {
  134. toString: function () {
  135. expect(gotPos).to.equal(false);
  136. gotStr = true;
  137. return 'xyz';
  138. },
  139. startsWith: String.prototype.startsWith
  140. };
  141. var idx = {
  142. valueOf: function () {
  143. expect(gotStr).to.equal(true);
  144. gotPos = true;
  145. return 42;
  146. }
  147. };
  148. myobj.startsWith('elephant', idx);
  149. expect(gotPos).to.equal(true);
  150. });
  151. it('should handle large positions', function () {
  152. expect('abc'.startsWith('a', 42)).to.equal(false);
  153. expect('abc'.startsWith('a', Infinity)).to.equal(false);
  154. });
  155. it('should coerce to a string', function () {
  156. expect('abcd'.startsWith({ toString: function () { return 'ab'; } })).to.equal(true);
  157. expect('abcd'.startsWith({ toString: function () { return 'foo'; } })).to.equal(false);
  158. });
  159. it('should not allow a regex', function () {
  160. expect(function () { return 'abcd'.startsWith(/abc/); }).to['throw'](TypeError);
  161. expect(function () { return 'abcd'.startsWith(new RegExp('abc')); }).to['throw'](TypeError);
  162. });
  163. ifSymbolsDescribe('Symbol.match', function () {
  164. if (!hasSymbols || !Symbol.match) {
  165. return it('exists', function () {
  166. expect(Symbol).to.have.property('match');
  167. });
  168. }
  169. it('allows a regex with Symbol.match set to a falsy value', function () {
  170. var re = /a/g;
  171. re[Symbol.match] = false;
  172. expect(function () { return 'abcd'.startsWith(re); }).not.to['throw']();
  173. expect('abcd'.startsWith(re)).to.equal('abcd'.startsWith(String(re)));
  174. });
  175. });
  176. });
  177. describe('#endsWith()', function () {
  178. if (!Object.prototype.hasOwnProperty.call(String.prototype, 'endsWith')) {
  179. return it('exists', function () {
  180. expect(String.prototype).to.have.property('endsWith');
  181. });
  182. }
  183. ifFunctionsHaveNamesIt('has the right name', function () {
  184. expect(String.prototype.endsWith).to.have.property('name', 'endsWith');
  185. });
  186. it('is not enumerable', function () {
  187. expect(String.prototype).ownPropertyDescriptor('endsWith').to.have.property('enumerable', false);
  188. });
  189. it('has the right arity', function () {
  190. // WebKit nightly had this bug, fixed in https://bugs.webkit.org/show_bug.cgi?id=143659
  191. expect(String.prototype.endsWith).to.have.property('length', 1);
  192. });
  193. it('should throw a TypeError when called on null or undefined', function () {
  194. testObjectCoercible('endsWith');
  195. });
  196. it('should be truthy on correct results', function () {
  197. expect('test'.endsWith('st')).to.equal(true);
  198. expect('test'.endsWith('te')).to.equal(false);
  199. expect(''.endsWith('/')).to.equal(false);
  200. expect('#'.endsWith('/')).to.equal(false);
  201. expect('##'.endsWith('///')).to.equal(false);
  202. expect('abc'.endsWith('abc')).to.equal(true);
  203. expect('abcd'.endsWith('bcd')).to.equal(true);
  204. expect('abc'.endsWith('c')).to.equal(true);
  205. expect('abc'.endsWith('abcd')).to.equal(false);
  206. expect('abc'.endsWith('bbc')).to.equal(false);
  207. expect('abc'.endsWith('b')).to.equal(false);
  208. expect('abc'.endsWith('abc', 3)).to.equal(true);
  209. expect('abc'.endsWith('bc', 3)).to.equal(true);
  210. expect('abc'.endsWith('a', 3)).to.equal(false);
  211. expect('abc'.endsWith('bc', 3)).to.equal(true);
  212. expect('abc'.endsWith('a', 1)).to.equal(true);
  213. expect('abc'.endsWith('abc', 1)).to.equal(false);
  214. expect('abc'.endsWith('b', 2)).to.equal(true);
  215. expect('abc'.endsWith('d', 2)).to.equal(false);
  216. expect('abc'.endsWith('dcd', 2)).to.equal(false);
  217. expect('abc'.endsWith('bc', undefined)).to.equal(true);
  218. expect('abc'.endsWith('bc', NaN)).to.equal(false);
  219. if (hasStrictMode) {
  220. expect(function () {
  221. return ''.endsWith.call(null, 'ull');
  222. }).to['throw'](TypeError);
  223. expect(function () {
  224. return ''.endsWith.call(undefined, 'ned');
  225. }).to['throw'](TypeError);
  226. }
  227. var myobj = {
  228. toString: function () { return 'abc'; },
  229. endsWith: String.prototype.endsWith
  230. };
  231. expect(myobj.endsWith('abc')).to.equal(true);
  232. expect(myobj.endsWith('ab')).to.equal(false);
  233. var gotStr = false;
  234. var gotPos = false;
  235. myobj = {
  236. toString: function () {
  237. expect(gotPos).to.equal(false);
  238. gotStr = true;
  239. return 'xyz';
  240. },
  241. endsWith: String.prototype.endsWith
  242. };
  243. var idx = {
  244. valueOf: function () {
  245. expect(gotStr).to.equal(true);
  246. gotPos = true;
  247. return 42;
  248. }
  249. };
  250. myobj.endsWith('elephant', idx);
  251. expect(gotPos).to.equal(true);
  252. });
  253. it('should coerce to a string', function () {
  254. expect('abcd'.endsWith({ toString: function () { return 'cd'; } })).to.equal(true);
  255. expect('abcd'.endsWith({ toString: function () { return 'foo'; } })).to.equal(false);
  256. });
  257. it('should not allow a regex', function () {
  258. expect(function () { return 'abcd'.endsWith(/abc/); }).to['throw'](TypeError);
  259. expect(function () { return 'abcd'.endsWith(new RegExp('abc')); }).to['throw'](TypeError);
  260. });
  261. it('should handle negative and zero endPositions properly', function () {
  262. expect('abcd'.endsWith('bcd', 0)).to.equal(false);
  263. expect('abcd'.endsWith('bcd', -2)).to.equal(false);
  264. expect('abcd'.endsWith('b', -2)).to.equal(false);
  265. expect('abcd'.endsWith('ab', -2)).to.equal(false);
  266. expect('abc'.endsWith('bc', -43)).to.equal(false);
  267. expect('abc'.endsWith('bc', -Infinity)).to.equal(false);
  268. });
  269. it('should handle large endPositions properly', function () {
  270. expect('abc'.endsWith('a', 42)).to.equal(false);
  271. expect('abc'.endsWith('bc', Infinity)).to.equal(true);
  272. expect('abc'.endsWith('a', Infinity)).to.equal(false);
  273. });
  274. ifSymbolsDescribe('Symbol.match', function () {
  275. if (!hasSymbols || !Symbol.match) {
  276. return it('exists', function () {
  277. expect(Symbol).to.have.property('match');
  278. });
  279. }
  280. it('allows a regex with Symbol.match set to a falsy value', function () {
  281. var re = /a/g;
  282. re[Symbol.match] = false;
  283. expect(function () { return 'abcd'.startsWith(re); }).not.to['throw']();
  284. expect('abcd'.endsWith(re)).to.equal('abcd'.endsWith(String(re)));
  285. });
  286. });
  287. });
  288. describe('#includes()', function () {
  289. if (!Object.prototype.hasOwnProperty.call(String.prototype, 'includes')) {
  290. return it('exists', function () {
  291. expect(String.prototype).to.have.property('includes');
  292. });
  293. }
  294. ifFunctionsHaveNamesIt('has the right name', function () {
  295. expect(String.prototype.includes).to.have.property('name', 'includes');
  296. });
  297. it('is not enumerable', function () {
  298. expect(String.prototype).ownPropertyDescriptor('includes').to.have.property('enumerable', false);
  299. });
  300. it('has the right arity', function () {
  301. // WebKit nightly had this bug, fixed in https://bugs.webkit.org/show_bug.cgi?id=143659
  302. expect(String.prototype.includes).to.have.property('length', 1);
  303. });
  304. it('should throw a TypeError when called on null or undefined', function () {
  305. testObjectCoercible('includes');
  306. });
  307. it('throws a TypeError when given a regex', function () {
  308. expect(function () { 'foo'.includes(/a/g); }).to['throw'](TypeError);
  309. });
  310. it('should be truthy on correct results', function () {
  311. expect('test'.includes('es')).to.equal(true);
  312. expect('abc'.includes('a')).to.equal(true);
  313. expect('abc'.includes('b')).to.equal(true);
  314. expect('abc'.includes('abc')).to.equal(true);
  315. expect('abc'.includes('bc')).to.equal(true);
  316. expect('abc'.includes('d')).to.equal(false);
  317. expect('abc'.includes('abcd')).to.equal(false);
  318. expect('abc'.includes('ac')).to.equal(false);
  319. expect('abc'.includes('abc', 0)).to.equal(true);
  320. expect('abc'.includes('bc', 0)).to.equal(true);
  321. expect('abc'.includes('de', 0)).to.equal(false);
  322. expect('abc'.includes('bc', 1)).to.equal(true);
  323. expect('abc'.includes('c', 1)).to.equal(true);
  324. expect('abc'.includes('a', 1)).to.equal(false);
  325. expect('abc'.includes('abc', 1)).to.equal(false);
  326. expect('abc'.includes('c', 2)).to.equal(true);
  327. expect('abc'.includes('d', 2)).to.equal(false);
  328. expect('abc'.includes('dcd', 2)).to.equal(false);
  329. expect('abc'.includes('ab', NaN)).to.equal(true);
  330. expect('abc'.includes('cd', NaN)).to.equal(false);
  331. var myobj = {
  332. toString: function () { return 'abc'; },
  333. includes: String.prototype.includes
  334. };
  335. expect(myobj.includes('abc')).to.equal(true);
  336. expect(myobj.includes('cd')).to.equal(false);
  337. var gotStr = false;
  338. var gotPos = false;
  339. myobj = {
  340. toString: function () {
  341. expect(gotPos).to.equal(false);
  342. gotStr = true;
  343. return 'xyz';
  344. },
  345. includes: String.prototype.includes
  346. };
  347. var idx = {
  348. valueOf: function () {
  349. expect(gotStr).to.equal(true);
  350. gotPos = true;
  351. return 42;
  352. }
  353. };
  354. myobj.includes('elephant', idx);
  355. expect(gotPos).to.equal(true);
  356. });
  357. it('should handle large positions', function () {
  358. expect('abc'.includes('a', 42)).to.equal(false);
  359. expect('abc'.includes('a', Infinity)).to.equal(false);
  360. });
  361. it('should handle negative positions', function () {
  362. expect('abc'.includes('ab', -43)).to.equal(true);
  363. expect('abc'.includes('cd', -42)).to.equal(false);
  364. expect('abc'.includes('ab', -Infinity)).to.equal(true);
  365. expect('abc'.includes('cd', -Infinity)).to.equal(false);
  366. });
  367. it('should be falsy on incorrect results', function () {
  368. expect('test'.includes('1290')).to.equal(false);
  369. });
  370. ifSymbolsDescribe('Symbol.match', function () {
  371. if (!hasSymbols || !Symbol.match) {
  372. return it('exists', function () {
  373. expect(Symbol).to.have.property('match');
  374. });
  375. }
  376. it('allows a regex with Symbol.match set to a falsy value', function () {
  377. var re = /a/g;
  378. re[Symbol.match] = false;
  379. expect(function () { return 'abcd'.includes(re); }).not.to['throw']();
  380. expect('abcd'.includes(re)).to.equal('abcd'.includes(String(re)));
  381. });
  382. });
  383. });
  384. describe('.fromCodePoint()', function () {
  385. if (!Object.prototype.hasOwnProperty.call(String, 'fromCodePoint')) {
  386. return it('exists', function () {
  387. expect(String).to.have.property('fromCodePoint');
  388. });
  389. }
  390. ifFunctionsHaveNamesIt('has the right name', function () {
  391. expect(String.fromCodePoint).to.have.property('name', 'fromCodePoint');
  392. });
  393. it('is not enumerable', function () {
  394. expect(String).ownPropertyDescriptor('fromCodePoint').to.have.property('enumerable', false);
  395. });
  396. it('has the right arity', function () {
  397. expect(String.fromCodePoint).to.have.property('length', 1);
  398. });
  399. it('throws a RangeError', function () {
  400. var invalidValues = [
  401. 'abc',
  402. {},
  403. -1,
  404. 0x10FFFF + 1
  405. ];
  406. invalidValues.forEach(function (value) {
  407. expect(function () { return String.fromCodePoint(value); }).to['throw'](RangeError);
  408. });
  409. });
  410. it('returns the empty string with no args', function () {
  411. expect(String.fromCodePoint()).to.equal('');
  412. });
  413. it('works', function () {
  414. var codePoints = [];
  415. var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789…?!';
  416. for (var i = 0; i < chars.length; ++i) {
  417. codePoints.push(chars.charCodeAt(i));
  418. expect(String.fromCodePoint(chars.charCodeAt(i))).to.equal(chars[i]);
  419. }
  420. expect(String.fromCodePoint.apply(String, codePoints)).to.equal(chars);
  421. });
  422. it('works with unicode', function () {
  423. expect(String.fromCodePoint(0x2500)).to.equal('\u2500');
  424. expect(String.fromCodePoint(0x010000)).to.equal('\ud800\udc00');
  425. expect(String.fromCodePoint(0x10FFFF)).to.equal('\udbff\udfff');
  426. });
  427. });
  428. describe('#codePointAt()', function () {
  429. if (!Object.prototype.hasOwnProperty.call(String.prototype, 'codePointAt')) {
  430. return it('exists', function () {
  431. expect(String.prototype).to.have.property('codePointAt');
  432. });
  433. }
  434. ifFunctionsHaveNamesIt('has the right name', function () {
  435. expect(String.prototype.codePointAt).to.have.property('name', 'codePointAt');
  436. });
  437. it('is not enumerable', function () {
  438. expect(String.prototype).ownPropertyDescriptor('codePointAt').to.have.property('enumerable', false);
  439. });
  440. it('has the right arity', function () {
  441. expect(String.prototype.codePointAt).to.have.property('length', 1);
  442. });
  443. it('should throw a TypeError when called on null or undefined', function () {
  444. testObjectCoercible('codePointAt');
  445. });
  446. it('should work', function () {
  447. var str = 'abc';
  448. expect(str.codePointAt(0)).to.equal(97);
  449. expect(str.codePointAt(1)).to.equal(98);
  450. expect(str.codePointAt(2)).to.equal(99);
  451. });
  452. it('should work with unicode', function () {
  453. expect('\u2500'.codePointAt(0)).to.equal(0x2500);
  454. expect('\ud800\udc00'.codePointAt(0)).to.equal(0x10000);
  455. expect('\udbff\udfff'.codePointAt(0)).to.equal(0x10ffff);
  456. expect('\ud800\udc00\udbff\udfff'.codePointAt(0)).to.equal(0x10000);
  457. expect('\ud800\udc00\udbff\udfff'.codePointAt(1)).to.equal(0xdc00);
  458. expect('\ud800\udc00\udbff\udfff'.codePointAt(2)).to.equal(0x10ffff);
  459. expect('\ud800\udc00\udbff\udfff'.codePointAt(3)).to.equal(0xdfff);
  460. });
  461. it('should return undefined when pos is negative or too large', function () {
  462. var str = 'abc';
  463. expect(str.codePointAt(-1)).to.equal(undefined);
  464. expect(str.codePointAt(str.length)).to.equal(undefined);
  465. });
  466. });
  467. describe('#[Symbol.iterator]()', function () {
  468. if (!Object.prototype.hasOwnProperty.call(Array, 'from')) {
  469. return it('requires Array.from to test', function () {
  470. expect(Array).to.have.property('from');
  471. });
  472. }
  473. it('should work with plain strings', function () {
  474. var str = 'abc';
  475. expect(Array.from(str)).to.eql(['a', 'b', 'c']);
  476. });
  477. it('should work with surrogate characters', function () {
  478. var str = '\u2500\ud800\udc00\udbff\udfff\ud800';
  479. expect(Array.from(str)).to.eql(['\u2500', '\ud800\udc00', '\udbff\udfff', '\ud800']);
  480. });
  481. });
  482. describe('.raw()', function () {
  483. if (!Object.prototype.hasOwnProperty.call(String, 'raw')) {
  484. return it('exists', function () {
  485. expect(String).to.have.property('raw');
  486. });
  487. }
  488. ifFunctionsHaveNamesIt('has the right name', function () {
  489. expect(String.raw).to.have.property('name', 'raw');
  490. });
  491. it('is not enumerable', function () {
  492. expect(String).ownPropertyDescriptor('raw').to.have.property('enumerable', false);
  493. });
  494. it('has the right arity', function () {
  495. expect(String.raw).to.have.property('length', 1);
  496. });
  497. it('works with template.raw: Array', function () {
  498. var template = {};
  499. var str = 'The total is 10 ($11 with tax)';
  500. template.raw = ['The total is ', ' ($', ' with tax)'];
  501. expect(String.raw(template, 10, 11)).to.eql(str);
  502. // eslint-disable-next-line no-template-curly-in-string
  503. str = 'The total is {total} (${total * 1.01} with tax)';
  504. template.raw = ['The total is ', ' ($', ' with tax)'];
  505. expect(String.raw(template, '{total}', '{total * 1.01}')).to.eql(str);
  506. });
  507. it('works with template.raw: array-like object', function () {
  508. var template = {};
  509. var str = 'The total is 10 ($11 with tax)';
  510. template.raw = { 0: 'The total is ', 1: ' ($', 2: ' with tax)', length: 3 };
  511. expect(String.raw(template, 10, 11)).to.eql(str);
  512. // eslint-disable-next-line no-template-curly-in-string
  513. str = 'The total is {total} (${total * 1.01} with tax)';
  514. template.raw = { 0: 'The total is ', 1: ' ($', 2: ' with tax)', length: 3 };
  515. expect(String.raw(template, '{total}', '{total * 1.01}')).to.eql(str);
  516. });
  517. it('works with template.raw: empty Objects', function () {
  518. var template = { raw: {} };
  519. expect(String.raw(template, '{total}', '{total * 1.01}')).to.eql('');
  520. expect(String.raw(template)).to.equal('');
  521. });
  522. it('ReturnIfAbrupt - Less Substitutions', function () {
  523. var template = {
  524. raw: { 0: 'The total is ', 1: ' ($', 2: ' with tax)', length: 3 }
  525. };
  526. var str = 'The total is 10 ($ with tax)';
  527. expect(String.raw(template, 10)).to.equal(str);
  528. });
  529. ifSymbolsDescribe('Symbol substitutions', function () {
  530. it('throws', function () {
  531. var template = {
  532. raw: { 0: 'The total is ', 1: ' ($', 2: ' with tax)', length: 3 }
  533. };
  534. var str = 'The total is 10 ($ with tax)';
  535. expect(function () { String.raw(template, Symbol()); }).to['throw'](TypeError);
  536. });
  537. });
  538. });
  539. describe('#trim()', function () {
  540. if (!Object.prototype.hasOwnProperty.call(String.prototype, 'trim')) {
  541. return it('exists', function () {
  542. expect(String.prototype).to.have.property('trim');
  543. });
  544. }
  545. ifFunctionsHaveNamesIt('has the right name', function () {
  546. expect(String.prototype.trim).to.have.property('name', 'trim');
  547. });
  548. it('is not enumerable', function () {
  549. expect(String.prototype).ownPropertyDescriptor('trim').to.have.property('enumerable', false);
  550. });
  551. it('has the right arity', function () {
  552. expect(String.prototype.trim).to.have.property('length', 0);
  553. });
  554. it('should trim the correct characters', function () {
  555. var whitespace = [
  556. '\u0009',
  557. '\u000b',
  558. '\u000c',
  559. '\u0020',
  560. '\u00a0',
  561. '\u1680',
  562. '\u2000',
  563. '\u2001',
  564. '\u2002',
  565. '\u2003',
  566. '\u2004',
  567. '\u2005',
  568. '\u2006',
  569. '\u2007',
  570. '\u2008',
  571. '\u2009',
  572. '\u200A',
  573. '\u202f',
  574. '\u205f',
  575. '\u3000'
  576. ].join('');
  577. var lineTerminators = [
  578. '\u000a',
  579. '\u000d',
  580. '\u2028',
  581. '\u2029'
  582. ].join('');
  583. var trimmed = (whitespace + lineTerminators).trim();
  584. expect(trimmed).to.have.property('length', 0);
  585. expect(trimmed).to.equal('');
  586. });
  587. it('should not trim U+0085', function () {
  588. var trimmed = '\u0085'.trim();
  589. expect(trimmed).to.have.property('length', 1);
  590. expect(trimmed).to.equal('\u0085');
  591. });
  592. it('should trim on both sides', function () {
  593. var trimmed = ' a '.trim();
  594. expect(trimmed).to.have.property('length', 1);
  595. expect(trimmed).to.equal('a');
  596. });
  597. });
  598. describe('#search()', function () {
  599. it('works with strings', function () {
  600. expect('abc'.search('a')).to.equal(0);
  601. expect('abc'.search('b')).to.equal(1);
  602. expect('abc'.search('c')).to.equal(2);
  603. expect('abc'.search('d')).to.equal(-1);
  604. });
  605. it('works with regexes', function () {
  606. expect('abc'.search(/a/)).to.equal(0);
  607. expect('abc'.search(/b/)).to.equal(1);
  608. expect('abc'.search(/c/)).to.equal(2);
  609. expect('abc'.search(/d/)).to.equal(-1);
  610. });
  611. ifSymbolsDescribe('Symbol.search', function () {
  612. it('is a symbol', function () {
  613. expect(typeof Symbol.search).to.equal('symbol');
  614. });
  615. if (!hasSymbols || typeof Symbol.search !== 'symbol') {
  616. return;
  617. }
  618. it('is nonconfigurable', function () {
  619. expect(Symbol).ownPropertyDescriptor('search').to.have.property('configurable', false);
  620. });
  621. it('is nonenumerable', function () {
  622. expect(Symbol).ownPropertyDescriptor('search').to.have.property('enumerable', false);
  623. });
  624. it('is nonwritable', function () {
  625. expect(Symbol).ownPropertyDescriptor('search').to.have.property('writable', false);
  626. });
  627. it('is respected', function () {
  628. var str = Object('a');
  629. var obj = {};
  630. obj[Symbol.search] = function (string) { return string === str && this === obj; };
  631. expect(str.search(obj)).to.equal(true);
  632. });
  633. });
  634. });
  635. describe('#replace()', function () {
  636. it('works', function () {
  637. expect('abcabc'.replace('c', 'd')).to.equal('abdabc');
  638. expect('abcabc'.replace(/c/, 'd')).to.equal('abdabc');
  639. expect('abcabc'.replace(/c/g, 'd')).to.equal('abdabd');
  640. expect('abcabc'.replace(/C/ig, 'd')).to.equal('abdabd');
  641. });
  642. ifSymbolsDescribe('Symbol.replace', function () {
  643. it('is a symbol', function () {
  644. expect(typeof Symbol.replace).to.equal('symbol');
  645. });
  646. if (!hasSymbols || typeof Symbol.replace !== 'symbol') {
  647. return;
  648. }
  649. it('is nonconfigurable', function () {
  650. expect(Symbol).ownPropertyDescriptor('replace').to.have.property('configurable', false);
  651. });
  652. it('is nonenumerable', function () {
  653. expect(Symbol).ownPropertyDescriptor('replace').to.have.property('enumerable', false);
  654. });
  655. it('is nonwritable', function () {
  656. expect(Symbol).ownPropertyDescriptor('replace').to.have.property('writable', false);
  657. });
  658. it('respects Symbol.replace', function () {
  659. var str = Object('a');
  660. var replaceVal = Object('replaceValue');
  661. var obj = {};
  662. obj[Symbol.replace] = function (string, replaceValue) {
  663. return string === str && replaceValue === replaceVal && this === obj;
  664. };
  665. expect(str.replace(obj, replaceVal)).to.equal(true);
  666. });
  667. });
  668. });
  669. describe('#split()', function () {
  670. it('works', function () {
  671. expect('abcabc'.split('b')).to.eql(['a', 'ca', 'c']);
  672. expect('abcabc'.split('b', 2)).to.eql(['a', 'ca']);
  673. expect('abcabc'.split(/b.?/)).to.eql(['a', 'a', '']);
  674. expect('abcabc'.split(/b.?/, 2)).to.eql(['a', 'a']);
  675. expect('abcabc'.split(/b/)).to.eql(['a', 'ca', 'c']);
  676. expect('abcabc'.split(/b/, 2)).to.eql(['a', 'ca']);
  677. expect('abcabc'.split(/b/g)).to.eql(['a', 'ca', 'c']);
  678. expect('abcabc'.split(/b/g, 2)).to.eql(['a', 'ca']);
  679. expect('abcabc'.split(/B/i)).to.eql(['a', 'ca', 'c']);
  680. expect('abcabc'.split(/B/i, 2)).to.eql(['a', 'ca']);
  681. expect('abcabc'.split(/B/gi)).to.eql(['a', 'ca', 'c']);
  682. expect('abcabc'.split(/B/gi, 2)).to.eql(['a', 'ca']);
  683. });
  684. ifSymbolsDescribe('Symbol.split', function () {
  685. it('is a symbol', function () {
  686. expect(typeof Symbol.split).to.equal('symbol');
  687. });
  688. if (!hasSymbols || typeof Symbol.split !== 'symbol') {
  689. return;
  690. }
  691. it('is nonconfigurable', function () {
  692. expect(Symbol).ownPropertyDescriptor('split').to.have.property('configurable', false);
  693. });
  694. it('is nonenumerable', function () {
  695. expect(Symbol).ownPropertyDescriptor('split').to.have.property('enumerable', false);
  696. });
  697. it('is nonwritable', function () {
  698. expect(Symbol).ownPropertyDescriptor('split').to.have.property('writable', false);
  699. });
  700. it('respects Symbol.split', function () {
  701. var str = Object('a');
  702. var limitVal = Object(42);
  703. var obj = {};
  704. obj[Symbol.split] = function (string, limit) { return string === str && limit === limitVal && this === obj; };
  705. expect(str.split(obj, limitVal)).to.equal(true);
  706. });
  707. });
  708. });
  709. describe('#match()', function () {
  710. it('works with a string', function () {
  711. var str = 'abca';
  712. var match = str.match('a');
  713. expect(match.index).to.equal(0);
  714. expect(match.input).to.equal(str);
  715. expect(Array.prototype.slice.call(match)).to.eql(['a']);
  716. });
  717. it('works with a regex', function () {
  718. var str = 'abca';
  719. var match = str.match(/a/);
  720. expect(match.index).to.equal(0);
  721. expect(match.input).to.equal(str);
  722. expect(Array.prototype.slice.call(match)).to.eql(['a']);
  723. });
  724. ifSymbolsDescribe('Symbol.match', function () {
  725. it('is a symbol', function () {
  726. expect(typeof Symbol.match).to.equal('symbol');
  727. });
  728. if (!hasSymbols || typeof Symbol.match !== 'symbol') {
  729. return;
  730. }
  731. it('is nonconfigurable', function () {
  732. expect(Symbol).ownPropertyDescriptor('match').to.have.property('configurable', false);
  733. });
  734. it('is nonenumerable', function () {
  735. expect(Symbol).ownPropertyDescriptor('match').to.have.property('enumerable', false);
  736. });
  737. it('is nonwritable', function () {
  738. expect(Symbol).ownPropertyDescriptor('match').to.have.property('writable', false);
  739. });
  740. it('respects Symbol.match', function () {
  741. var str = Object('a');
  742. var obj = {};
  743. obj[Symbol.match] = function (string) { return string === str && this === obj; };
  744. expect(str.match(obj)).to.equal(true);
  745. });
  746. });
  747. });
  748. });
  749. describe('Annex B', function () {
  750. it('has #anchor', function () {
  751. expect('foo'.anchor('bar"baz"')).to.equal('<a name="bar&quot;baz&quot;">foo</a>');
  752. });
  753. it('has #big', function () {
  754. expect('foo'.big()).to.equal('<big>foo</big>');
  755. });
  756. it('has #blink', function () {
  757. expect('foo'.blink()).to.equal('<blink>foo</blink>');
  758. });
  759. it('has #bold', function () {
  760. expect('foo'.bold()).to.equal('<b>foo</b>');
  761. });
  762. it('has #fixed', function () {
  763. expect('foo'.fixed()).to.equal('<tt>foo</tt>');
  764. });
  765. it('has #fontcolor', function () {
  766. expect('foo'.fontcolor('blue"red"green')).to.equal('<font color="blue&quot;red&quot;green">foo</font>');
  767. });
  768. it('has #fontsize', function () {
  769. expect('foo'.fontsize('10"large"small')).to.equal('<font size="10&quot;large&quot;small">foo</font>');
  770. });
  771. it('has #italics', function () {
  772. expect('foo'.italics()).to.equal('<i>foo</i>');
  773. });
  774. it('has #link', function () {
  775. expect('foo'.link('url"http://"')).to.equal('<a href="url&quot;http://&quot;">foo</a>');
  776. });
  777. it('has #small', function () {
  778. expect('foo'.small()).to.equal('<small>foo</small>');
  779. });
  780. it('has #strike', function () {
  781. expect('foo'.strike()).to.equal('<strike>foo</strike>');
  782. });
  783. it('has #sub', function () {
  784. expect('foo'.sub()).to.equal('<sub>foo</sub>');
  785. });
  786. it('has #sup', function () {
  787. expect('foo'.sup()).to.equal('<sup>foo</sup>');
  788. });
  789. });
  790. };
  791. describe('clean Object.prototype', function () {
  792. return runStringTests.call(this, it);
  793. });
  794. describe('polluted Object.prototype', function () {
  795. var shimmedIt = function () {
  796. /* eslint-disable no-extend-native */
  797. Object.prototype[1] = 42;
  798. /* eslint-enable no-extend-native */
  799. it.apply(this, arguments);
  800. delete Object.prototype[1];
  801. };
  802. shimmedIt.skip = it.skip;
  803. return runStringTests.call(this, shimmedIt);
  804. });