Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F1262306
single-reference.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
single-reference.ts
View Options
import
{
is
,
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
JDDContext
}
from
"../jdd-context.js"
;
import
{
MaybePromise
}
from
"../utils/util-types.js"
;
import
{
StringBasedArgument
}
from
"./string-based-argument.js"
;
import
{
Collection
,
CollectionItem
,
Context
}
from
"sealious"
;
function
pickRandom
<
T
>
(
array
:
T
[])
:
T
{
return
array
[
Math
.
floor
(
Math
.
random
()
*
array
.
length
)];
}
export
class
SingleReference
<
C
extends
Collection
>
extends
StringBasedArgument
<
string
>
{
constructor
(
public
getSealiousCollection
:
()
=>
Promise
<
C
>
,
public
getSealiousContext
:
(
jdd_context
:
JDDContext
)
=>
Context
,
public
itemToLabel
:
(
item
:
CollectionItem
<
C
>
)
=>
Promise
<
string
>
)
{
super
();
}
getTypeName
()
{
return
"single-reference"
;
}
async
getEmptyValue
(
jdd_ctx
:
JDDContext
)
{
const
context
=
this
.
getSealiousContext
(
jdd_ctx
);
const
ids
=
(
await
(
await
this
.
getSealiousCollection
())
.
list
(
context
)
.
paginate
({
items
:
5
})
.
fetch
()
).
items
.
map
(({
id
})
=>
id
);
if
(
!
ids
.
length
)
{
return
""
;
}
else
{
return
pickRandom
(
ids
);
}
}
async
getValues
(
jdd_ctx
:
JDDContext
)
:
Promise
<
{
label
:
string
;
value
:
string
}[]
>
{
const
context
=
this
.
getSealiousContext
(
jdd_ctx
);
const
{
items
}
=
await
(
await
this
.
getSealiousCollection
())
.
list
(
context
)
.
fetch
();
const
labels
=
Promise
.
all
(
items
.
map
(
async
(
item
)
=>
({
value
:
item
.
id
,
label
:
await
this
.
itemToLabel
(
item
),
}))
);
return
labels
;
}
getExampleValue
(
context
:
JDDContext
)
{
return
this
.
getEmptyValue
(
context
);
}
countWords
()
:
number
{
return
0
;
}
example_values
=
[];
parseFormInput
(
_
:
JDDContext
,
input
:
unknown
)
:
MaybePromise
<
string
|
string
[]
|
null
>
{
if
(
is
(
input
,
predicates
.
string
))
{
return
input
;
}
else
{
return
null
;
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-Algol68
Expires
Thu, Jan 23, 19:19 (20 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
600841
Default Alt Text
single-reference.ts (1 KB)
Attached To
Mode
rJDD jdd
Attached
Detach File
Event Timeline
Log In to Comment