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

my-load-data

v1.1.0

Published

Loads data from directories for me.

Downloads

12

Readme

my-load-data

This is my module that loads files of a few format including JSON, YAML and plain JS for me.

installation

 $ npm install my-load-data

Examples

Basic usage

const mld = require('my-load-data');

// mld.fromFile() returns Promise
mld.fromFile('./data/foo.json').then(obj=>console.log(obj))
.catch(err=>console.error(err));

// It can also load all files in a directory
mld.fromDirectory('./data').then(obj=>console.log(obj))
.catch(err=>console.error(err));
/*
If ./data contains foo.json, bar.yaml and baz.js:

{
    "foo": {...},
    "bar": {...},
    "baz": {...},
}
*/

// Array of paths is accepted
mld.fromDirectory(['./data', './data1']).then(obj=>console.log(obj));

Loader

You can instantiate Loaders instead of using the default Loader. Loaders can be customized through addHandler (see API below).

const mld = require('my-load-data');

const loader = new mld.Loader();

loader.fromFile('./data/foo.json').then(obj=>console.log(obj));

API

mld.fromFile(path[, options]) / loader.fromFile(path[, options])

  • Return value: Promise<any>
  • path: string; Path to the file to load data from.
  • options:
    • mtime: boolean | string; If the value is truthy, mtime of the file is added to the result object. If the value is string, it is used as the field name. The default field name is $mtime.
    • cache: Object; If this option is used, unchanged files (determined by mtime) is not loaded and its value is taken from cache. The cache should be the return value of previous call to fromFile with mtime enabled (When cache is used, mtime is automatically enabled).
    • nocacheFilter: RegExp | Function; Filter function to temporally disable cache based on file names. If the function returns true, the file is newly loaded regardless of its time.

mld.fromDirectory(path[, options]) / loader.fromDirectory(path[, options])

  • Return value: Promise<any>
  • path: string | Array; Path to the directory to load data from.
  • options: See above.

If path is an Array, directories are visited sequentially.

loader.addHandler(ext, handler)

  • ext: string; Name of extension to add handler.
  • handler: (path: string)=>any;

Use this method to add a custom handler. handler is passed an absolute path of a file to load: path.

As shown in the example below, return value of handler is treated as a result of loading. handle also can return Promise for asynchronous operations.

example

const fs = require('fs');
const mld = require('my-load-data');

// add a handler to load .txt file as string.
mld.addHandler('txt', path=> fs.readFileSync(path, 'utf8'));

// Now mld can load .txt file
mld.fromFile('data/hoge.txt').then(str=>console.log(str));

// fromDirectory now finds and handles .txt files
mld.fromDirectory('data').then(obj=>console.log(obj.hoge));

Gimmicks

  • json file is loaded using JSON.parse.
  • yaml file is loaded using js-yaml. This module uses safeLoad. To customize this behavior, add a custom handler using addHandler.
  • js file is loaded using require. js files can export Promises to make an asynchronous operation.

Changelog

  • v1.1.0 (2016-xx-xx) Add the mtime, cache and nocacheFilter option for me.
  • v1.0.0 (2016-06-10)

License

MIT

Contribution

Following npm scripts are useful:

  • npm build: build source files into dist/.
  • npm watch: watch changes in lib/ and test/; on changes, compiling, linting and testing are triggered.
  • npm clean: removes dist/ directory.