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

json2kv

v0.0.1

Published

Dependencyless JS library that converts a generic JSON-like object to a map-like, key-value object.

Downloads

6

Readme

json2kv

Dependencyless JS library that converts a generic JSON-like object to a map-like, key-value object.


Travis npm npm codecov

Overview

json2kv is a library that converts a generic JSON to a map-like, key-value object. All it needs is a JSON-ish datum (that is, either a real JSON file or a JS serializable Object), and a property name. User defined exceptions are supported too. At the moment, only a property from the immediate children is supported.

It is fully tested, with 100% coverage.

Installation

With yarn:

  • yarn add json2kv

Or, with npm:

  • npm i -S json2kv

Motivation

The foundations of this library have been laid on StackOverflow, after I've managed to provide [a meaningful answer](map given the 'value' property of all the nested objects) to this problem: create a key-value object given a JSON and a certain property, which is common among some of the nested objects. In other words, the problem this package solves is converting a JSON to a map structure (key-value object), filtering out the unnecessary props. Hence the name json2kv.

Usage

// import the library
import json2kv from 'json2kv';

// define the JSON-like data
const data = {
  some: 'string',
  someObjWithoutValueProp: {
    yep: 'nope'
  },
  battery: {
    value: 4.08,
    metadata: {
      // ...
    }
  },
  location: {
    value: {
      coordinates: 'Some location value'
    }
  },
  temperature: {
    value: 32.5,
    metadata: {
      // ...
    }
  }
};

const prop = 'value';
const result = json2kv(data, prop);

console.log(result);
/*
  {
    battery: 4.08,                         // value of data.battery.value 
    location: {                            // value of data.location.value
      coordinates: 'Some location value',
    },
    temperature: 32.5,                     // value of data.temperature.value
  }
*/

You can also define custom exceptions to override the behaviour for specific properties. To do you you need to define a Map<string, string>, having a child property of data as key (in this case, location, as in data.location), and the object path of the desired mapping as value (in this case, value.coordinates, as in data.location.value.coordinates).

const exceptions = new Map(); // Map<string, string>
exceptions.set('location', 'value.coordinates');
const resultWithExceptions = json2kv(data, prop, exceptions);

console.log(resultWithExceptions);
/*
  {
    battery: 4.08,                    // value of data.battery.value 
    location: 'Some location value',  // value of data.location.value.coordinates
    temperature: 32.5                 // value of data.temperature.value
  }
*/

Contributing

As always, contributions are always welcome, and remember:

  • ⇄ Pull requests and ★ Stars are really welcome too.
  • Just check out the dev scripts right below to get started.

Development scripts

flow

checks if Flow definitions are written properly

lint

checks if code conforms to linting rules (eslint)

  • lint - will check js

  • lint --fix - will automatically fix js

publish

Runs all the linting/test suites Creates a brand new build Pushes a release to npm

test

checks if all unit tests pass (jest)

  • test:watch - run tests in watch-mode
  • test:cov - run tests and displays coverage (which should't get below 100%!)
  • test:ci - run global tests and checks, including linting and flow errors

Build note

You can build your own light version of setting the env.targets property in .babelrc to "node": "current". The version deployed to npm requires at least NodeJS 6.0.0.

Credits

This library is a fork of another project of mine, node-lib-boilerplate.

License

This project is MIT licensed.