Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F3010636
boolean.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
boolean.ts
View Options
import
{
is
,
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
Context
}
from
"koa"
;
import
{
FormDataValue
}
from
"../form-types.js"
;
import
{
FieldParseResult
,
FormField
}
from
"./field.js"
;
const
truthy
=
[
"true"
,
"on"
,
"yes"
,
"1"
];
const
falsy
=
[
"false"
,
"off"
,
"no"
,
"0"
];
export
class
Boolean
<
Required
extends
boolean
>
extends
FormField
<
Required
,
boolean
>
{
predicate
=
predicates
.
boolean
;
constructor
(
required
:
Required
,
public
error_message
:
string
=
""
)
{
super
(
required
);
}
public
async
isValueValid
(
_
:
Context
,
value
:
boolean
)
:
Promise
<
{
valid
:
boolean
;
message
:
string
}
>
{
if
(
this
.
required
&&
!
value
)
{
return
{
valid
:
false
,
message
:
this
.
error_message
};
}
return
{
valid
:
true
,
message
:
""
};
}
async
parse
(
_
:
Context
,
value
:
FormDataValue
)
:
Promise
<
FieldParseResult
<
boolean
>>
{
if
(
is
(
value
,
predicates
.
boolean
))
{
return
{
parsable
:
true
,
parsed_value
:
value
,
error
:
null
};
}
else
if
(
is
(
value
,
predicates
.
string
)
&&
[...
truthy
,
...
falsy
].
includes
(
value
)
)
{
return
{
parsable
:
true
,
parsed_value
:
truthy
.
includes
(
value
),
error
:
null
,
};
}
else
if
(
is
(
value
,
predicates
.
undefined
))
{
return
{
parsable
:
true
,
parsed_value
:
false
,
error
:
null
,
};
}
else
if
(
value
===
null
)
{
return
{
parsable
:
true
,
parsed_value
:
false
,
error
:
null
};
}
else
{
return
{
parsable
:
false
,
parsed_value
:
null
,
error
:
"Please enter a boolean"
,
};
}
}
public
getEmptyValue
()
:
boolean
{
return
false
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Wed, May 7, 19:45 (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
625284
Default Alt Text
boolean.ts (1 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment