Fix iOS 12 crashing in scroller
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { toBoolean, toFloat } from './string';
|
||||
|
||||
describe('toBoolean', () => {
|
||||
it('Should return the boolean represented by the string', () => {
|
||||
let bool = toBoolean('true');
|
||||
expect(bool).toBe(true);
|
||||
|
||||
bool = toBoolean('false', true);
|
||||
expect(bool).toBe(false);
|
||||
});
|
||||
|
||||
it('Should return default value for invalid values', () => {
|
||||
let bool = toBoolean('test');
|
||||
expect(bool).toBe(false);
|
||||
|
||||
bool = toBoolean(undefined);
|
||||
expect(bool).toBe(false);
|
||||
|
||||
bool = toBoolean(null, true);
|
||||
expect(bool).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toFloat()', () => {
|
||||
it('Should return a float', () => {
|
||||
const number = toFloat('3.14159');
|
||||
expect(number).toBe(3.14159);
|
||||
});
|
||||
|
||||
it('Should return default value for NaN', () => {
|
||||
let number = toFloat('test');
|
||||
expect(number).toBe(0);
|
||||
|
||||
number = toFloat(undefined);
|
||||
expect(number).toBe(0);
|
||||
|
||||
number = toFloat(null, -1);
|
||||
expect(number).toBe(-1);
|
||||
});
|
||||
});
|
||||
+6
-5
@@ -19,9 +19,10 @@ export function toBoolean(value: string | undefined | null, defaultValue = false
|
||||
* @returns {number} The value.
|
||||
*/
|
||||
export function toFloat(value: string | null | undefined, defaultValue = 0) {
|
||||
if (!value || isNaN(value as never)) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
return parseFloat(value);
|
||||
}
|
||||
if (!value) return defaultValue;
|
||||
|
||||
const number = parseFloat(value);
|
||||
if (isNaN(number)) return defaultValue;
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user