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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convertToJson.js","names":["parseDate","Integer","isInteger","URL","isURL","Email","isEmail","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","stringifiedValue","Date","valueOf","date","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,OAAOA,SAAP,MAAsB,iBAAtB;AAEA,OAAOC,OAAP,IAAkBC,SAAlB,QAAmC,wBAAnC;AACA,OAAOC,GAAP,IAAcC,KAAd,QAA2B,oBAA3B;AACA,OAAOC,KAAP,IAAgBC,OAAhB,QAA+B,sBAA/B;AAEA,IAAMC,eAAe,GAAG;EACtBC,gBAAgB,EAAE;AADI,CAAxB;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,UAASC,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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EAA4B;EACjC,IAAMM,MAAM,GAAG,EAAf;EACA,IAAIC,KAAK,GAAG,CAAZ;;EACA,OAAOA,KAAK,GAAGP,MAAM,CAACpD,MAAtB,EAA8B;IAC5B,gBAA4BmD,QAAQ,CAACC,MAAD,EAAS,GAAT,EAAcO,KAAd,CAApC;IAAA;IAAA,IAAOJ,SAAP;IAAA,IAAkBvD,MAAlB;;IACA2D,KAAK,IAAI3D,MAAM,GAAG,IAAIA,MAAtB;IACA0D,MAAM,CAACvD,IAAP,CAAYoD,SAAS,CAACK,IAAV,EAAZ;EACD;;EACD,OAAOF,MAAP;AACD,C,CAED;AACA;;AACA,IAAM/D,SAAS,GAAG,SAAZA,SAAY,CAAA2B,KAAK;EAAA,OAAIA,KAAK,CAAC,CAAD,CAAL,CAASE,GAAT,CAAa,UAACqC,CAAD,EAAI9D,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,IAAMoD,KAAK,GAAGxE,MAAM,CAACoB,GAAD,CAApB;;IACA,IAAI,CAACoD,KAAK,CAAChC,IAAX,EAAiB;MACf,MAAM,IAAImB,KAAJ,mDAAkDvC,GAAlD,SAAN;IACD;EACF;AACF"}
|
|
1
|
+
{"version":3,"file":"convertToJson.js","names":["NumberType","StringType","BooleanType","DateType","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","Number","Date","properties","Boolean","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,OAAOA,UAAP,MAAuB,uBAAvB;AACA,OAAOC,UAAP,MAAuB,uBAAvB;AACA,OAAOC,WAAP,MAAwB,wBAAxB;AACA,OAAOC,QAAP,MAAqB,qBAArB;AAEA,IAAMC,eAAe,GAAG;EACtBC,gBAAgB,EAAE;AADI,CAAxB;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,UAASC,IAAT,EAAeC,MAAf,EAAuBC,OAAvB,EAAgC;EAC7C,IAAIA,OAAJ,EAAa;IACXA,OAAO,mCACFJ,eADE,GAEFI,OAFE,CAAP;EAID,CALD,MAKO;IACLA,OAAO,GAAGJ,eAAV;E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AK,IAAItD,MAAM,GAAG,IAAIA,MAAtB;IACAqD,MAAM,CAAClD,IAAP,CAAY+C,SAAS,CAACK,IAAV,EAAZ;EACD;;EACD,OAAOF,MAAP;AACD,C,CAED;AACA;;AACA,IAAM1D,SAAS,GAAG,SAAZA,SAAY,CAAA2B,KAAK;EAAA,OAAIA,KAAK,CAAC,CAAD,CAAL,CAASE,GAAT,CAAa,UAACgC,CAAD,EAAIzD,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,IAAM+C,KAAK,GAAGnE,MAAM,CAACoB,GAAD,CAApB;;IACA,IAAI,CAAC+C,KAAK,CAAC3B,IAAX,EAAiB;MACf,MAAM,IAAIc,KAAJ,mDAAkDlC,GAAlD,SAAN;IACD;EACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Boolean.js","names":["InvalidError","BooleanType","value"],"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,OAAOA,YAAP,MAAyB,mBAAzB;AAEA,eAAe,SAASC,WAAT,CAAqBC,KAArB,EAA4B;EAC1C,IAAI,OAAOA,KAAP,KAAiB,SAArB,EAAgC;IAC7B,OAAOA,KAAP;EACD;;EACD,MAAM,IAAIF,YAAJ,CAAiB,eAAjB,CAAN;AACD"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import parseDate from '../read/parseDate.js';
|
|
2
|
+
import InvalidError from './InvalidError.js';
|
|
3
|
+
export default function DateType(value, _ref) {
|
|
4
|
+
var properties = _ref.properties;
|
|
5
|
+
|
|
6
|
+
// XLSX has no specific format for dates.
|
|
7
|
+
// Sometimes a date can be heuristically detected.
|
|
8
|
+
// https://github.com/catamphetamine/read-excel-file/issues/3#issuecomment-395770777
|
|
9
|
+
if (value instanceof Date) {
|
|
10
|
+
if (isNaN(value.valueOf())) {
|
|
11
|
+
throw new InvalidError('out_of_bounds');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (typeof value === 'number') {
|
|
18
|
+
if (isNaN(value)) {
|
|
19
|
+
throw new InvalidError('invalid_number');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!isFinite(value)) {
|
|
23
|
+
throw new InvalidError('out_of_bounds');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var date = parseDate(value, properties);
|
|
27
|
+
|
|
28
|
+
if (isNaN(date.valueOf())) {
|
|
29
|
+
throw new InvalidError('out_of_bounds');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return date;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
throw new InvalidError('not_a_date');
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=Date.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Date.js","names":["parseDate","InvalidError","DateType","value","properties","Date","isNaN","valueOf","isFinite","date"],"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,OAAOA,SAAP,MAAsB,sBAAtB;AACA,OAAOC,YAAP,MAAyB,mBAAzB;AAEA,eAAe,SAASC,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,IAAIN,YAAJ,CAAiB,eAAjB,CAAN;IACD;;IACD,OAAOE,KAAP;EACD;;EACD,IAAI,OAAOA,KAAP,KAAiB,QAArB,EAA+B;IAC7B,IAAIG,KAAK,CAACH,KAAD,CAAT,EAAkB;MAChB,MAAM,IAAIF,YAAJ,CAAiB,gBAAjB,CAAN;IACD;;IACD,IAAI,CAACO,QAAQ,CAACL,KAAD,CAAb,EAAsB;MACpB,MAAM,IAAIF,YAAJ,CAAiB,eAAjB,CAAN;IACD;;IACD,IAAMQ,IAAI,GAAGT,SAAS,CAACG,KAAD,EAAQC,UAAR,CAAtB;;IACA,IAAIE,KAAK,CAACG,IAAI,CAACF,OAAL,EAAD,CAAT,EAA2B;MACzB,MAAM,IAAIN,YAAJ,CAAiB,eAAjB,CAAN;IACD;;IACD,OAAOQ,IAAP;EACD;;EACD,MAAM,IAAIR,YAAJ,CAAiB,YAAjB,CAAN;AACD"}
|
package/modules/types/Email.js
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
import InvalidError from './InvalidError.js';
|
|
2
|
+
export default function Email(value) {
|
|
3
|
+
if (typeof value === 'string') {
|
|
4
|
+
if (isEmail(value)) {
|
|
5
|
+
return value;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
throw new InvalidError('not_an_email');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
throw new InvalidError('not_a_string');
|
|
12
|
+
}
|
|
2
13
|
var regexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
|
|
3
14
|
export function isEmail(value) {
|
|
4
15
|
return regexp.test(value);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Email.js","names":["Email","
|
|
1
|
+
{"version":3,"file":"Email.js","names":["InvalidError","Email","value","isEmail","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,OAAOA,YAAP,MAAyB,mBAAzB;AAEA,eAAe,SAASC,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,IAAIF,YAAJ,CAAiB,cAAjB,CAAN;EACD;;EACD,MAAM,IAAIA,YAAJ,CAAiB,cAAjB,CAAN;AACD;AAED,IAAMI,MAAM,GAAG,0CAAf;AAEA,OAAO,SAASD,OAAT,CAAiBD,KAAjB,EAAwB;EAC9B,OAAOE,MAAM,CAACC,IAAP,CAAYH,KAAZ,CAAP;AACA"}
|
package/modules/types/Integer.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
import InvalidError from './InvalidError.js';
|
|
2
|
+
import NumberType from './Number.js';
|
|
3
|
+
export default function Integer(value) {
|
|
4
|
+
value = NumberType(value);
|
|
5
|
+
|
|
6
|
+
if (!isInteger(value)) {
|
|
7
|
+
throw new InvalidError('not_an_integer');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
2
12
|
export function isInteger(x) {
|
|
3
13
|
// https://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript
|
|
4
14
|
return (x | 0) === x;
|
|
@@ -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":["InvalidError","NumberType","Integer","value","isInteger","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,OAAOA,YAAP,MAAyB,mBAAzB;AACA,OAAOC,UAAP,MAAuB,aAAvB;AAEA,eAAe,SAASC,OAAT,CAAiBC,KAAjB,EAAwB;EACtCA,KAAK,GAAGF,UAAU,CAACE,KAAD,CAAlB;;EACC,IAAI,CAACC,SAAS,CAACD,KAAD,CAAd,EAAuB;IACrB,MAAM,IAAIH,YAAJ,CAAiB,gBAAjB,CAAN;EACD;;EACD,OAAOG,KAAP;AACD;AAED,OAAO,SAASC,SAAT,CAAmBC,CAAnB,EAAsB;EAC5B;EACA,OAAO,CAACA,CAAC,GAAG,CAAL,MAAYA,CAAnB;AACA"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
+
|
|
3
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
4
|
+
|
|
5
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
6
|
+
|
|
7
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
8
|
+
|
|
9
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
10
|
+
|
|
11
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
12
|
+
|
|
13
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
14
|
+
|
|
15
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
16
|
+
|
|
17
|
+
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
|
18
|
+
|
|
19
|
+
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
|
|
20
|
+
|
|
21
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
22
|
+
|
|
23
|
+
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
|
|
24
|
+
|
|
25
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
26
|
+
|
|
27
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
28
|
+
|
|
29
|
+
var InvalidError = /*#__PURE__*/function (_Error) {
|
|
30
|
+
_inherits(InvalidError, _Error);
|
|
31
|
+
|
|
32
|
+
var _super = _createSuper(InvalidError);
|
|
33
|
+
|
|
34
|
+
function InvalidError(reason) {
|
|
35
|
+
var _this;
|
|
36
|
+
|
|
37
|
+
_classCallCheck(this, InvalidError);
|
|
38
|
+
|
|
39
|
+
_this = _super.call(this, 'invalid');
|
|
40
|
+
_this.reason = reason;
|
|
41
|
+
return _this;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return _createClass(InvalidError);
|
|
45
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
46
|
+
|
|
47
|
+
export { InvalidError as default };
|
|
48
|
+
//# sourceMappingURL=InvalidError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InvalidError.js","names":["InvalidError","reason","Error"],"sources":["../../source/types/InvalidError.js"],"sourcesContent":["export default class InvalidError extends Error {\r\n constructor(reason) {\r\n super('invalid')\r\n this.reason = reason\r\n }\r\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;IAAqBA,Y;;;;;EACnB,sBAAYC,MAAZ,EAAoB;IAAA;;IAAA;;IAClB,0BAAM,SAAN;IACA,MAAKA,MAAL,GAAcA,MAAd;IAFkB;EAGnB;;;iCAJuCC,K;;SAArBF,Y"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import InvalidError from './InvalidError.js';
|
|
2
|
+
export default function NumberType(value) {
|
|
3
|
+
// An XLSX file editing software might not always correctly
|
|
4
|
+
// detect numeric values in string-type cells. Users won't bother
|
|
5
|
+
// manually selecting a cell type, so the editing software has to guess
|
|
6
|
+
// based on the user's input. One can assume that such auto-detection
|
|
7
|
+
// might not always work.
|
|
8
|
+
//
|
|
9
|
+
// So, if a cell is supposed to be a numeric one, convert a string value to a number.
|
|
10
|
+
//
|
|
11
|
+
if (typeof value === 'string') {
|
|
12
|
+
var stringifiedValue = value;
|
|
13
|
+
value = Number(value);
|
|
14
|
+
|
|
15
|
+
if (String(value) !== stringifiedValue) {
|
|
16
|
+
throw new InvalidError('not_a_number');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (typeof value !== 'number') {
|
|
21
|
+
throw new InvalidError('not_a_number');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (isNaN(value)) {
|
|
25
|
+
throw new InvalidError('invalid_number');
|
|
26
|
+
} // At this point, `value` can only be a number.
|
|
27
|
+
//
|
|
28
|
+
// The global `isFinite()` function filters out:
|
|
29
|
+
// * NaN
|
|
30
|
+
// * -Infinity
|
|
31
|
+
// * Infinity
|
|
32
|
+
//
|
|
33
|
+
// All other values pass (including non-numbers).
|
|
34
|
+
//
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
if (!isFinite(value)) {
|
|
38
|
+
throw new InvalidError('out_of_bounds');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=Number.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Number.js","names":["InvalidError","NumberType","value","stringifiedValue","Number","String","isNaN","isFinite"],"sources":["../../source/types/Number.js"],"sourcesContent":["import InvalidError from './InvalidError.js'\r\n\r\nexport default function NumberType(value) {\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 throw new InvalidError('not_a_number')\r\n }\r\n }\r\n if (typeof value !== 'number') {\r\n throw new InvalidError('not_a_number')\r\n }\r\n if (isNaN(value)) {\r\n throw new InvalidError('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 throw new InvalidError('out_of_bounds')\r\n }\r\n return value\r\n}"],"mappings":"AAAA,OAAOA,YAAP,MAAyB,mBAAzB;AAEA,eAAe,SAASC,UAAT,CAAoBC,KAApB,EAA2B;EACxC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,IAAI,OAAOA,KAAP,KAAiB,QAArB,EAA+B;IAC7B,IAAMC,gBAAgB,GAAGD,KAAzB;IACAA,KAAK,GAAGE,MAAM,CAACF,KAAD,CAAd;;IACA,IAAIG,MAAM,CAACH,KAAD,CAAN,KAAkBC,gBAAtB,EAAwC;MACtC,MAAM,IAAIH,YAAJ,CAAiB,cAAjB,CAAN;IACD;EACF;;EACD,IAAI,OAAOE,KAAP,KAAiB,QAArB,EAA+B;IAC7B,MAAM,IAAIF,YAAJ,CAAiB,cAAjB,CAAN;EACD;;EACD,IAAIM,KAAK,CAACJ,KAAD,CAAT,EAAkB;IAChB,MAAM,IAAIF,YAAJ,CAAiB,gBAAjB,CAAN;EACD,CArBuC,CAsBxC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;EACA,IAAI,CAACO,QAAQ,CAACL,KAAD,CAAb,EAAsB;IACpB,MAAM,IAAIF,YAAJ,CAAiB,eAAjB,CAAN;EACD;;EACD,OAAOE,KAAP;AACD"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import InvalidError from './InvalidError.js';
|
|
2
|
+
export default function StringType(value) {
|
|
3
|
+
if (typeof value === 'string') {
|
|
4
|
+
return value;
|
|
5
|
+
} // Excel tends to perform a forced automatic convertion of string-type values
|
|
6
|
+
// to number-type ones when the user has input them. Otherwise, users wouldn't
|
|
7
|
+
// be able to perform formula calculations on those cell values because users
|
|
8
|
+
// won't bother manually choosing a "numeric" cell type for each cell, and
|
|
9
|
+
// even if they did, choosing a "numeric" cell type every time wouldn't be an
|
|
10
|
+
// acceptable "user experience".
|
|
11
|
+
//
|
|
12
|
+
// So, if a cell value is supposed to be a string and Excel has automatically
|
|
13
|
+
// converted it to a number, perform a backwards conversion.
|
|
14
|
+
//
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
if (typeof value === 'number') {
|
|
18
|
+
if (isNaN(value)) {
|
|
19
|
+
throw new InvalidError('invalid_number');
|
|
20
|
+
} // The global `isFinite()` function filters out:
|
|
21
|
+
// * NaN
|
|
22
|
+
// * -Infinity
|
|
23
|
+
// * Infinity
|
|
24
|
+
//
|
|
25
|
+
// All other values pass (including non-numbers).
|
|
26
|
+
//
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
if (!isFinite(value)) {
|
|
30
|
+
throw new InvalidError('out_of_bounds');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return String(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
throw new InvalidError('not_a_string');
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=String.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"String.js","names":["InvalidError","StringType","value","isNaN","isFinite","String"],"sources":["../../source/types/String.js"],"sourcesContent":["import InvalidError from './InvalidError.js'\r\n\r\nexport default function StringType(value) {\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 throw new InvalidError('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 throw new InvalidError('out_of_bounds')\r\n }\r\n return String(value)\r\n }\r\n throw new InvalidError('not_a_string')\r\n}"],"mappings":"AAAA,OAAOA,YAAP,MAAyB,mBAAzB;AAEA,eAAe,SAASC,UAAT,CAAoBC,KAApB,EAA2B;EACxC,IAAI,OAAOA,KAAP,KAAiB,QAArB,EAA+B;IAC7B,OAAOA,KAAP;EACD,CAHuC,CAIxC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;EACA,IAAI,OAAOA,KAAP,KAAiB,QAArB,EAA+B;IAC7B,IAAIC,KAAK,CAACD,KAAD,CAAT,EAAkB;MAChB,MAAM,IAAIF,YAAJ,CAAiB,gBAAjB,CAAN;IACD,CAH4B,CAI7B;IACA;IACA;IACA;IACA;IACA;IACA;;;IACA,IAAI,CAACI,QAAQ,CAACF,KAAD,CAAb,EAAsB;MACpB,MAAM,IAAIF,YAAJ,CAAiB,eAAjB,CAAN;IACD;;IACD,OAAOK,MAAM,CAACH,KAAD,CAAb;EACD;;EACD,MAAM,IAAIF,YAAJ,CAAiB,cAAjB,CAAN;AACD"}
|
package/modules/types/URL.js
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
import InvalidError from './InvalidError.js';
|
|
2
|
+
export default function URL(value) {
|
|
3
|
+
if (typeof value === 'string') {
|
|
4
|
+
if (isURL(value)) {
|
|
5
|
+
return value;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
throw new InvalidError('not_a_url');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
throw new InvalidError('not_a_string');
|
|
12
|
+
} // URL regexp explanation:
|
|
2
13
|
//
|
|
3
14
|
// /^
|
|
4
15
|
//
|
package/modules/types/URL.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"URL.js","names":["URL","
|
|
1
|
+
{"version":3,"file":"URL.js","names":["InvalidError","URL","value","isURL","regexp","test"],"sources":["../../source/types/URL.js"],"sourcesContent":["import InvalidError from './InvalidError.js'\r\n\r\nexport default function URL(value) {\r\n if (typeof value === 'string') {\r\n if (isURL(value)) {\r\n return value\r\n }\r\n throw new InvalidError('not_a_url')\r\n }\r\n throw new InvalidError('not_a_string')\r\n}\r\n\r\n// URL regexp explanation:\r\n//\r\n// /^\r\n//\r\n// \t(?:\r\n// \t // Matches optional \"http(s):\" or \"ftp:\":\r\n// \t\t(?:\r\n// \t\t\t(?:https?|ftp):\r\n// \t\t)?\r\n//\r\n// \t // Matches \"//\" (required):\r\n// \t\t\\/\\/\r\n// \t)\r\n//\r\n// \t// Matches a valid non-local IP address:\r\n// \t(?:\r\n// \t\t(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])\r\n// \t\t(?:\r\n// \t\t\t\\.\r\n// \t\t\t(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])\r\n// \t\t){2}\r\n// \t\t(?:\r\n// \t\t\t\\.\r\n// \t\t\t(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4])\r\n// \t\t)\r\n//\r\n// \t // Or,\r\n// \t\t|\r\n//\r\n// \t // Matches an alpha-numeric domain name.\r\n// \t\t(?:\r\n// \t\t\t(?:\r\n// \t\t\t\t[a-z0-9\\u00a1-\\uffff]\r\n// \t\t\t\t[a-z0-9\\u00a1-\\uffff_-]{0,62}\r\n// \t\t\t)?\r\n// \t\t\t[a-z0-9\\u00a1-\\uffff]\r\n// \t\t\t\\.\r\n// \t\t)*\r\n// \t\t(?:\r\n// \t // Domain zone: \"com\", \"net\", etc (required):\r\n// \t\t\t[a-z\\u00a1-\\uffff]{2,}\r\n// \t\t)\r\n// \t)\r\n//\r\n// \t// Matches a colon and a port number:\r\n// \t(?::\\d{2,5})?\r\n//\r\n// \t// Matches everything after the \"origin\":\r\n// \t// * pathname\r\n// \t// * query\r\n// \t// * hash\r\n// \t(?:[/?#]\\S*)?\r\n//\r\n// $/i\r\n\r\nconst regexp = /^(?:(?:(?:https?|ftp):)?\\/\\/)(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z0-9\\u00a1-\\uffff][a-z0-9\\u00a1-\\uffff_-]{0,62})?[a-z0-9\\u00a1-\\uffff]\\.)*(?:[a-z\\u00a1-\\uffff]{2,}))(?::\\d{2,5})?(?:[/?#]\\S*)?$/i\r\n\r\n// https://stackoverflow.com/questions/8667070/javascript-regular-expression-to-validate-url\r\nexport function isURL(value) {\r\n\treturn regexp.test(value)\r\n}"],"mappings":"AAAA,OAAOA,YAAP,MAAyB,mBAAzB;AAEA,eAAe,SAASC,GAAT,CAAaC,KAAb,EAAoB;EACjC,IAAI,OAAOA,KAAP,KAAiB,QAArB,EAA+B;IAC7B,IAAIC,KAAK,CAACD,KAAD,CAAT,EAAkB;MAChB,OAAOA,KAAP;IACD;;IACD,MAAM,IAAIF,YAAJ,CAAiB,WAAjB,CAAN;EACD;;EACD,MAAM,IAAIA,YAAJ,CAAiB,cAAjB,CAAN;AACD,C,CAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,IAAMI,MAAM,GAAG,+RAAf,C,CAEA;;AACA,OAAO,SAASD,KAAT,CAAeD,KAAf,EAAsB;EAC5B,OAAOE,MAAM,CAACC,IAAP,CAAYH,KAAZ,CAAP;AACA"}
|