Skip to content

Commit 3a2f1dc

Browse files
author
Amelia Wattenberger
authored
Merge pull request #23 from LoneRifle/patch-1
feat(fetch): recognize geojson and topojson
2 parents 0eab3f1 + 38a710f commit 3a2f1dc

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/api/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const getFilesFromRes = (res: any) => {
5252
.map((file: any) => file.path)
5353
.filter((path: string) => {
5454
const extension = path.split(".").pop() || "";
55-
const validExtensions = ["csv", "tsv", "json", "yml", "yaml"];
55+
const validExtensions = ["csv", "tsv", "json", "geojson", "topojson", "yml", "yaml"];
5656
return (
5757
validExtensions.includes(extension) &&
5858
!ignoredFiles.includes(path.split("/").slice(-1)[0]) &&
@@ -129,7 +129,7 @@ export function fetchDataFile(params: FileParamsWithSHA) {
129129
const { filename, name, owner, sha } = params;
130130
if (!filename) return [];
131131
const fileType = filename.split(".").pop() || "";
132-
const validTypes = ["csv", "tsv", "json", "yml", "yaml"];
132+
const validTypes = ["csv", "tsv", "json", "geojson", "topojson", "yml", "yaml"];
133133
if (!validTypes.includes(fileType)) return [];
134134

135135
return wretch()
@@ -145,6 +145,25 @@ export function fetchDataFile(params: FileParamsWithSHA) {
145145
try {
146146
if (fileType === "csv") {
147147
data = csvParse(res);
148+
} else if (["geojson", "topojson"].includes(fileType) || filename.endsWith(".geo.json")) {
149+
data = JSON.parse(res);
150+
if (data.features) {
151+
const features = data.features.map((feature: any) => {
152+
let geometry = {} as Record<string, any>
153+
Object.keys(feature?.geometry).forEach(key => {
154+
geometry[`geometry.${key}`] = feature.geometry[key];
155+
})
156+
let properties = {} as Record<string, any>
157+
Object.keys(feature?.properties).forEach(key => {
158+
properties[`properties.${key}`] = feature.properties[key];
159+
})
160+
const {geometry: g, properties: p, ...restOfKeys} = feature;
161+
return {...restOfKeys, ...geometry, ...properties};
162+
})
163+
// make features the first key of the object
164+
const { features: f, ...restOfData } = data
165+
data = { features, ...restOfData }
166+
}
148167
} else if (fileType === "json") {
149168
data = JSON.parse(res);
150169
} else if (fileType === "tsv") {

0 commit comments

Comments
 (0)