Skip to content

rdmurphy/deno-quaff

Repository files navigation

quaff

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