Skip to content
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

feat: csv to json #259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,10 @@ export default {
build: {
extractCSS: true,
transpile: ['lodash-es'],
// extend(config) {
// config.node = {
// fs: 'empty',
// }
// },
},
}
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"responsive-loader": "2.2.1",
"serve-handler": "6.1.3",
"sharp": "0.26.2",
"slugify": "1.4.6",
"sqip-loader": "1.0.0",
"stylelint": "13.8.0",
"stylelint-config-prettier": "8.0.2",
Expand Down
17 changes: 12 additions & 5 deletions pages/nurseries/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@
<script>
import { mdiMapMarker, mdiPhone } from '@mdi/js'
import { startCase } from 'lodash-es'
import { saveFile } from '~/utils/saveFile'
export default {
async asyncData({ $content }) {
const result = await $content('nurseries').fetch()
const { body } = result
const items = body
const folder = '/content/nurseries'
for (const item of items) {
await saveFile({ id: item.Nursery, folder, item })
}

return { items }
},
data: () => ({
items: [],
mdiMapMarker,
Expand All @@ -75,11 +87,6 @@ export default {
description:
"Nurseries and outlets that can supply free native plants under Brisbane City Council's Free Native Plants Program.",
}),
async fetch() {
const result = await this.$content('nurseries').fetch()
const { body } = result
this.items = body
},
head() {
return {
title: this.title,
Expand Down
Binary file added static/Brisbane-Plants-by-Suburb.pdf
Binary file not shown.
27 changes: 27 additions & 0 deletions utils/deepSort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function defaultSortFn(a: string, b: string) {
return a.localeCompare(b)
}

function deepSort(src: unknown, comparator = defaultSortFn): unknown {
const data = JSON.parse(JSON.stringify(src))

if (typeof data !== 'object' || !data) {
return data
}

if (Array.isArray(data)) {
return data.map((value) => deepSort(value, comparator))
}

return Object.keys(data)
.sort(comparator)
.reduce((o, k) => ({ ...o, [k]: deepSort(data[k], comparator) }), {})
}

/**
* inspired by:
* https://stackoverflow.com/questions/35811799/javascript-recursively-order-object-and-nested-objects-as-well-as-arrays
* https://github.com/IndigoUnited/js-deep-sort-object/blob/master/index.js
*/

export default deepSort
23 changes: 23 additions & 0 deletions utils/saveFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { promises as fsPromises } from 'fs'
// import { startCase } from 'lodash-es'
import slugify from 'slugify'
import deepSort from './deepSort'
const { mkdir, writeFile } = fsPromises

interface saveFileArgs {
id: string
folder: string
item: any
}

export async function saveFile({ id, folder, item }: saveFileArgs) {
const slug = `${slugify(id, {
lower: true,
strict: true,
})}`
await mkdir(folder, { recursive: true })
await writeFile(
`${folder}/${slug}.json`,
JSON.stringify(deepSort(item), undefined, 2) + '\n'
)
}