Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F1262923
table.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
table.ts
View Options
import
{
hasShape
,
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
JDDContext
}
from
"../index.js"
;
import
{
ComponentArgument
}
from
"./component-argument.js"
;
import
{
ContainerArgument
}
from
"./container-argument.js"
;
type
TableHeader
<
HeaderType
>
=
{
type
:
"header"
;
header_content
:
HeaderType
};
type
TableRegularRow
<
CellType
>
=
{
type
:
"row"
;
cells
:
CellType
[]
};
export
type
TableRow
<
CellType
,
HeaderType
>
=
|
TableHeader
<
HeaderType
>
|
TableRegularRow
<
CellType
>
;
export
type
TableData
<
CellType
,
HeaderType
>
=
{
rows
:
TableRow
<
CellType
,
HeaderType
>
[];
};
export
function
isTableHeader
(
x
:
unknown
)
:
x
is
TableHeader
<
unknown
>
{
return
hasShape
(
{
type
:
predicates
.
const
(
"header"
),
header_content
:
predicates
.
unknown
,
},
x
);
}
export
function
isTableRegularRow
<
CellType
=
unknown
>
(
x
:
unknown
)
:
x
is
TableRegularRow
<
CellType
>
{
return
hasShape
(
{
type
:
predicates
.
const
(
"row"
),
cells
:
predicates
.
array
(
predicates
.
unknown
),
},
x
);
}
export
function
isTableData
<
CellType
=
unknown
,
HeaderType
=
unknown
>
(
x
:
unknown
)
:
x
is
TableData
<
CellType
,
HeaderType
>
{
return
hasShape
(
{
rows
:
predicates
.
array
(
predicates
.
object
),
},
x
);
}
export
class
Table
<
CellType
,
HeaderType
>
extends
ContainerArgument
<
TableData
<
CellType
,
HeaderType
>
>
{
constructor
(
public
header_type
:
ComponentArgument
<
HeaderType
,
unknown
,
unknown
>
,
public
cell_type
:
ComponentArgument
<
CellType
,
unknown
,
unknown
>
)
{
super
();
cell_type
.
parent_argument
=
this
;
header_type
.
parent_argument
=
this
;
}
getSubArgument
(
[
_
,
key
,
...
rest
]
:
string
[],
value
:
TableData
<
CellType
,
HeaderType
>
)
{
if
(
isNaN
(
parseInt
(
key
)))
{
return
<
const
>
[
null
,
[]
as
string
[],
null
];
}
const
key_n
=
parseInt
(
key
);
const
row
=
value
.
rows
[
key_n
];
if
(
!
row
)
{
return
<
const
>
[
null
,
[]
as
string
[],
null
];
}
else
if
(
row
.
type
==
"header"
)
{
rest
.
shift
();
return
<
const
>
[
this
.
header_type
,
rest
,
row
.
header_content
];
}
else
{
let
cell_index
=
rest
.
shift
();
if
(
cell_index
==
"cells"
)
{
cell_index
=
rest
.
shift
();
}
if
(
!
cell_index
)
{
throw
new
Error
(
"Missing cell index"
);
}
const
parsed_cell_index
=
parseInt
(
cell_index
);
if
(
isNaN
(
parsed_cell_index
))
{
throw
new
Error
(
`Cell index should be a number, got:
${
cell_index
}
`
);
}
return
<
const
>
[
this
.
cell_type
,
rest
,
row
.
cells
[
parsed_cell_index
]];
}
}
getTypeName
()
{
return
"table"
;
}
async
getEmptyValue
(
context
:
JDDContext
)
{
return
{
rows
:
[
{
type
:
<
const
>
"header"
,
header_content
:
await
this
.
header_type
.
getEmptyValue
(
context
),
},
{
type
:
<
const
>
"row"
,
cells
:
[
await
this
.
cell_type
.
getEmptyValue
(
context
)],
},
],
};
}
async
getExampleValue
(
context
:
JDDContext
)
{
const
rows
=
Math
.
round
(
Math
.
random
()
*
5
);
const
columns
=
Math
.
round
(
Math
.
random
()
*
5
);
const
result
:
TableData
<
CellType
,
HeaderType
>
=
{
rows
:
[
{
type
:
"header"
,
header_content
:
await
this
.
header_type
.
getExampleValue
(
context
),
},
],
};
for
(
let
i
=
0
;
i
<
rows
;
i
++
)
{
const
cells
:
CellType
[]
=
[];
for
(
let
j
=
0
;
j
<
columns
;
j
++
)
{
// eslint-disable-next-line no-await-in-loop
cells
.
push
(
await
this
.
cell_type
.
getExampleValue
(
context
));
}
result
.
rows
.
push
({
type
:
"row"
,
cells
});
}
return
result
;
}
countWords
(
value
:
TableData
<
CellType
,
HeaderType
>
)
:
number
{
let
result
=
0
;
for
(
let
i
=
0
;
i
<
value
.
rows
.
length
;
i
++
)
{
const
row
=
value
.
rows
[
i
];
if
(
isTableHeader
(
row
))
{
result
+=
this
.
header_type
.
countWords
(
row
.
header_content
);
}
else
{
for
(
let
j
=
0
;
j
<
row
.
cells
.
length
;
j
++
)
{
result
+=
this
.
cell_type
.
countWords
(
row
.
cells
[
j
]);
}
}
}
return
result
;
}
async
processAllSubarguments
(
context
:
JDDContext
,
input
:
unknown
,
processing_function
:
(
argument
:
ComponentArgument
<
unknown
>
,
value
:
unknown
)
=>
Promise
<
unknown
>
)
{
if
(
!
hasShape
({
rows
:
predicates
.
array
(
predicates
.
object
)
},
input
))
{
return
{
rows
:
[]
};
}
else
{
const
result
:
TableData
<
CellType
,
HeaderType
>
=
{
rows
:
[]
};
const
row_promises
=
input
.
rows
.
map
(
async
(
row
,
row_index
)
=>
{
let
new_row
:
TableRow
<
CellType
,
HeaderType
>
;
if
(
hasShape
({
header_content
:
predicates
.
unknown
},
row
))
{
let
header_content
=
(
await
processing_function
(
this
.
header_type
,
row
.
header_content
))
as
HeaderType
|
HeaderType
[]
|
null
;
if
(
Array
.
isArray
(
header_content
))
{
header_content
=
header_content
[
0
];
}
if
(
header_content
==
null
)
{
header_content
=
await
this
.
header_type
.
getEmptyValue
(
context
);
}
new_row
=
{
type
:
"header"
,
header_content
,
};
result
.
rows
[
row_index
]
=
new_row
;
}
else
if
(
hasShape
(
{
cells
:
predicates
.
array
(
predicates
.
unknown
)
},
row
)
)
{
new_row
=
{
type
:
"row"
,
cells
:
await
Promise
.
all
(
row
.
cells
.
map
(
async
(
cell
)
=>
{
const
value
=
(
await
processing_function
(
this
.
cell_type
,
cell
))
as
CellType
|
CellType
[]
|
null
;
if
(
value
===
null
)
{
return
this
.
cell_type
.
getEmptyValue
(
context
);
}
else
if
(
Array
.
isArray
(
value
))
{
return
value
[
0
];
}
else
{
return
value
;
}
})
),
};
result
.
rows
[
row_index
]
=
new_row
;
}
});
await
Promise
.
all
(
row_promises
);
return
result
;
}
}
getColumnsCount
(
value
:
TableData
<
CellType
,
HeaderType
>
)
{
return
(
(
(
value
.
rows
.
filter
((
row
)
=>
row
.
type
==
"row"
)[
0
]
as
|
TableRegularRow
<
CellType
>
|
undefined
)
?
.
cells
||
[]
).
length
||
1
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Fri, Jan 24, 15:16 (17 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
600392
Default Alt Text
table.ts (5 KB)
Attached To
Mode
rJDD jdd
Attached
Detach File
Event Timeline
Log In to Comment