Provides information about structure of a JavaScript object.
Installation
Using npm:
$ npm i --save jsobj-analyzer
In a browser:
In node.js:
var JSOA = ;
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:
- used or overridden well-known Symbol toStringTag for objects;
- read-only property Function.name taken from constructor if the variable has been instantiated;
- 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:
{ thisname = name; } let data = x: 10 y: NaN e: MathE arr: 'p1' 'p2' 'p3' obj: 0: false 1: true a + b fn: { return i + 1; } Person man: 'John' date: ; console;
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;
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)