-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dsv type option #301
base: main
Are you sure you want to change the base?
dsv type option #301
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,38 @@ async function remote_fetch(file) { | |
return response; | ||
} | ||
|
||
async function dsv(file, delimiter, {array = false, typed = false} = {}) { | ||
function asTyper(type) { | ||
if (typeof type === "function") { | ||
return type; | ||
} | ||
switch (`${type}`) { | ||
case "number": return Number; | ||
case "string": return String; | ||
case "boolean": return Boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this differs from autotype in that it will not turn "false" into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, no, that wasn’t intentional. |
||
case "date": return NewDate; | ||
default: throw new Error(`unknown type: ${type}`); | ||
} | ||
} | ||
|
||
function NewDate(value) { | ||
return new Date(value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to do the timezone workaround for Safari? |
||
} | ||
|
||
function asTypers(types) { | ||
const typers = Object.create(null); | ||
for (const key in types) typers[key] = asTyper(types[key]); | ||
return (d, i) => { | ||
const object = Object.create(null); | ||
for (const key in typers) object[key] = typers[key](d[key], i); | ||
return object; | ||
}; | ||
} | ||
|
||
async function dsv(file, delimiter, {array = false, type, typed = false} = {}) { | ||
const text = await file.text(); | ||
return (delimiter === "\t" | ||
? (array ? tsvParseRows : tsvParse) | ||
: (array ? csvParseRows : csvParse))(text, typed && autoType); | ||
: (array ? csvParseRows : csvParse))(text, type ? asTypers(type) : typed ? autoType : null); | ||
} | ||
|
||
export class AbstractFile { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in autoType the string gets trimmed. probably not something to include in casting, but will be different