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

@ahwayakchih/logify

v2.0.0

Published

Turn objects/values into JSON string safely, keep result easy on the eyes

Downloads

14

Readme

logify

Yet another implementation of turning objects and/or values into a JSON string.

This one strives to be safe (JSON.stringify will throw on bigint and circular references) and result in a JSON that's easy on the eyes while keeping enough information:

  • references to objects are reported as a string with path to the first "encounter", e.g., "@one.two",
  • so are circular references too, but "@" character is doubled, e.g., "@@one.two",
  • undefined values are exported as an empty string, e.g., "",
  • bigint values are exported as a string, e.g., "5n",
  • Buffer values are exported as a string with it's data shown as hexadecimal, e.g., "[42]",
  • Infinity values are exported as a string, i.e., "Infinity",
  • NaN values are exported as a string, i.e., "NaN",
  • regular strings are prefixed with a less-than, i.e., "<text".

It's meant to be used for console.log-like output: for a quick check what's going on while debugging, and for testers to be able to quickly copy&paste information to developers.

Do not use it to store data! While it's output can be parsed back to objects, result will differ from the original data.

Installation

npm install @ahwayakchih/logify

or:

npm install https://github.com/ahwayakchih/logify

Usage

Example code:

const logify = require('logify');

const circular = {
  text: 'test'
};
circular.circular = circular;

const o = {};
o.one = 1;
o.two = "two";
o.three = undefined;
o.four = null;
o.five = 5n;
o.six = {
  oo: o,
  a: circular
};
o.seven = o;
o.eight = circular;
o.nine = function nine () { return 'Hello'; };
o.ten = () => 'World!';
o.eleven = {
  f1: function () {},
  f2: () => {}
};
o.twelve = Buffer.from('\x42');
o.thirteen = Infinity;
o.fourteen = NaN;
o.fifteen = [1,'2',circular,null,Infinity,NaN,15n,undefined];
o.sixteen = '<hack';

console.log(logify(o));

It should output something like this:

{
  "one": 1,
  "two": "<two",
  "three": "",
  "four": null,
  "five": "5n",
  "six": {
    "oo": "@@",
    "a": {
      "text": "<test",
      "circular": "@@six.a"
    }
  },
  "seven": "@@",
  "eight": "@six.a",
  "eleven": {},
  "twelve": "[42]",
  "thirteen": "Infinity",
  "fourteen": "NaN",
  "fifteen": [
    1,
    "<2",
    "@six.a",
    null,
    "Infinity",
    "NaN",
    "15n",
    ""
  ],
  "sixteen": "<<hack"
}

By enforcing empty spacer:

console.log(logify(o, ''));

You can get output similar to default JSON.stringify:

{"one":1,"two":"<two","three":"","four":null,"five":"5n","six":{"oo":"@@","a":{"text":"<test","circular":"@@six.a"}},"seven":"@@","eight":"@six.a","eleven":{},"twelve":"[42]","thirteen":"Infinity","fourteen":"NaN","fifteen":[1,"<2","@six.a",null,"Infinity","NaN","15n",""],"sixteen":"<<hack"}

Compatibility

Logify uses features from ES2015, so it should be compatible with most of the up-to-date browsers and Node.js. To make InternetExplorer use logify, you'll probably need some pollyfills and/or additional compile stage, e.g., using Babel.

Tests use features from ES2020, e.g., bigints (1n), so Node.js v12+ is required.

API Documentation

To generate documentation for this module, clone it from repository (package does not include required files) and use:

npm run doc

It will create HTML files inside logify/reports/jsdoc directory.

Testing

To run tests, clone module from repository and use:

npm test

To check coverage, use:

npm run checkCoverage

To check for any linting issues use:

npm run checkStyle