Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F9582440
insert.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
insert.ts
View Options
import
{
predicates
,
type
ShapeToType
}
from
"@sealcode/ts-predicates"
;
import
type
Context
from
"../../../../context.js"
;
import
{
ArrayAction
}
from
"./array-action.js"
;
const
InsertInputShape
=
{
insert
:
predicates
.
shape
({
index
:
predicates
.
maybe
(
predicates
.
or
(
predicates
.
number
,
predicates
.
string
)
),
value
:
predicates
.
maybe
(
predicates
.
any
),
}),
};
const
InsertParsedShape
=
{
insert
:
predicates
.
shape
({
index
:
predicates
.
number
,
value
:
predicates
.
any
,
}),
};
export
class
Insert
extends
ArrayAction
<
ShapeToType
<
typeof
InsertInputShape
>
,
ShapeToType
<
typeof
InsertParsedShape
>
>
{
InputShape
=
InsertInputShape
;
async
_validate
<
T
>
(
context
:
Context
,
action
:
ShapeToType
<
typeof
InsertInputShape
>
,
array
:
T
[]
)
:
Promise
<
{
valid
:
boolean
;
reason
:
string
}
>
{
const
validation_result
=
await
this
.
element_validator
(
context
,
action
.
insert
.
value
||
{},
action
.
insert
.
index
==
undefined
?
array
.
length
:
parseInt
(
action
.
insert
.
index
.
toString
())
);
if
(
!
validation_result
.
valid
)
{
return
{
valid
:
false
,
reason
:
validation_result
.
reason
,
};
}
return
{
valid
:
true
,
reason
:
"Insert action shape ok"
};
}
async
_parse
<
T
>
(
_context
:
Context
,
input
:
ShapeToType
<
typeof
InsertInputShape
>
,
array
:
T
[],
emptyValue
:
T
)
:
Promise
<
ShapeToType
<
typeof
InsertParsedShape
>
|
null
>
{
return
{
insert
:
{
index
:
parseInt
(
(
input
.
insert
.
index
||
array
.
length
).
toString
()
),
value
:
input
.
insert
.
value
||
emptyValue
,
},
};
}
async
run
<
T
>
(
_context
:
Context
,
action
:
ShapeToType
<
typeof
InsertParsedShape
>
,
array
:
T
[],
emptyElement
:
T
)
{
const
n
=
parseInt
(
(
action
.
insert
.
index
===
undefined
?
array
.
length
:
action
.
insert
.
index
).
toString
()
);
array
=
[
...
array
.
slice
(
0
,
n
),
action
.
insert
.
value
===
undefined
?
emptyElement
:
action
.
insert
.
value
,
...
array
.
slice
(
n
),
];
return
array
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Sat, Oct 11, 06:53 (9 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
983666
Default Alt Text
insert.ts (1 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment