-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate file loading from reading and thus support dbf files through…
… readData
- Loading branch information
Showing
11 changed files
with
1,888 additions
and
1,695 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import discernFormat from './discernFormat' | ||
import loaders from '../loaders/index' | ||
|
||
export default function discernLoader (filePath, opts_ = {}) { | ||
var which = opts_.sync === true ? 'sync' : 'async' | ||
var format = discernFormat(filePath) | ||
var loader = loaders[which][format] | ||
// If we don't have a loader for this format, read in as a normal file | ||
if (typeof loader === 'undefined') { | ||
loader = loaders[which]['txt'] | ||
} | ||
return loader | ||
} |
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,13 @@ | ||
var shapefile = require('shapefile') | ||
|
||
export default function dbf (filePath, parser, parserOptions, cb) { | ||
var values = [] | ||
shapefile.openDbf(filePath) | ||
.then(source => source.read() | ||
.then(function log (result) { | ||
if (result.done) return cb(null, values) | ||
values.push(parserOptions.map(result.value)) // TODO, figure out i | ||
return source.read().then(log) | ||
})) | ||
.catch(error => cb(error.stack)) | ||
} |
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,30 @@ | ||
import fs from 'fs' | ||
import discernFormat from '../helpers/discernFormat' | ||
import {formatsIndex} from '../config/equivalentFormats' | ||
|
||
export default function file (filePath, parser, parserOptions, cb) { | ||
fs.readFile(filePath, 'utf8', function (err, data) { | ||
var fileFormat = discernFormat(filePath) | ||
if ((fileFormat === 'json' || formatsIndex.json.indexOf(fileFormat) > -1) && data === '') { | ||
data = '[]' | ||
} | ||
if (err) { | ||
cb(err) | ||
return false | ||
} | ||
var parsed | ||
try { | ||
if (typeof parser === 'function') { | ||
parsed = parser(data, parserOptions) | ||
} else if (typeof parser === 'object' && typeof parser.parse === 'function') { | ||
parsed = parser.parse(data, parserOptions) | ||
} else { | ||
parsed = 'Your specified parser is not properly formatted. It must either be a function or have a `parse` method.' | ||
} | ||
} catch (err) { | ||
cb(err) | ||
return | ||
} | ||
cb(null, parsed) | ||
}) | ||
} |
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,25 @@ | ||
import fs from 'fs' | ||
import discernFormat from '../helpers/discernFormat' | ||
import {formatsIndex} from '../config/equivalentFormats' | ||
|
||
export default function file (filePath, parser, parserOptions, cb) { | ||
var data = fs.readFileSync(filePath, 'utf8') | ||
var fileFormat = discernFormat(filePath) | ||
if ((fileFormat === 'json' || formatsIndex.json.indexOf(fileFormat) > -1) && data === '') { | ||
data = '[]' | ||
} | ||
|
||
var parsed | ||
if (typeof parser === 'function') { | ||
parsed = parser(data, parserOptions) | ||
} else if (typeof parser === 'object' && typeof parser.parse === 'function') { | ||
parsed = parser.parse(data, parserOptions) | ||
} else { | ||
return new Error('Your specified parser is not properly formatted. It must either be a function or have a `parse` method.') | ||
} | ||
|
||
// if (opts_ && opts_.flatten) { | ||
// parsed = _.map(parsed, flatten) | ||
// } | ||
return parsed | ||
} |
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,36 @@ | ||
import file from './file' | ||
import fileSync from './fileSync' | ||
import dbf from './dbf' | ||
import {formatsList} from '../config/equivalentFormats' | ||
|
||
let loaders = { | ||
async: { | ||
aml: file, | ||
csv: file, | ||
psv: file, | ||
tsv: file, | ||
txt: file, | ||
json: file, | ||
yaml: file, | ||
dbf | ||
}, | ||
sync: { | ||
aml: fileSync, | ||
csv: fileSync, | ||
psv: fileSync, | ||
tsv: fileSync, | ||
txt: fileSync, | ||
json: fileSync, | ||
yaml: fileSync | ||
} | ||
} | ||
|
||
formatsList.forEach(format => { | ||
format.equivalents.forEach(equivalent => { | ||
Object.keys(loaders).forEach(key => { | ||
loaders[key][equivalent] = loaders[key][format.name] | ||
}) | ||
}) | ||
}) | ||
|
||
export default loaders |
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