String.uts 24.1 KB
Newer Older
Y
yurj26 已提交
1 2
import { describe, test, expect, Result } from './tests.uts'

3 4 5 6 7 8 9
export function testString() : Result {
  return describe("String", () => {
    test('length', () => {
      // #TEST String.length
      const x = "Mozilla";
      const e = "";

10
      console.log("Mozilla is " + `${x.length}` + " code units long");
11 12
      /* "Mozilla is 7 code units long" */

13
      console.log("The empty string is has a length of " + `${e.length}`);
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
      /* "The e string is has a length of 0" */
      // #END

      expect(x.length).toEqual(7);
      // expect(x[0]).toEqual('M');
      const empty = "";
      expect(empty.length).toEqual(0);
      // const adlam = "𞤲𞥋𞤣𞤫";
      // expect(adlam.length).toEqual(8);
      // const formula = "��𝑥∈ℝ,𝑥²��";
      // expect(formula.length).toEqual(11);

      // 1. web: ��大长度和js引擎有关,在v8中为 2^29 - 24
      // 超出边界报错: RangeError: Invalid string length
      // 2. kotlin: ��大长��2^31-1
      // 超出边界报错:  Error: targetMethod error::java.lang.OutOfMemoryError: char[] of length
      // 3. swift: ��大长度和内存有关
      // 超出边界没有返回信息
      // const length = Math.pow(2, 29) - 24
      // const str = 'x'.repeat(length);
      // expect(str.length).toEqual(11);
    })
    test('at', () => {
      // #TEST String.at
      const sentence = 'The quick brown fox jumps over the lazy dog.';
      let index = 5;
      console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
      // expected output: "Using an index of 5 the character returned is u"
      index = -4;
      console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
      // expected output: "Using an index of -4 the character returned is d"
      // #END

      index = 5;
      expect(sentence.at(index)).toEqual("u");
      index = -4;
      expect(sentence.at(index)).toEqual("d");
      expect(sentence.at(999)).toEqual(null);

      let invoiceRef = 'myinvoice01';
      expect(invoiceRef.at(-1)).toEqual("1");
      invoiceRef = 'myinvoice02';
      expect(invoiceRef.at(-1)).toEqual("2");
      expect(invoiceRef.at(-999)).toEqual(null);

      const empty = "";
      expect(empty.at(0)).toEqual(null);
    })
    test('charAt', () => {
      // #TEST String.charAt
      const anyString = "Brave new world";
      console.log("The character at index 0   is '" + anyString.charAt(0) + "'");
      // The character at index 0 is 'B'
      console.log("The character at index 1   is '" + anyString.charAt(1) + "'");
      // The character at index 1 is 'r'
      console.log("The character at index 2   is '" + anyString.charAt(2) + "'");
      // The character at index 2 is 'a'
      console.log("The character at index 3   is '" + anyString.charAt(3) + "'");
      // The character at index 3 is 'v'
      console.log("The character at index 4   is '" + anyString.charAt(4) + "'");
      // The character at index 4 is 'e'
      console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
      // The character at index 999 is ''
      // #END



      expect(anyString.charAt(0)).toEqual("B");
      expect(anyString.charAt(1)).toEqual("r");
      expect(anyString.charAt(2)).toEqual("a");
      expect(anyString.charAt(3)).toEqual("v");
      expect(anyString.charAt(4)).toEqual("e");
      expect(anyString.charAt(999)).toEqual("");
      expect(anyString.charAt(-1)).toEqual("");
      expect(anyString.charAt(-2)).toEqual("");
      expect(anyString.charAt(-999)).toEqual("");

      const empty = "";
      expect(empty.charAt(0)).toEqual("");
    })
    test('toWellFormed', () => {
      // #ifdef APP-ANDROID
      // #TEST String.toWellFormed
      let ret = "ab\uD800".toWellFormed()
      console.log(ret) //"ab\uFFFD"
      // #END
      
      expect(ret).toEqual("ab\uFFFD");
      expect("ab\uD800c".toWellFormed()).toEqual("ab\uFFFDc");
      expect("\uDFFFab".toWellFormed()).toEqual("\uFFFDab");
      expect("c\uDFFFab".toWellFormed()).toEqual("c\uFFFDab");
      expect("abc".toWellFormed()).toEqual("abc");
      expect("ab\uD83D\uDE04c".toWellFormed()).toEqual("ab\uD83D\uDE04c");
      expect("ab\uD83D\uDE04c".toWellFormed()).toEqual("ab\uD83D\uDE04c");
      expect("ab\uD83D\uDE04c\uD83D".toWellFormed()).toEqual("ab\uD83D\uDE04c\uFFFD");
      // #endif
    })
    test('isWellFormed', () => {
      // #ifdef APP-ANDROID
      // #TEST String.isWellFormed
      let ret = "ab\uD800".isWellFormed()
      console.log(ret) //false
      // #END
      
      expect(ret).toEqual(false);
      expect("ab\uD800c".isWellFormed()).toEqual(false);
      expect("\uDFFFab".isWellFormed()).toEqual(false);
      expect("c\uDFFFab".isWellFormed()).toEqual(false);
      expect("abc".isWellFormed()).toEqual(true);
      expect("ab\uD83D\uDE04c".isWellFormed()).toEqual(true);
      expect("ab\uD83D\uDE04c".isWellFormed()).toEqual(true);
      expect("ab\uD83D\uDE04c\uD83D".isWellFormed()).toEqual(false);
      // #endif
    })
    test('charCodeAt', () => {
      // #TEST String.charCodeAt
      const sentence = 'The quick brown fox jumps over the lazy dog.';
      const index = 4;
      console.log(`The character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(index)}`);
      // expected output: "The character code 113 is equal to q"
      // #END

      expect(sentence.charCodeAt(index)).toEqual(113);

      expect("ABC".charCodeAt(0)).toEqual(65);
      expect("ABC".charCodeAt(1)).toEqual(66);
      expect("ABC".charCodeAt(2)).toEqual(67);
      expect("ABC".charCodeAt(3)).toEqual(NaN);

      const empty = "";
      expect(empty.charCodeAt(0)).toEqual(NaN);
    })
    test('fromCharCode', () => {
      // #TEST String.fromCharCode
      console.log(String.fromCharCode(65, 66, 67));
      // expected output: "ABC"
      console.log(String.fromCharCode(0x12014));
      // expected output: "𝌆a𝌇"
      // #END

      expect(String.fromCharCode(65, 66, 67)).toEqual("ABC");
      expect(String.fromCharCode(0x12014)).toEqual("—");
      expect(String.fromCharCode(0xd834, 0xdf06, 0x61, 0xd834, 0xdf07)).toEqual("𝌆a𝌇");
    })
    test('concat', () => {
      // #TEST String.concat
      let hello = 'Hello, '
      let ret1 = hello.concat('Kevin', '. Have a nice day.')
      console.log(ret1)
      // Hello, Kevin. Have a nice day.
      // #END

      expect(ret1).toEqual("Hello, Kevin. Have a nice day.");
      expect("".concat('abc')).toEqual("abc");
    })
    test('endsWith', () => {
      // #TEST String.endsWith
      const str1 = 'Cats are the best!';
      console.log(str1.endsWith('best!'));
      // expected output: true
      console.log(str1.endsWith('best', 17));
      // expected output: true
      const str2 = 'Is this a question?';
      console.log(str2.endsWith('question'));
      // expected output: false
      // #END

      expect(str1.endsWith('best!')).toEqual(true);
      expect(str1.endsWith('best', 17)).toEqual(true);
      expect(str2.endsWith('question')).toEqual(false);
      expect("".includes("test")).toEqual(false);
    })
    test('includes', () => {

      // #TEST String.includes
      const sentence = 'The quick brown fox jumps over the lazy dog.';
      const word = 'fox';
      console.log(sentence.includes(word)) // true
      // #END

      expect(sentence.includes(word)).toEqual(true);

      expect("Blue Whale".includes("blue")).toEqual(false);
      expect("Blue Whale".toLowerCase().includes("blue")).toEqual(true);

      expect("".includes("test")).toEqual(false);
    })
    test('indexOf', () => {
      // #TEST String.indexOf
      const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
      const searchTerm = 'dog';
      const indexOfFirst = paragraph.indexOf(searchTerm);
      console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
      // expected output: "The index of the first "dog" from the beginning is 40"

      console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
      // expected output: "The index of the 2nd "dog" is 52"
      // #END

      expect(indexOfFirst).toEqual(40);
      expect(paragraph.indexOf(searchTerm, (indexOfFirst + 1))).toEqual(52);

      expect('Blue Whale'.indexOf('Blue')).toEqual(0);
      expect('Blue Whale'.indexOf('Blute')).toEqual(-1);
      expect('Blue Whale'.indexOf('Whale', 0)).toEqual(5);
      expect('Blue Whale'.indexOf('Whale', 5)).toEqual(5);
      expect('Blue Whale'.indexOf('Whale', 7)).toEqual(-1);

      expect("".indexOf("test")).toEqual(-1);
    })
    test('match', () => {
      // #TEST String.match
      const str = 'The quick brown fox jumps over the lazy dog. It barked.';
      const result = str.match(new RegExp('[A-Z]', 'g'));
      console.log(result![0])//"T"
      // #END
      // expect(result!.length).toEqual(2);
      expect(result![0]).toEqual("T");
      expect(result![1]).toEqual("I");

      const result2 = str.match(new RegExp('[A-Z]'));
      // expect(result2!.length).toEqual(1);
      expect(result2![0]).toEqual("T");

      // #ifdef APP-ANDROID
      const gradientString = 'linear-gradient(to right, rgb(255, 0, 0), #00FF00, hsl(120, 100%, 50%))';
      const pattern = /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)|#([a-fA-F0-9]{2}){3}|hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)/g;
      const result3 = gradientString.match(pattern);
      expect(result3?.length).toEqual(3);
      expect(result3?.[0]).toEqual("rgb(255, 0, 0)");
      expect(result3?.[2]).toEqual("hsl(120, 100%, 50%)");

      const pattern2 = /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)|#([a-fA-F0-9]{2}){3}|hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)/;
      const result4 = gradientString.match(pattern2);
      expect(result4?.length).toEqual(8);
      expect(result4?.[0]).toEqual("rgb(255, 0, 0)");
      expect(result4?.[1]).toEqual("255");
      expect(result4?.[2]).toEqual("0");


      const url = '';
      const urlRegex = /^(\w+):\/\/([^\/?#]+)([^?#]*)(\?[^#]*)?(#.*)?$/;
      const match = url.match(urlRegex);
      expect(JSON.stringify(match)).toEqual("null");
      // #endif

    })
    test('padEnd', () => {
       // #TEST String.padEnd
      const str1 = 'Breaded Mushrooms';
      let ret1= str1.padEnd(25, '.')
      console.log(ret1);
      // expected output: "Breaded Mushrooms........"
      const str2 = '200';
      let ret2=str2.padEnd(5)
      console.log(ret2);
      // expected output: "200  "
      // #END
      
      expect(str1.padEnd(25, '.')).toEqual("Breaded Mushrooms........");
      expect(ret2).toEqual("200  ");

      expect('abc'.padEnd(10)).toEqual("abc       ");
      expect('abc'.padEnd(10, "foo")).toEqual("abcfoofoof");
      expect('abc'.padEnd(6, "123456")).toEqual("abc123");
      expect('abc'.padEnd(1)).toEqual("abc");
    })
    test('padStart', () => {
       // #TEST String.padStart
      const str1 = '5';
      let ret = str1.padStart(2, '0')
      console.log(ret);
      // expected output: "05"
      // #END
      
      expect(ret).toEqual("05");

      expect('abc'.padStart(10)).toEqual("       abc");
      expect('abc'.padStart(10, "foo")).toEqual("foofoofabc");
      expect('abc'.padStart(6, "123456")).toEqual("123abc");
      expect('abc'.padStart(8, "0")).toEqual("00000abc");
      expect('abc'.padStart(1)).toEqual("abc");
    })
    test('repeat', () => {
      const str1 = 'abc';
      // #TEST String.repeat
      "abc".repeat(0)      // ""
      "abc".repeat(1)      // "abc"
      "abc".repeat(2)      // "abcabc"
      "abc".repeat(3.5)    // "abcabcabc" 参数 count 将会被自动转换成整数。
      // #END
      expect(str1.repeat(0)).toEqual("");
      expect(str1.repeat(1)).toEqual("abc");
      expect(str1.repeat(2)).toEqual("abcabc");
      expect(str1.repeat(3.5)).toEqual("abcabcabc");

      expect("".repeat(1)).toEqual("");
    })
    test('replace', () => {
      // #TEST String.replace
      const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
      let ret1 = p.replace('dog', 'monkey')

      console.log(ret1);
      // expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
      const regex = /Dog/i;
      let ret2 = p.replace(regex, 'ferret')
      console.log(ret2);
      // expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
      // #END

      expect(ret1).toEqual("The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?");
      expect(ret2).toEqual("The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?");
      const str = 'abc12345#$*%';
      const replacer = (match : string, p : string[], offset : number, string : string) : string => {
        // p1 is nondigits, p2 digits, and p3 non-alphanumerics
        expect(offset).toEqual(0);
        expect(match).toEqual(str);
        expect(string).toEqual(str);
        return p.join(' - ');
      }


      // #TEST String.replace_1
338
      // #ifdef APP-ANDROID
339 340 341 342 343 344 345 346 347
      // 不包含捕捉组的示例
      let a = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
      let b = a.replace(RegExp("fox"), function (match : string, offset : number, string : string) : string {
        console.log("match", match)
        console.log("offset", offset)
        console.log("string", string)
        return "cat"
      })
      console.log("b:", b)
348
      
349 350 351 352 353 354 355 356 357 358
      // 包含一个捕获组的示例。注意,目前android仅支持最多五个捕获组
      let a1 = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
      let b1 = a1.replace(RegExp("(fox)"), function (match : string, p1 : string, offset : number, string : string) : string {
        console.log("match", match)
        console.log("p1", p1)
        console.log("offset", offset)
        console.log("string", string)
        return "cat"
      })
      console.log("b1", b1)
359 360
      // #endif

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
      // #END

      // const REGEX_FORMAT = /[YMDHhms]o|\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a{1,2}|A{1,2}|m{1,2}|s{1,2}|Z{1,2}|SSS/g
      // const formatStr = 'This is a [sample] text with [another] capture group.'
      // const nextStr = formatStr.replace(REGEX_FORMAT, (match:string, p1: string|null, offset: number, string: string):string =>{
      // 	console.log('123', p1, match)
      // 	return p1 ?? match ?? ''
      // })
      // expect(nextStr).toEqual('This is a sample text with another capture group.');
      // var newString = str.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
      // expect(newString).toEqual("abc - 12345 - #$*%");
      // const str1 = 'hello, world';
      // const newString1 = str1.replace(/world/, (match: string, offset:number):string => `JavaScript (${offset})`);
      // expect(newString1).toEqual('hello, JavaScript (7)');
    })
    test('search', () => {
      // #TEST String.search
      const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
      const regex = /[^\w\s]/g;
380
      // #ifdef APP-ANDROID
381 382 383 384
      console.log(paragraph.search(regex));
      // expected output: 43
      console.log(paragraph[paragraph.search(regex)]);
      // expected output: "."
385
      // #endif
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
      // #END

      expect(paragraph.search(regex)).toEqual(43);

      var str = "hey JudE";
      var re = /[A-Z]/g;
      var re2 = /[.]/g;
      expect(str.search(re)).toEqual(4);
      expect(str.search(re2)).toEqual(-1);
      expect("".search(re2)).toEqual(-1);
    })
    test('slice', () => {
      // #TEST String.slice
      const str = 'The quick brown fox jumps over the lazy dog.';
      let ret = str.slice(31)
      console.log(ret);
      // expected output: "the lazy dog."
      console.log(str.slice(4, 19));
      // expected output: "quick brown fox"
      // #END

      expect(ret).toEqual("the lazy dog.");

      let str1 = 'The morning is upon us.', // str1 的长��length ��23��
        str2 = str1.slice(1, 8),
        str3 = str1.slice(4, -2),
        str4 = str1.slice(12),
        str5 = str1.slice(30),
        str6 = str1.slice();
      expect(str2).toEqual("he morn");
      expect(str3).toEqual("morning is upon u");
      expect(str4).toEqual("is upon us.");
      expect(str5).toEqual("");
      expect(str6).toEqual("The morning is upon us.");

      expect("".slice()).toEqual("");

      expect("abcdefg".slice(-1)).toEqual("g");
      expect("abcdefg".slice(-1, -2)).toEqual("");

    })
    test('split', () => {
      // #TEST String.split
      const str = 'The quick brown fox jumps over the lazy dog.';
      const words = str.split(' ');
      let ret1 = words[3]
      console.log(ret1);
      // expected output: "fox"
      const chars = str.split('');
      console.log(chars[8]);
      // expected output: "k"
      // #END

      expect(ret1).toEqual("fox");
      expect(chars[8]).toEqual("k");

      var myString = "Hello World. How are you doing?";
      var splits = myString.split(" ", 3);
      var splits1 = myString.split(" ", 10);
      expect(splits).toEqual(["Hello", "World.", "How"]);
      expect(splits1).toEqual(['Hello', 'World.', 'How', 'are', 'you', 'doing?']);

      expect(''.split('')).toEqual([])
      expect('abc'.split('b')).toEqual(["a", "c"])
      expect('abc'.split('d')).toEqual(["abc"])

      var str1 = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
      var re1 = /\s*(?:;|$)\s*/;
      expect(str1.split(re1)).toEqual(["Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", ""])

      var str2 = "a, b, c, d,    e";
      var re2 = /,\s*/g;
      expect(str2.split(re2)).toEqual(["a", "b", "c", "d", "e"]);

      var str3 = "foo-bar_baz(qux-quux)_corge";
      var re3 = new RegExp("[_-](?![^(]*\\))", "g");
      expect(str3.split(re3)).toEqual(["foo", "bar", "baz(qux-quux)", "corge"]);

      var str4 = "a, b, {c, d, e}, f, g, h";
      var re4 = /,(?![^{]*\})/;
      expect(str4.split(re4)).toEqual(["a", " b", " {c, d, e}", " f", " g", " h"]);
杜庆泉's avatar
杜庆泉 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
      
      let a1 = "#d1d1d1".replace(RegExp("\\s*,\\s*", "g"), ",")
      let a2 = a1.split(RegExp("\\s+", ""));
      expect(a2).toEqual(["#d1d1d1"]);
      
      let a3 = "123456a".split(RegExp("\\d+", ""));
      expect(a3).toEqual(["","a"]);
      
      let a4 = "123456a".split(RegExp("\\d+", ""));
      expect(a4).toEqual(["","a"]);
      
      let a5 = "778899	003213".split(RegExp("\\s+", ""));
      expect(a5).toEqual(["778899","003213"]);
      // #ifdef APP-ANDROID
      let sp1 = "😄😄".split(RegExp("(?:)", ""));
      let sp2 = "😄😄".split(RegExp("(?:)", "u"));
      expect(sp1.length).toEqual(4);
      expect(sp2.length).toEqual(2);
      // #endif
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
    })
    test('toLowerCase', () => {
      // #TEST String.toLowerCase
      const str1 = '中文简体 zh-CN || zh-Hans';
      const str2 = 'ALPHABET';
      console.log('str1'.toLowerCase());
      // 中文简体 zh-cn || zh-hans
      console.log(str2.toLowerCase());
      // "alphabet"
      // #END
      
      expect(str1.toLowerCase()).toEqual("中文简体 zh-cn || zh-hans");

      expect(str2.toLowerCase()).toEqual("alphabet");
    })
    test('toUpperCase', () => {
       // #TEST String.toUpperCase
      const sentence = 'The quick brown fox jumps over the lazy dog.';
      console.log(sentence.toUpperCase());
      // expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
      // #END
      
      expect(sentence.toUpperCase()).toEqual("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.");
    })

    test("lastIndexOf", () => {
      // #TEST String.lastIndexOf
      console.log('canal'.lastIndexOf('a'))//3
      // #END
      expect('canal'.lastIndexOf('a')).toEqual(3);
      expect('canal'.lastIndexOf('a', 2)).toEqual(1);
      expect('canal'.lastIndexOf('a', 0)).toEqual(-1);
      expect('canal'.lastIndexOf('')).toEqual(5);
      expect('canal'.lastIndexOf('', 2)).toEqual(2);
      expect("Blue Whale, Killer Whale".lastIndexOf("Whale")).toEqual(19);
      expect("Blue Whale, Killer Whale".lastIndexOf("blue")).toEqual(-1);
    })
    test("substring", () => {
       // #TEST String.substr
      var str1 = "Mozilla";
      let ret = str1.substring(0, 3)
      console.log(ret)//"Moz"
      // #END
      
      expect(ret).toEqual("Moz");
      expect(str1.substring(3, 0)).toEqual("Moz");
      expect(str1.substring(3, -3)).toEqual("Moz");

      var str2 = "hello world";
      // expect(str2.substring()).toEqual("hello world");
      expect(str2.substring(6)).toEqual("world");
      expect(str2.substring(0)).toEqual("hello world");
      expect(str2.substring(0, 5)).toEqual("hello");
      expect(str2.substring(6, 11)).toEqual("world");
      expect(str2.substring(3, 8)).toEqual("lo wo");
      expect(str2.substring(0, -1)).toEqual("");
      expect(str2.substring(0, 20)).toEqual("hello world");

      var str3 = "aaa";
      expect(str3.substring(10, 1)).toEqual("aa");
    })
    test("trim", () => {
      // #TEST String.trim
      let greeting = '   Hello world!   ';
      let ret = greeting.trim()
      console.log(ret) //Hello world!
      // #END
      expect(ret).toEqual("Hello world!");
      const orig : string = '   foo  ';
      expect(orig.trim()).toEqual("foo");
      const str = '\t\t\tworld\t\t\t';
      expect(str.trim()).toEqual('world');
      const str1 = '\n\n\nhello\n\n\n';
      expect(str1.trim()).toEqual('hello');
      const str2 = '';
      expect(str2.trim()).toEqual('');
    })
    test("trimStart", () => {
      const str = '    Hello World   ';
      expect(str.trimStart()).toEqual('Hello World   ');
      const str1 = 'Hello World';
      expect(str1.trimStart()).toEqual(str1);
      const str2 = '   \t  \n   ';
      expect(str2.trimStart()).toEqual('');
    })
    test("trimEnd", () => {
      const str = '    Hello World   ';
      expect(str.trimEnd()).toEqual('    Hello World');
      const str1 = 'Hello World';
      expect(str1.trimEnd()).toEqual(str1);
      const str2 = '   \t  \n   ';
      expect(str2.trimEnd()).toEqual('');
    })
    test("startsWith", () => {
      // #TEST String.startsWith
      const str = 'hello world';
      console.log(str.startsWith('hello'))//true
      // #END

      expect(str.startsWith('hello')).toEqual(true);
      expect(str.startsWith('h')).toEqual(true);
      expect(str.startsWith('HELLO')).toEqual(false);
      expect(str.startsWith('o')).toEqual(false);

      const str1 : string = "To be, or not to be, that is the question.";
      expect(str1.startsWith("To be")).toEqual(true);
      expect(str1.startsWith("not to be")).toEqual(false);
      expect(str1.startsWith("not to be", 10)).toEqual(true);
      expect(str1.startsWith("not to be", 8)).toEqual(false);
    })
    test("matchAll", () => {
      // const reg = /t(e)(st(\d?))/g;
      // const str = 'test1test2';
      // const result = [...str.matchAll(reg)];
      // expect(result[0]).toEqual(["test1", "e", "st1", "1"]);
      // expect(result[1]).toEqual(["test2", "e", "st2", "2"]);

      // const reg1 =  /[A-Z]/g;
      // const str1 = 'The quick brown fox jumps over the lazy dog';
      // const result1 = [...str1.matchAll(reg1)];
      // expect(result1[0]).toEqual(["T"]);
    })
    test("replaceAll", () => {
      // const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
      // expect(p.replaceAll('dog', 'monkey')).toEqual("The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?");
      // const regex = /Dog/ig;
      // expect(p.replaceAll(regex, 'ferret')).toEqual( "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?");

      // const reg = /[aeiou]/g;
      // const str = 'The quick brown fox jumps over the lazy dog';
      // const result = str.replaceAll(reg, '!');
      // expect(result).toEqual("Th! q!!ck br!wn f!x j!mps !v!r th! l!zy d!g");

      // const reg1 = /o/g;
      // const str1 = 'Hello World!';
      // const result1 = str1.replaceAll(reg1, (match, offset, original) => {
      //     expect(match).toEqual('o');
      //     expect(original[offset]).toEqual('o');
      //     expect(original).toEqual(str1);
      //     return `x`;
      // });
      // expect(result1).toEqual("Hellx Wxrld!");
Y
yurj26 已提交
628
    })
629 630
  })
}