Skip to content

Commit

Permalink
feat: add support to table expression translation
Browse files Browse the repository at this point in the history
  • Loading branch information
elmouradiaminedev committed Feb 11, 2024
1 parent 86d1bd4 commit 559c5ee
Show file tree
Hide file tree
Showing 11 changed files with 339 additions and 50 deletions.
57 changes: 46 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ import {
math,
MathTranslationOptions,
MathTranslationStyle,
MATH_TRANSLATION_STYLES,
} from "./translators/math";
import { sequence, SequenceTranslationOptions } from "./translators/sequence";
import { TranslationTool } from "./lib/internal";
import {
tree,
TreeTranslationOptions,
TreeTranslationStyle,
TREE_TRANSLATION_STYLES,
} from "./translators/tree";

export type {
MathTranslationOptions,
MathTranslationStyle,
SequenceTranslationOptions,
TreeTranslationOptions,
TreeTranslationStyle,
TranslationTool,
};
import {
TableTranslationOptions,
TableTranslationStyle,
TABLE_TRANSLATION_STYLES,
table,
} from "./translators/table";
import { TranslationTool } from "./lib/internal";

type Translator = {
/**
Expand Down Expand Up @@ -68,7 +67,7 @@ type Translator = {
* @param options - Options for tree translation.
* @returns The translated tree expression.
* @example
* const translatedTreeExpression = tree("expression", { style: "Unicode 2" });
* const translatedTreeExpression = tree("Linux\n Android\n Debian\n Ubuntu\n Lubuntu\n Kubuntu\n Xubuntu\n Xubuntu\n Mint\n Centos\n Fedora", { style: "Unicode 2" });
* console.log(translatedTreeExpression)
* // Linux
* // ├──Android
Expand All @@ -83,10 +82,46 @@ type Translator = {
* // └──Fedora
*/
tree: (expression: string, options?: TreeTranslationOptions) => string;
/**
* Translation function for Table expressions.
*
* @param expression - The table expression to be translated.
* @param options - Options for table translation.
* @returns The translated table expression.
* @example
* const translatedTableExpression = tree("Column 1,Column 2,Column 3\nC++,Web,Assembly\nJavascript,CSS,HTML", { style: "Unicode double" });
* console.log(translatedTableExpression)
* // ╔══════════╦════════╦════════╗
* // ║Column 1 ║Column 2║Column 3║
* // ╠══════════╬════════╬════════╣
* // ║C++ ║Web ║Assembly║
* // ╠══════════╬════════╬════════╣
* // ║Javascript║CSS ║HTML ║
* // ╚══════════╩════════╩════════╝
*/
table: (expression: string, options?: TableTranslationOptions) => string;
};

export const translate: Translator = {
math,
sequence,
tree,
table,
};

export type {
MathTranslationOptions,
MathTranslationStyle,
SequenceTranslationOptions,
TreeTranslationOptions,
TreeTranslationStyle,
TableTranslationOptions,
TableTranslationStyle,
TranslationTool,
};

export {
MATH_TRANSLATION_STYLES,
TREE_TRANSLATION_STYLES,
TABLE_TRANSLATION_STYLES,
};
2 changes: 1 addition & 1 deletion src/lib/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { camelToUnderscore } from "../utils";
/**
* Represents the translation tool.
*/
export type TranslationTool = "Math" | "Sequence" | "Tree";
export type TranslationTool = "Math" | "Sequence" | "Tree" | "Table";

/**
* Represents the type of translation function.
Expand Down
4 changes: 3 additions & 1 deletion src/translators/math.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createTranslationFunction } from "../lib/internal";

export const MATH_TRANSLATION_STYLES = ["Unicode", "ASCII", "Latex"] as const;

/**
* Represents the style of mathematical expression translation.
*/
export type MathTranslationStyle = "Unicode" | "ASCII" | "Latex";
export type MathTranslationStyle = (typeof MATH_TRANSLATION_STYLES)[number];

/**
* Options for mathematical expression translation.
Expand Down
37 changes: 37 additions & 0 deletions src/translators/table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createTranslationFunction } from "../lib/internal";

export const TABLE_TRANSLATION_STYLES = [
"unicode",
"unicode bold",
"unicode rounded",
"unicode double",
"unicode with bold header",
"unicode with double header",
"unicode cells",
"unicode cells 2",
"ascii",
"ascii rounded",
"ascii with header 1",
"ascii with header 2",
"ascii light header",
"ascii light header/separator",
"ascii light header/separator/border",
"ascii light separator/border",
"ascii light border",
"conceptual",
] as const;

/**
* Represents the style of a table expression translation.
*/
export type TableTranslationStyle = (typeof TABLE_TRANSLATION_STYLES)[number];

/**
* Options for table expression translation.
*/
export type TableTranslationOptions = {
style?: TableTranslationStyle;
};

export const table =
createTranslationFunction<TableTranslationOptions>("Table");
21 changes: 12 additions & 9 deletions src/translators/tree.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { createTranslationFunction } from "../lib/internal";

export const TREE_TRANSLATION_STYLES = [
"unicode 1",
"unicode 2",
"ASCII 1",
"ASCII 2",
"ASCII 3",
"unicode right top",
"unicode right center",
"unicode right bottom",
] as const;

/**
* Represents the possible styles for tree translation.
*/
export type TreeTranslationStyle =
| "Unicode 1"
| "Unicode 2"
| "ASCII 1"
| "ASCII 2"
| "ASCII 3"
| "Unicode right top"
| "Unicode right center"
| "Unicode right bottom";
export type TreeTranslationStyle = (typeof TREE_TRANSLATION_STYLES)[number];

/**
* Options for tree expression translation.
Expand Down
194 changes: 194 additions & 0 deletions tests/__snapshots__/table.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Table expression translation > With options {"style":"ascii light border"} > should translate a simple table 1`] = `
"+----------------------------+
|Column 1 Column 2 Column 3|
|C++ Web Assembly|
|Javascript CSS HTML |
+----------------------------+
"
`;

exports[`Table expression translation > With options {"style":"ascii light header"} > should translate a simple table 1`] = `
"Column 1 Column 2 Column 3
---------- -------- --------
C++ Web Assembly
Javascript CSS HTML
"
`;

exports[`Table expression translation > With options {"style":"ascii light header/separator"} > should translate a simple table 1`] = `
"Column 1 |Column 2|Column 3
----------|--------|--------
C++ |Web |Assembly
Javascript|CSS |HTML
"
`;

exports[`Table expression translation > With options {"style":"ascii light header/separator/border"} > should translate a simple table 1`] = `
"+----------+--------+--------+
|Column 1 |Column 2|Column 3|
+----------+--------+--------|
|C++ |Web |Assembly|
|Javascript|CSS |HTML |
+----------+--------+--------+
"
`;

exports[`Table expression translation > With options {"style":"ascii light separator/border"} > should translate a simple table 1`] = `
"+----------+--------+--------+
|Column 1 |Column 2|Column 3|
|C++ |Web |Assembly|
|Javascript|CSS |HTML |
+----------+--------+--------+
"
`;

exports[`Table expression translation > With options {"style":"ascii rounded"} > should translate a simple table 1`] = `
".----------+--------+--------.
|Column 1 |Column 2|Column 3|
|----------+--------+--------|
|C++ |Web |Assembly|
|----------+--------+--------|
|Javascript|CSS |HTML |
'----------+--------+--------'
"
`;

exports[`Table expression translation > With options {"style":"ascii with header 1"} > should translate a simple table 1`] = `
"|==============================|
| Column 1 |Column 2|Column 3 |
|==============================|
|C++ |Web |Assembly|
+----------+--------+--------+
|Javascript|CSS |HTML |
+----------+--------+--------+
"
`;

exports[`Table expression translation > With options {"style":"ascii with header 2"} > should translate a simple table 1`] = `
"==============================
|Column 1 |Column 2|Column 3|
==============================
|C++ |Web |Assembly|
+----------+--------+--------+
|Javascript|CSS |HTML |
+----------+--------+--------+
"
`;

exports[`Table expression translation > With options {"style":"ascii"} > should translate a simple table 1`] = `
"+----------+--------+--------+
|Column 1 |Column 2|Column 3|
+----------+--------+--------+
|C++ |Web |Assembly|
+----------+--------+--------+
|Javascript|CSS |HTML |
+----------+--------+--------+
"
`;

exports[`Table expression translation > With options {"style":"conceptual"} > should translate a simple table 1`] = `
" __________ ________ ________
/Column 1 \\/Column 2\\/Column 3\\
\\__________/\\________/\\________/
/C++ \\/Web \\/Assembly\\
\\__________/\\________/\\________/
/Javascript\\/CSS \\/HTML \\
\\__________/\\________/\\________/
"
`;

exports[`Table expression translation > With options {"style":"unicode bold"} > should translate a simple table 1`] = `
"┏━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━┓
┃Column 1 ┃Column 2┃Column 3┃
┣━━━━━━━━━━╋━━━━━━━━╋━━━━━━━━┫
┃C++ ┃Web ┃Assembly┃
┣━━━━━━━━━━╋━━━━━━━━╋━━━━━━━━┫
┃Javascript┃CSS ┃HTML ┃
┗━━━━━━━━━━┻━━━━━━━━┻━━━━━━━━┛
"
`;

exports[`Table expression translation > With options {"style":"unicode cells 2"} > should translate a simple table 1`] = `
"╭────────────────────────────────╮
│╭──────────╮╭────────╮╭────────╮│
││Column 1 ││Column 2││Column 3││
│╰──────────╯╰────────╯╰────────╯│
│╭──────────╮╭────────╮╭────────╮│
││C++ ││Web ││Assembly││
│╰──────────╯╰────────╯╰────────╯│
│╭──────────╮╭────────╮╭────────╮│
││Javascript││CSS ││HTML ││
│╰──────────╯╰────────╯╰────────╯│
╰────────────────────────────────╯
"
`;

exports[`Table expression translation > With options {"style":"unicode cells"} > should translate a simple table 1`] = `
"╭──────────╮╭────────╮╭────────╮
│Column 1 ││Column 2││Column 3│
╰──────────╯╰────────╯╰────────╯
╭──────────╮╭────────╮╭────────╮
│C++ ││Web ││Assembly│
╰──────────╯╰────────╯╰────────╯
╭──────────╮╭────────╮╭────────╮
│Javascript││CSS ││HTML │
╰──────────╯╰────────╯╰────────╯
"
`;

exports[`Table expression translation > With options {"style":"unicode double"} > should translate a simple table 1`] = `
"╔══════════╦════════╦════════╗
║Column 1 ║Column 2║Column 3║
╠══════════╬════════╬════════╣
║C++ ║Web ║Assembly║
╠══════════╬════════╬════════╣
║Javascript║CSS ║HTML ║
╚══════════╩════════╩════════╝
"
`;

exports[`Table expression translation > With options {"style":"unicode rounded"} > should translate a simple table 1`] = `
"╭──────────┬────────┬────────╮
│Column 1 │Column 2│Column 3│
├──────────┼────────┼────────┤
│C++ │Web │Assembly│
├──────────┼────────┼────────┤
│Javascript│CSS │HTML │
╰──────────┴────────┴────────╯
"
`;

exports[`Table expression translation > With options {"style":"unicode with bold header"} > should translate a simple table 1`] = `
"┏━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━┓
┃Column 1 ┃Column 2┃Column 3┃
┡━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━┩
│C++ │Web │Assembly│
├──────────┼────────┼────────┤
│Javascript│CSS │HTML │
└──────────┴────────┴────────┘
"
`;

exports[`Table expression translation > With options {"style":"unicode with double header"} > should translate a simple table 1`] = `
"╒══════════╤════════╤════════╕
│Column 1 │Column 2│Column 3│
╞══════════╪════════╪════════╡
│C++ │Web │Assembly│
├──────────┼────────┼────────┤
│Javascript│CSS │HTML │
└──────────┴────────┴────────┘
"
`;

exports[`Table expression translation > With options {"style":"unicode"} > should translate a simple table 1`] = `
"┌──────────┬────────┬────────┐
│Column 1 │Column 2│Column 3│
├──────────┼────────┼────────┤
│C++ │Web │Assembly│
├──────────┼────────┼────────┤
│Javascript│CSS │HTML │
└──────────┴────────┴────────┘
"
`;
12 changes: 6 additions & 6 deletions tests/math.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { expect, describe, it } from "vitest";
import Diagon, { MathTranslationStyle, MathTranslationOptions } from "../dist";
import Diagon, {
MATH_TRANSLATION_STYLES,
MathTranslationOptions,
} from "../dist";

function generateAllOptions(): MathTranslationOptions[] {
const styles: MathTranslationStyle[] = ["Unicode", "ASCII", "Latex"];
const transformMathLettersOptions: boolean[] = [true, false];

const allOptions: MathTranslationOptions[] = [];

for (const style of styles) {
for (const transformMathLetters of transformMathLettersOptions) {
for (const style of MATH_TRANSLATION_STYLES) {
for (const transformMathLetters of [true, false]) {
allOptions.push({
style,
transformMathLetters,
Expand Down
Loading

0 comments on commit 559c5ee

Please sign in to comment.