Commit 11b0e004 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate type-coercions to TS

parent 6d0bb257
...@@ -7,14 +7,9 @@ ...@@ -7,14 +7,9 @@
* Note that if the values passed are plain javascript values (such as ones * Note that if the values passed are plain javascript values (such as ones
* produced from JSON.parse), then these methods do not throw errors. * produced from JSON.parse), then these methods do not throw errors.
*/ */
import type { Ref } from 'preact';
/** export function toBoolean(value: any): boolean {
* Returns a boolean
*
* @param {any} value - initial value
* @return {boolean}
*/
export function toBoolean(value) {
if (typeof value === 'string') { if (typeof value === 'string') {
if (value.trim().toLocaleLowerCase() === 'false') { if (value.trim().toLocaleLowerCase() === 'false') {
// "false", "False", " false", "FALSE" all return false // "false", "False", " false", "FALSE" all return false
...@@ -25,28 +20,22 @@ export function toBoolean(value) { ...@@ -25,28 +20,22 @@ export function toBoolean(value) {
if (!isNaN(numericalVal)) { if (!isNaN(numericalVal)) {
return Boolean(numericalVal); return Boolean(numericalVal);
} }
// Any non numerical or falsely string should return true, otherwise return false // Any non-numerical or falsely string should return true, otherwise return false
return typeof value === 'string'; return typeof value === 'string';
} }
/** /**
* Returns either an integer or NaN * Returns either an integer or NaN
*
* @param {any} value - initial value
* @return {number}
*/ */
export function toInteger(value) { export function toInteger(value: any): number {
// Acts as a simple wrapper // Acts as a simple wrapper
return parseInt(value); return parseInt(value);
} }
/** /**
* Returns either the value if its an object or an empty object * Returns either the value if it's an object or an empty object
*
* @param {any} value - initial value
* @return {object}
*/ */
export function toObject(value) { export function toObject(value: any): object {
if (typeof value === 'object' && value !== null) { if (typeof value === 'object' && value !== null) {
return value; return value;
} }
...@@ -57,22 +46,14 @@ export function toObject(value) { ...@@ -57,22 +46,14 @@ export function toObject(value) {
/** /**
* Returns the value as a string or an empty string if the * Returns the value as a string or an empty string if the
* value undefined, null or otherwise falsely. * value undefined, null or otherwise falsely.
*
* @param {any} value - initial value
* @return {string}
*/ */
export function toString(value) { export function toString(value: any): string {
if (value && typeof value.toString === 'function') { if (value && typeof value.toString === 'function') {
return value.toString(); return value.toString();
} }
return ''; return '';
} }
/**
* @template T
* @typedef {import('preact').Ref<T>} Ref
*/
/** /**
* Helper for downcasting a ref to a more specific type, where that is safe * Helper for downcasting a ref to a more specific type, where that is safe
* to do. * to do.
...@@ -80,12 +61,7 @@ export function toString(value) { ...@@ -80,12 +61,7 @@ export function toString(value) {
* This is mainly useful to cast a generic `Ref<HTMLElement>` to a more specific * This is mainly useful to cast a generic `Ref<HTMLElement>` to a more specific
* element type (eg. `Ref<HTMLDivElement>`) for use with the `ref` prop of a JSX element. * element type (eg. `Ref<HTMLDivElement>`) for use with the `ref` prop of a JSX element.
* Since Preact only writes to the `ref` prop, such a cast is safe. * Since Preact only writes to the `ref` prop, such a cast is safe.
*
* @template T
* @template {T} U
* @param {Ref<T>|undefined} ref
* @return {Ref<U>|undefined}
*/ */
export function downcastRef(ref) { export function downcastRef<T, U>(ref: Ref<T> | undefined): Ref<U> | undefined {
return /** @type {Ref<U>|undefined} */ (ref); return ref as Ref<U> | undefined;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment