Skip to content

Commit

Permalink
smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Aug 18, 2023
1 parent 30e3426 commit 35684af
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 46 deletions.
29 changes: 18 additions & 11 deletions dist/uDSV.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ function isJSON(v) {
return false;
}

const T_STRING = 's';
const T_DATE = 'd';
const T_NUMBER = 'n';
const T_JSON = 'j';
const T_BOOLEAN = 'b';

function guessType(ci, rows) {
// row with a value to analyze
// trim()?
Expand All @@ -59,16 +65,16 @@ function guessType(ci, rows) {
r[ci] !== 'NaN'
);

let t = 'string';
let t = T_STRING;

if (row != null) {
let v = row[ci];

t = (
ISO8601.test(v) ? 'date' :
!Number.isNaN(Number.parseFloat(v)) ? 'number' :
BOOL_RE.test(v) ? `boolean:${boolTrue(v)}` :
isJSON(v) ? 'json' :
ISO8601.test(v) ? T_DATE :
!Number.isNaN(Number.parseFloat(v)) ? T_NUMBER :
BOOL_RE.test(v) ? `${T_BOOLEAN}:${boolTrue(v)}` :
isJSON(v) ? T_JSON :
t
);
}
Expand All @@ -82,13 +88,13 @@ function getValParseExpr(ci, col) {
let rv = `r[${ci}]`; // trim()?

let parseExpr =
type === 'date' ? `new Date(${rv})` :
type === 'json' ? `JSON.parse(${rv})` :
type === 'number' ? `Number.parseFloat(${rv})` :
type.startsWith('boolean') ? `${rv} === '${type.slice(8)}' ? true : false` :
type === T_DATE ? `new Date(${rv})` :
type === T_JSON ? `JSON.parse(${rv})` :
type === T_NUMBER ? `Number.parseFloat(${rv})` :
type[0] === T_BOOLEAN ? `${rv} === '${type.slice(2)}' ? true : false` :
rv;

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

Expand Down Expand Up @@ -146,11 +152,12 @@ function genToTypedRows(cols, objs = false, deep = false) {
buf += objs ? '}' : ']';
}

// r.trim()?
let fnBody = `
let arr = Array(rows.length);
for (let i = 0; i < rows.length; i++) {
let r = rows[i]; // trim()?
let r = rows[i];
arr[i] = ${buf};
}
Expand Down
29 changes: 18 additions & 11 deletions dist/uDSV.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ var uDSV = (function (exports) {
return false;
}

const T_STRING = 's';
const T_DATE = 'd';
const T_NUMBER = 'n';
const T_JSON = 'j';
const T_BOOLEAN = 'b';

function guessType(ci, rows) {
// row with a value to analyze
// trim()?
Expand All @@ -60,16 +66,16 @@ var uDSV = (function (exports) {
r[ci] !== 'NaN'
);

let t = 'string';
let t = T_STRING;

if (row != null) {
let v = row[ci];

t = (
ISO8601.test(v) ? 'date' :
!Number.isNaN(Number.parseFloat(v)) ? 'number' :
BOOL_RE.test(v) ? `boolean:${boolTrue(v)}` :
isJSON(v) ? 'json' :
ISO8601.test(v) ? T_DATE :
!Number.isNaN(Number.parseFloat(v)) ? T_NUMBER :
BOOL_RE.test(v) ? `${T_BOOLEAN}:${boolTrue(v)}` :
isJSON(v) ? T_JSON :
t
);
}
Expand All @@ -83,13 +89,13 @@ var uDSV = (function (exports) {
let rv = `r[${ci}]`; // trim()?

let parseExpr =
type === 'date' ? `new Date(${rv})` :
type === 'json' ? `JSON.parse(${rv})` :
type === 'number' ? `Number.parseFloat(${rv})` :
type.startsWith('boolean') ? `${rv} === '${type.slice(8)}' ? true : false` :
type === T_DATE ? `new Date(${rv})` :
type === T_JSON ? `JSON.parse(${rv})` :
type === T_NUMBER ? `Number.parseFloat(${rv})` :
type[0] === T_BOOLEAN ? `${rv} === '${type.slice(2)}' ? true : false` :
rv;

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

Expand Down Expand Up @@ -147,11 +153,12 @@ var uDSV = (function (exports) {
buf += objs ? '}' : ']';
}

// r.trim()?
let fnBody = `
let arr = Array(rows.length);
for (let i = 0; i < rows.length; i++) {
let r = rows[i]; // trim()?
let r = rows[i];
arr[i] = ${buf};
}
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.

29 changes: 18 additions & 11 deletions dist/uDSV.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ function isJSON(v) {
return false;
}

const T_STRING = 's';
const T_DATE = 'd';
const T_NUMBER = 'n';
const T_JSON = 'j';
const T_BOOLEAN = 'b';

function guessType(ci, rows) {
// row with a value to analyze
// trim()?
Expand All @@ -57,16 +63,16 @@ function guessType(ci, rows) {
r[ci] !== 'NaN'
);

let t = 'string';
let t = T_STRING;

if (row != null) {
let v = row[ci];

t = (
ISO8601.test(v) ? 'date' :
!Number.isNaN(Number.parseFloat(v)) ? 'number' :
BOOL_RE.test(v) ? `boolean:${boolTrue(v)}` :
isJSON(v) ? 'json' :
ISO8601.test(v) ? T_DATE :
!Number.isNaN(Number.parseFloat(v)) ? T_NUMBER :
BOOL_RE.test(v) ? `${T_BOOLEAN}:${boolTrue(v)}` :
isJSON(v) ? T_JSON :
t
);
}
Expand All @@ -80,13 +86,13 @@ function getValParseExpr(ci, col) {
let rv = `r[${ci}]`; // trim()?

let parseExpr =
type === 'date' ? `new Date(${rv})` :
type === 'json' ? `JSON.parse(${rv})` :
type === 'number' ? `Number.parseFloat(${rv})` :
type.startsWith('boolean') ? `${rv} === '${type.slice(8)}' ? true : false` :
type === T_DATE ? `new Date(${rv})` :
type === T_JSON ? `JSON.parse(${rv})` :
type === T_NUMBER ? `Number.parseFloat(${rv})` :
type[0] === T_BOOLEAN ? `${rv} === '${type.slice(2)}' ? true : false` :
rv;

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

Expand Down Expand Up @@ -144,11 +150,12 @@ function genToTypedRows(cols, objs = false, deep = false) {
buf += objs ? '}' : ']';
}

// r.trim()?
let fnBody = `
let arr = Array(rows.length);
for (let i = 0; i < rows.length; i++) {
let r = rows[i]; // trim()?
let r = rows[i];
arr[i] = ${buf};
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"devDependencies": {
"@rollup/plugin-terser": "^0.4.3",
"papaparse": "^5.4.1",
"rollup": "^3.28.0"
"rollup": "^3.28.0",
"rollup-plugin-re": "^1.0.7"
}
}
20 changes: 20 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const fs = require('fs');
// fs.writeFileSync('./dist/uDSV.min.css', minicss);

import terser from '@rollup/plugin-terser';
import replace from 'rollup-plugin-re';

const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const ver = "v" + pkg.version;
Expand Down Expand Up @@ -46,6 +47,17 @@ function bannerlessESM() {
};
}

function tplMin() {
return replace({
patterns: [
{
test: /`$[\s\S]+?^\s+`/gm,
replace: (code) => code.replace(/\s+/gm, ' '),
}
]
});
}

const terserOpts = {
compress: {
inline: 0,
Expand All @@ -71,6 +83,9 @@ export default [
format: 'es',
banner,
},
plugins: [
// tplMin(),
]
},
{
input: './src/uDSV.mjs',
Expand All @@ -81,6 +96,9 @@ export default [
exports: "auto",
banner,
},
plugins: [
// tplMin(),
]
},
{
input: 'uDSV',
Expand All @@ -93,6 +111,7 @@ export default [
},
plugins: [
bannerlessESM(),
// tplMin(),
]
},
{
Expand All @@ -106,6 +125,7 @@ export default [
},
plugins: [
bannerlessESM(),
tplMin(),
terser(terserOpts),
]
},
Expand Down
29 changes: 18 additions & 11 deletions src/uDSV.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ function isJSON(v) {
return false;
}

const T_STRING = 's';
const T_DATE = 'd';
const T_NUMBER = 'n';
const T_JSON = 'j';
const T_BOOLEAN = 'b';

function guessType(ci, rows) {
// row with a value to analyze
// trim()?
Expand All @@ -49,16 +55,16 @@ function guessType(ci, rows) {
r[ci] !== 'NaN'
);

let t = 'string';
let t = T_STRING;

if (row != null) {
let v = row[ci];

t = (
ISO8601.test(v) ? 'date' :
!Number.isNaN(Number.parseFloat(v)) ? 'number' :
BOOL_RE.test(v) ? `boolean:${boolTrue(v)}` :
isJSON(v) ? 'json' :
ISO8601.test(v) ? T_DATE :
!Number.isNaN(Number.parseFloat(v)) ? T_NUMBER :
BOOL_RE.test(v) ? `${T_BOOLEAN}:${boolTrue(v)}` :
isJSON(v) ? T_JSON :
t
);
}
Expand All @@ -72,13 +78,13 @@ function getValParseExpr(ci, col) {
let rv = `r[${ci}]`; // trim()?

let parseExpr =
type === 'date' ? `new Date(${rv})` :
type === 'json' ? `JSON.parse(${rv})` :
type === 'number' ? `Number.parseFloat(${rv})` :
type.startsWith('boolean') ? `${rv} === '${type.slice(8)}' ? true : false` :
type === T_DATE ? `new Date(${rv})` :
type === T_JSON ? `JSON.parse(${rv})` :
type === T_NUMBER ? `Number.parseFloat(${rv})` :
type[0] === T_BOOLEAN ? `${rv} === '${type.slice(2)}' ? true : false` :
rv;

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

Expand Down Expand Up @@ -136,11 +142,12 @@ function genToTypedRows(cols, objs = false, deep = false) {
buf += objs ? '}' : ']';
}

// r.trim()?
let fnBody = `
let arr = Array(rows.length);
for (let i = 0; i < rows.length; i++) {
let r = rows[i]; // trim()?
let r = rows[i];
arr[i] = ${buf};
}
Expand Down

0 comments on commit 35684af

Please sign in to comment.