Skip to content

Commit

Permalink
add some guards/sanitization for codegen of typed output
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Sep 5, 2023
1 parent bcaf1e3 commit 4e7472a
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 37 deletions.
21 changes: 12 additions & 9 deletions dist/uDSV.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,26 @@ function guessType(ci, rows) {
return t;
}

const toJSON = JSON.stringify;
const onlyStrEsc = v => typeof v === 'string' ? toJSON(v) : v;

function getValParseExpr(ci, col) {
let { type } = col;

let rv = `r[${ci}]`;

let parseExpr =
type === T_DATE ? `new Date(${rv})` :
type === T_JSON ? `JSON.parse(${rv})` :
type === T_NUMBER ? `+${rv}` :
type[0] === T_BOOLEAN ? `${rv} === '${type.slice(2)}' ? true : false` :
type === T_DATE ? `new Date(${rv})` :
type === T_JSON ? `JSON.parse(${rv})` :
type === T_NUMBER ? `+${rv}` :
type[0] === T_BOOLEAN ? `${rv} === ${toJSON(type.slice(2))} ? true : false` :
rv;

let { repl } = col;

let nanExpr = repl.NaN !== void 0 && type === T_NUMBER ? `${rv} === 'NaN' ? ${repl.NaN} : ` : '';
let nullExpr = repl.null !== void 0 ? `${rv} === 'null' || ${rv} === 'NULL' ? ${repl.null} : ` : '';
let emptyExpr = repl.empty !== void 0 ? `${rv} === '' ? ${repl.empty} : ` : '';
let nanExpr = repl.NaN !== void 0 && type === T_NUMBER ? `${rv} === 'NaN' ? ${onlyStrEsc(repl.NaN)} : ` : '';
let nullExpr = repl.null !== void 0 ? `${rv} === 'null' || ${rv} === 'NULL' ? ${onlyStrEsc(repl.null)} : ` : '';
let emptyExpr = repl.empty !== void 0 ? `${rv} === '' ? ${onlyStrEsc(repl.empty)} : ` : '';

return `${emptyExpr} ${nullExpr} ${nanExpr} ${parseExpr}`;
}
Expand Down Expand Up @@ -137,7 +140,7 @@ function genToTypedRows(cols, objs = false, deep = false) {
colIdx++;
} while (paths.length > 0);

