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

legson

v1.0.4

Published

Lego-like JSON loader

Downloads

2

Readme

Extensible lego-like JSON file loader

This library is intended to load a JSON object from multiple files in multiple formats.

It allows you to construct an object with key values which contains some external url data, html text from markdown source files, other json files and so on.

Library is based on perfect JSON5 library, so it supports JSON5 file format right out of the box.

Installation

$ npm i --save legson

Library exports a default class named LegSON

Usage example

"use strict";

const LegSON = require("legson");

const opts = {
    maxValueLength: 100,
    nullNonExistent: true,
    parseArrays: true,
    addPlugins: {
        "test": value => {
            return new Promise.resolve(value);
        },
    },
};

const loader = new LegSON(opts);
loader.load("file.json").then(obj => {
    console.log(loader._opts);
    console.log(obj);
}).catch(err => {
    console.error(err.stack || err.message);
});

Important: any pathnames inside loaded JSON files will NOT be expanded or normalized. They always will be searched from the current directory. You should use absolute paths if you can't set working directory to the root json's folder.

Methods

new LegSON(opts)

Creates new LegSON object with configuration

.load(filename)

Return promise of JSON object value which will be fulfilled when all values will be processed

Configuration options

basePath (string, default — process.cwd())

Base directory to search for source files in case their are pointed via relative paths

maxValueLength (integer, default: 100)

Sets maximum key value length that will be checked with loaders

nullNonExistent (bool, default: true)

If true every loader-matched but failed values will be nullified. Otherwise, values will be left intact.

addLoaders (array of arrays, no default value)

Additional value loaders dictionary. Where the key of the array is a prefix for @[prefix] pattern matching and value is a function returning new value or promise of a value.

processArrays

If true values inside arrays will be processed as well. Otherwise arrays will be left itact.

Bundled loaders

Each source object key's value will be tested for "@[loader]" prefix. If prefix will be found, proper loader will be used to load real content of the particular key.

Result of each loader will be parsed again like this contents was already there. This logic makes possible to include data recursively :)

@[raw]

Will load file contents and will not do anything else. I.e. use this to load html which you will send to the browser.

@[json]

Will load file contents and parse it with JSON5 library. Result is a json-object.

@[md]

Loads file and parses it from markdown to html.

@[url]

Performs get url request and returns raw reply body to the object's key.

TODO

  • Write full documentation ;)
  • Add "asis" loader for loading file contents without processing
  • Write tests for configuration options
  • Resolve issue with dataPath patching (conflicts with url loader)

Contacts

Feel free to contact me via email [email protected] or send pull requests via Github.

License

MIT