read-excel-file 5.6.1 → 5.7.0
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/CODE_OF_CONDUCT.md +78 -0
- package/README.md +16 -1
- package/bundle/read-excel-file.min.js +1 -1
- package/bundle/read-excel-file.min.js.map +1 -1
- package/commonjs/read/isDateTimestamp.js +1 -1
- package/commonjs/read/isDateTimestamp.js.map +1 -1
- package/commonjs/read/parseCellValue.js +26 -9
- package/commonjs/read/parseCellValue.js.map +1 -1
- package/commonjs/read/schema/convertToJson.js +53 -19
- package/commonjs/read/schema/convertToJson.js.map +1 -1
- package/commonjs/xml/xlsx.js +2 -0
- package/commonjs/xml/xlsx.js.map +1 -1
- package/modules/read/isDateTimestamp.js +1 -1
- package/modules/read/isDateTimestamp.js.map +1 -1
- package/modules/read/parseCellValue.js +26 -9
- package/modules/read/parseCellValue.js.map +1 -1
- package/modules/read/schema/convertToJson.js +53 -19
- package/modules/read/schema/convertToJson.js.map +1 -1
- package/modules/xml/xlsx.js +3 -1
- package/modules/xml/xlsx.js.map +1 -1
- package/package.json +1 -1
- package/types.d.ts +9 -6
|
@@ -87,6 +87,31 @@ function read(schema, row, rowIndex, columns, errors, options) {
|
|
|
87
87
|
var object = {};
|
|
88
88
|
var isEmptyObject = true;
|
|
89
89
|
|
|
90
|
+
var createError = function createError(_ref) {
|
|
91
|
+
var column = _ref.column,
|
|
92
|
+
value = _ref.value,
|
|
93
|
+
errorMessage = _ref.error,
|
|
94
|
+
reason = _ref.reason;
|
|
95
|
+
var error = {
|
|
96
|
+
error: errorMessage,
|
|
97
|
+
row: rowIndex + 1,
|
|
98
|
+
column: column,
|
|
99
|
+
value: value
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
if (reason) {
|
|
103
|
+
error.reason = reason;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (schema[column].type) {
|
|
107
|
+
error.type = schema[column].type;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return error;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
var pendingRequiredChecks = [];
|
|
114
|
+
|
|
90
115
|
var _loop = function _loop() {
|
|
91
116
|
var key = _Object$keys[_i];
|
|
92
117
|
var schemaEntry = schema[key];
|
|
@@ -135,27 +160,23 @@ function read(schema, row, rowIndex, columns, errors, options) {
|
|
|
135
160
|
}
|
|
136
161
|
}
|
|
137
162
|
|
|
138
|
-
if (!error && value === null
|
|
139
|
-
|
|
163
|
+
if (!error && value === null) {
|
|
164
|
+
if (typeof schemaEntry.required === 'function') {
|
|
165
|
+
pendingRequiredChecks.push({
|
|
166
|
+
column: key
|
|
167
|
+
});
|
|
168
|
+
} else if (schemaEntry.required === true) {
|
|
169
|
+
error = 'required';
|
|
170
|
+
}
|
|
140
171
|
}
|
|
141
172
|
|
|
142
173
|
if (error) {
|
|
143
|
-
|
|
144
|
-
error: error,
|
|
145
|
-
row: rowIndex + 1,
|
|
174
|
+
errors.push(createError({
|
|
146
175
|
column: key,
|
|
147
|
-
value: value
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
error.reason = reason;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (schemaEntry.type) {
|
|
155
|
-
error.type = schemaEntry.type;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
errors.push(error);
|
|
176
|
+
value: value,
|
|
177
|
+
error: error,
|
|
178
|
+
reason: reason
|
|
179
|
+
}));
|
|
159
180
|
} else {
|
|
160
181
|
if (isEmptyObject && value !== null) {
|
|
161
182
|
isEmptyObject = false;
|
|
@@ -175,6 +196,19 @@ function read(schema, row, rowIndex, columns, errors, options) {
|
|
|
175
196
|
return null;
|
|
176
197
|
}
|
|
177
198
|
|
|
199
|
+
for (var _i2 = 0, _pendingRequiredCheck = pendingRequiredChecks; _i2 < _pendingRequiredCheck.length; _i2++) {
|
|
200
|
+
var column = _pendingRequiredCheck[_i2].column;
|
|
201
|
+
var required = schema[column].required(object);
|
|
202
|
+
|
|
203
|
+
if (required) {
|
|
204
|
+
errors.push(createError({
|
|
205
|
+
column: column,
|
|
206
|
+
value: null,
|
|
207
|
+
error: 'required'
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
178
212
|
return object;
|
|
179
213
|
}
|
|
180
214
|
/**
|
|
@@ -358,8 +392,8 @@ var transpose = function transpose(array) {
|
|
|
358
392
|
};
|
|
359
393
|
|
|
360
394
|
function validateSchema(schema) {
|
|
361
|
-
for (var
|
|
362
|
-
var key = _Object$keys2[
|
|
395
|
+
for (var _i3 = 0, _Object$keys2 = Object.keys(schema); _i3 < _Object$keys2.length; _i3++) {
|
|
396
|
+
var key = _Object$keys2[_i3];
|
|
363
397
|
var entry = schema[key];
|
|
364
398
|
|
|
365
399
|
if (!entry.prop) {
|
|
@@ -1 +1 @@
|
|
|
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;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,IAAMC,MAAM,GAAG,EAAf;;EAEA,KAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGV,IAAI,CAACW,MAAzB,EAA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L,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"}
|
|
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","createError","column","value","errorMessage","reason","type","pendingRequiredChecks","key","schemaEntry","isNestedSchema","Array","isArray","rawValue","indexOf","undefined","notEmpty","array","parseArray","map","_value","parseValue","required","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\r\n const createError = ({\r\n column,\r\n value,\r\n error: errorMessage,\r\n reason\r\n }) => {\r\n const error = {\r\n error: errorMessage,\r\n row: rowIndex + 1,\r\n column,\r\n value\r\n }\r\n if (reason) {\r\n error.reason = reason\r\n }\r\n if (schema[column].type) {\r\n error.type = schema[column].type\r\n }\r\n return error\r\n }\r\n\r\n const pendingRequiredChecks = []\r\n\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\r\n let rawValue = row[columns.indexOf(key)]\r\n if (rawValue === undefined) {\r\n rawValue = null\r\n }\r\n\r\n let value\r\n let error\r\n let reason\r\n\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\r\n if (!error && value === null) {\r\n if (typeof schemaEntry.required === 'function') {\r\n pendingRequiredChecks.push({ column: key })\r\n } else if (schemaEntry.required === true) {\r\n error = 'required'\r\n }\r\n }\r\n\r\n if (error) {\r\n errors.push(createError({\r\n column: key,\r\n value,\r\n error,\r\n reason\r\n }))\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\r\n if (isEmptyObject) {\r\n return null\r\n }\r\n\r\n for (const { column } of pendingRequiredChecks) {\r\n const required = schema[column].required(object)\r\n if (required) {\r\n errors.push(createError({\r\n column,\r\n value: null,\r\n error: 'required'\r\n }))\r\n }\r\n }\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;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,C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GtE,MAAM,CAAC2B,GAAD,CAApB;;IACA,IAAI,CAAC2C,KAAK,CAAC3B,IAAX,EAAiB;MACf,MAAM,IAAIc,KAAJ,mDAAkD9B,GAAlD,SAAN;IACD;EACF;AACF"}
|
package/modules/xml/xlsx.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { findChild, findChildren, forEach, map, getTagName } from './dom.js';
|
|
1
|
+
import { findChild, findChildren, forEach, map, getTagName } from './dom.js'; // Returns an array of cells,
|
|
2
|
+
// each element being an XML DOM element representing a cell.
|
|
3
|
+
|
|
2
4
|
export function getCells(document) {
|
|
3
5
|
var worksheet = document.documentElement;
|
|
4
6
|
var sheetData = findChild(worksheet, 'sheetData');
|
package/modules/xml/xlsx.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xlsx.js","names":["findChild","findChildren","forEach","map","getTagName","getCells","document","worksheet","documentElement","sheetData","cells","row","cell","push","getMergedCells","mergedCells","mergedCellsInfo","mergedCell","getAttribute","getCellValue","node","getCellInlineStringValue","firstChild","textContent","getDimensions","dimensions","getBaseStyles","styleSheet","cellStyleXfs","getCellStyles","cellXfs","getNumberFormats","numberFormats","numFmts","getSharedStrings","sst","string","t","value","r","getWorkbookProperties","workbook","getRelationships","relationships","getSheets","sheets"],"sources":["../../source/xml/xlsx.js"],"sourcesContent":["import { findChild, findChildren, forEach, map, getTagName } from './dom.js'\r\n\r\nexport function getCells(document) {\r\n const worksheet = document.documentElement\r\n const sheetData = findChild(worksheet, 'sheetData')\r\n\r\n const cells = []\r\n forEach(sheetData, 'row', (row) => {\r\n forEach(row, 'c', (cell) => {\r\n cells.push(cell)\r\n })\r\n })\r\n return cells\r\n}\r\n\r\nexport function getMergedCells(document) {\r\n const worksheet = document.documentElement\r\n const mergedCells = findChild(worksheet, 'mergeCells')\r\n const mergedCellsInfo = []\r\n if (mergedCells) {\r\n forEach(mergedCells, 'mergeCell', (mergedCell) => {\r\n mergedCellsInfo.push(mergedCell.getAttribute('ref'))\r\n })\r\n }\r\n return mergedCellsInfo\r\n}\r\n\r\nexport function getCellValue(document, node) {\r\n return findChild(node, 'v')\r\n}\r\n\r\nexport function getCellInlineStringValue(document, node) {\r\n if (\r\n node.firstChild &&\r\n getTagName(node.firstChild) === 'is' &&\r\n node.firstChild.firstChild &&\r\n getTagName(node.firstChild.firstChild) === 't'\r\n ) {\r\n return node.firstChild.firstChild.textContent\r\n }\r\n}\r\n\r\nexport function getDimensions(document) {\r\n const worksheet = document.documentElement\r\n const dimensions = findChild(worksheet, 'dimension')\r\n if (dimensions) {\r\n return dimensions.getAttribute('ref')\r\n }\r\n}\r\n\r\nexport function getBaseStyles(document) {\r\n const styleSheet = document.documentElement\r\n const cellStyleXfs = findChild(styleSheet, 'cellStyleXfs')\r\n if (cellStyleXfs) {\r\n return findChildren(cellStyleXfs, 'xf')\r\n }\r\n return []\r\n}\r\n\r\nexport function getCellStyles(document) {\r\n const styleSheet = document.documentElement\r\n const cellXfs = findChild(styleSheet, 'cellXfs')\r\n if (!cellXfs) {\r\n return []\r\n }\r\n return findChildren(cellXfs, 'xf')\r\n}\r\n\r\nexport function getNumberFormats(document) {\r\n const styleSheet = document.documentElement\r\n let numberFormats = []\r\n const numFmts = findChild(styleSheet, 'numFmts')\r\n if (numFmts) {\r\n return findChildren(numFmts, 'numFmt')\r\n }\r\n return []\r\n}\r\n\r\nexport function getSharedStrings(document) {\r\n\t// An `<si/>` element can contain a `<t/>` (simplest case) or a set of `<r/>` (\"rich formatting\") elements having `<t/>`.\r\n\t// https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.sharedstringitem?redirectedfrom=MSDN&view=openxml-2.8.1\r\n\t// http://www.datypic.com/sc/ooxml/e-ssml_si-1.html\r\n\r\n const sst = document.documentElement\r\n return map(sst, 'si', string => {\r\n const t = findChild(string, 't')\r\n if (t) {\r\n return t.textContent\r\n }\r\n let value = ''\r\n forEach(string, 'r', (r) => {\r\n value += findChild(r, 't').textContent\r\n })\r\n return value\r\n })\r\n}\r\n\r\nexport function getWorkbookProperties(document) {\r\n const workbook = document.documentElement\r\n return findChild(workbook, 'workbookPr')\r\n}\r\n\r\nexport function getRelationships(document) {\r\n const relationships = document.documentElement\r\n return findChildren(relationships, 'Relationship')\r\n}\r\n\r\nexport function getSheets(document) {\r\n const workbook = document.documentElement\r\n const sheets = findChild(workbook, 'sheets')\r\n return findChildren(sheets, 'sheet')\r\n}"],"mappings":"AAAA,SAASA,SAAT,EAAoBC,YAApB,EAAkCC,OAAlC,EAA2CC,GAA3C,EAAgDC,UAAhD,QAAkE,UAAlE;
|
|
1
|
+
{"version":3,"file":"xlsx.js","names":["findChild","findChildren","forEach","map","getTagName","getCells","document","worksheet","documentElement","sheetData","cells","row","cell","push","getMergedCells","mergedCells","mergedCellsInfo","mergedCell","getAttribute","getCellValue","node","getCellInlineStringValue","firstChild","textContent","getDimensions","dimensions","getBaseStyles","styleSheet","cellStyleXfs","getCellStyles","cellXfs","getNumberFormats","numberFormats","numFmts","getSharedStrings","sst","string","t","value","r","getWorkbookProperties","workbook","getRelationships","relationships","getSheets","sheets"],"sources":["../../source/xml/xlsx.js"],"sourcesContent":["import { findChild, findChildren, forEach, map, getTagName } from './dom.js'\r\n\r\n// Returns an array of cells,\r\n// each element being an XML DOM element representing a cell.\r\nexport function getCells(document) {\r\n const worksheet = document.documentElement\r\n const sheetData = findChild(worksheet, 'sheetData')\r\n\r\n const cells = []\r\n forEach(sheetData, 'row', (row) => {\r\n forEach(row, 'c', (cell) => {\r\n cells.push(cell)\r\n })\r\n })\r\n return cells\r\n}\r\n\r\nexport function getMergedCells(document) {\r\n const worksheet = document.documentElement\r\n const mergedCells = findChild(worksheet, 'mergeCells')\r\n const mergedCellsInfo = []\r\n if (mergedCells) {\r\n forEach(mergedCells, 'mergeCell', (mergedCell) => {\r\n mergedCellsInfo.push(mergedCell.getAttribute('ref'))\r\n })\r\n }\r\n return mergedCellsInfo\r\n}\r\n\r\nexport function getCellValue(document, node) {\r\n return findChild(node, 'v')\r\n}\r\n\r\nexport function getCellInlineStringValue(document, node) {\r\n if (\r\n node.firstChild &&\r\n getTagName(node.firstChild) === 'is' &&\r\n node.firstChild.firstChild &&\r\n getTagName(node.firstChild.firstChild) === 't'\r\n ) {\r\n return node.firstChild.firstChild.textContent\r\n }\r\n}\r\n\r\nexport function getDimensions(document) {\r\n const worksheet = document.documentElement\r\n const dimensions = findChild(worksheet, 'dimension')\r\n if (dimensions) {\r\n return dimensions.getAttribute('ref')\r\n }\r\n}\r\n\r\nexport function getBaseStyles(document) {\r\n const styleSheet = document.documentElement\r\n const cellStyleXfs = findChild(styleSheet, 'cellStyleXfs')\r\n if (cellStyleXfs) {\r\n return findChildren(cellStyleXfs, 'xf')\r\n }\r\n return []\r\n}\r\n\r\nexport function getCellStyles(document) {\r\n const styleSheet = document.documentElement\r\n const cellXfs = findChild(styleSheet, 'cellXfs')\r\n if (!cellXfs) {\r\n return []\r\n }\r\n return findChildren(cellXfs, 'xf')\r\n}\r\n\r\nexport function getNumberFormats(document) {\r\n const styleSheet = document.documentElement\r\n let numberFormats = []\r\n const numFmts = findChild(styleSheet, 'numFmts')\r\n if (numFmts) {\r\n return findChildren(numFmts, 'numFmt')\r\n }\r\n return []\r\n}\r\n\r\nexport function getSharedStrings(document) {\r\n\t// An `<si/>` element can contain a `<t/>` (simplest case) or a set of `<r/>` (\"rich formatting\") elements having `<t/>`.\r\n\t// https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.sharedstringitem?redirectedfrom=MSDN&view=openxml-2.8.1\r\n\t// http://www.datypic.com/sc/ooxml/e-ssml_si-1.html\r\n\r\n const sst = document.documentElement\r\n return map(sst, 'si', string => {\r\n const t = findChild(string, 't')\r\n if (t) {\r\n return t.textContent\r\n }\r\n let value = ''\r\n forEach(string, 'r', (r) => {\r\n value += findChild(r, 't').textContent\r\n })\r\n return value\r\n })\r\n}\r\n\r\nexport function getWorkbookProperties(document) {\r\n const workbook = document.documentElement\r\n return findChild(workbook, 'workbookPr')\r\n}\r\n\r\nexport function getRelationships(document) {\r\n const relationships = document.documentElement\r\n return findChildren(relationships, 'Relationship')\r\n}\r\n\r\nexport function getSheets(document) {\r\n const workbook = document.documentElement\r\n const sheets = findChild(workbook, 'sheets')\r\n return findChildren(sheets, 'sheet')\r\n}"],"mappings":"AAAA,SAASA,SAAT,EAAoBC,YAApB,EAAkCC,OAAlC,EAA2CC,GAA3C,EAAgDC,UAAhD,QAAkE,UAAlE,C,CAEA;AACA;;AACA,OAAO,SAASC,QAAT,CAAkBC,QAAlB,EAA4B;EACjC,IAAMC,SAAS,GAAGD,QAAQ,CAACE,eAA3B;EACA,IAAMC,SAAS,GAAGT,SAAS,CAACO,SAAD,EAAY,WAAZ,CAA3B;EAEA,IAAMG,KAAK,GAAG,EAAd;EACAR,OAAO,CAACO,SAAD,EAAY,KAAZ,EAAmB,UAACE,GAAD,EAAS;IACjCT,OAAO,CAACS,GAAD,EAAM,GAAN,EAAW,UAACC,IAAD,EAAU;MAC1BF,KAAK,CAACG,IAAN,CAAWD,IAAX;IACD,CAFM,CAAP;EAGD,CAJM,CAAP;EAKA,OAAOF,KAAP;AACD;AAED,OAAO,SAASI,cAAT,CAAwBR,QAAxB,EAAkC;EACvC,IAAMC,SAAS,GAAGD,QAAQ,CAACE,eAA3B;EACA,IAAMO,WAAW,GAAGf,SAAS,CAACO,SAAD,EAAY,YAAZ,CAA7B;EACA,IAAMS,eAAe,GAAG,EAAxB;;EACA,IAAID,WAAJ,EAAiB;IACfb,OAAO,CAACa,WAAD,EAAc,WAAd,EAA2B,UAACE,UAAD,EAAgB;MAChDD,eAAe,CAACH,IAAhB,CAAqBI,UAAU,CAACC,YAAX,CAAwB,KAAxB,CAArB;IACD,CAFM,CAAP;EAGD;;EACD,OAAOF,eAAP;AACD;AAED,OAAO,SAASG,YAAT,CAAsBb,QAAtB,EAAgCc,IAAhC,EAAsC;EAC3C,OAAOpB,SAAS,CAACoB,IAA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AAD,EAAW,QAAX,CAAxB;EACA,OAAOxC,YAAY,CAAC4C,MAAD,EAAS,OAAT,CAAnB;AACD"}
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -2,6 +2,9 @@ export function Integer(): void;
|
|
|
2
2
|
export function URL(): void;
|
|
3
3
|
export function Email(): void;
|
|
4
4
|
|
|
5
|
+
type Cell = string | number | boolean | typeof Date
|
|
6
|
+
export type Row = Cell[]
|
|
7
|
+
|
|
5
8
|
type BasicType =
|
|
6
9
|
| string
|
|
7
10
|
| number
|
|
@@ -13,11 +16,13 @@ type BasicType =
|
|
|
13
16
|
|
|
14
17
|
export type Type<T> = (value: Cell) => T | undefined;
|
|
15
18
|
|
|
19
|
+
type SchemaEntryRequired = boolean | (row: Row) => boolean;
|
|
20
|
+
|
|
16
21
|
interface SchemaEntryBasic<T> {
|
|
17
22
|
prop: string;
|
|
18
23
|
type?: BasicType | Type<T>;
|
|
19
24
|
oneOf?: T[];
|
|
20
|
-
required?:
|
|
25
|
+
required?: SchemaEntryRequired;
|
|
21
26
|
validate?(value: T): void;
|
|
22
27
|
}
|
|
23
28
|
|
|
@@ -27,7 +32,7 @@ interface SchemaEntryParsed<T> {
|
|
|
27
32
|
prop: string;
|
|
28
33
|
parse: (value: Cell) => T | undefined;
|
|
29
34
|
oneOf?: T[];
|
|
30
|
-
required?:
|
|
35
|
+
required?: SchemaEntryRequired;
|
|
31
36
|
validate?(value: T): void;
|
|
32
37
|
}
|
|
33
38
|
|
|
@@ -36,7 +41,7 @@ interface SchemaEntryParsed<T> {
|
|
|
36
41
|
interface SchemaEntryRecursive {
|
|
37
42
|
prop: string;
|
|
38
43
|
type: Record<string, SchemaEntry>;
|
|
39
|
-
required?:
|
|
44
|
+
required?: SchemaEntryRequired;
|
|
40
45
|
}
|
|
41
46
|
|
|
42
47
|
type SchemaEntry = SchemaEntryBasic<any> | SchemaEntryParsed<any> | SchemaEntryRecursive
|
|
@@ -52,9 +57,6 @@ export interface Error {
|
|
|
52
57
|
type?: SchemaEntry;
|
|
53
58
|
}
|
|
54
59
|
|
|
55
|
-
type Cell = string | number | boolean | typeof Date
|
|
56
|
-
export type Row = Cell[]
|
|
57
|
-
|
|
58
60
|
export interface ParsedObjectsResult<T extends object> {
|
|
59
61
|
rows: T[];
|
|
60
62
|
errors: Error[];
|
|
@@ -63,6 +65,7 @@ export interface ParsedObjectsResult<T extends object> {
|
|
|
63
65
|
interface ParseCommonOptions {
|
|
64
66
|
sheet?: number | string;
|
|
65
67
|
trim?: boolean;
|
|
68
|
+
parseNumber?: (string: string) => any;
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
export interface ParseWithSchemaOptions<T extends object> extends ParseCommonOptions {
|