Skip to content

Commit 8bb0632

Browse files
committed
fixed some strings being incorrectly recognized as numbers
1 parent 92bfc37 commit 8bb0632

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

exampleVault/Input Fields/Inline Select.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
select: a
3-
select2: 2
3+
select2: 1
4+
select3: 1 hour
45
---
56

67
```meta-bind
@@ -11,3 +12,6 @@ INPUT[inlineSelect(option(a), option(b), showcase):select]
1112
INPUT[inlineSelect(option(1, a), option(2, b), showcase):select2]
1213
```
1314

15+
```meta-bind
16+
INPUT[inlineSelect(option(1 hour, a), option(2 hours, b), showcase):select3]
17+
```

src/utils/Utils.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { traverseObjectByPath } from '@opd-libs/opd-utils-lib/lib/ObjectTraversalUtils';
22
import { KeyValuePair } from '@opd-libs/opd-utils-lib/lib/Utils';
33
import structuredClone from '@ungap/structured-clone';
4+
import { P_UTILS } from '@lemons_dev/parsinom/lib/ParserUtils';
5+
import { P } from '@lemons_dev/parsinom/lib/ParsiNOM';
6+
import { Parser } from '@lemons_dev/parsinom/lib/Parser';
47

58
if (!('structuredClone' in globalThis)) {
69
// @ts-ignore
@@ -279,6 +282,11 @@ export function deepCopy<T extends object>(object: T): T {
279282
export type MBLiteral = string | number | boolean | null;
280283
export type MBExtendedLiteral = MBLiteral | MBLiteral[];
281284

285+
const numberParser: Parser<number> = P.or(
286+
P.sequenceMap((a, b, c) => Number(a + b + c), P_UTILS.digits(), P.string('.'), P_UTILS.digits()),
287+
P_UTILS.digits().map(x => Number(x))
288+
).thenEof();
289+
282290
export function parseLiteral(literalString: string): MBLiteral {
283291
if (literalString.toLowerCase() === 'null') {
284292
return null;
@@ -287,8 +295,13 @@ export function parseLiteral(literalString: string): MBLiteral {
287295
} else if (literalString === 'false') {
288296
return false;
289297
} else {
290-
const parsedNumber = Number.parseFloat(literalString);
291-
return !Number.isNaN(parsedNumber) ? parsedNumber : literalString;
298+
const parseResult = numberParser.tryParse(literalString);
299+
300+
if (parseResult.success) {
301+
return parseResult.value;
302+
} else {
303+
return literalString;
304+
}
292305
}
293306
}
294307

0 commit comments

Comments
 (0)