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

jsobj-analyzer

v0.1.4

Published

Provides information about structure of a JavaScript object.

Downloads

11

Readme

JavaScript Object Analyzer Build Status

Provides information about structure of a JavaScript object.

Installation

Using npm:

$ npm i --save jsobj-analyzer

In a browser:

<script src="dist/jsoa.min.js"></script>

In node.js:

var JSOA = require('jsobj-analyzer');

Also the module exported as AMD module.

Documentation

Get Tag

string getTag(Mixed any)

The analyzes needs to determine the type of any variable. This function relies on:

  1. used or overridden well-known Symbol toStringTag for objects;
  2. read-only property Function.name taken from constructor if the variable has been instantiated;
  3. finally result of Object.prototype.toString which can process any variable at all.

Actually built-in method Object.prototype.toString may do all work, but getting type for any user-typed variables is more conveniently by using constructor.name by default if it's possible.

Some examples:

Definition of A | getTag(A) --- | --- 10 | Number Math.PI | Number NaN | Number -0 | Number undefined | Undefined null | Null true | Boolean 'Hello' | String { x: 1, y: 0 } | Object [1, 2, 3] | Array new Set | Set Set | Function JSON | JSON class Animal {} new Animal | Animal (x, y) => x + y | Function function* A(i) { while(1) yield i += 1; } | GeneratorFunction async function A() { /* await a promise and return */ } | AsyncFunction

Flat Inspection

object inspFlat(object obj)

The function collects statistics about an object structure. Requires plain or iterable object as first parameter.

For example let's describe next object:

class Person {
    constructor(name) {
        this.name = name;
    }
}
    
let data = {
    x: 10,
    y: NaN,
    e: Math.E,
    arr: [ 'p1', 'p2', 'p3' ],
    obj: {
        0: new Set([ false ]),
        1: new Set([ true ])
    },
    sum: (a, b) => a + b,
    fn: {
        inc: function(i) {
            return i + 1;
        },
        Person,
        man: new Person('John')
    },
    date: new Date
};

console.log(JSOA.inspFlat(data));

The output will be:

{ Number: 3, Array: 1, Object: 2, Function: 1, Date: 1 }

Deep Inspection

object inspFlat(object obj, number maxDepth = 6)

Unlike the previous function collects statistics recursively over all nested plain and iterable objects too.

The same data with deep inspection:

console.log(JSOA.inspDeep(data));

The output will be:

{ Number: 3, String: 3, Boolean: 2, Function: 3, Person: 1, Date: 1 }

The optional second parameter maxDepth limits the depth of inspection. By default is 6: it's enough for most cases.

Changelog

0.1.4

  • Supported parameter maxDepth for deep inspection (Closed Issue #2)