From 3c61157abb72229d4ae345a401f472bd7f5c0fb2 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Tue, 27 Feb 2024 19:54:07 +0900 Subject: [PATCH] Enhance `RandomGenerator.pattern()`. Default random seeder of `typia.random()` function is `RandomGenerator`, and its `RandomGenerator.pattern()` function is utilizing the `randexp` library. By the way, `randexp` rarely failed to generate proper patten string value, so that it can't pass the validate function of the `typia.is()` function with same type. This PR avoids such rare mis-pattern string generation case, by repeating the random pattern string generation about 10 times, until succeded to pass the pattern string validation. --- benchmark/package.json | 2 +- errors/package.json | 2 +- package.json | 2 +- packages/typescript-json/package.json | 4 ++-- src/utils/RandomGenerator/RandomGenerator.ts | 9 ++++++++- test/package.json | 2 +- website/package.json | 2 +- 7 files changed, 15 insertions(+), 8 deletions(-) diff --git a/benchmark/package.json b/benchmark/package.json index 7b296574b2..39685fecf8 100644 --- a/benchmark/package.json +++ b/benchmark/package.json @@ -73,6 +73,6 @@ "suppress-warnings": "^1.0.2", "tstl": "^2.5.13", "uuid": "^9.0.1", - "typia": "D:\\github\\samchon\\typia\\typia-5.4.12.tgz" + "typia": "D:\\github\\samchon\\typia\\typia-5.4.13.tgz" } } \ No newline at end of file diff --git a/errors/package.json b/errors/package.json index c96766fbbd..740333898a 100644 --- a/errors/package.json +++ b/errors/package.json @@ -32,6 +32,6 @@ "typescript": "^5.3.2" }, "dependencies": { - "typia": "D:\\github\\samchon\\typia\\typia-5.4.12.tgz" + "typia": "D:\\github\\samchon\\typia\\typia-5.4.13.tgz" } } \ No newline at end of file diff --git a/package.json b/package.json index a212b86de4..fa79017200 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typia", - "version": "5.4.12", + "version": "5.4.13", "description": "Superfast runtime validators with only one line", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/packages/typescript-json/package.json b/packages/typescript-json/package.json index abf5a0ffb6..592840531e 100644 --- a/packages/typescript-json/package.json +++ b/packages/typescript-json/package.json @@ -1,6 +1,6 @@ { "name": "typescript-json", - "version": "5.4.12", + "version": "5.4.13", "description": "Superfast runtime validators with only one line", "main": "lib/index.js", "typings": "lib/index.d.ts", @@ -61,7 +61,7 @@ }, "homepage": "https://typia.io", "dependencies": { - "typia": "5.4.12" + "typia": "5.4.13" }, "peerDependencies": { "typescript": ">=4.8.0 <5.5.0" diff --git a/src/utils/RandomGenerator/RandomGenerator.ts b/src/utils/RandomGenerator/RandomGenerator.ts index cd6e1d3672..8cf10a4743 100644 --- a/src/utils/RandomGenerator/RandomGenerator.ts +++ b/src/utils/RandomGenerator/RandomGenerator.ts @@ -28,7 +28,14 @@ export const array = (closure: (index: number) => T, count?: number): T[] => new Array(count ?? length()).fill(0).map((_e, index) => closure(index)); export const pick = (array: T[]): T => array[integer(0, array.length - 1)]!; export const length = () => integer(0, 3); -export const pattern = (regex: RegExp): string => new RandExp(regex).gen(); +export const pattern = (regex: RegExp): string => { + const r: RandExp = new RandExp(regex); + for (let i: number = 0; i < 10; ++i) { + const str: string = r.gen(); + if (regex.test(str)) return str; + } + return r.gen(); +}; /* ----------------------------------------------------------- SECIAL FORMATS diff --git a/test/package.json b/test/package.json index 687480f258..22342f374e 100644 --- a/test/package.json +++ b/test/package.json @@ -51,6 +51,6 @@ "suppress-warnings": "^1.0.2", "tstl": "^2.5.13", "uuid": "^9.0.1", - "typia": "D:\\github\\samchon\\typia\\typia-5.4.12.tgz" + "typia": "D:\\github\\samchon\\typia\\typia-5.4.13.tgz" } } \ No newline at end of file diff --git a/website/package.json b/website/package.json index dceabbc02c..4e3dbdc915 100644 --- a/website/package.json +++ b/website/package.json @@ -43,6 +43,6 @@ "next-sitemap": "^4.0.7", "rimraf": "^5.0.0", "ts-node": "^10.9.1", - "typia": "5.4.12" + "typia": "5.4.13" } }