https://github.com/tdast/tdast-util-to-array
tdast utility to transform tdast to a JS array
https://github.com/tdast/tdast-util-to-array
array ast json tabular-data tdast tdast-util unist util
Last synced: about 2 months ago
JSON representation
tdast utility to transform tdast to a JS array
- Host: GitHub
- URL: https://github.com/tdast/tdast-util-to-array
- Owner: tdast
- License: mit
- Created: 2020-09-11T07:31:12.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2020-09-13T08:01:19.000Z (almost 6 years ago)
- Last Synced: 2025-06-27T20:43:19.941Z (about 1 year ago)
- Topics: array, ast, json, tabular-data, tdast, tdast-util, unist, util
- Language: JavaScript
- Homepage: https://github.com/tdast/
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: license
Awesome Lists containing this project
README
# tdast-util-to-array
[**tdast**][tdast] utility to transform from tdast to JS array.
---
## Install
```sh
npm install tdast-util-to-array
```
## Use
```js
import toArray from 'tdast-util-to-array';
const tdast = {
type: 'table',
children: [
{
type: 'row',
index: 0,
children: [
{
type: 'column',
index: 0,
value: 'col1',
},
{
type: 'column',
index: 1,
value: 'col2',
},
{
type: 'column',
index: 2,
value: 'col3',
},
],
},
{
type: 'row',
index: 1,
children: [
{
type: 'cell',
columnIndex: 0,
rowIndex: 1,
value: 'row1col1',
},
{
type: 'cell',
columnIndex: 1,
rowIndex: 1,
value: 'row1col2',
},
{
type: 'cell',
columnIndex: 2,
rowIndex: 1,
value: 'row1col3',
},
],
},
{
type: 'row',
index: 2,
children: [
{
type: 'cell',
columnIndex: 0,
rowIndex: 2,
value: 'row2col1',
},
{
type: 'cell',
columnIndex: 1,
rowIndex: 2,
value: 'row2col2',
},
{
type: 'cell',
columnIndex: 2,
rowIndex: 2,
value: 'row2col3',
},
],
},
],
};
expect(toArray(tdast)).toEqual({
{ col1: 'row1col1', col2: 'row1col2', col3: 'row1col3' },
{ col1: 'row2col1', col2: 'row2col2', col3: 'row2col3' },
});
```
## API
### `toArray(array[, options])`
#### Interface
```ts
function toArray(
// tdast Table node
tdast: Table,
// options to configure transformer
options?: Options,
): Array>;
```
Transforms a [tdast][] `Table` node into a JS array of objects.
If columns and row cell cardinality do not match, an error will be thrown to inform that the JS array cannot be transformed.
By default, `toArray` will infer `Column` node values to use as keys for the JS array objects. If the tdast node does not contain `Column` nodes, or you wish to use explicit column keys, you can specify these with the `options.columns` property as detailed in the example below.
#### Example
Using the same tdast tree in the earlier example
```js
import toArray from 'tdast-util-to-array';
const options = {
columns ['Col A', 'Col B', 'Col C'],
};
expect(toArray(tdast, options)).toEqual([
{ 'Col A': 'row1col1', 'Col B': 'row1col2', 'Col C': 'row1col3' },
{ 'Col A': 'row2col1', 'Col B': 'row2col2', 'Col C': 'row2col3' },
]);
```
#### Related interfaces
```ts
interface Options {
// array of column strings that will be used as object keys. Overrides the column values detected in the tdast tree.
columns?: string[];
}
```
[tdast]: https://github.com/tdast/tdast