buf = JSON.stringify(tplObj).replace(/"¦(\d+)¦"/g, (m, ci) => getValParseExpr(+ci, cols[+ci]));
buf = toJSON(tplObj).replace(/"¦(\d+)¦"/g, (m, ci) => getValParseExpr(+ci, cols[+ci]));
}
else {
if (!objs && cols.every(c => c.type === T_STRING))
Expand All @@ -146,7 +149,7 @@ function genToTypedRows(cols, objs = false, deep = false) {
buf = objs ? '{' : '[';

cols.forEach((col, ci) => {
buf += objs ? `"${col.name.replaceAll('"', '\\"')}":` : '';
buf += objs ? `${toJSON(col.name)}:` : '';
let parseVal = getValParseExpr(ci, col);
buf += `${parseVal},`;
});
Expand Down
21 changes: 12 additions & 9 deletions dist/uDSV.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,26 @@ var uDSV = (function (exports) {
return t;
}

const toJSON = JSON.stringify;
const onlyStrEsc = v => typeof v === 'string' ? toJSON(v) : v;

function getValParseExpr(ci, col) {
let { type } = col;

let rv = `r[${ci}]`;

let parseExpr =
type === T_DATE ? `new Date(${rv})` :
type === T_JSON ? `JSON.parse(${rv})` :
type === T_NUMBER ? `+${rv}` :
type[0] === T_BOOLEAN ? `${rv} === '${type.slice(2)}' ? true : false` :
type === T_DATE ? `new Date(${rv})` :
type === T_JSON ? `JSON.parse(${rv})` :
type === T_NUMBER ? `+${rv}` :
type[0] === T_BOOLEAN ? `${rv} === ${toJSON(type.slice(2))} ? true : false` :
rv;

let { repl } = col;

let nanExpr = repl.NaN !== void 0 && type === T_NUMBER ? `${rv} === 'NaN' ? ${repl.NaN} : ` : '';
let nullExpr = repl.null !== void 0 ? `${rv} === 'null' || ${rv} === 'NULL' ? ${repl.null} : ` : '';
let emptyExpr = repl.empty !== void 0 ? `${rv} === '' ? ${repl.empty} : ` : '';
let nanExpr = repl.NaN !== void 0 && type === T_NUMBER ? `${rv} === 'NaN' ? ${onlyStrEsc(repl.NaN)} : ` : '';
let nullExpr = repl.null !== void 0 ? `${rv} === 'null' || ${rv} === 'NULL' ? ${onlyStrEsc(repl.null)} : ` : '';
let emptyExpr = repl.empty !== void 0 ? `${rv} === '' ? ${onlyStrEsc(repl.empty)} : ` : '';

return `${emptyExpr} ${nullExpr} ${nanExpr} ${parseExpr}`;
}
Expand Down Expand Up @@ -138,7 +141,7 @@ var uDSV = (function (exports) {
colIdx++;
} while (paths.length > 0);

buf = JSON.stringify(tplObj).replace(/"¦(\d+)¦"/g, (m, ci) => getValParseExpr(+ci, cols[+ci]));
buf = toJSON(tplObj).replace(/"¦(\d+)¦"/g, (m, ci) => getValParseExpr(+ci, cols[+ci]));
}
else {
if (!objs && cols.every(c => c.type === T_STRING))
Expand All @@ -147,7 +150,7 @@ var uDSV = (function (exports) {
buf = objs ? '{' : '[';

cols.forEach((col, ci) => {
buf += objs ? `"${col.name.replaceAll('"', '\\"')}":` : '';
buf += objs ? `${toJSON(col.name)}:` : '';
let parseVal = getValParseExpr(ci, col);
buf += `${parseVal},`;
});
Expand Down
2 changes: 1 addition & 1 deletion dist/uDSV.iife.min.js

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

21 changes: 12 additions & 9 deletions dist/uDSV.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,26 @@ function guessType(ci, rows) {
return t;
}

const toJSON = JSON.stringify;
const onlyStrEsc = v => typeof v === 'string' ? toJSON(v) : v;

function getValParseExpr(ci, col) {
let { type } = col;

let rv = `r[${ci}]`;

let parseExpr =
type === T_DATE ? `new Date(${rv})` :
type === T_JSON ? `JSON.parse(${rv})` :
type === T_NUMBER ? `+${rv}` :
type[0] === T_BOOLEAN ? `${rv} === '${type.slice(2)}' ? true : false` :
type === T_DATE ? `new Date(${rv})` :
type === T_JSON ? `JSON.parse(${rv})` :
type === T_NUMBER ? `+${rv}` :
type[0] === T_BOOLEAN ? `${rv} === ${toJSON(type.slice(2))} ? true : false` :
rv;

let { repl } = col;

let nanExpr = repl.NaN !== void 0 && type === T_NUMBER ? `${rv} === 'NaN' ? ${repl.NaN} : ` : '';
let nullExpr = repl.null !== void 0 ? `${rv} === 'null' || ${rv} === 'NULL' ? ${repl.null} : ` : '';
let emptyExpr = repl.empty !== void 0 ? `${rv} === '' ? ${repl.empty} : ` : '';
let nanExpr = repl.NaN !== void 0 && type === T_NUMBER ? `${rv} === 'NaN' ? ${onlyStrEsc(repl.NaN)} : ` : '';
let nullExpr = repl.null !== void 0 ? `${rv} === 'null' || ${rv} === 'NULL' ? ${onlyStrEsc(repl.null)} : ` : '';
let emptyExpr = repl.empty !== void 0 ? `${rv} === '' ? ${onlyStrEsc(repl.empty)} : ` : '';

return `${emptyExpr} ${nullExpr} ${nanExpr} ${parseExpr}`;
}
Expand Down Expand Up @@ -135,7 +138,7 @@ function genToTypedRows(cols, objs = false, deep = false) {
colIdx++;
} while (paths.length > 0);

buf = JSON.stringify(tplObj).replace(/"¦(\d+)¦"/g, (m, ci) => getValParseExpr(+ci, cols[+ci]));
buf = toJSON(tplObj).replace(/"¦(\d+)¦"/g, (m, ci) => getValParseExpr(+ci, cols[+ci]));
}
else {
if (!objs && cols.every(c => c.type === T_STRING))
Expand All @@ -144,7 +147,7 @@ function genToTypedRows(cols, objs = false, deep = false) {
buf = objs ? '{' : '[';

cols.forEach((col, ci) => {
buf += objs ? `"${col.name.replaceAll('"', '\\"')}":` : '';
buf += objs ? `${toJSON(col.name)}:` : '';
let parseVal = getValParseExpr(ci, col);
buf += `${parseVal},`;
});
Expand Down
Loading

0 comments on commit 4e7472a

Please sign in to comment.