Skip to content

Commit

Permalink
fix: use column index instead of unique id
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin-Alexa Robinson committed Jul 23, 2024
1 parent 0199e26 commit a4c329f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 61 deletions.
10 changes: 0 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"eslint-config-prettier": "8.8.0",
"eslint-plugin-jest": "27.2.1",
"eslint-plugin-prettier": "4.2.1",
"hexoid": "^1.0.0",
"husky": "8.0.3",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import hexoid from "hexoid";
import {
JSON,
SliceAnnotation,
Expand All @@ -14,8 +13,6 @@ import OffsetSource, {
TextAlignment,
} from "../index";

const generateId = hexoid(8);

function extractPlainContents(
doc: OffsetSource,
annotation: { start: number; end: number }
Expand Down Expand Up @@ -93,14 +90,17 @@ function extractAlignment(tableCell: Annotation<{ style: string }>) {
return undefined;
}

function generateFieldName(
columnName: string | null,
index: number,
tableIdentifier: string
) {
/**
* generates a unique field name for each column, filling in blank or missing column names with "column \<index\>"
* and then appending (\<index\>) to the name in order to ensure there are no duplicates.
* @param columnName the human-readable column name derived from the rich text contents of the table
* @param index the index of the column in the table
* @returns the unique column name as a string
*/
function generateFieldName(columnName: string | null, index: number) {
return (
(columnName?.length ? columnName : `column ${index + 1}`) +
` (${tableIdentifier}-${generateId()})`
` (${index + 1})`
);
}

Expand All @@ -113,7 +113,6 @@ export function convertHTMLTablesToDataSet(
if (isInvalidTable(doc, table, vendor)) {
return;
}
let tableIdentifier = generateId();
let dataSetSchemaEntries: [name: string, type: ColumnType][] = [];
let columnConfigs: Exclude<Table["attributes"]["columns"], undefined> = [];
let dataRows: Record<string, { slice: string; jsonValue: JSON }>[] = [];
Expand Down Expand Up @@ -145,7 +144,7 @@ export function convertHTMLTablesToDataSet(
});
doc.replaceAnnotation(headCell, slice);
let columnName = extractPlainContents(doc, slice);
let fieldName = generateFieldName(columnName, index, tableIdentifier);
let fieldName = generateFieldName(columnName, index);

dataSetSchemaEntries.push([fieldName, ColumnType.RICH_TEXT]);

Expand Down Expand Up @@ -198,7 +197,7 @@ export function convertHTMLTablesToDataSet(
}

if (!hasColumnHeaders) {
let columnName = generateFieldName(null, index, tableIdentifier);
let columnName = generateFieldName(null, index);

let columnConfig: (typeof columnConfigs)[number] = {
name: columnName,
Expand Down
19 changes: 0 additions & 19 deletions packages/@atjson/source-commonmark/test/extensions.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
jest.mock("hexoid", () => {
let ticket = 0;
function hexoid() {
return () => `mock_uid_${ticket++}`;
}

hexoid.reset = () => {
ticket = 0;
};

return hexoid;
});

import hexoid from "hexoid";

import {
InlineAnnotation,
getConverterFor,
Expand Down Expand Up @@ -101,10 +86,6 @@ describe("strikethrough", () => {
});

describe("tables", () => {
beforeEach(() => {
(hexoid as unknown as { reset(): void }).reset();
});

const tableExample = `
| name | age | job | [*notes*](https://ja.wikipedia.org/wiki/ダンジョン飯) |
|:-------- | ---:| ------- |:------------------------------------------------------------------------------------------------------------------------------------------------:|
Expand Down
19 changes: 0 additions & 19 deletions packages/@atjson/source-html/test/table.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
jest.mock("hexoid", () => {
let ticket = 0;
function hexoid() {
return () => `mock_uid_${ticket++}`;
}

hexoid.reset = () => {
ticket = 0;
};

return hexoid;
});

import hexoid from "hexoid";

import OffsetSource from "@atjson/offset-annotations";
import HTMLSource from "../src";
import { serialize } from "@atjson/document";

describe("Table", () => {
beforeEach(() => {
(hexoid as unknown as { reset(): void }).reset();
});

describe("valid tables", () => {
test("converts simple tables", () => {
let tableMarkup = `
Expand Down

0 comments on commit a4c329f

Please sign in to comment.