Git Product home page Git Product logo

deno-quaff's Introduction

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

deno-quaff's People

Contributors

rdmurphy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.