Skip to content

Commit

Permalink
Feature/#4 n dimension json to table (#5)
Browse files Browse the repository at this point in the history
* updated dependencies

* fixed comments, documentation and typos

* Added functionality to flatten n-dimensional JSON input.

* added n-dimensional tests

* split up function

* work in progress

* separated functions and changed tests

* added test cases, fixed fails

* extended test cases and fix issues

* fixed tests. made smaller functions

* changed tests and renamed functions

* added tests and clean up

* updated dependencies

* updated version: 1.0.3
  • Loading branch information
dario-baumberger authored Sep 1, 2024
1 parent 2a762f2 commit a92475e
Show file tree
Hide file tree
Showing 12 changed files with 2,184 additions and 983 deletions.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"id": "json-table",
"name": "JSON table",
"version": "1.0.2",
"version": "1.0.3",
"minAppVersion": "0.15.0",
"description": "Simply switch between JSON and tables. Generate a table from a JSON string or a URL (which returns JSON) in your notes. Generate JSON from a table in your notes.",
"author": "Dario Baumberger",
"authorUrl": "https://github.com/dario-baumberger",
"fundingUrl": "https://www.buymeacoffee.com/dariobaumberger",
"isDesktopOnly": false
}
}
1,605 changes: 842 additions & 763 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-json-table",
"version": "1.0.2",
"version": "1.0.3",
"description": "Simply switch between JSON and tables. Generate a table from a JSON string or a URL (which returns JSON) in your notes. Generate JSON from a table in your notes.",
"main": "main.js",
"scripts": {
Expand All @@ -16,15 +16,16 @@
"author": "Dario Baumberger",
"license": "MIT",
"devDependencies": {
"@types/node": "^20.11.16",
"@types/node": "^20.16.2",
"@typescript-eslint/eslint-plugin": "6.21.0",
"@typescript-eslint/parser": "6.21.0 ",
"@vitest/coverage-v8": "^1.2.2",
"@vitest/coverage-v8": "^1.6.0",
"builtin-modules": "3.3.0",
"esbuild": "0.20.0",
"eslint": "^8.56.0",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"obsidian": "^1.4.11",
"obsidian": "^1.6.6",
"prettier": "^3.3.3",
"tslib": "2.6.2",
"typescript": "5.3.3",
"vitest": "^1.2.2"
Expand Down
107 changes: 34 additions & 73 deletions src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
/**
* Removes all whitespaces before and after |
*
* @param string string
* @returns string
*/
export function trimSeperatorSpaces(string: string): string {
return string.replace(/([^\S\r\n]*[|][^\S\r\n]*)/g, "|");
}
import {collectAllKeys, flattenStructure, processRow} from "./json.utils";
import {
createDataRow,
createHeaderRow,
createSeparatorRow,
getRowContent,
getTableLines,
parseHeader,
removeDuplicateWhitespaces,
trimSeperatorSpaces
} from "./md.utils";

/**
* Search all keys in json and return as string array
* Convert JSON string to Markdown table
*
* @param input []
* @returns string
*/
export function collectAllKeys(input: unknown[]): string[] {
const keys: string[] = [];

for (const obj of input) {
const jsonObject = obj as {[key: string]: never};
for (const key in jsonObject) {
if (jsonObject.hasOwnProperty(key) && !keys.includes(key)) {
keys.push(key);
}
}
}

return keys;
}

/**
* Convert json string to markdown table
*
* @param content sring
* @returns string
* @param {string} content
* @returns {string}
*/
export function jsonToTable(content: string): string {
const jsonData = JSON.parse(content);
Expand All @@ -42,65 +23,45 @@ export function jsonToTable(content: string): string {
return "";
}

// create header and separators
const headers = collectAllKeys(jsonData);
const headerRow = `| ${headers.join(" | ")} |`;
const separatorRow = `| ${headers.map(() => "---").join(" | ")} |`;
const flatData = jsonData.map((data: string[]) => flattenStructure(data));

// create table body
const dataRows: string[] = jsonData.map(
(data: {[key: string]: unknown}) => {
return `| ${headers.map((header) => data[header]).join(" | ")} |`;
}
const headers = collectAllKeys(flatData);
const headerRow = createHeaderRow(headers);
const separatorRow = createSeparatorRow(headers);
const dataRows = flatData.map((data: {[key: string]: unknown}) =>
createDataRow(data, headers)
);

// make table array and removed dupplicate whitespaces
const markdownTable = [
headerRow,
separatorRow,
...dataRows.map((row: string) => row.replace(/( {2,})/g, " "))
...dataRows.map((data: string) => {
return removeDuplicateWhitespaces(data);
})
].join("\n");

return markdownTable;
}

/**
* Convert markdown table to json string
* Convert Markdown table to JSON string
*
* @param content string
* @returns Array
* @param {string} content
* @returns {Array}
*/
export function tableToJson(content: string): unknown[] {
const tableObject: unknown[] = [];

// prepare input to work with
export function tableToJson(content: string): Record<string, unknown>[] {
content = trimSeperatorSpaces(content);

// get lines
const lines = content.split("\n").map((line) => line.trim());

// Do not proceed if only header row and/or seperator row are given
const lines = getTableLines(content);
if (lines.length <= 2) {
return tableObject;
return [];
}

// get headers from first line
const headers = lines[0].substring(1, lines[0].length - 1).split("|");

// get content rows (no header row, no separators row)
const headers = getRowContent(lines[0]);
const rows = lines.slice(2);
const parsedHeaders = headers.map(parseHeader);

for (const row of rows) {
//remove leading and trailing |, after split by |
const rowData = row.slice(1, -1).split("|");
const rowObject: {[key: string]: unknown} = {};

for (let i = 0; i < headers.length; i++) {
rowObject[headers[i]] = rowData[i];
}

tableObject.push(rowObject);
}

return tableObject;
return rows
.map((row) => processRow(row, parsedHeaders))
.filter((row) => Object.keys(row).length > 0);
}
Loading

0 comments on commit a92475e

Please sign in to comment.