Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F7188701
swap.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
swap.ts
View Options
import
{
predicates
,
ShapeToType
}
from
"@sealcode/ts-predicates"
;
import
type
Context
from
"../../../../context"
;
import
{
ArrayAction
}
from
"./array-action"
;
const
SwapInputShape
=
{
swap
:
predicates
.
array
(
predicates
.
or
(
predicates
.
string
,
predicates
.
number
)),
};
const
SwapParsedShape
=
{
swap
:
predicates
.
array
(
predicates
.
number
)
};
export
class
Swap
extends
ArrayAction
<
ShapeToType
<
typeof
SwapInputShape
>
,
ShapeToType
<
typeof
SwapParsedShape
>
>
{
InputShape
=
SwapInputShape
;
async
_validate
<
T
>
(
_context
:
Context
,
action
:
ShapeToType
<
typeof
SwapInputShape
>
,
array
:
T
[]
)
:
Promise
<
{
valid
:
boolean
;
reason
:
string
}
>
{
if
(
action
.
swap
.
length
!=
2
)
{
return
{
valid
:
false
,
reason
:
"swap action parameter should be a list of two numbers"
,
};
}
if
(
action
.
swap
.
some
((
index
)
=>
index
>=
array
.
length
||
index
<
0
))
{
return
{
valid
:
false
,
reason
:
"swap action parameter out of range"
,
};
}
return
{
valid
:
true
,
reason
:
"swap action parameters ok"
,
};
}
async
_parse
(
_context
:
Context
,
input
:
ShapeToType
<
typeof
SwapInputShape
>
)
:
Promise
<
ShapeToType
<
typeof
SwapParsedShape
>
|
null
>
{
return
{
swap
:
[
parseInt
(
input
.
swap
[
0
].
toString
()),
parseInt
(
input
.
swap
[
1
].
toString
()),
],
};
}
async
run
<
T
>
(
_context
:
Context
,
action
:
ShapeToType
<
typeof
SwapParsedShape
>
,
array
:
T
[]
)
{
const
temp
=
array
[
action
.
swap
[
0
]];
array
[
action
.
swap
[
0
]]
=
array
[
action
.
swap
[
1
]];
array
[
action
.
swap
[
1
]]
=
temp
;
return
array
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Tue, Jul 8, 08:28 (18 h, 11 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
800511
Default Alt Text
swap.ts (1 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment