google-docs-components
Advanced tools
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function matchesName(componentDef, title) { | ||
const matchers = Array.isArray(componentDef.matchName) | ||
? componentDef.matchName | ||
: [componentDef.matchName]; | ||
if (matchers.find((matcher) => matcher == title)) { | ||
const matchers = Array.isArray(componentDef.matchName) ? componentDef.matchName : [componentDef.matchName]; | ||
if (matchers.find(matcher => matcher == title)) { | ||
return true; | ||
@@ -13,5 +11,5 @@ } | ||
function matchesPropOrSlot(componentDef, key) { | ||
const matchesKey = (name) => name.toLowerCase() == key.toLowerCase(); | ||
const matchesKey = name => name.toLowerCase() == key.toLowerCase(); | ||
const foundProp = componentDef.props && Object.keys(componentDef.props).find(matchesKey); | ||
if (foundProp || componentDef.props === "any") { | ||
if (foundProp) { | ||
return { prop: foundProp }; | ||
@@ -62,3 +60,5 @@ } | ||
} | ||
text = text.replace("\n", "").trim(); | ||
text = text | ||
.replace("\n", "") | ||
.trim(); | ||
return text; | ||
@@ -78,5 +78,3 @@ } | ||
if (tryToParse === false) { | ||
return { | ||
errorMessage: "must consist of a single line with uniform styling.", | ||
}; | ||
return { errorMessage: "must consist of a single line with uniform styling." }; | ||
} | ||
@@ -88,3 +86,3 @@ return { text: tryToParse }; | ||
function findComponent(table, componentDefs) { | ||
if (table.rows.some((row) => row.length > 2)) { | ||
if (table.rows.some(row => row.length > 2)) { | ||
return { error: "A row in the table has more than two entries" }; | ||
@@ -98,6 +96,6 @@ } | ||
const title = verifyTitle.text; | ||
const matchingDef = componentDefs.find((def) => matchesName(def, title)); | ||
const matchingDef = componentDefs.find(def => matchesName(def, title)); | ||
if (matchingDef) | ||
return { | ||
component: matchingDef, | ||
component: matchingDef | ||
}; | ||
@@ -107,4 +105,4 @@ if (table.rows.length == 1) { | ||
devSlot: { | ||
slot: title, | ||
}, | ||
slot: title | ||
} | ||
}; | ||
@@ -115,12 +113,7 @@ } | ||
function default_1(componentDefs, inputTable, parseContent, defaultToTable = true, classProp = false) { | ||
var _a, _b, _c, _d; | ||
var _a, _b; | ||
let matchingDef; | ||
const [componentNotFoundError, invalidPropError, tableFormatError, componentError,] = [ | ||
"ComponentNotFoundError", | ||
"InvalidPropError", | ||
"TableFormatError", | ||
"ComponentError", | ||
].map((error) => (message) => { | ||
return Object.assign({ error, | ||
message }, (matchingDef && { parsingAs: matchingDef.componentName })); | ||
const [componentNotFoundError, invalidPropError, tableFormatError, componentError] = ["ComponentNotFoundError", "InvalidPropError", "TableFormatError", "ComponentError"] | ||
.map((error) => (message) => { | ||
return Object.assign({ error, message }, (matchingDef && { parsingAs: matchingDef.componentName })); | ||
}); | ||
@@ -132,6 +125,6 @@ const table = Object.assign({}, inputTable); | ||
const propName = typeof classProp === "string" ? classProp : "class"; | ||
const isClassPropRow = (row) => { | ||
if (row.length != 2 || | ||
row.some((index) => index < 0) || | ||
row[1] - row[0] != 1) { | ||
const isClassPropRow = row => { | ||
if (row.length != 2 | ||
|| row.some(index => index < 0) | ||
|| row[1] - row[0] != 1) { | ||
return false; | ||
@@ -152,11 +145,5 @@ } | ||
const cellsRemoved = 2; | ||
const decrementedIndices = (row) => row.map((index) => index - cellsRemoved); | ||
table.rows = [ | ||
...table.rows.slice(0, foundRowIndex), | ||
...table.rows.slice(foundRowIndex + 1).map(decrementedIndices), | ||
]; | ||
table.cells = [ | ||
...table.cells.slice(0, foundRow[0]), | ||
...table.cells.slice(foundRow[0] + cellsRemoved), | ||
]; | ||
const decrementedIndices = row => row.map(index => index - cellsRemoved); | ||
table.rows = [...table.rows.slice(0, foundRowIndex), ...table.rows.slice(foundRowIndex + 1).map(decrementedIndices)]; | ||
table.cells = [...table.cells.slice(0, foundRow[0]), ...table.cells.slice(foundRow[0] + cellsRemoved)]; | ||
} | ||
@@ -177,4 +164,4 @@ } | ||
const returnData = Object.assign({ component: matchingDef.componentName }, (className && { className })); | ||
const isSlot = (row) => row.length == 1 || row[0] == row[1]; | ||
const defaultSlotIndex = table.rows.findIndex((row, index) => index > 0 && isSlot(row)); | ||
const isSlot = row => (row.length == 1 || row[0] == row[1]); | ||
const defaultSlotIndex = table.rows.findIndex((row, index) => (index > 0) && isSlot(row)); | ||
if (defaultSlotIndex > -1) { | ||
@@ -185,8 +172,8 @@ if (defaultSlotIndex != table.rows.length - 1) | ||
returnData.slots = { | ||
default: parseContent(table.cells[cellIndex]), | ||
default: parseContent(table.cells[cellIndex]) | ||
}; | ||
} | ||
const requiredProps = new Set(); | ||
if (typeof matchingDef.props === "object") { | ||
Object.keys(matchingDef.props).forEach((prop) => { | ||
if (matchingDef.props) { | ||
Object.keys(matchingDef.props).forEach(prop => { | ||
if (matchingDef.props[prop].required) { | ||
@@ -209,4 +196,3 @@ requiredProps.add(prop); | ||
if (!matches) { | ||
return invalidPropError(`Key ${keyString} on row #${i + | ||
2} doesn't match a prop or slot on component ${matchingDef.componentName}`); | ||
return invalidPropError(`Key ${keyString} on row #${i + 2} doesn't match a prop or slot on component ${matchingDef.componentName}`); | ||
} | ||
@@ -232,3 +218,3 @@ const valueCell = table.cells[row[1]]; | ||
let value = verifyValue.text; | ||
if (((_d = (_c = matchingDef.props) === null || _c === void 0 ? void 0 : _c[matches.prop]) === null || _d === void 0 ? void 0 : _d.type) == "number") { | ||
if (matchingDef.props[matches.prop].type == "number") { | ||
value = Number(value); | ||
@@ -235,0 +221,0 @@ } |
@@ -37,3 +37,3 @@ export declare type ContentData = ElementData | ComponentData | TableData | DevSlotData; | ||
[propName: string]: { | ||
type: "string" | "number"; | ||
type: 'string' | 'number'; | ||
required?: boolean; | ||
@@ -53,3 +53,3 @@ }; | ||
componentName: string; | ||
props?: "any" | PropsDef; | ||
props?: PropsDef; | ||
slots?: "any" | SlotsDef; | ||
@@ -56,0 +56,0 @@ }; |
function matchesName(componentDef, title) { | ||
const matchers = Array.isArray(componentDef.matchName) | ||
? componentDef.matchName | ||
: [componentDef.matchName]; | ||
if (matchers.find((matcher) => matcher == title)) { | ||
const matchers = Array.isArray(componentDef.matchName) ? componentDef.matchName : [componentDef.matchName]; | ||
if (matchers.find(matcher => matcher == title)) { | ||
return true; | ||
@@ -11,5 +9,5 @@ } | ||
function matchesPropOrSlot(componentDef, key) { | ||
const matchesKey = (name) => name.toLowerCase() == key.toLowerCase(); | ||
const matchesKey = name => name.toLowerCase() == key.toLowerCase(); | ||
const foundProp = componentDef.props && Object.keys(componentDef.props).find(matchesKey); | ||
if (foundProp || componentDef.props === "any") { | ||
if (foundProp) { | ||
return { prop: foundProp }; | ||
@@ -60,3 +58,5 @@ } | ||
} | ||
text = text.replace("\n", "").trim(); | ||
text = text | ||
.replace("\n", "") | ||
.trim(); | ||
return text; | ||
@@ -76,5 +76,3 @@ } | ||
if (tryToParse === false) { | ||
return { | ||
errorMessage: "must consist of a single line with uniform styling.", | ||
}; | ||
return { errorMessage: "must consist of a single line with uniform styling." }; | ||
} | ||
@@ -86,3 +84,3 @@ return { text: tryToParse }; | ||
function findComponent(table, componentDefs) { | ||
if (table.rows.some((row) => row.length > 2)) { | ||
if (table.rows.some(row => row.length > 2)) { | ||
return { error: "A row in the table has more than two entries" }; | ||
@@ -96,6 +94,6 @@ } | ||
const title = verifyTitle.text; | ||
const matchingDef = componentDefs.find((def) => matchesName(def, title)); | ||
const matchingDef = componentDefs.find(def => matchesName(def, title)); | ||
if (matchingDef) | ||
return { | ||
component: matchingDef, | ||
component: matchingDef | ||
}; | ||
@@ -105,4 +103,4 @@ if (table.rows.length == 1) { | ||
devSlot: { | ||
slot: title, | ||
}, | ||
slot: title | ||
} | ||
}; | ||
@@ -113,12 +111,7 @@ } | ||
export default function (componentDefs, inputTable, parseContent, defaultToTable = true, classProp = false) { | ||
var _a, _b, _c, _d; | ||
var _a, _b; | ||
let matchingDef; | ||
const [componentNotFoundError, invalidPropError, tableFormatError, componentError,] = [ | ||
"ComponentNotFoundError", | ||
"InvalidPropError", | ||
"TableFormatError", | ||
"ComponentError", | ||
].map((error) => (message) => { | ||
return Object.assign({ error, | ||
message }, (matchingDef && { parsingAs: matchingDef.componentName })); | ||
const [componentNotFoundError, invalidPropError, tableFormatError, componentError] = ["ComponentNotFoundError", "InvalidPropError", "TableFormatError", "ComponentError"] | ||
.map((error) => (message) => { | ||
return Object.assign({ error, message }, (matchingDef && { parsingAs: matchingDef.componentName })); | ||
}); | ||
@@ -130,6 +123,6 @@ const table = Object.assign({}, inputTable); | ||
const propName = typeof classProp === "string" ? classProp : "class"; | ||
const isClassPropRow = (row) => { | ||
if (row.length != 2 || | ||
row.some((index) => index < 0) || | ||
row[1] - row[0] != 1) { | ||
const isClassPropRow = row => { | ||
if (row.length != 2 | ||
|| row.some(index => index < 0) | ||
|| row[1] - row[0] != 1) { | ||
return false; | ||
@@ -150,11 +143,5 @@ } | ||
const cellsRemoved = 2; | ||
const decrementedIndices = (row) => row.map((index) => index - cellsRemoved); | ||
table.rows = [ | ||
...table.rows.slice(0, foundRowIndex), | ||
...table.rows.slice(foundRowIndex + 1).map(decrementedIndices), | ||
]; | ||
table.cells = [ | ||
...table.cells.slice(0, foundRow[0]), | ||
...table.cells.slice(foundRow[0] + cellsRemoved), | ||
]; | ||
const decrementedIndices = row => row.map(index => index - cellsRemoved); | ||
table.rows = [...table.rows.slice(0, foundRowIndex), ...table.rows.slice(foundRowIndex + 1).map(decrementedIndices)]; | ||
table.cells = [...table.cells.slice(0, foundRow[0]), ...table.cells.slice(foundRow[0] + cellsRemoved)]; | ||
} | ||
@@ -175,4 +162,4 @@ } | ||
const returnData = Object.assign({ component: matchingDef.componentName }, (className && { className })); | ||
const isSlot = (row) => row.length == 1 || row[0] == row[1]; | ||
const defaultSlotIndex = table.rows.findIndex((row, index) => index > 0 && isSlot(row)); | ||
const isSlot = row => (row.length == 1 || row[0] == row[1]); | ||
const defaultSlotIndex = table.rows.findIndex((row, index) => (index > 0) && isSlot(row)); | ||
if (defaultSlotIndex > -1) { | ||
@@ -183,8 +170,8 @@ if (defaultSlotIndex != table.rows.length - 1) | ||
returnData.slots = { | ||
default: parseContent(table.cells[cellIndex]), | ||
default: parseContent(table.cells[cellIndex]) | ||
}; | ||
} | ||
const requiredProps = new Set(); | ||
if (typeof matchingDef.props === "object") { | ||
Object.keys(matchingDef.props).forEach((prop) => { | ||
if (matchingDef.props) { | ||
Object.keys(matchingDef.props).forEach(prop => { | ||
if (matchingDef.props[prop].required) { | ||
@@ -207,4 +194,3 @@ requiredProps.add(prop); | ||
if (!matches) { | ||
return invalidPropError(`Key ${keyString} on row #${i + | ||
2} doesn't match a prop or slot on component ${matchingDef.componentName}`); | ||
return invalidPropError(`Key ${keyString} on row #${i + 2} doesn't match a prop or slot on component ${matchingDef.componentName}`); | ||
} | ||
@@ -230,3 +216,3 @@ const valueCell = table.cells[row[1]]; | ||
let value = verifyValue.text; | ||
if (((_d = (_c = matchingDef.props) === null || _c === void 0 ? void 0 : _c[matches.prop]) === null || _d === void 0 ? void 0 : _d.type) == "number") { | ||
if (matchingDef.props[matches.prop].type == "number") { | ||
value = Number(value); | ||
@@ -233,0 +219,0 @@ } |
@@ -37,3 +37,3 @@ export declare type ContentData = ElementData | ComponentData | TableData | DevSlotData; | ||
[propName: string]: { | ||
type: "string" | "number"; | ||
type: 'string' | 'number'; | ||
required?: boolean; | ||
@@ -53,3 +53,3 @@ }; | ||
componentName: string; | ||
props?: "any" | PropsDef; | ||
props?: PropsDef; | ||
slots?: "any" | SlotsDef; | ||
@@ -56,0 +56,0 @@ }; |
{ | ||
"name": "google-docs-components", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"main": "./dist/cjs/index.js", | ||
@@ -5,0 +5,0 @@ "module": "./dist/esm/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
73574
-1.29%1044
-2.61%