提交 5720bdd5 编写于 作者: M mahaifeng

[string]去除文档中手动生成的代码,添加注释

上级 42cfe104
import { describe, test, expect, Result } from './tests.uts' import { describe, test, expect, Result } from './tests.uts'
export function testString(): Result { export function testString() : Result {
return describe("String", () => { return describe("String", () => {
test('length', () => { test('length', () => {
const x = "Mozilla"; // #TEST String.length
expect(x.length).toEqual(7); const x = "Mozilla";
// expect(x[0]).toEqual('M'); const e = "";
const empty = "";
expect(empty.length).toEqual(0); console.log("Mozilla is " + x.length + " code units long");
// const adlam = "𞤲𞥋𞤣𞤫"; /* "Mozilla is 7 code units long" */
// expect(adlam.length).toEqual(8);
// const formula = "��𝑥∈ℝ,𝑥²��"; console.log("The empty string is has a length of " + e.length);
// expect(formula.length).toEqual(11); /* "The e string is has a length of 0" */
// #END
// 1. web: ��大长度和js引擎有关,在v8中为 2^29 - 24
// 超出边界报错: RangeError: Invalid string length expect(x.length).toEqual(7);
// 2. kotlin: ��大长��2^31-1 // expect(x[0]).toEqual('M');
// 超出边界报错: Error: targetMethod error::java.lang.OutOfMemoryError: char[] of length const empty = "";
// 3. swift: ��大长度和内存有关 expect(empty.length).toEqual(0);
// 超出边界没有返回信息 // const adlam = "𞤲𞥋𞤣𞤫";
// const length = Math.pow(2, 29) - 24 // expect(adlam.length).toEqual(8);
// const str = 'x'.repeat(length); // const formula = "��𝑥∈ℝ,𝑥²��";
// expect(str.length).toEqual(11); // expect(formula.length).toEqual(11);
})
test('at', () => { // 1. web: ��大长度和js引擎有关,在v8中为 2^29 - 24
const sentence = 'The quick brown fox jumps over the lazy dog.'; // 超出边界报错: RangeError: Invalid string length
let index = 5; // 2. kotlin: ��大长��2^31-1
expect(sentence.at(index)).toEqual("u"); // 超出边界报错: Error: targetMethod error::java.lang.OutOfMemoryError: char[] of length
index = -4; // 3. swift: ��大长度和内存有关
expect(sentence.at(index)).toEqual("d"); // 超出边界没有返回信息
expect(sentence.at(999)).toEqual(null); // const length = Math.pow(2, 29) - 24
// const str = 'x'.repeat(length);
let invoiceRef = 'myinvoice01'; // expect(str.length).toEqual(11);
expect(invoiceRef.at(-1)).toEqual("1"); })
invoiceRef = 'myinvoice02'; test('at', () => {
expect(invoiceRef.at(-1)).toEqual("2"); // #TEST String.at
expect(invoiceRef.at(-999)).toEqual(null); const sentence = 'The quick brown fox jumps over the lazy dog.';
let index = 5;
const empty = ""; console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
expect(empty.at(0)).toEqual(null); // expected output: "Using an index of 5 the character returned is u"
}) index = -4;
test('charAt', () => { console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
const anyString = "Brave new world"; // expected output: "Using an index of -4 the character returned is d"
expect(anyString.charAt(0)).toEqual("B"); // #END
expect(anyString.charAt(1)).toEqual("r");
expect(anyString.charAt(2)).toEqual("a"); index = 5;
expect(anyString.charAt(3)).toEqual("v"); expect(sentence.at(index)).toEqual("u");
expect(anyString.charAt(4)).toEqual("e"); index = -4;
expect(anyString.charAt(999)).toEqual(""); expect(sentence.at(index)).toEqual("d");
expect(anyString.charAt(-1)).toEqual(""); expect(sentence.at(999)).toEqual(null);
expect(anyString.charAt(-2)).toEqual("");
expect(anyString.charAt(-999)).toEqual(""); let invoiceRef = 'myinvoice01';
expect(invoiceRef.at(-1)).toEqual("1");
const empty = ""; invoiceRef = 'myinvoice02';
expect(empty.charAt(0)).toEqual(""); expect(invoiceRef.at(-1)).toEqual("2");
}) expect(invoiceRef.at(-999)).toEqual(null);
test('toWellFormed', () => {
// #ifdef APP-ANDROID const empty = "";
expect("ab\uD800".toWellFormed()).toEqual("ab\uFFFD"); expect(empty.at(0)).toEqual(null);
expect("ab\uD800c".toWellFormed()).toEqual("ab\uFFFDc"); })
expect("\uDFFFab".toWellFormed()).toEqual("\uFFFDab"); test('charAt', () => {
expect("c\uDFFFab".toWellFormed()).toEqual("c\uFFFDab"); // #TEST String.charAt
expect("abc".toWellFormed()).toEqual("abc"); const anyString = "Brave new world";
expect("ab\uD83D\uDE04c".toWellFormed()).toEqual("ab\uD83D\uDE04c"); console.log("The character at index 0 is '" + anyString.charAt(0) + "'");
expect("ab\uD83D\uDE04c".toWellFormed()).toEqual("ab\uD83D\uDE04c"); // The character at index 0 is 'B'
expect("ab\uD83D\uDE04c\uD83D".toWellFormed()).toEqual("ab\uD83D\uDE04c\uFFFD"); console.log("The character at index 1 is '" + anyString.charAt(1) + "'");
// #endif // The character at index 1 is 'r'
}) console.log("The character at index 2 is '" + anyString.charAt(2) + "'");
test('isWellFormed', () => { // The character at index 2 is 'a'
// #ifdef APP-ANDROID console.log("The character at index 3 is '" + anyString.charAt(3) + "'");
expect("ab\uD800".isWellFormed()).toEqual(false); // The character at index 3 is 'v'
expect("ab\uD800c".isWellFormed()).toEqual(false); console.log("The character at index 4 is '" + anyString.charAt(4) + "'");
expect("\uDFFFab".isWellFormed()).toEqual(false); // The character at index 4 is 'e'
expect("c\uDFFFab".isWellFormed()).toEqual(false); console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
expect("abc".isWellFormed()).toEqual(true); // The character at index 999 is ''
expect("ab\uD83D\uDE04c".isWellFormed()).toEqual(true); // #END
expect("ab\uD83D\uDE04c".isWellFormed()).toEqual(true);
expect("ab\uD83D\uDE04c\uD83D".isWellFormed()).toEqual(false);
// #endif
}) expect(anyString.charAt(0)).toEqual("B");
test('charCodeAt', () => { expect(anyString.charAt(1)).toEqual("r");
const sentence = 'The quick brown fox jumps over the lazy dog.'; expect(anyString.charAt(2)).toEqual("a");
const index = 4; expect(anyString.charAt(3)).toEqual("v");
expect(sentence.charCodeAt(index)).toEqual(113); expect(anyString.charAt(4)).toEqual("e");
expect(anyString.charAt(999)).toEqual("");
expect("ABC".charCodeAt(0)).toEqual(65); expect(anyString.charAt(-1)).toEqual("");
expect("ABC".charCodeAt(1)).toEqual(66); expect(anyString.charAt(-2)).toEqual("");
expect("ABC".charCodeAt(2)).toEqual(67); expect(anyString.charAt(-999)).toEqual("");
expect("ABC".charCodeAt(3)).toEqual(NaN);
const empty = "";
const empty = ""; expect(empty.charAt(0)).toEqual("");
expect(empty.charCodeAt(0)).toEqual(NaN); })
}) test('toWellFormed', () => {
test('fromCharCode', () => { // #ifdef APP-ANDROID
expect(String.fromCharCode(65, 66, 67)).toEqual("ABC"); // #TEST String.toWellFormed
expect(String.fromCharCode(0x12014)).toEqual("—"); let ret = "ab\uD800".toWellFormed()
expect(String.fromCharCode(0xd834, 0xdf06, 0x61, 0xd834, 0xdf07)).toEqual("𝌆a𝌇"); console.log(ret) //"ab\uFFFD"
}) // #END
test('concat', () => {
let hello = 'Hello, ' expect(ret).toEqual("ab\uFFFD");
expect(hello.concat('Kevin', '. Have a nice day.')).toEqual("Hello, Kevin. Have a nice day."); expect("ab\uD800c".toWellFormed()).toEqual("ab\uFFFDc");
expect("".concat('abc')).toEqual("abc"); expect("\uDFFFab".toWellFormed()).toEqual("\uFFFDab");
}) expect("c\uDFFFab".toWellFormed()).toEqual("c\uFFFDab");
test('endsWith', () => { expect("abc".toWellFormed()).toEqual("abc");
const str1 = 'Cats are the best!'; expect("ab\uD83D\uDE04c".toWellFormed()).toEqual("ab\uD83D\uDE04c");
expect(str1.endsWith('best!')).toEqual(true); expect("ab\uD83D\uDE04c".toWellFormed()).toEqual("ab\uD83D\uDE04c");
expect(str1.endsWith('best', 17)).toEqual(true); expect("ab\uD83D\uDE04c\uD83D".toWellFormed()).toEqual("ab\uD83D\uDE04c\uFFFD");
const str2 = 'Is this a question?'; // #endif
expect(str2.endsWith('question')).toEqual(false); })
test('isWellFormed', () => {
expect("".includes("test")).toEqual(false); // #ifdef APP-ANDROID
}) // #TEST String.isWellFormed
test('includes', () => { let ret = "ab\uD800".isWellFormed()
const sentence = 'The quick brown fox jumps over the lazy dog.'; console.log(ret) //false
const word = 'fox'; // #END
expect(sentence.includes(word)).toEqual(true);
expect(ret).toEqual(false);
expect("Blue Whale".includes("blue")).toEqual(false); expect("ab\uD800c".isWellFormed()).toEqual(false);
expect("Blue Whale".toLowerCase().includes("blue")).toEqual(true); expect("\uDFFFab".isWellFormed()).toEqual(false);
expect("c\uDFFFab".isWellFormed()).toEqual(false);
expect("".includes("test")).toEqual(false); expect("abc".isWellFormed()).toEqual(true);
}) expect("ab\uD83D\uDE04c".isWellFormed()).toEqual(true);
test('indexOf', () => { expect("ab\uD83D\uDE04c".isWellFormed()).toEqual(true);
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?'; expect("ab\uD83D\uDE04c\uD83D".isWellFormed()).toEqual(false);
const searchTerm = 'dog'; // #endif
const indexOfFirst = paragraph.indexOf(searchTerm); })
expect(indexOfFirst).toEqual(40); test('charCodeAt', () => {
expect(paragraph.indexOf(searchTerm, (indexOfFirst + 1))).toEqual(52); // #TEST String.charCodeAt
const sentence = 'The quick brown fox jumps over the lazy dog.';
expect('Blue Whale'.indexOf('Blue')).toEqual(0); const index = 4;
expect('Blue Whale'.indexOf('Blute')).toEqual(-1); console.log(`The character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(index)}`);
expect('Blue Whale'.indexOf('Whale', 0)).toEqual(5); // expected output: "The character code 113 is equal to q"
expect('Blue Whale'.indexOf('Whale', 5)).toEqual(5); // #END
expect('Blue Whale'.indexOf('Whale', 7)).toEqual(-1);
expect(sentence.charCodeAt(index)).toEqual(113);
expect("".indexOf("test")).toEqual(-1);
}) expect("ABC".charCodeAt(0)).toEqual(65);
test('match', () => { expect("ABC".charCodeAt(1)).toEqual(66);
const str = 'The quick brown fox jumps over the lazy dog. It barked.'; expect("ABC".charCodeAt(2)).toEqual(67);
const result = str.match(new RegExp('[A-Z]', 'g')); expect("ABC".charCodeAt(3)).toEqual(NaN);
// expect(result!.length).toEqual(2);
expect(result![0]).toEqual("T"); const empty = "";
expect(result![1]).toEqual("I"); expect(empty.charCodeAt(0)).toEqual(NaN);
})
const result2 = str.match(new RegExp('[A-Z]')); test('fromCharCode', () => {
// expect(result2!.length).toEqual(1); // #TEST String.fromCharCode
expect(result2![0]).toEqual("T"); console.log(String.fromCharCode(65, 66, 67));
// expected output: "ABC"
// #ifdef APP-ANDROID console.log(String.fromCharCode(0x12014));
const gradientString = 'linear-gradient(to right, rgb(255, 0, 0), #00FF00, hsl(120, 100%, 50%))'; // expected output: "𝌆a𝌇"
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; // #END
const result3 = gradientString.match(pattern);
expect(result3?.length).toEqual(3); expect(String.fromCharCode(65, 66, 67)).toEqual("ABC");
expect(result3?.[0]).toEqual("rgb(255, 0, 0)"); expect(String.fromCharCode(0x12014)).toEqual("—");
expect(result3?.[2]).toEqual("hsl(120, 100%, 50%)"); expect(String.fromCharCode(0xd834, 0xdf06, 0x61, 0xd834, 0xdf07)).toEqual("𝌆a𝌇");
})
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*\)/; test('concat', () => {
const result4 = gradientString.match(pattern2); // #TEST String.concat
expect(result4?.length).toEqual(8); let hello = 'Hello, '
expect(result4?.[0]).toEqual("rgb(255, 0, 0)"); let ret1 = hello.concat('Kevin', '. Have a nice day.')
expect(result4?.[1]).toEqual("255"); console.log(ret1)
expect(result4?.[2]).toEqual("0"); // Hello, Kevin. Have a nice day.
// #END
const url = ''; expect(ret1).toEqual("Hello, Kevin. Have a nice day.");
const urlRegex = /^(\w+):\/\/([^\/?#]+)([^?#]*)(\?[^#]*)?(#.*)?$/; expect("".concat('abc')).toEqual("abc");
const match = url.match(urlRegex); })
expect(JSON.stringify(match)).toEqual("null"); test('endsWith', () => {
// #endif // #TEST String.endsWith
const str1 = 'Cats are the best!';
}) console.log(str1.endsWith('best!'));
test('padEnd', () => { // expected output: true
const str1 = 'Breaded Mushrooms'; console.log(str1.endsWith('best', 17));
expect(str1.padEnd(25, '.')).toEqual("Breaded Mushrooms........"); // expected output: true
const str2 = '200'; const str2 = 'Is this a question?';
expect(str2.padEnd(5)).toEqual("200 "); console.log(str2.endsWith('question'));
// expected output: false
expect('abc'.padEnd(10)).toEqual("abc "); // #END
expect('abc'.padEnd(10, "foo")).toEqual("abcfoofoof");
expect('abc'.padEnd(6, "123456")).toEqual("abc123"); expect(str1.endsWith('best!')).toEqual(true);
expect('abc'.padEnd(1)).toEqual("abc"); expect(str1.endsWith('best', 17)).toEqual(true);
}) expect(str2.endsWith('question')).toEqual(false);
test('padStart', () => { expect("".includes("test")).toEqual(false);
const str1 = '5'; })
expect(str1.padStart(2, '0')).toEqual("05"); test('includes', () => {
expect('abc'.padStart(10)).toEqual(" abc"); // #TEST String.includes
expect('abc'.padStart(10, "foo")).toEqual("foofoofabc"); const sentence = 'The quick brown fox jumps over the lazy dog.';
expect('abc'.padStart(6, "123456")).toEqual("123abc"); const word = 'fox';
expect('abc'.padStart(8, "0")).toEqual("00000abc"); console.log(sentence.includes(word)) // true
expect('abc'.padStart(1)).toEqual("abc"); // #END
})
test('repeat', () => { expect(sentence.includes(word)).toEqual(true);
const str1 = 'abc';
expect(str1.repeat(0)).toEqual(""); expect("Blue Whale".includes("blue")).toEqual(false);
expect(str1.repeat(1)).toEqual("abc"); expect("Blue Whale".toLowerCase().includes("blue")).toEqual(true);
expect(str1.repeat(2)).toEqual("abcabc");
expect(str1.repeat(3.5)).toEqual("abcabcabc"); expect("".includes("test")).toEqual(false);
})
expect("".repeat(1)).toEqual(""); test('indexOf', () => {
}) // #TEST String.indexOf
test('replace', () => { const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?'; const searchTerm = 'dog';
expect(p.replace('dog', 'monkey')).toEqual("The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"); const indexOfFirst = paragraph.indexOf(searchTerm);
const regex = /Dog/i; console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
expect(p.replace(regex, 'ferret')).toEqual("The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"); // expected output: "The index of the first "dog" from the beginning is 40"
const str = 'abc12345#$*%';
const replacer = (match:string, p: string[], offset:number, string:string): string => { console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// p1 is nondigits, p2 digits, and p3 non-alphanumerics // expected output: "The index of the 2nd "dog" is 52"
expect(offset).toEqual(0); // #END
expect(match).toEqual(str);
expect(string).toEqual(str); expect(indexOfFirst).toEqual(40);
return p.join(' - '); expect(paragraph.indexOf(searchTerm, (indexOfFirst + 1))).toEqual(52);
}
expect('Blue Whale'.indexOf('Blue')).toEqual(0);
// 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 expect('Blue Whale'.indexOf('Blute')).toEqual(-1);
// const formatStr = 'This is a [sample] text with [another] capture group.' expect('Blue Whale'.indexOf('Whale', 0)).toEqual(5);
// const nextStr = formatStr.replace(REGEX_FORMAT, (match:string, p1: string|null, offset: number, string: string):string =>{ expect('Blue Whale'.indexOf('Whale', 5)).toEqual(5);
// console.log('123', p1, match) expect('Blue Whale'.indexOf('Whale', 7)).toEqual(-1);
// return p1 ?? match ?? ''
// }) expect("".indexOf("test")).toEqual(-1);
// expect(nextStr).toEqual('This is a sample text with another capture group.'); })
// var newString = str.replace(/([^\d]*)(\d*)([^\w]*)/, replacer); test('match', () => {
// expect(newString).toEqual("abc - 12345 - #$*%"); // #TEST String.match
// const str1 = 'hello, world'; const str = 'The quick brown fox jumps over the lazy dog. It barked.';
// const newString1 = str1.replace(/world/, (match: string, offset:number):string => `JavaScript (${offset})`); const result = str.match(new RegExp('[A-Z]', 'g'));
// expect(newString1).toEqual('hello, JavaScript (7)'); console.log(result![0])//"T"
}) // #END
test('search', () => { // expect(result!.length).toEqual(2);
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?'; expect(result![0]).toEqual("T");
const regex = /[^\w\s]/g; expect(result![1]).toEqual("I");
expect(paragraph.search(regex)).toEqual(43);
const result2 = str.match(new RegExp('[A-Z]'));
var str = "hey JudE"; // expect(result2!.length).toEqual(1);
var re = /[A-Z]/g; expect(result2![0]).toEqual("T");
var re2 = /[.]/g;
expect(str.search(re)).toEqual(4); // #ifdef APP-ANDROID
expect(str.search(re2)).toEqual(-1); const gradientString = 'linear-gradient(to right, rgb(255, 0, 0), #00FF00, hsl(120, 100%, 50%))';
expect("".search(re2)).toEqual(-1); 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);
test('slice', () => { expect(result3?.length).toEqual(3);
const str = 'The quick brown fox jumps over the lazy dog.'; expect(result3?.[0]).toEqual("rgb(255, 0, 0)");
expect(str.slice(31)).toEqual("the lazy dog."); expect(result3?.[2]).toEqual("hsl(120, 100%, 50%)");
let str1 = 'The morning is upon us.', // str1 的长��length ��23�� 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*\)/;
str2 = str1.slice(1, 8), const result4 = gradientString.match(pattern2);
str3 = str1.slice(4, -2), expect(result4?.length).toEqual(8);
str4 = str1.slice(12), expect(result4?.[0]).toEqual("rgb(255, 0, 0)");
str5 = str1.slice(30), expect(result4?.[1]).toEqual("255");
str6 = str1.slice(); expect(result4?.[2]).toEqual("0");
expect(str2).toEqual("he morn");
expect(str3).toEqual("morning is upon u");
expect(str4).toEqual("is upon us."); const url = '';
expect(str5).toEqual(""); const urlRegex = /^(\w+):\/\/([^\/?#]+)([^?#]*)(\?[^#]*)?(#.*)?$/;
expect(str6).toEqual("The morning is upon us."); const match = url.match(urlRegex);
expect(JSON.stringify(match)).toEqual("null");
expect("".slice()).toEqual(""); // #endif
expect("abcdefg".slice(-1)).toEqual("g"); })
expect("abcdefg".slice(-1,-2)).toEqual(""); test('padEnd', () => {
// #TEST String.padEnd
}) const str1 = 'Breaded Mushrooms';
test('split', () => { let ret1= str1.padEnd(25, '.')
const str = 'The quick brown fox jumps over the lazy dog.'; console.log(ret1);
const words = str.split(' '); // expected output: "Breaded Mushrooms........"
expect(words[3]).toEqual("fox"); const str2 = '200';
const chars = str.split(''); let ret2=str2.padEnd(5)
expect(chars[8]).toEqual("k"); console.log(ret2);
// expected output: "200 "
var myString = "Hello World. How are you doing?"; // #END
var splits = myString.split(" ", 3);
var splits1 = myString.split(" ", 10); expect(str1.padEnd(25, '.')).toEqual("Breaded Mushrooms........");
expect(splits).toEqual(["Hello", "World.", "How"]); expect(ret2).toEqual("200 ");
expect(splits1).toEqual(['Hello', 'World.', 'How', 'are', 'you', 'doing?']);
expect('abc'.padEnd(10)).toEqual("abc ");
expect(''.split('')).toEqual([]) expect('abc'.padEnd(10, "foo")).toEqual("abcfoofoof");
expect('abc'.split('b')).toEqual(["a", "c"]) expect('abc'.padEnd(6, "123456")).toEqual("abc123");
expect('abc'.split('d')).toEqual(["abc"]) expect('abc'.padEnd(1)).toEqual("abc");
})
var str1 = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand "; test('padStart', () => {
var re1 = /\s*(?:;|$)\s*/; // #TEST String.padStart
expect(str1.split(re1)).toEqual([ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", "" ]) const str1 = '5';
let ret = str1.padStart(2, '0')
var str2 = "a, b, c, d, e"; console.log(ret);
var re2 = /,\s*/g; // expected output: "05"
expect(str2.split(re2)).toEqual(["a", "b", "c", "d", "e"]); // #END
var str3 = "foo-bar_baz(qux-quux)_corge"; expect(ret).toEqual("05");
var re3 = new RegExp("[_-](?![^(]*\\))", "g");
expect(str3.split(re3)).toEqual(["foo", "bar", "baz(qux-quux)", "corge"]); expect('abc'.padStart(10)).toEqual(" abc");
expect('abc'.padStart(10, "foo")).toEqual("foofoofabc");
var str4 = "a, b, {c, d, e}, f, g, h"; expect('abc'.padStart(6, "123456")).toEqual("123abc");
var re4 = /,(?![^{]*\})/; expect('abc'.padStart(8, "0")).toEqual("00000abc");
expect(str4.split(re4)).toEqual(["a", " b", " {c, d, e}", " f", " g", " h"]); expect('abc'.padStart(1)).toEqual("abc");
}) })
test('toLowerCase', () => { test('repeat', () => {
const str1 = '中文����zh-CN || zh-Hans'; const str1 = 'abc';
expect(str1.toLowerCase()).toEqual("中文����zh-cn || zh-hans"); // #TEST String.repeat
const str2 = 'ALPHABET'; "abc".repeat(0) // ""
expect(str2.toLowerCase()).toEqual("alphabet"); "abc".repeat(1) // "abc"
}) "abc".repeat(2) // "abcabc"
test('toUpperCase', () => { "abc".repeat(3.5) // "abcabcabc" 参数 count 将会被自动转换成整数。
const sentence = 'The quick brown fox jumps over the lazy dog.'; // #END
expect(sentence.toUpperCase()).toEqual("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."); expect(str1.repeat(0)).toEqual("");
}) expect(str1.repeat(1)).toEqual("abc");
expect(str1.repeat(2)).toEqual("abcabc");
test("lastIndexOf", () => { expect(str1.repeat(3.5)).toEqual("abcabcabc");
expect('canal'.lastIndexOf('a')).toEqual(3);
expect('canal'.lastIndexOf('a', 2)).toEqual(1); expect("".repeat(1)).toEqual("");
expect('canal'.lastIndexOf('a', 0)).toEqual(-1); })
expect('canal'.lastIndexOf('')).toEqual(5); test('replace', () => {
expect('canal'.lastIndexOf('', 2)).toEqual(2); // #TEST String.replace
expect("Blue Whale, Killer Whale".lastIndexOf("Whale")).toEqual(19); const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
expect("Blue Whale, Killer Whale".lastIndexOf("blue")).toEqual(-1); let ret1 = p.replace('dog', 'monkey')
})
test("substring", () => { console.log(ret1);
var str1 = "Mozilla"; // expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
expect(str1.substring(0, 3)).toEqual("Moz"); const regex = /Dog/i;
expect(str1.substring(3, 0)).toEqual("Moz"); let ret2 = p.replace(regex, 'ferret')
expect(str1.substring(3, -3)).toEqual("Moz"); console.log(ret2);
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
var str2 = "hello world"; // #END
// expect(str2.substring()).toEqual("hello world");
expect(str2.substring(6)).toEqual("world"); expect(ret1).toEqual("The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?");
expect(str2.substring(0)).toEqual("hello world"); expect(ret2).toEqual("The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?");
expect(str2.substring(0, 5)).toEqual("hello"); const str = 'abc12345#$*%';
expect(str2.substring(6, 11)).toEqual("world"); const replacer = (match : string, p : string[], offset : number, string : string) : string => {
expect(str2.substring(3, 8)).toEqual("lo wo"); // p1 is nondigits, p2 digits, and p3 non-alphanumerics
expect(str2.substring(0, -1)).toEqual(""); expect(offset).toEqual(0);
expect(str2.substring(0, 20)).toEqual("hello world"); expect(match).toEqual(str);
expect(string).toEqual(str);
var str3 = "aaa"; return p.join(' - ');
expect(str3.substring(10, 1)).toEqual("aa"); }
})
test("trim", () => {
const greeting = ' Hello world! '; // #TEST String.replace_1
expect(greeting).toEqual(" Hello world! "); // 不包含捕捉组的示例
expect(greeting.trim()).toEqual("Hello world!"); let a = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
const orig:string = ' foo '; let b = a.replace(RegExp("fox"), function (match : string, offset : number, string : string) : string {
expect(orig.trim()).toEqual("foo"); console.log("match", match)
const str = '\t\t\tworld\t\t\t'; console.log("offset", offset)
expect(str.trim()).toEqual('world'); console.log("string", string)
const str1 = '\n\n\nhello\n\n\n'; return "cat"
expect(str1.trim()).toEqual('hello'); })
const str2 = ''; console.log("b:", b)
expect(str2.trim()).toEqual('');
}) // 包含一个捕获组的示例。注意,目前android仅支持最多五个捕获组
test("trimStart", () => { let a1 = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
const str = ' Hello World '; let b1 = a1.replace(RegExp("(fox)"), function (match : string, p1 : string, offset : number, string : string) : string {
expect(str.trimStart()).toEqual('Hello World '); console.log("match", match)
const str1 = 'Hello World'; console.log("p1", p1)
expect(str1.trimStart()).toEqual(str1); console.log("offset", offset)
const str2 = ' \t \n '; console.log("string", string)
expect(str2.trimStart()).toEqual(''); return "cat"
}) })
test("trimEnd", () => { console.log("b1", b1)
const str = ' Hello World '; // #END
expect(str.trimEnd()).toEqual(' Hello World');
const str1 = 'Hello World'; // 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
expect(str1.trimEnd()).toEqual(str1); // const formatStr = 'This is a [sample] text with [another] capture group.'
const str2 = ' \t \n '; // const nextStr = formatStr.replace(REGEX_FORMAT, (match:string, p1: string|null, offset: number, string: string):string =>{
expect(str2.trimEnd()).toEqual(''); // console.log('123', p1, match)
}) // return p1 ?? match ?? ''
test("startsWith", () => { // })
const str = 'hello world'; // expect(nextStr).toEqual('This is a sample text with another capture group.');
expect(str.startsWith('hello')).toEqual(true); // var newString = str.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
expect(str.startsWith('h')).toEqual(true); // expect(newString).toEqual("abc - 12345 - #$*%");
expect(str.startsWith('HELLO')).toEqual(false); // const str1 = 'hello, world';
expect(str.startsWith('o')).toEqual(false); // const newString1 = str1.replace(/world/, (match: string, offset:number):string => `JavaScript (${offset})`);
// expect(newString1).toEqual('hello, JavaScript (7)');
const str1:string = "To be, or not to be, that is the question."; })
expect(str1.startsWith("To be")).toEqual(true); test('search', () => {
expect(str1.startsWith("not to be")).toEqual(false); // #TEST String.search
expect(str1.startsWith("not to be", 10)).toEqual(true); const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
expect(str1.startsWith("not to be", 8)).toEqual(false); const regex = /[^\w\s]/g;
}) console.log(paragraph.search(regex));
test("matchAll", () => { // expected output: 43
// const reg = /t(e)(st(\d?))/g; console.log(paragraph[paragraph.search(regex)]);
// const str = 'test1test2'; // expected output: "."
// const result = [...str.matchAll(reg)]; // #END
// expect(result[0]).toEqual(["test1", "e", "st1", "1"]);
// expect(result[1]).toEqual(["test2", "e", "st2", "2"]); expect(paragraph.search(regex)).toEqual(43);
// const reg1 = /[A-Z]/g; var str = "hey JudE";
// const str1 = 'The quick brown fox jumps over the lazy dog'; var re = /[A-Z]/g;
// const result1 = [...str1.matchAll(reg1)]; var re2 = /[.]/g;
// expect(result1[0]).toEqual(["T"]); expect(str.search(re)).toEqual(4);
}) expect(str.search(re2)).toEqual(-1);
test("replaceAll", () => { expect("".search(re2)).toEqual(-1);
// 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?"); test('slice', () => {
// const regex = /Dog/ig; // #TEST String.slice
// expect(p.replaceAll(regex, 'ferret')).toEqual( "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"); const str = 'The quick brown fox jumps over the lazy dog.';
let ret = str.slice(31)
// const reg = /[aeiou]/g; console.log(ret);
// const str = 'The quick brown fox jumps over the lazy dog'; // expected output: "the lazy dog."
// const result = str.replaceAll(reg, '!'); console.log(str.slice(4, 19));
// expect(result).toEqual("Th! q!!ck br!wn f!x j!mps !v!r th! l!zy d!g"); // expected output: "quick brown fox"
// #END
// const reg1 = /o/g;
// const str1 = 'Hello World!'; expect(ret).toEqual("the lazy dog.");
// const result1 = str1.replaceAll(reg1, (match, offset, original) => {
// expect(match).toEqual('o'); let str1 = 'The morning is upon us.', // str1 的长��length ��23��
// expect(original[offset]).toEqual('o'); str2 = str1.slice(1, 8),
// expect(original).toEqual(str1); str3 = str1.slice(4, -2),
// return `x`; str4 = str1.slice(12),
// }); str5 = str1.slice(30),
// expect(result1).toEqual("Hellx Wxrld!"); 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"]);
})
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!");
}) })
} })
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册