Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
11b0e004
Commit
11b0e004
authored
Apr 21, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Apr 25, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate type-coercions to TS
parent
6d0bb257
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
33 deletions
+9
-33
type-coercions.ts
src/shared/type-coercions.ts
+9
-33
No files found.
src/shared/type-coercions.
j
s
→
src/shared/type-coercions.
t
s
View file @
11b0e004
...
...
@@ -7,14 +7,9 @@
* Note that if the values passed are plain javascript values (such as ones
* produced from JSON.parse), then these methods do not throw errors.
*/
import
type
{
Ref
}
from
'preact'
;
/**
* Returns a boolean
*
* @param {any} value - initial value
* @return {boolean}
*/
export
function
toBoolean
(
value
)
{
export
function
toBoolean
(
value
:
any
):
boolean
{
if
(
typeof
value
===
'string'
)
{
if
(
value
.
trim
().
toLocaleLowerCase
()
===
'false'
)
{
// "false", "False", " false", "FALSE" all return false
...
...
@@ -25,28 +20,22 @@ export function toBoolean(value) {
if
(
!
isNaN
(
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'
;
}
/**
* 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
return
parseInt
(
value
);
}
/**
* Returns either the value if its an object or an empty object
*
* @param {any} value - initial value
* @return {object}
* Returns either the value if it's an object or an empty object
*/
export
function
toObject
(
value
)
{
export
function
toObject
(
value
:
any
):
object
{
if
(
typeof
value
===
'object'
&&
value
!==
null
)
{
return
value
;
}
...
...
@@ -57,22 +46,14 @@ export function toObject(value) {
/**
* Returns the value as a string or an empty string if the
* 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'
)
{
return
value
.
toString
();
}
return
''
;
}
/**
* @template T
* @typedef {import('preact').Ref<T>} Ref
*/
/**
* Helper for downcasting a ref to a more specific type, where that is safe
* to do.
...
...
@@ -80,12 +61,7 @@ export function toString(value) {
* 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.
* 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
)
{
return
/** @type {Ref<U>|undefined} */
(
ref
)
;
export
function
downcastRef
<
T
,
U
>
(
ref
:
Ref
<
T
>
|
undefined
):
Ref
<
U
>
|
undefined
{
return
ref
as
Ref
<
U
>
|
undefined
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment