Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F969778
int.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
int.ts
View Options
import
{
Field
,
Context
,
Errors
}
from
"../../../main.js"
;
import
humanComparatorToQuery
,
{
ComparatorObject
,
HumanComparator
,
DBComparator
,
}
from
"../../../utils/human-comparator-to-query.js"
;
export
type
IntStorageParams
=
{
min
?:
number
;
max
?:
number
};
/** A field that stores it's value as an integer number. Can be configured to accept only values within specified range.
*
* **Params**:
* - `min` - `Number` - (optional) the minimum acceptable value
* - `max` - `Number` - (optional) the maximum acceptable value
*
* **Filters**:
* When filtering a list of resources, one can use wither a simple equality test, or a more complex range comparison. Consult the examples below:
* - `{ age: 2 }`
* - `{ age: {">": 2}}`
* - `{ age: {">": 2, "<" 10}}`
*/
export
abstract
class
IntStorage
<
Output
=
number
,
Input
extends
number
|
string
=
number
|
string
>
extends
Field
<
Output
,
Input
,
number
>
{
/** the min allowed value */
min
?:
number
;
/** tha max allowed value*/
max
?:
number
;
async
isProperValue
(
context
:
Context
,
new_value
:
number
|
string
)
{
const
number
=
parseInt
(
new_value
.
toString
(),
10
);
if
(
number
.
toString
()
!==
new_value
.
toString
().
trim
()
||
number
===
null
||
isNaN
(
number
)
)
{
return
Field
.
invalid
(
context
.
app
.
i18n
(
"invalid_integer"
,
[
new_value
])
);
}
if
(
this
.
min
!==
undefined
&&
new_value
<
this
.
min
)
{
return
Field
.
invalid
(
context
.
app
.
i18n
(
"too_small_integer"
,
[
number
,
this
.
min
])
);
}
if
(
this
.
max
!==
undefined
&&
new_value
>
this
.
max
)
{
return
Field
.
invalid
(
context
.
app
.
i18n
(
"too_big_integer"
,
[
number
,
this
.
max
])
);
}
return
Field
.
valid
();
}
constructor
(
params
:
IntStorageParams
=
{})
{
super
();
this
.
max
=
params
.
max
;
this
.
min
=
params
.
min
;
}
async
getMatchQueryValue
(
_
:
Context
,
field_filter
:
number
|
string
|
ComparatorObject
<
number
>
)
{
if
(
typeof
field_filter
!==
"object"
)
{
return
{
$eq
:
parseInt
(
field_filter
.
toString
(),
10
),
};
}
// treating filter as a query here
const
new_filter
:
{
[
db_comp
in
DBComparator
]
?:
number
}
=
{};
for
(
const
comparator
in
field_filter
)
{
if
(
!
comparator
)
{
continue
;
}
const
new_comparator
=
humanComparatorToQuery
(
comparator
as
HumanComparator
);
if
(
new_comparator
===
undefined
)
{
throw
new
Errors
.
ValidationError
(
`Unknown comparator: '
${
comparator
}
'.`
);
}
const
value
=
field_filter
[
comparator
as
HumanComparator
]
?
.
toString
();
if
(
value
===
undefined
)
{
continue
;
}
new_filter
[
new_comparator
]
=
parseInt
(
value
,
10
);
}
return
new_filter
;
}
encode
=
async
(
_
:
Context
,
value
:
number
|
string
|
null
)
=>
{
if
(
value
===
null
)
{
return
null
;
}
const
ret
=
parseInt
(
value
.
toString
(),
10
);
return
ret
;
};
async
hasIndex
()
:
Promise
<
boolean
>
{
return
true
;
}
}
/** An integer field. Consult {@link IntStorage} for information on
* customizing it's behavior.*/
export
default
class
Int
extends
IntStorage
<
number
,
number
|
string
>
{
typeName
=
"int"
;
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Sat, Nov 23, 17:55 (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
548018
Default Alt Text
int.ts (2 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment