quaff
A port of the quaff
Node.js library to
Deno. Mostly done as an experiment to see how easy it would be, but itās also
fully tested and adds support for TOML! š
Key features
- š A data pipeline helper that works similar to Middlemanās Data Files collector
- š¦ Point the library at a folder filled with JS, AML (ArchieML), JSON, YAML, CSV, TSV and/or TOML files and get a JavaScript object back that reflects the folderās structure and content/exports
Usage
Assume a folder with this structure.
data/
mammals/
cats.json
dogs.json
bears.csv
birds/
parrots.yml
story.aml
After import
āing quaff
:
import { load } from "https://deno.land/x/quaff/mod.ts";
const data = await load("./data/");
console.log(data);
And the resultsā¦
{
"mammals": {
"cats": ["Marty", "Sammy"],
"dogs": ["Snazzy", "Cally"],
"bears": [
{
"name": "Steve",
"type": "Polar bear"
},
{
"name": "Angelica",
"type": "Sun bear"
}
]
},
"birds": {
"parrots": {
"alive": ["Buzz"],
"dead": ["Moose"]
},
"story": {
"title": "All about birds",
"prose": [
{ "type": "text", "value": "Do you know how great birds are?" },
{ "type": "text", "value": "Come with me on this journey." }
]
}
}
}
Itās also possible to load a single file at a time, allowing more custom
approaches in case load
doesnāt work exactly the way youād like.
import { loadFile } from "https://deno.land/x/quaff/mod.ts";
const data = await loadFile("./data/mammals/bears.csv");
console.log(data);
And the resultsā¦
[
{
"name": "Steve",
"type": "Polar bear"
},
{
"name": "Angelica",
"type": "Sun bear"
}
]
Advanced Usage with JavaScript files
quaff
has the ability to load JavaScript files. But how exactly does that
work?
JavaScript files that are consumed by quaff
have to follow one simple rule -
they must export default
a function, an async function or value. All three of
these are valid and return the same value:
export default [
{
name: "Pudge",
instagram: "https://instagram.com/pudgethecorgi/",
},
];
export default () => [
{
name: "Pudge",
instagram: "https://instagram.com/pudgethecorgi/",
},
];
export default async () => [
{
name: "Pudge",
instagram: "https://instagram.com/pudgethecorgi/",
},
];
The final example above is the most interesting one - async
functions also
work! This means you can write code to hit API endpoints, or do other
asynchronous work, and quaff
will wait for those to resolve.
export default async () => {
const res = await fetch("https://my-cool-api/");
const data = await res.json();
// whatever the API returned will be added to the quaff object!
return data;
};
Donāt have a Promise
to do async work with? Working with a callback interface?
Just wrap it in one!
import { apiHelper } from "npm:my-callback-api";
export default () => {
return new Promise((resolve, reject) => {
apiHelper("people", (err, data) => {
if (err) return reject(err);
// quaff will take it from here!
resolve(data);
});
});
};
License
MIT