npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

quaff

v5.0.0

Published

Collect JS/JSON/YAML/YML/CSV/TSV/ArchieML files from a source folder and convert them into a single object.

Downloads

1,617

Readme

Important!

quaff is now a pure ESM package. It can no longer be require()'d from CommonJS. If this functionality is still needed please continue to use quaff@^4.

Key features

  • 🚚 A data pipeline helper written in Node.js that works similar to Middleman's Data Files collector
  • 📦 Point the library at a folder filled with JS, AML (ArchieML), JSON, YAML, CSV and/or TSV files and get a JavaScript object back that reflects the folder's structure and content/exports
  • 🤓 Under the hood it uses parse-json (for better JSON error support), js-yaml and d3-dsv to read files efficiently

Installation

npm install quaff --save-dev

quaff requires Node.js 12.20.0 or later.

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 'quaff';

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." }
			]
		}
	}
}

As of 5.0.0 it's now possible to load a single file at a time, enabling more custom approaches in case load doesn't work exactly the way you'd like.

import { loadFile } from 'quaff';

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

One of the biggest features added in quaff 4.0 is 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.

import fetch from 'node-fetch';

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 '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

By Ryan Murphy.

Available under the MIT license.