Skip to content

Commit 7b666ff

Browse files
committed
feat: bootstrap implementation and tests
0 parents  commit 7b666ff

File tree

14 files changed

+845
-0
lines changed

14 files changed

+845
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
indent_size = 2
8+
indent_style = space
9+
insert_final_newline = true
10+
max_line_length = 80
11+
trim_trailing_whitespace = true
12+
13+
[*.md]
14+
max_line_length = 0
15+
trim_trailing_whitespace = false
16+
17+
[COMMIT_EDITMSG]
18+
max_line_length = 0

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# node
2+
node_modules
3+
4+
# logs
5+
*.log
6+
7+
# files and IDE
8+
.DS_STORE
9+
*~
10+
*.swp
11+
*.swo
12+
13+
# project
14+
package-lock.json
15+
dist

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env'],
3+
};

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './lib/td';

lib/node.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
export const CELL = 'cell';
2+
export const COLUMN = 'column';
3+
export const ROW = 'row';
4+
export const TABLE = 'table';
5+
6+
function create(type, props) {
7+
const node = { ...props, type };
8+
const { data, position } = props;
9+
if (data instanceof Object) {
10+
node.data = data;
11+
}
12+
13+
if (position instanceof Object) {
14+
node.position = position;
15+
}
16+
17+
return node;
18+
}
19+
20+
export function cell(value, props = {}) {
21+
const { data, position } = props;
22+
return create(CELL, { data, position, value });
23+
}
24+
25+
export function column(value, props = {}) {
26+
const { data, dataType, index, label, position } = props;
27+
return create(COLUMN, { data, dataType, index, label, position, value });
28+
}
29+
30+
export function row(props = {}, children = []) {
31+
const { data, index, position } = props;
32+
let firstChildType;
33+
const updatedChildren = children
34+
.map((child) => {
35+
const isNode = child?.type;
36+
if (!firstChildType) {
37+
if (isNode) {
38+
if ([CELL, COLUMN].includes(child.type)) {
39+
firstChildType = child.type;
40+
}
41+
} else {
42+
firstChildType = CELL;
43+
}
44+
}
45+
if (!isNode) {
46+
return cell(child);
47+
}
48+
if (isNode && firstChildType === child.type) {
49+
return child;
50+
}
51+
return null;
52+
})
53+
.filter((child) => child)
54+
.map((child, index) => {
55+
return child.type === COLUMN ? { ...child, index } : child;
56+
});
57+
return create(ROW, { children: updatedChildren, data, index, position });
58+
}
59+
60+
export function table(props = {}, children = []) {
61+
const { data, position } = props;
62+
const updatedChildren = children
63+
.map((child) => {
64+
const isRowNode = child?.type === ROW;
65+
if (isRowNode) {
66+
return row(child, child.children);
67+
}
68+
return null;
69+
})
70+
.filter((child) => child)
71+
.map((child, index) => ({ ...child, index }));
72+
return create(TABLE, { children: updatedChildren, data, position });
73+
}

lib/td.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { CELL, COLUMN, ROW, TABLE, cell, column, row, table } from './node';
2+
3+
function fromArgs(...args) {
4+
const [type, arg2, arg3] = args;
5+
let children = [];
6+
let props = {};
7+
8+
if (Array.isArray(arg2)) {
9+
children = arg2;
10+
} else {
11+
children = arg3;
12+
if (arg2 instanceof Object) {
13+
props = arg2;
14+
} else {
15+
props = { value: arg2 };
16+
}
17+
}
18+
19+
return { children, props, type };
20+
}
21+
22+
export default function td(...args) {
23+
const { children, props, type } = fromArgs(...args);
24+
switch (type) {
25+
case TABLE: {
26+
return table(props, children);
27+
}
28+
case ROW: {
29+
return row(props, children);
30+
}
31+
case CELL: {
32+
const { value, ...restProps } = props;
33+
return cell(value, restProps);
34+
}
35+
case COLUMN: {
36+
const { value, ...restProps } = props;
37+
return column(value, restProps);
38+
}
39+
default: {
40+
return table();
41+
}
42+
}
43+
}

license

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Chris Zhou <chrisrzhou@pm.me>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

package.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"name": "tdastscript",
3+
"version": "0.1.0",
4+
"description": "utility to create tdast trees",
5+
"license": "MIT",
6+
"homepage": "https://github.com/tdast/tdastscript",
7+
"repository": "https://github.com/tdast/tdastscript",
8+
"bugs": "https://github.com/tdast/tdastscript/issues",
9+
"author": "Chris Zhou <chrisrzhou@pm.me> (https://chrisrzhou.io)",
10+
"keywords": [
11+
"tdast",
12+
"script",
13+
"tabular",
14+
"data",
15+
"ast",
16+
"unist",
17+
"util"
18+
],
19+
"scripts": {
20+
"bootstrap": "npm install",
21+
"build": "microbundle",
22+
"clean": "rm -rf dist",
23+
"dev": "microbundle watch",
24+
"lint": "xo --fix; tsc",
25+
"prepare": "npm run clean; npm run build",
26+
"release": "standard-version -s --infile changelog.md",
27+
"test": "jest --watch packages",
28+
"test:run": "jest"
29+
},
30+
"typings": "types/index.d.ts",
31+
"files": [
32+
"dist",
33+
"types"
34+
],
35+
"dependencies": {
36+
"tdast-types": "^0.1.1"
37+
},
38+
"devDependencies": {
39+
"@babel/preset-env": "^7.11.5",
40+
"@types/jest": "^26.0.9",
41+
"babel-jest": "^26.2.2",
42+
"husky": "^4.3.0",
43+
"jest": "^26.4.2",
44+
"microbundle": "^0.12.3",
45+
"standard-version": "^9.0.0",
46+
"typescript": "^4.0.2",
47+
"xo": "^0.33.1"
48+
},
49+
"husky": {
50+
"hooks": {
51+
"pre-push": "npm prepare; npm run lint; npm run test:run"
52+
}
53+
},
54+
"prettier": {
55+
"bracketSpacing": true,
56+
"jsxBracketSameLine": true,
57+
"trailingComma": "all",
58+
"useTabs": false
59+
},
60+
"xo": {
61+
"env": [
62+
"jest"
63+
],
64+
"prettier": true,
65+
"rules": {
66+
"capitalized-comments": "off",
67+
"padding-line-between-statements": "off"
68+
}
69+
}
70+
}

readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# tdastscript
2+
3+
utility to create [**tdast**][tdast] trees.
4+
5+
---
6+
7+
## Install
8+
9+
```sh
10+
npm install tdastscript
11+
```
12+
13+
## Use
14+
15+
```js
16+
import td from 'tdastscript';
17+
```
18+
19+
<!-- Definitions -->
20+
[tdast]: https://github.com/tdast/tdast

0 commit comments

Comments
 (0)