Skip to content

Commit 554f86d

Browse files
committed
Made a correction for marshalling strings to buest guess
1 parent 03dc3a1 commit 554f86d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/Marshaller.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,17 @@ export class Marshaller implements IMarshaller {
599599
return this.marshalFromStringToBestGuess(stringified);
600600
}
601601

602+
/**
603+
* If the string starts with a number but should be a string nevertheless, this method returns true.
604+
* @param {string} value
605+
* @returns {boolean}
606+
*/
607+
private startsWithNumberButShouldEnforceString (value: string): boolean {
608+
// If the value starts with a digit and possibly a '.' character but then goes on with something
609+
// else, enforce a string.
610+
return /^\d+[.]*[^\d.]+/.test(value.trim());
611+
}
612+
602613
/**
603614
* Parses the string and and marshals it into the most fitting type based on heuristics.
604615
* @param {string} data
@@ -617,7 +628,10 @@ export class Marshaller implements IMarshaller {
617628

618629
// It might be a number.
619630
const toNum = Number.parseFloat(primitive);
620-
if (!isNaN(toNum)) return toNum;
631+
if (!isNaN(toNum)) {
632+
if (this.startsWithNumberButShouldEnforceString(primitive)) return primitive;
633+
return toNum;
634+
}
621635

622636
// It might be an array
623637
if (primitive.startsWith("[") && primitive.endsWith("]")) return JSON.parse(primitive);

0 commit comments

Comments
 (0)