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

json-to-fs-structure

v1.2.12

Published

A simple module that takes a simple JSON file and produces the properties as a file structure in the same directory or given directory.

Downloads

8

Readme

json-to-fs-structure

Clean and simple JavaScript project for turning JSON objects into directory structures.

Development

For development, clone into this repository and to install run: npm install To test run: npm test

Usage

To use this node module, install it like so: yarn add json-to-fs-structure npm install json-to-fs-structure And (as it's currently intended to be used on the server) usage looks like this on a simple node server:

var express = require('express');
var { jsonToFsStructure } = require('json-to-fs-structure');
var app = express();

app.get('/', function(req, res){
  const options = {
    jsonObject: {
      testArrayField5: [
        { somedir: {} },
        { anotherdir: {} },
        {
          andanotherdir: {
            interiorone: {
              interiortwo: {
                interiorthree: [{ interiorfour: {} }, { interiorfive: {} }]
              }
            }
          }
        }
      ]
    }
  };
  jsonToFsStructure(options);
  res.send("Hello world!");
});

app.listen(3000);

Or if you want to see how it works synchronously (server only returns after it is written to the root directory), your code would look more like this:

var express = require('express');
var { jsonToFsStructure } = require('json-to-fs-structure');
var app = express();

app.get('/', function(req, res){
  const options = {
    jsonObject: {
      testArrayField5: [
        { somedir: {} },
        { anotherdir: {} },
        {
          andanotherdir: {
            interiorone: {
              interiortwo: {
                interiorthree: [{ interiorfour: {} }, { interiorfive: {} }]
              }
            }
          }
        }
      ]
    },
    filePath: ".",
    callback: () => res.send("Hello world!"))
  };
  jsonToFsStructure(options);
});

app.listen(3000);

Both of those examples would leave you with directory trees that look like this:

directory example of json to fs

Executing functions in place

Suppose you want to execute a function for each directory whether it be a terminating (leaf) directory or another directory. The function format is as follows:

const procedure = (newPath, accumulator, obj) => {
  // something that takes the just created directory (relative newPath)
  // and the accumulator (a structure you can provide that continues through each call)
  // and the obj, the value of the nested structure if this is a leaf node it's {}

  // lastly we return the accumulator to persist it
  return accumulator;
}

The corresponding functions for the above executing procedures are: jsonToFsWithLeafFunction, jsonToFsWithNonLeafFunction and jsonToFsWithFunction. So an example usage in a simple express server would look like this:

var express = require('express');
var { jsonToFsWithLeafFunction } = require('json-to-fs-structure');
var app = express();

const procedure = (newPath, accumulator, obj) => {
  accumulator.contextvalue += 2;
  accumulator.paths.push(newPath);
  return accumulator;
};

app.get('/', function(req, res){
  let passByValueContext = {"contextvalue": 1, paths: []};
  const options = {
    jsonObject: {
      testArrayField5: [
        { somedir: {} },
        { anotherdir: {} },
        {
          andanotherdir: {
            interiorone: {
              interiortwo: {
                interiorthree: [{ interiorfour: {} }, { interiorfive: {} }]
              }
            }
          }
        }
      ]
    },
    procedure,
    context: passByValueContext
  };
  jsonToFsWithLeafFunction(options);
  console.log(passByValueContext);
  res.send("Hello world!");
});

app.listen(3000);

What we expect to see in the console when this runs is:

{ contextvalue: 9,
  paths:
   [ './testArrayField5/somedir',
     './testArrayField5/anotherdir',
     './testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfour',
     './testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfive' ] }

And since our context value is scoped to each request, you will see that for every GET method.