Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F3010596
number.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
number.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"
;
export
class
NumberField
<
DefaultValue
extends
number
|
null
=
number
,
Required
extends
boolean
=
boolean
>
extends
FormField
<
Required
,
number
|
DefaultValue
>
{
constructor
(
required
:
Required
,
public
default_value
:
DefaultValue
,
public
options
:
Partial
<
{
max
:
number
;
min
:
number
;
decimalDigits
:
number
;
}
>
=
{}
)
{
super
(
required
);
}
predicate
=
predicates
.
number
;
public
async
isValueValid
(
_
:
Context
,
value
:
unknown
)
{
if
(
(
is
(
value
,
predicates
.
string
)
&&
!
isNaN
(
parseFloat
(
value
))
&&
parseFloat
(
value
).
toString
()
==
value
.
trim
())
||
is
(
value
,
predicates
.
number
)
||
((
is
(
value
,
predicates
.
undefined
)
||
value
==
""
)
&&
!
this
.
required
)
)
{
return
{
valid
:
true
,
message
:
""
};
}
return
{
valid
:
false
,
message
:
"Proszę wprowadzić liczbę"
};
}
async
parse
(
_
:
Context
,
value
:
FormDataValue
)
:
Promise
<
FieldParseResult
<
number
>>
{
if
(
(
is
(
value
,
predicates
.
string
)
&&
!
isNaN
(
parseFloat
(
value
))
&&
parseFloat
(
value
).
toString
()
==
value
.
trim
())
||
is
(
value
,
predicates
.
number
)
)
{
return
{
parsable
:
true
,
parsed_value
:
this
.
options
.
decimalDigits
?
parseFloat
(
parseFloat
(
value
.
toString
()).
toFixed
(
this
.
options
.
decimalDigits
)
)
:
parseFloat
(
value
.
toString
()),
error
:
null
,
};
}
else
{
return
{
parsable
:
false
,
parsed_value
:
null
,
error
:
"Please enter a number"
,
};
}
}
public
getEmptyValue
()
:
DefaultValue
{
return
this
.
default_value
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Wed, May 7, 19:44 (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
624804
Default Alt Text
number.ts (1 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment