File tree Expand file tree Collapse file tree 4 files changed +52
-0
lines changed
packages/smithy-client/src Expand file tree Collapse file tree 4 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ export * from "./get-array-if-single-item";
7
7
export * from "./get-value-from-text-node" ;
8
8
export * from "./lazy-json" ;
9
9
export * from "./parse-utils" ;
10
+ export * from "./ser-utils" ;
10
11
export * from "./date-utils" ;
11
12
export * from "./split-every" ;
12
13
export * from "./constants" ;
Original file line number Diff line number Diff line change @@ -65,3 +65,27 @@ export function expectString(value: any): string | undefined {
65
65
}
66
66
throw new TypeError ( `Expected string, got ${ typeof value } ` ) ;
67
67
}
68
+
69
+ /**
70
+ * Asserts a value is a number and returns it, and also converts string
71
+ * representations of non-numeric floats into Numbers.
72
+ *
73
+ * @param value A number or string representation of a non-numeric float.
74
+ * @returns The value as a number, undefined if it's null/undefined,
75
+ * otherwise an error is thrown.
76
+ */
77
+ export const handleFloat = ( value : string | number ) : number | undefined => {
78
+ if ( typeof value == "string" ) {
79
+ switch ( value ) {
80
+ case "NaN" :
81
+ return NaN ;
82
+ case "Infinity" :
83
+ return Infinity ;
84
+ case "-Infinity" :
85
+ return - Infinity ;
86
+ default :
87
+ throw new Error ( `Unable to parse float value: ${ value } ` ) ;
88
+ }
89
+ }
90
+ return expectNumber ( value ) ;
91
+ } ;
Original file line number Diff line number Diff line change
1
+ import { serializeFloat } from "./ser-utils" ;
2
+
3
+ describe ( "serializeFloat" , ( ) => {
4
+ it ( "handles non-numerics" , ( ) => {
5
+ expect ( serializeFloat ( NaN ) ) . toEqual ( "NaN" ) ;
6
+ } ) ;
7
+ } ) ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Serializes a number, turning non-numeric values into strings.
3
+ *
4
+ * @param value The number to serialize.
5
+ * @returns A number, or a string if the given number was non-numeric.
6
+ */
7
+ export const serializeFloat = ( value : number ) : string | number => {
8
+ // NaN is not equal to everything, including itself.
9
+ if ( value !== value ) {
10
+ return "NaN" ;
11
+ }
12
+ switch ( value ) {
13
+ case Infinity :
14
+ return "Infinity" ;
15
+ case - Infinity :
16
+ return "-Infinity" ;
17
+ default :
18
+ return value ;
19
+ }
20
+ } ;
You can’t perform that action at this time.
0 commit comments