Skip to content

Commit

Permalink
Enhance RandomGenerator.pattern().
Browse files Browse the repository at this point in the history
Default random seeder of `typia.random<T>()` 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<T>()` 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.
  • Loading branch information
samchon committed Feb 27, 2024
1 parent cefa989 commit 3c61157
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion src/utils/RandomGenerator/RandomGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ export const array = <T>(closure: (index: number) => T, count?: number): T[] =>
new Array(count ?? length()).fill(0).map((_e, index) => closure(index));
export const pick = <T>(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
Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 3c61157

Please sign in to comment.