read-excel-file 5.5.3 → 5.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -3
- package/bundle/read-excel-file.min.js +1 -1
- package/bundle/read-excel-file.min.js.map +1 -1
- package/commonjs/read/isDateTimestamp.js +5 -1
- package/commonjs/read/isDateTimestamp.js.map +1 -1
- package/commonjs/read/readXlsxFileNode.test.js.map +1 -1
- package/commonjs/read/schema/convertToJson.js +20 -215
- package/commonjs/read/schema/convertToJson.js.map +1 -1
- package/commonjs/types/Boolean.js +19 -0
- package/commonjs/types/Boolean.js.map +1 -0
- package/commonjs/types/Date.js +48 -0
- package/commonjs/types/Date.js.map +1 -0
- package/commonjs/types/Email.js +15 -1
- package/commonjs/types/Email.js.map +1 -1
- package/commonjs/types/Integer.js +15 -1
- package/commonjs/types/Integer.js.map +1 -1
- package/commonjs/types/InvalidError.js +55 -0
- package/commonjs/types/InvalidError.js.map +1 -0
- package/commonjs/types/Number.js +53 -0
- package/commonjs/types/Number.js.map +1 -0
- package/commonjs/types/String.js +48 -0
- package/commonjs/types/String.js.map +1 -0
- package/commonjs/types/URL.js +15 -1
- package/commonjs/types/URL.js.map +1 -1
- package/modules/read/isDateTimestamp.js +5 -1
- package/modules/read/isDateTimestamp.js.map +1 -1
- package/modules/read/readXlsxFileNode.test.js.map +1 -1
- package/modules/read/schema/convertToJson.js +20 -211
- package/modules/read/schema/convertToJson.js.map +1 -1
- package/modules/types/Boolean.js +9 -0
- package/modules/types/Boolean.js.map +1 -0
- package/modules/types/Date.js +37 -0
- package/modules/types/Date.js.map +1 -0
- package/modules/types/Email.js +12 -1
- package/modules/types/Email.js.map +1 -1
- package/modules/types/Integer.js +11 -1
- package/modules/types/Integer.js.map +1 -1
- package/modules/types/InvalidError.js +48 -0
- package/modules/types/InvalidError.js.map +1 -0
- package/modules/types/Number.js +43 -0
- package/modules/types/Number.js.map +1 -0
- package/modules/types/String.js +38 -0
- package/modules/types/String.js.map +1 -0
- package/modules/types/URL.js +12 -1
- package/modules/types/URL.js.map +1 -1
- package/package.json +1 -1
|
@@ -29,8 +29,12 @@ function isDateTimestamp(value, styleId, styles, options) {
|
|
|
29
29
|
throw new Error("Cell style not found: ".concat(styleId));
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
if (!style.numberFormat) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
32
36
|
if ( // Whether it's a "number format" that's conventionally used for storing date timestamps.
|
|
33
|
-
BUILT_IN_DATE_NUMBER_FORMAT_IDS.indexOf(
|
|
37
|
+
BUILT_IN_DATE_NUMBER_FORMAT_IDS.indexOf(Number(style.numberFormat.id)) >= 0 || // Whether it's a "number format" that uses a "formatting template"
|
|
34
38
|
// that the developer is certain is a date formatting template.
|
|
35
39
|
options.dateFormat && style.numberFormat.template === options.dateFormat || // Whether the "smart formatting template" feature is not disabled
|
|
36
40
|
// and it has detected that it's a date formatting template by looking at it.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isDateTimestamp.js","names":["isDateTimestamp","value","styleId","styles","options","style","Error","
|
|
1
|
+
{"version":3,"file":"isDateTimestamp.js","names":["isDateTimestamp","value","styleId","styles","options","style","Error","numberFormat","BUILT_IN_DATE_NUMBER_FORMAT_IDS","indexOf","Number","id","dateFormat","template","smartDateParser","isDateTemplate","DATE_FORMAT_WEIRD_PREFIX","DATE_FORMAT_WEIRD_POSTFIX","toLowerCase","replace","tokens","split","token","DATE_TEMPLATE_TOKENS"],"sources":["../../source/read/isDateTimestamp.js"],"sourcesContent":["// XLSX does have \"d\" type for dates, but it's not commonly used.\r\n// Instead, it prefers using \"n\" type for storing dates as timestamps.\r\n//\r\n// Whether a numeric value is a number or a date timestamp, it sometimes could be\r\n// detected by looking at the value \"format\" and seeing if it's a date-specific one.\r\n// https://github.com/catamphetamine/read-excel-file/issues/3#issuecomment-395770777\r\n//\r\n// The list of generic numeric value \"formats\":\r\n// https://xlsxwriter.readthedocs.io/format.html#format-set-num-format\r\n//\r\nexport default function isDateTimestamp(value, styleId, styles, options) {\r\n if (styleId) {\r\n const style = styles[styleId]\r\n if (!style) {\r\n throw new Error(`Cell style not found: ${styleId}`)\r\n }\r\n if (!style.numberFormat) {\r\n return false\r\n }\r\n if (\r\n // Whether it's a \"number format\" that's conventionally used for storing date timestamps.\r\n BUILT_IN_DATE_NUMBER_FORMAT_IDS.indexOf(Number(style.numberFormat.id)) >= 0 ||\r\n // Whether it's a \"number format\" that uses a \"formatting template\"\r\n // that the developer is certain is a date formatting template.\r\n (options.dateFormat && style.numberFormat.template === options.dateFormat) ||\r\n // Whether the \"smart formatting template\" feature is not disabled\r\n // and it has detected that it's a date formatting template by looking at it.\r\n (options.smartDateParser !== false && style.numberFormat.template && isDateTemplate(style.numberFormat.template))\r\n ) {\r\n return true\r\n }\r\n }\r\n}\r\n\r\n// https://hexdocs.pm/xlsxir/number_styles.html\r\nconst BUILT_IN_DATE_NUMBER_FORMAT_IDS = [14,15,16,17,18,19,20,21,22,27,30,36,45,46,47,50,57]\r\n\r\n// On some date formats, there's an \"[$-414]\" prefix.\r\n// I don't have any idea what that is.\r\n//\r\n// https://stackoverflow.com/questions/4730152/what-indicates-an-office-open-xml-cell-contains-a-date-time-value\r\n//\r\n// Examples:\r\n//\r\n// * 27 (built-in format) \"[$-404]e/m/d\"\r\n// * 164 (custom format) \"[$-414]mmmm\\ yyyy;@\"\r\n//\r\nconst DATE_FORMAT_WEIRD_PREFIX = /^\\[\\$-414\\]/\r\n\r\n// On some date formats, there's an \";@\" postfix.\r\n// I don't have any idea what that is.\r\n// Examples:\r\n//\r\n// * 164 (custom format) \"m/d/yyyy;@\"\r\n// * 164 (custom format) \"[$-414]mmmm\\ yyyy;@\"\r\n//\r\nconst DATE_FORMAT_WEIRD_POSTFIX = /;@$/\r\n\r\nfunction isDateTemplate(template) {\r\n // Date format tokens could be in upper case or in lower case.\r\n // There seems to be no single standard.\r\n // So lowercase the template first.\r\n template = template.toLowerCase()\r\n\r\n // On some date formats, there's an \"[$-414]\" prefix.\r\n // I don't have any idea what that is. Trim it.\r\n template = template.replace(DATE_FORMAT_WEIRD_PREFIX, '')\r\n\r\n // On some date formats, there's an \";@\" postfix.\r\n // I don't have any idea what that is. Trim it.\r\n template = template.replace(DATE_FORMAT_WEIRD_POSTFIX, '')\r\n\r\n const tokens = template.split(/\\W+/)\r\n for (const token of tokens) {\r\n if (DATE_TEMPLATE_TOKENS.indexOf(token) < 0) {\r\n return false\r\n }\r\n }\r\n return true\r\n}\r\n\r\n// These tokens could be in upper case or in lower case.\r\n// There seems to be no single standard, so using lower case.\r\nconst DATE_TEMPLATE_TOKENS = [\r\n // Seconds (min two digits). Example: \"05\".\r\n 'ss',\r\n // Minutes (min two digits). Example: \"05\". Could also be \"Months\". Weird.\r\n 'mm',\r\n // Hours. Example: \"1\".\r\n 'h',\r\n // Hours (min two digits). Example: \"01\".\r\n 'hh',\r\n // \"AM\" part of \"AM/PM\". Lowercased just in case.\r\n 'am',\r\n // \"PM\" part of \"AM/PM\". Lowercased just in case.\r\n 'pm',\r\n // Day. Example: \"1\"\r\n 'd',\r\n // Day (min two digits). Example: \"01\"\r\n 'dd',\r\n // Month (numeric). Example: \"1\".\r\n 'm',\r\n // Month (numeric, min two digits). Example: \"01\". Could also be \"Minutes\". Weird.\r\n 'mm',\r\n // Month (shortened month name). Example: \"Jan\".\r\n 'mmm',\r\n // Month (full month name). Example: \"January\".\r\n 'mmmm',\r\n // Two-digit year. Example: \"20\".\r\n 'yy',\r\n // Full year. Example: \"2020\".\r\n 'yyyy',\r\n\r\n // I don't have any idea what \"e\" means.\r\n // It's used in \"built-in\" XLSX formats:\r\n // * 27 '[$-404]e/m/d';\r\n // * 36 '[$-404]e/m/d';\r\n // * 50 '[$-404]e/m/d';\r\n // * 57 '[$-404]e/m/d';\r\n 'e'\r\n];"],"mappings":";;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;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,GAAGA,QAAQ,CAACK,WAAT,EAAX,CAJgC,CAMhC;EACA;;EACAL,QAAQ,GAAGA,QAAQ,CAACM,OAAT,CAAiBH,wBAAjB,EAA2C,EAA3C,CAAX,CARgC,CAUhC;EACA;;EACAH,QAAQ,GAAGA,QAAQ,CAACM,OAAT,CAAiBF,yBAAjB,EAA4C,EAA5C,CAAX;EAEA,IAAMG,MAAM,GAAGP,QAAQ,CAACQ,KAAT,CAAe,KAAf,CAAf;;EACA,qDAAoBD,MAApB,wCAA4B;IAAA,IAAjBE,KAAiB;;IAC1B,IAAIC,oBAAoB,CAACd,OAArB,CAA6Ba,KAA7B,IAAsC,CAA1C,EAA6C;MAC3C,OAAO,KAAP;IACD;EACF;;EACD,OAAO,IAAP;AACD,C,CAED;AACA;;;AACA,IAAMC,oBAAoB,GAAG,CAC3B;AACA,IAF2B,EAG3B;AACA,IAJ2B,EAK3B;AACA,GAN2B,EAO3B;AACA,IAR2B,EAS3B;AACA,IAV2B,EAW3B;AACA,IAZ2B,EAa3B;AACA,GAd2B,EAe3B;AACA,IAhB2B,EAiB3B;AACA,GAlB2B,EAmB3B;AACA,IApB2B,EAqB3B;AACA,KAtB2B,EAuB3B;AACA,MAxB2B,EAyB3B;AACA,IA1B2B,EA2B3B;AACA,MA5B2B,EA8B3B;AACA;AACA;AACA;AACA;AACA;AACA,GApC2B,CAA7B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readXlsxFileNode.test.js","names":["describe","it","schema","prop","type","Date","Number","required","Boolean","String","parse","value","rowMap","readXlsxFileNode","path","resolve","then","rows","date","getTime","should","deep","equal","convertToUTCTimezone","numberOfStudents","course","isFree","cost","title","contact","map","errors","getTimezoneOffset"],"sources":["../../source/read/readXlsxFileNode.test.js"],"sourcesContent":["import path from 'path'\r\n\r\nimport readXlsxFileNode from './readXlsxFileNode.js'\r\n\r\ndescribe('readXlsxFileNode', () => {\r\n\tit('should read *.xlsx file on Node.js and parse it to JSON', () => {\r\n\t\tconst schema = {\r\n\t\t\t'START DATE': {\r\n\t\t\t\tprop: 'date',\r\n\t\t\t\ttype: Date\r\n\t\t\t},\r\n\t\t\t'NUMBER OF STUDENTS': {\r\n\t\t\t\tprop: 'numberOfStudents',\r\n\t\t\t\ttype: Number,\r\n\t\t\t\trequired: true\r\n\t\t\t},\r\n\t\t\t'COURSE': {\r\n\t\t\t\tprop: 'course',\r\n\t\t\t\ttype: {\r\n\t\t\t\t\t'IS FREE': {\r\n\t\t\t\t\t\tprop: 'isFree',\r\n\t\t\t\t\t\ttype: Boolean\r\n\t\t\t\t\t\t// Excel
|
|
1
|
+
{"version":3,"file":"readXlsxFileNode.test.js","names":["describe","it","schema","prop","type","Date","Number","required","Boolean","String","parse","value","rowMap","readXlsxFileNode","path","resolve","then","rows","date","getTime","should","deep","equal","convertToUTCTimezone","numberOfStudents","course","isFree","cost","title","contact","map","errors","getTimezoneOffset"],"sources":["../../source/read/readXlsxFileNode.test.js"],"sourcesContent":["import path from 'path'\r\n\r\nimport readXlsxFileNode from './readXlsxFileNode.js'\r\n\r\ndescribe('readXlsxFileNode', () => {\r\n\tit('should read *.xlsx file on Node.js and parse it to JSON', () => {\r\n\t\tconst schema = {\r\n\t\t\t'START DATE': {\r\n\t\t\t\tprop: 'date',\r\n\t\t\t\ttype: Date\r\n\t\t\t},\r\n\t\t\t'NUMBER OF STUDENTS': {\r\n\t\t\t\tprop: 'numberOfStudents',\r\n\t\t\t\ttype: Number,\r\n\t\t\t\trequired: true\r\n\t\t\t},\r\n\t\t\t'COURSE': {\r\n\t\t\t\tprop: 'course',\r\n\t\t\t\ttype: {\r\n\t\t\t\t\t'IS FREE': {\r\n\t\t\t\t\t\tprop: 'isFree',\r\n\t\t\t\t\t\ttype: Boolean\r\n\t\t\t\t\t\t// Excel stores booleans as numbers:\r\n\t\t\t\t\t\t// `1` is `true` and `0` is `false`.\r\n\t\t\t\t\t\t// Such numbers are parsed into booleans.\r\n\t\t\t\t\t},\r\n\t\t\t\t\t'COST': {\r\n\t\t\t\t\t\tprop: 'cost',\r\n\t\t\t\t\t\ttype: Number\r\n\t\t\t\t\t},\r\n\t\t\t\t\t'COURSE TITLE': {\r\n\t\t\t\t\t\tprop: 'title',\r\n\t\t\t\t\t\ttype: String\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t},\r\n\t\t\t'CONTACT': {\r\n\t\t\t\tprop: 'contact',\r\n\t\t\t\trequired: true,\r\n\t\t\t\tparse(value) {\r\n\t\t\t\t\treturn '+11234567890'\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst rowMap = []\r\n\r\n\t\treturn readXlsxFileNode(path.resolve('./test/spreadsheets/course.xlsx'), { schema, rowMap }).then(({ rows }) => {\r\n\t\t\trows[0].date = rows[0].date.getTime()\r\n\t\t\trows.should.deep.equal([{\r\n\t\t\t\tdate: convertToUTCTimezone(new Date(2018, 2, 24)).getTime(),\r\n\t\t\t\tnumberOfStudents: 123,\r\n\t\t\t\tcourse: {\r\n\t\t\t\t\tisFree: false,\r\n\t\t\t\t\tcost: 210.45,\r\n\t\t\t\t\ttitle: 'Chemistry'\r\n\t\t\t\t},\r\n\t\t\t\tcontact: '+11234567890'\r\n\t\t\t}])\r\n\t\t\trowMap.should.deep.equal([0, 1])\r\n\t\t})\r\n\t})\r\n\r\n\tit('should read *.xlsx file on Node.js and map it to JSON', () => {\r\n\t\tconst map = {\r\n\t\t\t'START DATE': 'date',\r\n\t\t\t'NUMBER OF STUDENTS': 'numberOfStudents',\r\n\t\t\t'COURSE': {\r\n\t\t\t\t'course': {\r\n\t\t\t\t\t'IS FREE': 'isFree',\r\n\t\t\t\t\t'COST': 'cost',\r\n\t\t\t\t\t'COURSE TITLE': 'title'\r\n\t\t\t\t}\r\n\t\t\t},\r\n\t\t\t'CONTACT': 'contact'\r\n\t\t}\r\n\r\n\t\tconst rowMap = []\r\n\r\n\t\treturn readXlsxFileNode(path.resolve('./test/spreadsheets/course.xlsx'), { map, rowMap }).then(({ rows, errors }) => {\r\n\t\t\terrors.should.deep.equal([])\r\n\t\t\trows[0].date = rows[0].date.getTime()\r\n\t\t\trows.should.deep.equal([{\r\n\t\t\t\tdate: convertToUTCTimezone(new Date(2018, 2, 24)).getTime(),\r\n\t\t\t\tnumberOfStudents: 123,\r\n\t\t\t\tcourse: {\r\n\t\t\t\t\tisFree: false,\r\n\t\t\t\t\tcost: 210.45,\r\n\t\t\t\t\ttitle: 'Chemistry'\r\n\t\t\t\t},\r\n\t\t\t\tcontact: '(123) 456-7890'\r\n\t\t\t}])\r\n\t\t\trowMap.should.deep.equal([0, 1])\r\n\t\t})\r\n\t})\r\n})\r\n\r\n// Converts timezone to UTC while preserving the same time\r\nfunction convertToUTCTimezone(date) {\r\n\t// Doesn't account for leap seconds but I guess that's ok\r\n\t// given that javascript's own `Date()` does not either.\r\n\t// https://www.timeanddate.com/time/leap-seconds-background.html\r\n\t//\r\n\t// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset\r\n\t//\r\n\treturn new Date(date.getTime() - date.getTimezoneOffset() * 60 * 1000)\r\n}\r\n"],"mappings":";;AAAA;;AAEA;;;;AAEAA,QAAQ,CAAC,kBAAD,EAAqB,YAAM;EAClCC,EAAE,CAAC,yDAAD,EAA4D,YAAM;IACnE,IAAMC,MAAM,GAAG;MACd,cAAc;QACbC,IAAI,EAAE,MADO;QAEbC,IAAI,EAAEC;MAFO,CADA;MAKd,sBAAsB;QACrBF,IAAI,EAAE,kBADe;QAErBC,IAAI,EAAEE,MAFe;QAGrBC,QAAQ,EAAE;MAHW,CALR;MAUd,UAAU;QACTJ,IAAI,EAAE,QADG;QAETC,IAAI,EAAE;UACL,WAAW;YACVD,IAAI,EAAE,QADI;YAEVC,IAAI,EAAEI,OAFI,CAGV;YACA;YACA;;UALU,CADN;UAQL,QAAQ;YACPL,IAAI,EAAE,MADC;YAEPC,IAAI,EAAEE;UAFC,CARH;UAYL,gBAAgB;YAC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Block;
|
|
|
8
8
|
exports.parseArray = parseArray;
|
|
9
9
|
exports.parseValue = parseValue;
|
|
10
10
|
|
|
11
|
-
var
|
|
11
|
+
var _Number = _interopRequireDefault(require("../../types/Number.js"));
|
|
12
12
|
|
|
13
|
-
var
|
|
13
|
+
var _String = _interopRequireDefault(require("../../types/String.js"));
|
|
14
14
|
|
|
15
|
-
var
|
|
15
|
+
var _Boolean = _interopRequireDefault(require("../../types/Boolean.js"));
|
|
16
16
|
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
20
|
-
|
|
21
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
17
|
+
var _Date = _interopRequireDefault(require("../../types/Date.js"));
|
|
22
18
|
|
|
23
19
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
24
20
|
|
|
@@ -277,9 +273,15 @@ function parseCustomValue(value, parse) {
|
|
|
277
273
|
value: value
|
|
278
274
|
};
|
|
279
275
|
} catch (error) {
|
|
280
|
-
|
|
276
|
+
var result = {
|
|
281
277
|
error: error.message
|
|
282
278
|
};
|
|
279
|
+
|
|
280
|
+
if (error.reason) {
|
|
281
|
+
result.reason = error.reason;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return result;
|
|
283
285
|
}
|
|
284
286
|
}
|
|
285
287
|
/**
|
|
@@ -293,224 +295,27 @@ function parseCustomValue(value, parse) {
|
|
|
293
295
|
function parseValueOfType(value, type, options) {
|
|
294
296
|
switch (type) {
|
|
295
297
|
case String:
|
|
296
|
-
|
|
297
|
-
return {
|
|
298
|
-
value: value
|
|
299
|
-
};
|
|
300
|
-
} // Excel tends to perform a forced automatic convertion of string-type values
|
|
301
|
-
// to number-type ones when the user has input them. Otherwise, users wouldn't
|
|
302
|
-
// be able to perform formula calculations on those cell values because users
|
|
303
|
-
// won't bother manually choosing a "numeric" cell type for each cell, and
|
|
304
|
-
// even if they did, choosing a "numeric" cell type every time wouldn't be an
|
|
305
|
-
// acceptable "user experience".
|
|
306
|
-
//
|
|
307
|
-
// So, if a cell value is supposed to be a string and Excel has automatically
|
|
308
|
-
// converted it to a number, perform a backwards conversion.
|
|
309
|
-
//
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
if (typeof value === 'number') {
|
|
313
|
-
if (isNaN(value)) {
|
|
314
|
-
return {
|
|
315
|
-
error: 'invalid',
|
|
316
|
-
reason: 'invalid_number'
|
|
317
|
-
};
|
|
318
|
-
} // The global `isFinite()` function filters out:
|
|
319
|
-
// * NaN
|
|
320
|
-
// * -Infinity
|
|
321
|
-
// * Infinity
|
|
322
|
-
//
|
|
323
|
-
// All other values pass (including non-numbers).
|
|
324
|
-
//
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
if (!isFinite(value)) {
|
|
328
|
-
return {
|
|
329
|
-
error: 'invalid',
|
|
330
|
-
reason: 'out_of_bounds'
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
return {
|
|
335
|
-
value: String(value)
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
return {
|
|
340
|
-
error: 'invalid',
|
|
341
|
-
reason: 'not_a_string'
|
|
342
|
-
};
|
|
298
|
+
return parseCustomValue(value, _String["default"]);
|
|
343
299
|
|
|
344
300
|
case Number:
|
|
345
|
-
|
|
346
|
-
// An XLSX file editing software might not always correctly
|
|
347
|
-
// detect numeric values in string-type cells. Users won't bother
|
|
348
|
-
// manually selecting a cell type, so the editing software has to guess
|
|
349
|
-
// based on the user's input. One can assume that such auto-detection
|
|
350
|
-
// might not always work.
|
|
351
|
-
//
|
|
352
|
-
// So, if a cell is supposed to be a numeric one, convert a string value to a number.
|
|
353
|
-
//
|
|
354
|
-
if (typeof value === 'string') {
|
|
355
|
-
var stringifiedValue = value;
|
|
356
|
-
value = Number(value);
|
|
357
|
-
|
|
358
|
-
if (String(value) !== stringifiedValue) {
|
|
359
|
-
return {
|
|
360
|
-
error: 'invalid',
|
|
361
|
-
reason: 'not_a_number'
|
|
362
|
-
};
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
if (typeof value !== 'number') {
|
|
367
|
-
return {
|
|
368
|
-
error: 'invalid',
|
|
369
|
-
reason: 'not_a_number'
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
if (isNaN(value)) {
|
|
374
|
-
return {
|
|
375
|
-
error: 'invalid',
|
|
376
|
-
reason: 'invalid_number'
|
|
377
|
-
};
|
|
378
|
-
} // At this point, `value` can only be a number.
|
|
379
|
-
//
|
|
380
|
-
// The global `isFinite()` function filters out:
|
|
381
|
-
// * NaN
|
|
382
|
-
// * -Infinity
|
|
383
|
-
// * Infinity
|
|
384
|
-
//
|
|
385
|
-
// All other values pass (including non-numbers).
|
|
386
|
-
//
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
if (!isFinite(value)) {
|
|
390
|
-
return {
|
|
391
|
-
error: 'invalid',
|
|
392
|
-
reason: 'out_of_bounds'
|
|
393
|
-
};
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
if (type === _Integer["default"] && !(0, _Integer.isInteger)(value)) {
|
|
397
|
-
return {
|
|
398
|
-
error: 'invalid',
|
|
399
|
-
reason: 'not_an_integer'
|
|
400
|
-
};
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
return {
|
|
404
|
-
value: value
|
|
405
|
-
};
|
|
406
|
-
|
|
407
|
-
case _URL["default"]:
|
|
408
|
-
if (typeof value === 'string') {
|
|
409
|
-
if ((0, _URL.isURL)(value)) {
|
|
410
|
-
return {
|
|
411
|
-
value: value
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
return {
|
|
416
|
-
error: 'invalid',
|
|
417
|
-
reason: 'not_a_url'
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
return {
|
|
422
|
-
error: 'invalid',
|
|
423
|
-
reason: 'not_a_string'
|
|
424
|
-
};
|
|
425
|
-
|
|
426
|
-
case _Email["default"]:
|
|
427
|
-
if (typeof value === 'string') {
|
|
428
|
-
if ((0, _Email.isEmail)(value)) {
|
|
429
|
-
return {
|
|
430
|
-
value: value
|
|
431
|
-
};
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
return {
|
|
435
|
-
error: 'invalid',
|
|
436
|
-
reason: 'not_an_email'
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
return {
|
|
441
|
-
error: 'invalid',
|
|
442
|
-
reason: 'not_a_string'
|
|
443
|
-
};
|
|
301
|
+
return parseCustomValue(value, _Number["default"]);
|
|
444
302
|
|
|
445
303
|
case Date:
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
return {
|
|
452
|
-
error: 'invalid',
|
|
453
|
-
reason: 'out_of_bounds'
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
return {
|
|
458
|
-
value: value
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
if (typeof value === 'number') {
|
|
463
|
-
if (isNaN(value)) {
|
|
464
|
-
return {
|
|
465
|
-
error: 'invalid',
|
|
466
|
-
reason: 'invalid_number'
|
|
467
|
-
};
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
if (!isFinite(value)) {
|
|
471
|
-
return {
|
|
472
|
-
error: 'invalid',
|
|
473
|
-
reason: 'out_of_bounds'
|
|
474
|
-
};
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
var date = (0, _parseDate["default"])(value, options.properties);
|
|
478
|
-
|
|
479
|
-
if (isNaN(date.valueOf())) {
|
|
480
|
-
return {
|
|
481
|
-
error: 'invalid',
|
|
482
|
-
reason: 'out_of_bounds'
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
return {
|
|
487
|
-
value: date
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
return {
|
|
492
|
-
error: 'invalid',
|
|
493
|
-
reason: 'not_a_date'
|
|
494
|
-
};
|
|
304
|
+
return parseCustomValue(value, function (value) {
|
|
305
|
+
return (0, _Date["default"])(value, {
|
|
306
|
+
properties: options.properties
|
|
307
|
+
});
|
|
308
|
+
});
|
|
495
309
|
|
|
496
310
|
case Boolean:
|
|
497
|
-
|
|
498
|
-
return {
|
|
499
|
-
value: value
|
|
500
|
-
};
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
return {
|
|
504
|
-
error: 'invalid',
|
|
505
|
-
reason: 'not_a_boolean'
|
|
506
|
-
};
|
|
311
|
+
return parseCustomValue(value, _Boolean["default"]);
|
|
507
312
|
|
|
508
313
|
default:
|
|
509
314
|
if (typeof type === 'function') {
|
|
510
315
|
return parseCustomValue(value, type);
|
|
511
316
|
}
|
|
512
317
|
|
|
513
|
-
throw new Error("
|
|
318
|
+
throw new Error("Unsupported schema type: ".concat(type && type.name || type));
|
|
514
319
|
}
|
|
515
320
|
}
|
|
516
321
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convertToJson.js","names":["DEFAULT_OPTIONS","isColumnOriented","data","schema","options","rowMap","ignoreEmptyRows","validateSchema","transpose","columns","results","errors","i","length","result","read","push","error","row","rows","rowIndex","object","isEmptyObject","key","schemaEntry","isNestedSchema","type","Array","isArray","rawValue","indexOf","undefined","value","reason","notEmpty","array","parseArray","map","_value","parseValue","required","column","includeNullValues","prop","Object","keys","parse","parseCustomValue","parseValueOfType","oneOf","validate","message","String","isNaN","isFinite","Number","Integer","stringifiedValue","isInteger","URL","isURL","Email","isEmail","Date","valueOf","date","parseDate","properties","Boolean","Error","name","getBlock","string","endCharacter","startIndex","substring","character","block","blocks","index","trim","_","entry"],"sources":["../../../source/read/schema/convertToJson.js"],"sourcesContent":["import parseDate from '../parseDate.js'\r\n\r\nimport Integer, { isInteger } from '../../types/Integer.js'\r\nimport URL, { isURL } from '../../types/URL.js'\r\nimport Email, { isEmail } from '../../types/Email.js'\r\n\r\nconst DEFAULT_OPTIONS = {\r\n isColumnOriented: false\r\n}\r\n\r\n/**\r\n * Convert 2D array to nested objects.\r\n * If row oriented data, row 0 is dotted key names.\r\n * Column oriented data is transposed.\r\n * @param {any[][]} data - An array of rows, each row being an array of cells.\r\n * @param {object} schema\r\n * @return {object[]}\r\n */\r\nexport default function(data, schema, options) {\r\n if (options) {\r\n options = {\r\n ...DEFAULT_OPTIONS,\r\n ...options\r\n }\r\n } else {\r\n options = DEFAULT_OPTIONS\r\n }\r\n\r\n const {\r\n isColumnOriented,\r\n rowMap,\r\n ignoreEmptyRows\r\n } = options\r\n\r\n validateSchema(schema)\r\n\r\n if (isColumnOriented) {\r\n data = transpose(data)\r\n }\r\n\r\n const columns = data[0]\r\n\r\n const results = []\r\n const errors = []\r\n\r\n for (let i = 1; i < data.length; i++) {\r\n const result = read(schema, data[i], i, columns, errors, options)\r\n if (result !== null || ignoreEmptyRows === false) {\r\n results.push(result)\r\n }\r\n }\r\n\r\n // Correct error rows.\r\n if (rowMap) {\r\n for (const error of errors) {\r\n // Convert the `row` index in `data` to the\r\n // actual `row` index in the spreadsheet.\r\n // `- 1` converts row number to row index.\r\n // `+ 1` converts row index to row number.\r\n error.row = rowMap[error.row - 1] + 1\r\n }\r\n }\r\n\r\n return {\r\n rows: results,\r\n errors\r\n }\r\n}\r\n\r\nfunction read(schema, row, rowIndex, columns, errors, options) {\r\n const object = {}\r\n let isEmptyObject = true\r\n for (const key of Object.keys(schema)) {\r\n const schemaEntry = schema[key]\r\n const isNestedSchema = typeof schemaEntry.type === 'object' && !Array.isArray(schemaEntry.type)\r\n let rawValue = row[columns.indexOf(key)]\r\n if (rawValue === undefined) {\r\n rawValue = null\r\n }\r\n let value\r\n let error\r\n let reason\r\n if (isNestedSchema) {\r\n value = read(schemaEntry.type, row, rowIndex, columns, errors, options)\r\n } else {\r\n if (rawValue === null) {\r\n value = null\r\n }\r\n else if (Array.isArray(schemaEntry.type)) {\r\n let notEmpty = false\r\n const array = parseArray(rawValue).map((_value) => {\r\n const result = parseValue(_value, schemaEntry, options)\r\n if (result.error) {\r\n value = _value\r\n error = result.error\r\n reason = result.reason\r\n }\r\n if (result.value !== null) {\r\n notEmpty = true\r\n }\r\n return result.value\r\n })\r\n if (!error) {\r\n value = notEmpty ? array : null\r\n }\r\n } else {\r\n const result = parseValue(rawValue, schemaEntry, options)\r\n error = result.error\r\n reason = result.reason\r\n value = error ? rawValue : result.value\r\n }\r\n }\r\n if (!error && value === null && schemaEntry.required) {\r\n error = 'required'\r\n }\r\n if (error) {\r\n error = {\r\n error,\r\n row: rowIndex + 1,\r\n column: key,\r\n value\r\n }\r\n if (reason) {\r\n error.reason = reason\r\n }\r\n if (schemaEntry.type) {\r\n error.type = schemaEntry.type\r\n }\r\n errors.push(error)\r\n } else {\r\n if (isEmptyObject && value !== null) {\r\n isEmptyObject = false\r\n }\r\n if (value !== null || options.includeNullValues) {\r\n object[schemaEntry.prop] = value\r\n }\r\n }\r\n }\r\n if (isEmptyObject) {\r\n return null\r\n }\r\n return object\r\n}\r\n\r\n/**\r\n * Converts textual value to a javascript typed value.\r\n * @param {any} value\r\n * @param {object} schemaEntry\r\n * @return {{ value: any, error: string }}\r\n */\r\nexport function parseValue(value, schemaEntry, options) {\r\n if (value === null) {\r\n return { value: null }\r\n }\r\n let result\r\n if (schemaEntry.parse) {\r\n result = parseCustomValue(value, schemaEntry.parse)\r\n } else if (schemaEntry.type) {\r\n result = parseValueOfType(\r\n value,\r\n // Supports parsing array types.\r\n // See `parseArray()` function for more details.\r\n // Example `type`: String[]\r\n // Input: 'Barack Obama, \"String, with, colons\", Donald Trump'\r\n // Output: ['Barack Obama', 'String, with, colons', 'Donald Trump']\r\n Array.isArray(schemaEntry.type) ? schemaEntry.type[0] : schemaEntry.type,\r\n options\r\n )\r\n } else {\r\n result = { value: value }\r\n // throw new Error('Invalid schema entry: no .type and no .parse():\\n\\n' + JSON.stringify(schemaEntry, null, 2))\r\n }\r\n // If errored then return the error.\r\n if (result.error) {\r\n return result\r\n }\r\n if (result.value !== null) {\r\n if (schemaEntry.oneOf && schemaEntry.oneOf.indexOf(result.value) < 0) {\r\n return { error: 'invalid', reason: 'unknown' }\r\n }\r\n if (schemaEntry.validate) {\r\n try {\r\n schemaEntry.validate(result.value)\r\n } catch (error) {\r\n return { error: error.message }\r\n }\r\n }\r\n }\r\n return result\r\n}\r\n\r\n/**\r\n * Converts textual value to a custom value using supplied `.parse()`.\r\n * @param {any} value\r\n * @param {function} parse\r\n * @return {{ value: any, error: string }}\r\n */\r\nfunction parseCustomValue(value, parse) {\r\n try {\r\n value = parse(value)\r\n if (value === undefined) {\r\n return { value: null }\r\n }\r\n return { value }\r\n } catch (error) {\r\n return { error: error.message }\r\n }\r\n}\r\n\r\n/**\r\n * Converts textual value to a javascript typed value.\r\n * @param {any} value\r\n * @param {} type\r\n * @return {{ value: (string|number|Date|boolean), error: string, reason?: string }}\r\n */\r\nfunction parseValueOfType(value, type, options) {\r\n switch (type) {\r\n case String:\r\n if (typeof value === 'string') {\r\n return { value }\r\n }\r\n // Excel tends to perform a forced automatic convertion of string-type values\r\n // to number-type ones when the user has input them. Otherwise, users wouldn't\r\n // be able to perform formula calculations on those cell values because users\r\n // won't bother manually choosing a \"numeric\" cell type for each cell, and\r\n // even if they did, choosing a \"numeric\" cell type every time wouldn't be an\r\n // acceptable \"user experience\".\r\n //\r\n // So, if a cell value is supposed to be a string and Excel has automatically\r\n // converted it to a number, perform a backwards conversion.\r\n //\r\n if (typeof value === 'number') {\r\n if (isNaN(value)) {\r\n return { error: 'invalid', reason: 'invalid_number' }\r\n }\r\n // The global `isFinite()` function filters out:\r\n // * NaN\r\n // * -Infinity\r\n // * Infinity\r\n //\r\n // All other values pass (including non-numbers).\r\n //\r\n if (!isFinite(value)) {\r\n return { error: 'invalid', reason: 'out_of_bounds' }\r\n }\r\n return { value: String(value) }\r\n }\r\n return { error: 'invalid', reason: 'not_a_string' }\r\n\r\n case Number:\r\n case Integer:\r\n // An XLSX file editing software might not always correctly\r\n // detect numeric values in string-type cells. Users won't bother\r\n // manually selecting a cell type, so the editing software has to guess\r\n // based on the user's input. One can assume that such auto-detection\r\n // might not always work.\r\n //\r\n // So, if a cell is supposed to be a numeric one, convert a string value to a number.\r\n //\r\n if (typeof value === 'string') {\r\n const stringifiedValue = value\r\n value = Number(value)\r\n if (String(value) !== stringifiedValue) {\r\n return { error: 'invalid', reason: 'not_a_number' }\r\n }\r\n }\r\n if (typeof value !== 'number') {\r\n return { error: 'invalid', reason: 'not_a_number' }\r\n }\r\n if (isNaN(value)) {\r\n return { error: 'invalid', reason: 'invalid_number' }\r\n }\r\n // At this point, `value` can only be a number.\r\n //\r\n // The global `isFinite()` function filters out:\r\n // * NaN\r\n // * -Infinity\r\n // * Infinity\r\n //\r\n // All other values pass (including non-numbers).\r\n //\r\n if (!isFinite(value)) {\r\n return { error: 'invalid', reason: 'out_of_bounds' }\r\n }\r\n if (type === Integer && !isInteger(value)) {\r\n return { error: 'invalid', reason: 'not_an_integer' }\r\n }\r\n return { value }\r\n\r\n case URL:\r\n if (typeof value === 'string') {\r\n if (isURL(value)) {\r\n return { value }\r\n }\r\n return { error: 'invalid', reason: 'not_a_url' }\r\n }\r\n return { error: 'invalid', reason: 'not_a_string' }\r\n\r\n case Email:\r\n if (typeof value === 'string') {\r\n if (isEmail(value)) {\r\n return { value }\r\n }\r\n return { error: 'invalid', reason: 'not_an_email' }\r\n }\r\n return { error: 'invalid', reason: 'not_a_string' }\r\n\r\n case Date:\r\n // XLSX has no specific format for dates.\r\n // Sometimes a date can be heuristically detected.\r\n // https://github.com/catamphetamine/read-excel-file/issues/3#issuecomment-395770777\r\n if (value instanceof Date) {\r\n if (isNaN(value.valueOf())) {\r\n return { error: 'invalid', reason: 'out_of_bounds' }\r\n }\r\n return { value }\r\n }\r\n if (typeof value === 'number') {\r\n if (isNaN(value)) {\r\n return { error: 'invalid', reason: 'invalid_number' }\r\n }\r\n if (!isFinite(value)) {\r\n return { error: 'invalid', reason: 'out_of_bounds' }\r\n }\r\n const date = parseDate(value, options.properties)\r\n if (isNaN(date.valueOf())) {\r\n return { error: 'invalid', reason: 'out_of_bounds' }\r\n }\r\n return { value: date }\r\n }\r\n return { error: 'invalid', reason: 'not_a_date' }\r\n\r\n case Boolean:\r\n if (typeof value === 'boolean') {\r\n return { value }\r\n }\r\n return { error: 'invalid', reason: 'not_a_boolean' }\r\n\r\n default:\r\n if (typeof type === 'function') {\r\n return parseCustomValue(value, type)\r\n }\r\n throw new Error(`Unknown schema type: ${type && type.name || type}`)\r\n }\r\n}\r\n\r\nexport function getBlock(string, endCharacter, startIndex) {\r\n let i = 0\r\n let substring = ''\r\n let character\r\n while (startIndex + i < string.length) {\r\n const character = string[startIndex + i]\r\n if (character === endCharacter) {\r\n return [substring, i]\r\n }\r\n else if (character === '\"') {\r\n const block = getBlock(string, '\"', startIndex + i + 1)\r\n substring += block[0]\r\n i += '\"'.length + block[1] + '\"'.length\r\n }\r\n else {\r\n substring += character\r\n i++\r\n }\r\n }\r\n return [substring, i]\r\n}\r\n\r\n/**\r\n * Parses a string of comma-separated substrings into an array of substrings.\r\n * (the `export` is just for tests)\r\n * @param {string} string — A string of comma-separated substrings.\r\n * @return {string[]} An array of substrings.\r\n */\r\nexport function parseArray(string) {\r\n const blocks = []\r\n let index = 0\r\n while (index < string.length) {\r\n const [substring, length] = getBlock(string, ',', index)\r\n index += length + ','.length\r\n blocks.push(substring.trim())\r\n }\r\n return blocks\r\n}\r\n\r\n// Transpose a 2D array.\r\n// https://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript\r\nconst transpose = array => array[0].map((_, i) => array.map(row => row[i]))\r\n\r\nfunction validateSchema(schema) {\r\n for (const key of Object.keys(schema)) {\r\n const entry = schema[key]\r\n if (!entry.prop) {\r\n throw new Error(`\"prop\" not defined for schema entry \"${key}\".`)\r\n }\r\n }\r\n}"],"mappings":";;;;;;;;;;AAAA;;AAEA;;AACA;;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,IAAMA,eAAe,GAAG;EACtBC,gBAAgB,EAAE;AADI,CAAxB;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACe,kBAASC,IAAT,EAAeC,MAAf,EAAuBC,OAAvB,EAAgC;EAC7C,IAAIA,OAAJ,EAAa;IACXA,OAAO,mCACFJ,eADE,GAEFI,OAFE,CAAP;EAID,CALD,MAKO;IACLA,OAAO,GAAGJ,eAAV;EACD;;EAED,eAIII,OAJJ;EAAA,IACEH,gBADF,YACEA,gBADF;EAAA,IAEEI,MAFF,YAEEA,MAFF;EAAA,IAGEC,eAHF,YAGEA,eAHF;EAMAC,cAAc,CAACJ,MAAD,CAAd;;EAEA,IAAIF,gBAAJ,EAAsB;IACpBC,IAAI,GAAGM,SAAS,CAACN,IAAD,CAAhB;EACD;;EAED,IAAMO,OAAO,GAAGP,IAAI,CAAC,CAAD,CAApB;EAEA,IAAMQ,OAAO,GAAG,EAAhB;EACA,IAA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ApC;IAAA;IAAA,IAAOJ,SAAP;IAAA,IAAkB9D,MAAlB;;IACAkE,KAAK,IAAIlE,MAAM,GAAG,IAAIA,MAAtB;IACAiE,MAAM,CAAC9D,IAAP,CAAY2D,SAAS,CAACK,IAAV,EAAZ;EACD;;EACD,OAAOF,MAAP;AACD,C,CAED;AACA;;;AACA,IAAMtE,SAAS,GAAG,SAAZA,SAAY,CAAA2B,KAAK;EAAA,OAAIA,KAAK,CAAC,CAAD,CAAL,CAASE,GAAT,CAAa,UAAC4C,CAAD,EAAIrE,CAAJ;IAAA,OAAUuB,KAAK,CAACE,GAAN,CAAU,UAAAnB,GAAG;MAAA,OAAIA,GAAG,CAACN,CAAD,CAAP;IAAA,CAAb,CAAV;EAAA,CAAb,CAAJ;AAAA,CAAvB;;AAEA,SAASL,cAAT,CAAwBJ,MAAxB,EAAgC;EAC9B,kCAAkByC,MAAM,CAACC,IAAP,CAAY1C,MAAZ,CAAlB,qCAAuC;IAAlC,IAAMoB,GAAG,qBAAT;IACH,IAAM2D,KAAK,GAAG/E,MAAM,CAACoB,GAAD,CAApB;;IACA,IAAI,CAAC2D,KAAK,CAACvC,IAAX,EAAiB;MACf,MAAM,IAAI0B,KAAJ,mDAAkD9C,GAAlD,SAAN;IACD;EACF;AACF"}
|
|
1
|
+
{"version":3,"file":"convertToJson.js","names":["DEFAULT_OPTIONS","isColumnOriented","data","schema","options","rowMap","ignoreEmptyRows","validateSchema","transpose","columns","results","errors","i","length","result","read","push","error","row","rows","rowIndex","object","isEmptyObject","key","schemaEntry","isNestedSchema","type","Array","isArray","rawValue","indexOf","undefined","value","reason","notEmpty","array","parseArray","map","_value","parseValue","required","column","includeNullValues","prop","Object","keys","parse","parseCustomValue","parseValueOfType","oneOf","validate","message","String","StringType","Number","NumberType","Date","DateType","properties","Boolean","BooleanType","Error","name","getBlock","string","endCharacter","startIndex","substring","character","block","blocks","index","trim","_","entry"],"sources":["../../../source/read/schema/convertToJson.js"],"sourcesContent":["import NumberType from '../../types/Number.js'\r\nimport StringType from '../../types/String.js'\r\nimport BooleanType from '../../types/Boolean.js'\r\nimport DateType from '../../types/Date.js'\r\n\r\nconst DEFAULT_OPTIONS = {\r\n isColumnOriented: false\r\n}\r\n\r\n/**\r\n * Convert 2D array to nested objects.\r\n * If row oriented data, row 0 is dotted key names.\r\n * Column oriented data is transposed.\r\n * @param {any[][]} data - An array of rows, each row being an array of cells.\r\n * @param {object} schema\r\n * @return {object[]}\r\n */\r\nexport default function(data, schema, options) {\r\n if (options) {\r\n options = {\r\n ...DEFAULT_OPTIONS,\r\n ...options\r\n }\r\n } else {\r\n options = DEFAULT_OPTIONS\r\n }\r\n\r\n const {\r\n isColumnOriented,\r\n rowMap,\r\n ignoreEmptyRows\r\n } = options\r\n\r\n validateSchema(schema)\r\n\r\n if (isColumnOriented) {\r\n data = transpose(data)\r\n }\r\n\r\n const columns = data[0]\r\n\r\n const results = []\r\n const errors = []\r\n\r\n for (let i = 1; i < data.length; i++) {\r\n const result = read(schema, data[i], i, columns, errors, options)\r\n if (result !== null || ignoreEmptyRows === false) {\r\n results.push(result)\r\n }\r\n }\r\n\r\n // Correct error rows.\r\n if (rowMap) {\r\n for (const error of errors) {\r\n // Convert the `row` index in `data` to the\r\n // actual `row` index in the spreadsheet.\r\n // `- 1` converts row number to row index.\r\n // `+ 1` converts row index to row number.\r\n error.row = rowMap[error.row - 1] + 1\r\n }\r\n }\r\n\r\n return {\r\n rows: results,\r\n errors\r\n }\r\n}\r\n\r\nfunction read(schema, row, rowIndex, columns, errors, options) {\r\n const object = {}\r\n let isEmptyObject = true\r\n for (const key of Object.keys(schema)) {\r\n const schemaEntry = schema[key]\r\n const isNestedSchema = typeof schemaEntry.type === 'object' && !Array.isArray(schemaEntry.type)\r\n let rawValue = row[columns.indexOf(key)]\r\n if (rawValue === undefined) {\r\n rawValue = null\r\n }\r\n let value\r\n let error\r\n let reason\r\n if (isNestedSchema) {\r\n value = read(schemaEntry.type, row, rowIndex, columns, errors, options)\r\n } else {\r\n if (rawValue === null) {\r\n value = null\r\n }\r\n else if (Array.isArray(schemaEntry.type)) {\r\n let notEmpty = false\r\n const array = parseArray(rawValue).map((_value) => {\r\n const result = parseValue(_value, schemaEntry, options)\r\n if (result.error) {\r\n value = _value\r\n error = result.error\r\n reason = result.reason\r\n }\r\n if (result.value !== null) {\r\n notEmpty = true\r\n }\r\n return result.value\r\n })\r\n if (!error) {\r\n value = notEmpty ? array : null\r\n }\r\n } else {\r\n const result = parseValue(rawValue, schemaEntry, options)\r\n error = result.error\r\n reason = result.reason\r\n value = error ? rawValue : result.value\r\n }\r\n }\r\n if (!error && value === null && schemaEntry.required) {\r\n error = 'required'\r\n }\r\n if (error) {\r\n error = {\r\n error,\r\n row: rowIndex + 1,\r\n column: key,\r\n value\r\n }\r\n if (reason) {\r\n error.reason = reason\r\n }\r\n if (schemaEntry.type) {\r\n error.type = schemaEntry.type\r\n }\r\n errors.push(error)\r\n } else {\r\n if (isEmptyObject && value !== null) {\r\n isEmptyObject = false\r\n }\r\n if (value !== null || options.includeNullValues) {\r\n object[schemaEntry.prop] = value\r\n }\r\n }\r\n }\r\n if (isEmptyObject) {\r\n return null\r\n }\r\n return object\r\n}\r\n\r\n/**\r\n * Converts textual value to a javascript typed value.\r\n * @param {any} value\r\n * @param {object} schemaEntry\r\n * @return {{ value: any, error: string }}\r\n */\r\nexport function parseValue(value, schemaEntry, options) {\r\n if (value === null) {\r\n return { value: null }\r\n }\r\n let result\r\n if (schemaEntry.parse) {\r\n result = parseCustomValue(value, schemaEntry.parse)\r\n } else if (schemaEntry.type) {\r\n result = parseValueOfType(\r\n value,\r\n // Supports parsing array types.\r\n // See `parseArray()` function for more details.\r\n // Example `type`: String[]\r\n // Input: 'Barack Obama, \"String, with, colons\", Donald Trump'\r\n // Output: ['Barack Obama', 'String, with, colons', 'Donald Trump']\r\n Array.isArray(schemaEntry.type) ? schemaEntry.type[0] : schemaEntry.type,\r\n options\r\n )\r\n } else {\r\n result = { value: value }\r\n // throw new Error('Invalid schema entry: no .type and no .parse():\\n\\n' + JSON.stringify(schemaEntry, null, 2))\r\n }\r\n // If errored then return the error.\r\n if (result.error) {\r\n return result\r\n }\r\n if (result.value !== null) {\r\n if (schemaEntry.oneOf && schemaEntry.oneOf.indexOf(result.value) < 0) {\r\n return { error: 'invalid', reason: 'unknown' }\r\n }\r\n if (schemaEntry.validate) {\r\n try {\r\n schemaEntry.validate(result.value)\r\n } catch (error) {\r\n return { error: error.message }\r\n }\r\n }\r\n }\r\n return result\r\n}\r\n\r\n/**\r\n * Converts textual value to a custom value using supplied `.parse()`.\r\n * @param {any} value\r\n * @param {function} parse\r\n * @return {{ value: any, error: string }}\r\n */\r\nfunction parseCustomValue(value, parse) {\r\n try {\r\n value = parse(value)\r\n if (value === undefined) {\r\n return { value: null }\r\n }\r\n return { value }\r\n } catch (error) {\r\n const result = { error: error.message }\r\n if (error.reason) {\r\n result.reason = error.reason;\r\n }\r\n return result\r\n }\r\n}\r\n\r\n/**\r\n * Converts textual value to a javascript typed value.\r\n * @param {any} value\r\n * @param {} type\r\n * @return {{ value: (string|number|Date|boolean), error: string, reason?: string }}\r\n */\r\nfunction parseValueOfType(value, type, options) {\r\n switch (type) {\r\n case String:\r\n return parseCustomValue(value, StringType)\r\n\r\n case Number:\r\n return parseCustomValue(value, NumberType)\r\n\r\n case Date:\r\n return parseCustomValue(value, (value) => DateType(value, { properties: options.properties }))\r\n\r\n case Boolean:\r\n return parseCustomValue(value, BooleanType)\r\n\r\n default:\r\n if (typeof type === 'function') {\r\n return parseCustomValue(value, type)\r\n }\r\n throw new Error(`Unsupported schema type: ${type && type.name || type}`)\r\n }\r\n}\r\n\r\nexport function getBlock(string, endCharacter, startIndex) {\r\n let i = 0\r\n let substring = ''\r\n let character\r\n while (startIndex + i < string.length) {\r\n const character = string[startIndex + i]\r\n if (character === endCharacter) {\r\n return [substring, i]\r\n }\r\n else if (character === '\"') {\r\n const block = getBlock(string, '\"', startIndex + i + 1)\r\n substring += block[0]\r\n i += '\"'.length + block[1] + '\"'.length\r\n }\r\n else {\r\n substring += character\r\n i++\r\n }\r\n }\r\n return [substring, i]\r\n}\r\n\r\n/**\r\n * Parses a string of comma-separated substrings into an array of substrings.\r\n * (the `export` is just for tests)\r\n * @param {string} string — A string of comma-separated substrings.\r\n * @return {string[]} An array of substrings.\r\n */\r\nexport function parseArray(string) {\r\n const blocks = []\r\n let index = 0\r\n while (index < string.length) {\r\n const [substring, length] = getBlock(string, ',', index)\r\n index += length + ','.length\r\n blocks.push(substring.trim())\r\n }\r\n return blocks\r\n}\r\n\r\n// Transpose a 2D array.\r\n// https://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript\r\nconst transpose = array => array[0].map((_, i) => array.map(row => row[i]))\r\n\r\nfunction validateSchema(schema) {\r\n for (const key of Object.keys(schema)) {\r\n const entry = schema[key]\r\n if (!entry.prop) {\r\n throw new Error(`\"prop\" not defined for schema entry \"${key}\".`)\r\n }\r\n }\r\n}"],"mappings":";;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,IAAMA,eAAe,GAAG;EACtBC,gBAAgB,EAAE;AADI,CAAxB;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACe,kBAASC,IAAT,EAAeC,MAAf,EAAuBC,OAAvB,EAAgC;EAC7C,IAAIA,OAAJ,EAAa;IACXA,OAAO,mCACFJ,eADE,GAEFI,OAFE,CAAP;EAID,CALD,MAKO;IACLA,OAAO,GAAGJ,eAAV;EACD;;EAED,eAIII,OAJJ;EAAA,IACEH,gBADF,YACEA,gBADF;EAAA,IAEEI,MAFF,YAEEA,MAFF;EAAA,IAGEC,eAHF,YAGEA,eAHF;EAMAC,cAAc,CAACJ,MAAD,CAAd;;EAEA,IAAIF,gBAAJ,EAAsB;IACpBC,IAAI,GAAGM,SAAS,CAACN,IAAD,CAAhB;EACD;;EAED,IAAMO,OAAO,GAAGP,IAAI,CAAC,CAAD,CAApB;E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UuB,KAAK,CAACE,GAAN,CAAU,UAAAnB,GAAG;MAAA,OAAIA,GAAG,CAACN,CAAD,CAAP;IAAA,CAAb,CAAV;EAAA,CAAb,CAAJ;AAAA,CAAvB;;AAEA,SAASL,cAAT,CAAwBJ,MAAxB,EAAgC;EAC9B,kCAAkByC,MAAM,CAACC,IAAP,CAAY1C,MAAZ,CAAlB,qCAAuC;IAAlC,IAAMoB,GAAG,qBAAT;IACH,IAAMmD,KAAK,GAAGvE,MAAM,CAACoB,GAAD,CAApB;;IACA,IAAI,CAACmD,KAAK,CAAC/B,IAAX,EAAiB;MACf,MAAM,IAAIkB,KAAJ,mDAAkDtC,GAAlD,SAAN;IACD;EACF;AACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = BooleanType;
|
|
7
|
+
|
|
8
|
+
var _InvalidError = _interopRequireDefault(require("./InvalidError.js"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
11
|
+
|
|
12
|
+
function BooleanType(value) {
|
|
13
|
+
if (typeof value === 'boolean') {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
throw new _InvalidError["default"]('not_a_boolean');
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=Boolean.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Boolean.js","names":["BooleanType","value","InvalidError"],"sources":["../../source/types/Boolean.js"],"sourcesContent":["import InvalidError from './InvalidError.js'\r\n\r\nexport default function BooleanType(value) {\r\n\tif (typeof value === 'boolean') {\r\n return value\r\n }\r\n throw new InvalidError('not_a_boolean')\r\n}"],"mappings":";;;;;;;AAAA;;;;AAEe,SAASA,WAAT,CAAqBC,KAArB,EAA4B;EAC1C,IAAI,OAAOA,KAAP,KAAiB,SAArB,EAAgC;IAC7B,OAAOA,KAAP;EACD;;EACD,MAAM,IAAIC,wBAAJ,CAAiB,eAAjB,CAAN;AACD"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = DateType;
|
|
7
|
+
|
|
8
|
+
var _parseDate = _interopRequireDefault(require("../read/parseDate.js"));
|
|
9
|
+
|
|
10
|
+
var _InvalidError = _interopRequireDefault(require("./InvalidError.js"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
+
|
|
14
|
+
function DateType(value, _ref) {
|
|
15
|
+
var properties = _ref.properties;
|
|
16
|
+
|
|
17
|
+
// XLSX has no specific format for dates.
|
|
18
|
+
// Sometimes a date can be heuristically detected.
|
|
19
|
+
// https://github.com/catamphetamine/read-excel-file/issues/3#issuecomment-395770777
|
|
20
|
+
if (value instanceof Date) {
|
|
21
|
+
if (isNaN(value.valueOf())) {
|
|
22
|
+
throw new _InvalidError["default"]('out_of_bounds');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof value === 'number') {
|
|
29
|
+
if (isNaN(value)) {
|
|
30
|
+
throw new _InvalidError["default"]('invalid_number');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!isFinite(value)) {
|
|
34
|
+
throw new _InvalidError["default"]('out_of_bounds');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
var date = (0, _parseDate["default"])(value, properties);
|
|
38
|
+
|
|
39
|
+
if (isNaN(date.valueOf())) {
|
|
40
|
+
throw new _InvalidError["default"]('out_of_bounds');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return date;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
throw new _InvalidError["default"]('not_a_date');
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=Date.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Date.js","names":["DateType","value","properties","Date","isNaN","valueOf","InvalidError","isFinite","date","parseDate"],"sources":["../../source/types/Date.js"],"sourcesContent":["import parseDate from '../read/parseDate.js'\r\nimport InvalidError from './InvalidError.js'\r\n\r\nexport default function DateType(value, { properties }) {\r\n\t// XLSX has no specific format for dates.\r\n // Sometimes a date can be heuristically detected.\r\n // https://github.com/catamphetamine/read-excel-file/issues/3#issuecomment-395770777\r\n if (value instanceof Date) {\r\n if (isNaN(value.valueOf())) {\r\n throw new InvalidError('out_of_bounds')\r\n }\r\n return value\r\n }\r\n if (typeof value === 'number') {\r\n if (isNaN(value)) {\r\n throw new InvalidError('invalid_number')\r\n }\r\n if (!isFinite(value)) {\r\n throw new InvalidError('out_of_bounds')\r\n }\r\n const date = parseDate(value, properties)\r\n if (isNaN(date.valueOf())) {\r\n throw new InvalidError('out_of_bounds')\r\n }\r\n return date\r\n }\r\n throw new InvalidError('not_a_date')\r\n}"],"mappings":";;;;;;;AAAA;;AACA;;;;AAEe,SAASA,QAAT,CAAkBC,KAAlB,QAAyC;EAAA,IAAdC,UAAc,QAAdA,UAAc;;EACvD;EACC;EACA;EACA,IAAID,KAAK,YAAYE,IAArB,EAA2B;IACzB,IAAIC,KAAK,CAACH,KAAK,CAACI,OAAN,EAAD,CAAT,EAA4B;MAC1B,MAAM,IAAIC,wBAAJ,CAAiB,eAAjB,CAAN;IACD;;IACD,OAAOL,KAAP;EACD;;EACD,IAAI,OAAOA,KAAP,KAAiB,QAArB,EAA+B;IAC7B,IAAIG,KAAK,CAACH,KAAD,CAAT,EAAkB;MAChB,MAAM,IAAIK,wBAAJ,CAAiB,gBAAjB,CAAN;IACD;;IACD,IAAI,CAACC,QAAQ,CAACN,KAAD,CAAb,EAAsB;MACpB,MAAM,IAAIK,wBAAJ,CAAiB,eAAjB,CAAN;IACD;;IACD,IAAME,IAAI,GAAG,IAAAC,qBAAA,EAAUR,KAAV,EAAiBC,UAAjB,CAAb;;IACA,IAAIE,KAAK,CAACI,IAAI,CAACH,OAAL,EAAD,CAAT,EAA2B;MACzB,MAAM,IAAIC,wBAAJ,CAAiB,eAAjB,CAAN;IACD;;IACD,OAAOE,IAAP;EACD;;EACD,MAAM,IAAIF,wBAAJ,CAAiB,YAAjB,CAAN;AACD"}
|
package/commonjs/types/Email.js
CHANGED
|
@@ -6,7 +6,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports["default"] = Email;
|
|
7
7
|
exports.isEmail = isEmail;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
var _InvalidError = _interopRequireDefault(require("./InvalidError.js"));
|
|
10
|
+
|
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
12
|
+
|
|
13
|
+
function Email(value) {
|
|
14
|
+
if (typeof value === 'string') {
|
|
15
|
+
if (isEmail(value)) {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
throw new _InvalidError["default"]('not_an_email');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
throw new _InvalidError["default"]('not_a_string');
|
|
23
|
+
}
|
|
10
24
|
|
|
11
25
|
var regexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
|
|
12
26
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Email.js","names":["Email","
|
|
1
|
+
{"version":3,"file":"Email.js","names":["Email","value","isEmail","InvalidError","regexp","test"],"sources":["../../source/types/Email.js"],"sourcesContent":["import InvalidError from './InvalidError.js'\r\n\r\nexport default function Email(value) {\r\n if (typeof value === 'string') {\r\n if (isEmail(value)) {\r\n return value\r\n }\r\n throw new InvalidError('not_an_email')\r\n }\r\n throw new InvalidError('not_a_string')\r\n}\r\n\r\nconst regexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}$/i\r\n\r\nexport function isEmail(value) {\r\n\treturn regexp.test(value)\r\n}"],"mappings":";;;;;;;;AAAA;;;;AAEe,SAASA,KAAT,CAAeC,KAAf,EAAsB;EACnC,IAAI,OAAOA,KAAP,KAAiB,QAArB,EAA+B;IAC7B,IAAIC,OAAO,CAACD,KAAD,CAAX,EAAoB;MAClB,OAAOA,KAAP;IACD;;IACD,MAAM,IAAIE,wBAAJ,CAAiB,cAAjB,CAAN;EACD;;EACD,MAAM,IAAIA,wBAAJ,CAAiB,cAAjB,CAAN;AACD;;AAED,IAAMC,MAAM,GAAG,0CAAf;;AAEO,SAASF,OAAT,CAAiBD,KAAjB,EAAwB;EAC9B,OAAOG,MAAM,CAACC,IAAP,CAAYJ,KAAZ,CAAP;AACA"}
|
|
@@ -6,7 +6,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports["default"] = Integer;
|
|
7
7
|
exports.isInteger = isInteger;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
var _InvalidError = _interopRequireDefault(require("./InvalidError.js"));
|
|
10
|
+
|
|
11
|
+
var _Number = _interopRequireDefault(require("./Number.js"));
|
|
12
|
+
|
|
13
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
14
|
+
|
|
15
|
+
function Integer(value) {
|
|
16
|
+
value = (0, _Number["default"])(value);
|
|
17
|
+
|
|
18
|
+
if (!isInteger(value)) {
|
|
19
|
+
throw new _InvalidError["default"]('not_an_integer');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
10
24
|
|
|
11
25
|
function isInteger(x) {
|
|
12
26
|
// https://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Integer.js","names":["Integer","isInteger","x"],"sources":["../../source/types/Integer.js"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"Integer.js","names":["Integer","value","NumberType","isInteger","InvalidError","x"],"sources":["../../source/types/Integer.js"],"sourcesContent":["import InvalidError from './InvalidError.js'\r\nimport NumberType from './Number.js'\r\n\r\nexport default function Integer(value) {\r\n\tvalue = NumberType(value)\r\n if (!isInteger(value)) {\r\n throw new InvalidError('not_an_integer')\r\n }\r\n return value\r\n}\r\n\r\nexport function isInteger(x) {\r\n\t// https://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript\r\n\treturn (x | 0) === x\r\n}"],"mappings":";;;;;;;;AAAA;;AACA;;;;AAEe,SAASA,OAAT,CAAiBC,KAAjB,EAAwB;EACtCA,KAAK,GAAG,IAAAC,kBAAA,EAAWD,KAAX,CAAR;;EACC,IAAI,CAACE,SAAS,CAACF,KAAD,CAAd,EAAuB;IACrB,MAAM,IAAIG,wBAAJ,CAAiB,gBAAjB,CAAN;EACD;;EACD,OAAOH,KAAP;AACD;;AAEM,SAASE,SAAT,CAAmBE,CAAnB,EAAsB;EAC5B;EACA,OAAO,CAACA,CAAC,GAAG,CAAL,MAAYA,CAAnB;AACA"}
|