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

schematizr

v4.3.0

Published

A lightweight library built for modifying deeply nested JSON.

Downloads

17

Readme

schematizr

Schematizr is a lightweight library built to modify deeply nested JSON. It has been designed for easy function composition via currying and so has an intentionally small API. This means that instead of a large number of out of the box "use once" methods, schematizr only has enough to enable you to quickly build the functionality you need for your project and can stay small as a result.

Installation

npm install --save schematizr

API

Function | Paramters | Description -------------|-----------------------|-------------------------------------------------- assemble | (JSON, key='$id') | Takes an JSON literal and a key which defaults to $id and adds it, along with a value which is an unique number to each object. disassemble | (JSON, key='$id') | Takes an JSON literal and a key which defaults to $id and removes the key from each object. map | (Function, JSON) | Takes a callback which receives all the values in the JSON literal and returns a new value. find | (Function, Value, JSON) | Takes a callback and a value. The callback receives any values which match the given value and returns a new value. findObjWith | (Function, Shape,JSON) | Takes a callback and a shape. The shape is an object with key, value pairs. The callback receives any objects which contain the key, value pairs specified in the shape and returns a new object. filter | (Function, JSON) | Takes a callback which receives all the values from the JSON literal and returns a boolean.

Usage

import { assemble, disassemble, map, findObjWith, find, filter } from 'schematizr';

const data = {
  todoList: [
    {  text: 'Exercise',
      sublist: [
        { text: '5k run' },
        { text: '30min stretch' }
      ]
    },
    { text: 'Trim nose hairs' }
  ]
}

assemble(nestedJson, key = '_$id')


const schemarised = assemble(data);

// {
//   todoList: [
//    { text: 'Exercise',
//      sublist: [
//        { text: '5k run', $id: 3 },
//        { text: '30min stretch', $id: 4 }
//      ],
//      $id: 2
//    },
//    { text: 'Trim nose hairs', $id: 5 }
//   ],
//   $id: 1
// }

disassemble(nestedJson, key = '$id')


const deSchemarised = disassemble(schemarised);

// {
//   todoList: [
//     {  text: 'Exercise',
//       sublist: [
//         { text: '5k run' },
//         { text: '30min stretch' }
//       ]
//     },
//     { text: 'Trim nose hairs' }
//   ]
// }

map(callback, nestedJson)


const capitalised = map((value) => {
  return typeof value === 'string' ? value.toUpperCase() : value
}, data);


// {
//   todoList: [
//     {
//       text: "EXERCISE",
//       sublist: [
//         { text: "5K RUN" },
//         { text: "30MIN STRETCH" }
//       ]
//     },
//     { text: "TRIM NOSE HAIRS" }
//   ]
// }

findObjWith(callback, object, nestedJson)


const increasedRun = findObjWith((object) => {
  return Object.assign({}, object, {
    text: '100k run'
  });
}, { $id: 2 }, schemarised);

// { todoList: [
//     { $id: 1,
//       text: 'Exercise',
//       sublist: [
//         { $id: 2,
//           text: '100k run' },
//         { $id: 3,
//           text: '30min stretch' }
//       ]
//     },
//     { $id: 4,
//       text: 'Trim nose hairs' }
//   ]
// }

find(callback, value, nestedObject)


const increasedStretch = find(function(value) {
  return '60min stretch';
}, '30min stretch' , data);

// { todoList: [
//     { text: 'Exercise',
//       sublist: [
//         { text: '5k run' },
//         { text: '60min stretch' }
//       ]
//     },
//     { text: 'Trim nose hairs' }
//   ]
// }

filter(callback, nestedObject)


const removeRun = filter(function(value) {
  return value !== '5k run';
}, data);
// { todoList: [
//     { text: 'Exercise',
//       sublist: [
//         { text: '30min stretch' }
//       ]
//     },
//     { text: 'Trim nose hairs' }
//   ]
// }

Curried Example:


const removeSublists = map((value) => {
  if (value && value.sublist) {
    delete value['sublist']
    return value;
  }
  return value;
})

const removed = removeSublists(data)

// {
//   todoList: [
//    { text: 'Exercise' },
//    { text: 'Trim nose hairs' }
//   ]
// }