Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix number pattern of stringify to support exponential log value #749

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "4.2.0",
"version": "4.2.1-dev.20230808",
"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": "4.2.0",
"version": "4.2.1-dev.20230808",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -68,7 +68,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "4.2.0"
"typia": "4.2.1-dev.20230808"
},
"peerDependencies": {
"typescript": ">= 4.7.4"
Expand Down
4 changes: 1 addition & 3 deletions src/programmers/RandomProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ export namespace RandomProgrammer {
const expressions: ts.Expression[] = [];
if (meta.any)
expressions.push(
ts.factory.createStringLiteral(
"any type used...",
),
ts.factory.createStringLiteral("any type used..."),
);

// NULL COALESCING
Expand Down
4 changes: 4 additions & 0 deletions src/programmers/internal/stringify_dynamic_properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ export const stringify_dynamic_properties = (
);
statements.push(condition);
}
statements.push(
ts.factory.createReturnStatement(ts.factory.createStringLiteral("")),
);

return output();
};

Expand Down
5 changes: 4 additions & 1 deletion src/utils/PatternUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export namespace PatternUtil {
.replace(/-/g, "\\x2d");
};

export const NUMBER = "-?\\d+\\.?\\d*";
export const NUMBER =
"[+-]?" + // optional sign
"\\d+(?:\\.\\d+)?" + // integer or decimal
"(?:[eE][+-]?\\d+)?"; // optional exponent
export const BOOLEAN = "true|false";
export const STRING = "(.*)";
}
Expand Down
24 changes: 24 additions & 0 deletions test/features/issues/test_issue_exponential_key_stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import typia from "typia";

import { DynamicComposite } from "../../structures/DynamicComposite";

