├── package.json
├── index.html
├── README.md
├── LICENSE
├── .gitignore
└── index.js
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "invert-markdown-table",
3 | "version": "1.2.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/harttle/invert-markdown-table.git"
12 | },
13 | "author": "harttle",
14 | "license": "MIT",
15 | "bugs": {
16 | "url": "https://github.com/harttle/invert-markdown-table/issues"
17 | },
18 | "homepage": "https://github.com/harttle/invert-markdown-table#readme"
19 | }
20 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Invert a GFM Table
6 |
7 |
8 |
14 |
21 |
22 |
23 |
24 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Online inverter here: , also available on [npm](https://www.npmjs.com/package/invert-markdown-table):
2 |
3 | ```bash
4 | npm i invert-markdown-table
5 | ```
6 |
7 | # Usage
8 |
9 | ```javascript
10 | var invertMarkdownTable = require('invert-markdown-table');
11 | var source = 'a | b\n---|---\nc | d'
12 | var result = invertMarkdownTable(source);
13 | console.log(result);
14 | // Outputs:
15 | // a | c\n---|---\nb | d
16 | ```
17 |
18 | # Use in Browser
19 |
20 | Global Variable:
21 |
22 | ```javascript
23 | var source = 'a | b\n---|---\nc | d';
24 | var result = window.invertMarkdownTable(source);
25 | ```
26 |
27 | RequireJS:
28 |
29 | ```javascript
30 | require(['invert-markdown-table'], function (invertMarkdownTable) {
31 | var source = 'a | b\n---|---\nc | d'
32 | var result = invertMarkdownTable(source);
33 | });
34 | ```
35 |
36 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Jun Yang
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 all
13 | 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 THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 |
8 | # Runtime data
9 | pids
10 | *.pid
11 | *.seed
12 | *.pid.lock
13 |
14 | # Directory for instrumented libs generated by jscoverage/JSCover
15 | lib-cov
16 |
17 | # Coverage directory used by tools like istanbul
18 | coverage
19 |
20 | # nyc test coverage
21 | .nyc_output
22 |
23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24 | .grunt
25 |
26 | # Bower dependency directory (https://bower.io/)
27 | bower_components
28 |
29 | # node-waf configuration
30 | .lock-wscript
31 |
32 | # Compiled binary addons (https://nodejs.org/api/addons.html)
33 | build/Release
34 |
35 | # Dependency directories
36 | node_modules/
37 | jspm_packages/
38 |
39 | # TypeScript v1 declaration files
40 | typings/
41 |
42 | # Optional npm cache directory
43 | .npm
44 |
45 | # Optional eslint cache
46 | .eslintcache
47 |
48 | # Optional REPL history
49 | .node_repl_history
50 |
51 | # Output of 'npm pack'
52 | *.tgz
53 |
54 | # Yarn Integrity file
55 | .yarn-integrity
56 |
57 | # dotenv environment variables file
58 | .env
59 |
60 | # next.js build output
61 | .next
62 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
3 | module.exports = invertMarkdownTable;
4 | }
5 | else if (typeof define === 'function' && define.amd) {
6 | define('invert-markdown-table', function() {
7 | return invertMarkdownTable;
8 | });
9 | } else {
10 | window.invertMarkdownTable = invertMarkdownTable;
11 | }
12 |
13 | function invertMarkdownTable(markdown) {
14 | markdown = markdown.trim();
15 |
16 | var maxColumn = 0;
17 | var lines = markdown.split('\n').map(function (line) {
18 | var columns = line.replace(/^\s*\|/, '').replace(/\|\s*$/, '').split('|').map(trim);
19 | columns.maxLength = columns.reduce(function (prev, curr) {
20 | return Math.max(prev, wordLength(curr));
21 | }, 0);
22 | maxColumn = Math.max(columns.length, maxColumn);
23 | return columns;
24 | });
25 | lines.splice(1, 1);
26 |
27 | var revertedLines = [];
28 | for (var i=0; i 2
52 | // 2 for Chinese characters, '我们' -> 4
53 | function wordLength(word) {
54 | return Array.prototype.reduce.call(word, function (prev, char) {
55 | return prev + characterLength(char);
56 | }, 0);
57 | }
58 |
59 | function characterLength(char) {
60 | if (/[\u4E00-\u9FA5]/.test(char)) {
61 | return 2;
62 | }
63 | if (/[\uFE30-\uFFA0]/.test(char)) {
64 | return 2;
65 | }
66 | return 1;
67 | }
68 |
69 | function pad(word, len) {
70 | return word + array(' ', len).join('').substr(wordLength(word));
71 | }
72 | function trim(word) {
73 | return word.trim();
74 | }
75 | function array(val, n) {
76 | var ret = [];
77 | while (n--) ret.push(val);
78 | return ret;
79 | }
80 | })();
81 |
--------------------------------------------------------------------------------