-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff591a4
commit 863ea02
Showing
13 changed files
with
169 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import InvalidError from './InvalidError.js' | ||
|
||
export default function BooleanType(value) { | ||
if (typeof value === 'boolean') { | ||
return value | ||
} | ||
throw new InvalidError('not_a_boolean') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import parseDate from '../read/parseDate.js' | ||
import InvalidError from './InvalidError.js' | ||
|
||
export default function DateType(value, { properties }) { | ||
// XLSX has no specific format for dates. | ||
// Sometimes a date can be heuristically detected. | ||
// https://github.com/catamphetamine/read-excel-file/issues/3#issuecomment-395770777 | ||
if (value instanceof Date) { | ||
if (isNaN(value.valueOf())) { | ||
throw new InvalidError('out_of_bounds') | ||
} | ||
return value | ||
} | ||
if (typeof value === 'number') { | ||
if (isNaN(value)) { | ||
throw new InvalidError('invalid_number') | ||
} | ||
if (!isFinite(value)) { | ||
throw new InvalidError('out_of_bounds') | ||
} | ||
const date = parseDate(value, properties) | ||
if (isNaN(date.valueOf())) { | ||
throw new InvalidError('out_of_bounds') | ||
} | ||
return date | ||
} | ||
throw new InvalidError('not_a_date') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default class InvalidError extends Error { | ||
constructor(reason) { | ||
super('invalid') | ||
this.reason = reason | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import InvalidError from './InvalidError.js' | ||
|
||
export default function NumberType(value) { | ||
// An XLSX file editing software might not always correctly | ||
// detect numeric values in string-type cells. Users won't bother | ||
// manually selecting a cell type, so the editing software has to guess | ||
// based on the user's input. One can assume that such auto-detection | ||
// might not always work. | ||
// | ||
// So, if a cell is supposed to be a numeric one, convert a string value to a number. | ||
// | ||
if (typeof value === 'string') { | ||
const stringifiedValue = value | ||
value = Number(value) | ||
if (String(value) !== stringifiedValue) { | ||
throw new InvalidError('not_a_number') | ||
} | ||
} | ||
if (typeof value !== 'number') { | ||
throw new InvalidError('not_a_number') | ||
} | ||
if (isNaN(value)) { | ||
throw new InvalidError('invalid_number') | ||
} | ||
// At this point, `value` can only be a number. | ||
// | ||
// The global `isFinite()` function filters out: | ||
// * NaN | ||
// * -Infinity | ||
// * Infinity | ||
// | ||
// All other values pass (including non-numbers). | ||
// | ||
if (!isFinite(value)) { | ||
throw new InvalidError('out_of_bounds') | ||
} | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import InvalidError from './InvalidError.js' | ||
|
||
export default function StringType(value) { | ||
if (typeof value === 'string') { | ||
return value | ||
} | ||
// Excel tends to perform a forced automatic convertion of string-type values | ||
// to number-type ones when the user has input them. Otherwise, users wouldn't | ||
// be able to perform formula calculations on those cell values because users | ||
// won't bother manually choosing a "numeric" cell type for each cell, and | ||
// even if they did, choosing a "numeric" cell type every time wouldn't be an | ||
// acceptable "user experience". | ||
// | ||
// So, if a cell value is supposed to be a string and Excel has automatically | ||
// converted it to a number, perform a backwards conversion. | ||
// | ||
if (typeof value === 'number') { | ||
if (isNaN(value)) { | ||
throw new InvalidError('invalid_number') | ||
} | ||
// The global `isFinite()` function filters out: | ||
// * NaN | ||
// * -Infinity | ||
// * Infinity | ||
// | ||
// All other values pass (including non-numbers). | ||
// | ||
if (!isFinite(value)) { | ||
throw new InvalidError('out_of_bounds') | ||
} | ||
return String(value) | ||
} | ||
throw new InvalidError('not_a_string') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters