GithubHelp home page GithubHelp logo

legson's Introduction

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

legson's People

Contributors

sitnin 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.