export const test_issue_exponential_key_stringify = () => {
const data: DynamicComposite = {
id: "id",
name: "name",
"5.175933557310941e-7": -0.170261004873707,
prefix_upzzoug: "udwpvy",
sqqfzv_postfix: "xpwmpwr",
"value_0.18500123790254852": true,
"value_-0.4744943749449395": -0.12959620300810482,
"value_-0.41442283348249553": "wchfdtils",
"between_tmzivbd_and_-0.45295511336433414": false,
};
const json: string = typia.stringify(data);
const expected: string = JSON.stringify(data);

if (json !== expected)
throw new Error(
"Bug on typia.stringify: failed to understand exponential numbered key.",
);
};
30 changes: 24 additions & 6 deletions test/generated/output/assert/test_assert_DynamicComposite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export const test_assert_DynamicComposite = _test_assert(
Object.keys(input).every((key: any) => {
const value = input[key];
if (undefined === value) return true;
if (RegExp(/^-?\d+\.?\d*$/).test(key))
if (
RegExp(
/^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/,
).test(key)
)
return (
"number" === typeof value &&
Number.isFinite(value)
Expand All @@ -24,15 +28,21 @@ export const test_assert_DynamicComposite = _test_assert(
return "string" === typeof value;
if (RegExp(/((.*)_postfix)$/).test(key))
return "string" === typeof value;
if (RegExp(/^(value_-?\d+\.?\d*)$/).test(key))
if (
RegExp(
/^(value_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
"string" === typeof value ||
("number" === typeof value &&
Number.isFinite(value)) ||
"boolean" === typeof value
);
if (
RegExp(/^(between_(.*)_and_-?\d+\.?\d*)$/).test(key)
RegExp(
/^(between_(.*)_and_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return "boolean" === typeof value;
return true;
Expand Down Expand Up @@ -70,7 +80,11 @@ export const test_assert_DynamicComposite = _test_assert(
Object.keys(input).every((key: any) => {
const value = input[key];
if (undefined === value) return true;
if (RegExp(/^-?\d+\.?\d*$/).test(key))
if (
RegExp(
/^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/,
).test(key)
)
return (
("number" === typeof value &&
Number.isFinite(value)) ||
Expand Down Expand Up @@ -98,7 +112,11 @@ export const test_assert_DynamicComposite = _test_assert(
value: value,
})
);
if (RegExp(/^(value_-?\d+\.?\d*)$/).test(key))
if (
RegExp(
/^(value_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
"string" === typeof value ||
("number" === typeof value &&
Expand All @@ -113,7 +131,7 @@ export const test_assert_DynamicComposite = _test_assert(
);
if (
RegExp(
/^(between_(.*)_and_-?\d+\.?\d*)$/,
/^(between_(.*)_and_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
Expand Down
22 changes: 16 additions & 6 deletions test/generated/output/assert/test_assert_DynamicTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ export const test_assert_DynamicTemplate = _test_assert(
return "string" === typeof value;
if (RegExp(/((.*)_postfix)$/).test(key))
return "string" === typeof value;
if (RegExp(/^(value_-?\d+\.?\d*)$/).test(key))
if (
RegExp(
/^(value_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
"number" === typeof value &&
Number.isFinite(value)
);
if (
RegExp(/^(between_(.*)_and_-?\d+\.?\d*)$/).test(key)
RegExp(
/^(between_(.*)_and_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return "boolean" === typeof value;
return true;
Expand Down Expand Up @@ -70,7 +76,11 @@ export const test_assert_DynamicTemplate = _test_assert(
value: value,
})
);
if (RegExp(/^(value_-?\d+\.?\d*)$/).test(key))
if (
RegExp(
/^(value_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
("number" === typeof value &&
Number.isFinite(value)) ||
Expand All @@ -81,9 +91,9 @@ export const test_assert_DynamicTemplate = _test_assert(
})
);
if (
RegExp(/^(between_(.*)_and_-?\d+\.?\d*)$/).test(
key,
)
RegExp(
/^(between_(.*)_and_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
"boolean" === typeof value ||
Expand Down
16 changes: 12 additions & 4 deletions test/generated/output/assert/test_assert_DynamicUnion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ export const test_assert_DynamicUnion = _test_assert(
Object.keys(input).every((key: any) => {
const value = input[key];
if (undefined === value) return true;
if (RegExp(/^-?\d+\.?\d*$/).test(key))
if (
RegExp(
/^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/,
).test(key)
)
return "string" === typeof value;
if (RegExp(/^(prefix_(.*))/).test(key))
return "string" === typeof value;
if (RegExp(/((.*)_postfix)$/).test(key))
return "string" === typeof value;
if (
RegExp(
/^(value_between_-?\d+\.?\d*_and_-?\d+\.?\d*)$/,
/^(value_between_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?_and_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
Expand Down Expand Up @@ -54,7 +58,11 @@ export const test_assert_DynamicUnion = _test_assert(
Object.keys(input).every((key: any) => {
const value = input[key];
if (undefined === value) return true;
if (RegExp(/^-?\d+\.?\d*$/).test(key))
if (
RegExp(
/^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/,
).test(key)
)
return (
"string" === typeof value ||
$guard(_exceptionable, {
Expand Down Expand Up @@ -83,7 +91,7 @@ export const test_assert_DynamicUnion = _test_assert(
);
if (
RegExp(
/^(value_between_-?\d+\.?\d*_and_-?\d+\.?\d*)$/,
/^(value_between_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?_and_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
Expand Down
16 changes: 8 additions & 8 deletions test/generated/output/assert/test_assert_TemplateAtomic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export const test_assert_TemplateAtomic = _test_assert(
input.middle_string_empty,
) &&
"string" === typeof input.middle_numeric &&
RegExp(/^the_-?\d+\.?\d*_value$/).test(
input.middle_numeric,
) &&
RegExp(
/^the_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?_value$/,
).test(input.middle_numeric) &&
("the_false_value" === input.middle_boolean ||
"the_true_value" === input.middle_boolean) &&
"string" === typeof input.ipv4 &&
RegExp(
/^-?\d+\.?\d*\.-?\d+\.?\d*\.-?\d+\.?\d*\.-?\d+\.?\d*$/,
/^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\.[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\.[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\.[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/,
).test(input.ipv4) &&
"string" === typeof input.email &&
RegExp(/(.*)@(.*)\.(.*)/).test(input.email);
Expand Down Expand Up @@ -80,9 +80,9 @@ export const test_assert_TemplateAtomic = _test_assert(
value: input.middle_string_empty,
})) &&
(("string" === typeof input.middle_numeric &&
RegExp(/^the_-?\d+\.?\d*_value$/).test(
input.middle_numeric,
)) ||
RegExp(
/^the_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?_value$/,
).test(input.middle_numeric)) ||
$guard(_exceptionable, {
path: _path + ".middle_numeric",
expected: "`the_${number}_value`",
Expand All @@ -98,7 +98,7 @@ export const test_assert_TemplateAtomic = _test_assert(
})) &&
(("string" === typeof input.ipv4 &&
RegExp(
/^-?\d+\.?\d*\.-?\d+\.?\d*\.-?\d+\.?\d*\.-?\d+\.?\d*$/,
/^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\.[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\.[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\.[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/,
).test(input.ipv4)) ||
$guard(_exceptionable, {
path: _path + ".ipv4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export const test_assertClone_DynamicComposite = _test_assertClone(
Object.keys(input).every((key: any) => {
const value = input[key];
if (undefined === value) return true;
if (RegExp(/^-?\d+\.?\d*$/).test(key))
if (
RegExp(
/^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/,
).test(key)
)
return (
"number" === typeof value &&
Number.isFinite(value)
Expand All @@ -25,17 +29,21 @@ export const test_assertClone_DynamicComposite = _test_assertClone(
return "string" === typeof value;
if (RegExp(/((.*)_postfix)$/).test(key))
return "string" === typeof value;
if (RegExp(/^(value_-?\d+\.?\d*)$/).test(key))
if (
RegExp(
/^(value_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
"string" === typeof value ||
("number" === typeof value &&
Number.isFinite(value)) ||
"boolean" === typeof value
);
if (
RegExp(/^(between_(.*)_and_-?\d+\.?\d*)$/).test(
key,
)
RegExp(
/^(between_(.*)_and_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return "boolean" === typeof value;
return true;
Expand Down Expand Up @@ -75,7 +83,11 @@ export const test_assertClone_DynamicComposite = _test_assertClone(
Object.keys(input).every((key: any) => {
const value = input[key];
if (undefined === value) return true;
if (RegExp(/^-?\d+\.?\d*$/).test(key))
if (
RegExp(
/^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/,
).test(key)
)
return (
("number" === typeof value &&
Number.isFinite(value)) ||
Expand Down Expand Up @@ -104,9 +116,9 @@ export const test_assertClone_DynamicComposite = _test_assertClone(
})
);
if (
RegExp(/^(value_-?\d+\.?\d*)$/).test(
key,
)
RegExp(
/^(value_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
"string" === typeof value ||
Expand All @@ -122,7 +134,7 @@ export const test_assertClone_DynamicComposite = _test_assertClone(
);
if (
RegExp(
/^(between_(.*)_and_-?\d+\.?\d*)$/,
/^(between_(.*)_and_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
)
return (
Expand Down Expand Up @@ -162,7 +174,11 @@ export const test_assertClone_DynamicComposite = _test_assertClone(
name: input.name as any,
} as any;
for (const [key, value] of Object.entries(input)) {
if (RegExp(/^-?\d+\.?\d*$/).test(key)) {
if (
RegExp(
/^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/,
).test(key)
) {
output[key] = value as any;
continue;
}
Expand All @@ -174,12 +190,18 @@ export const test_assertClone_DynamicComposite = _test_assertClone(
output[key] = value as any;
continue;
}
if (RegExp(/^(value_-?\d+\.?\d*)$/).test(key)) {
if (
RegExp(
/^(value_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
) {
output[key] = value as any;
continue;
}
if (
RegExp(/^(between_(.*)_and_-?\d+\.?\d*)$/).test(key)
RegExp(
/^(between_(.*)_and_[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/,
).test(key)
) {
output[key] = value as any;
continue;
Expand Down
Loading
Loading