From 8206a21452c6ccf0cad692da3ce6cb4993414a83 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Thu, 27 Jun 2024 12:00:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85JSON.stringify=20replacer?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uts-tests/utssdk/JSON.uts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/uni_modules/uts-tests/utssdk/JSON.uts b/uni_modules/uts-tests/utssdk/JSON.uts index 1c4ab59..1b1e348 100644 --- a/uni_modules/uts-tests/utssdk/JSON.uts +++ b/uni_modules/uts-tests/utssdk/JSON.uts @@ -23,6 +23,20 @@ type PersonJSON = { } +function countOccurrences(str:string, substr:string):number { + + let count = 0 + let position = 0 + + while (true) { + position = str.indexOf(substr, position) + if (position == -1) break + count++ + position += substr.length + } + + return count; +} export function testJSON() : Result { @@ -220,8 +234,15 @@ export function testJSON() : Result { let a6 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z']) expect(a6).toEqual('{"x":111,"y":"aaa","z":{"x":123}}'); - let a7 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z']) + let a7 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z'],8) expect(a7.length).toEqual(91); + + let a8 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z'],"99999") + + expect(a8.length).toEqual(73); + expect(countOccurrences(a8,"99999")).toEqual(6); + + // #endif }) test('stringify', () => { -- GitLab