vdumper

Show content of objects and arrays in the console. Its similar to print_r and var_dump from PHP.

Usage no npm install needed!

<script type="module">
  import vdumper from 'https://cdn.skypack.dev/vdumper';
</script>

README

  • Description
  • Installation
  • Usage
  • Examples
  • Params
  • Show types (default:false)
  • Show functions (default:false)
  • Show small log (default:false)
  • Circular objects
  • Config
  • Colors
  • License

Description

This is a small module to print the 'content' of an object / array in the console. Similar to PHP: var_dump / print_r . Returns also result as a string.

Installation

$ npm i vdumper

Usage

require

const vdumper = require("vdumper");
const dump = vdumper.dump;

Examples

Printing basic variables

num = 123;
str = "My name is Mykola Brozhyna";
test = null;
booleano = true;
NAN = NaN;

dump(num);
dump(str);
dump(test);
dump(booleano);
dump(NAN); 

Console

 number:  123
 string(26): "My name is Mykola Brozhyna"
 object:  null
 boolean:  true
 number:  NaN

Printing object

test = function (){
   return {
      num: 123,
      str: "My name is Mykola Brozhyna"
   };
}

a = new test;
a.b = new test;
a.b.c = new test;
a.b.c.d = new test;
a.b.c.d.e = new test;
dump(a);

Console

{
  [num]=> 123
  [str]=> "My name is Mykola Brozhyna" (26)
  [b] (3) {
    [num]=> 123
    [str]=> "My name is Mykola Brozhyna" (26)
    [c] (3) {
      [num]=> 123
      [str]=> "My name is Mykola Brozhyna" (26)
      [d] (3) {
        [num]=> 123
        [str]=> "My name is Mykola Brozhyna" (26)
        [e] (2) {
          [num]=> 123
          [str]=> "My name is Mykola Brozhyna" (26)
        }
      }
    }
  }
}

Params

By default it accepts 4 arguments.

          1                2               3                  4
dump (`any var` , `show types`  , `show functions` , `show basic log`) 
dump (object    ,(false / true) , (false / true)   , (false / true) )

Show types (default:false)

You can also include types in the result

dump (a,true);

Console ( Previous result bit with showTypes = true )

{
  [num]=> 123 number
  [str]=> "My name is Mykola Brozhyna" (26) string
  object [b] (3) {
    [num]=> 123 number
    [str]=> "My name is Mykola Brozhyna" (26) string
    object [c] (3) {
      [num]=> 123 number
      [str]=> "My name is Mykola Brozhyna" (26) string
      object [d] (3) {
        [num]=> 123 number
        [str]=> "My name is Mykola Brozhyna" (26) string
        object [e] (2) {
          [num]=> 123 number
          [str]=> "My name is Mykola Brozhyna" (26) string
        }
      }
    }
  }
}

Show functions (default:false)

( You can also toggle function to show content of a function )

( First as showFunctions = false )

      var,showTypes,showFunctions
dump (a,false,false);

Console

{
  [num]=> 123
  [str]=> "My name is Mykola Brozhyna" (26)
  [func]=> [Function]
  [b] (4) {
    [num]=> 123
    [str]=> "My name is Mykola Brozhyna" (26)
    [func]=> [Function]
    [c] (3) {
      [num]=> 123
      [str]=> "My name is Mykola Brozhyna" (26)
      [func]=> [Function]
    }
  }
}

( Now as showFunctions = true )

      var,showTypes,showFunctions
dump (a,false,true);

Console

{
  [num]=> 123
  [str]=> "My name is Mykola Brozhyna" (26)
  [func]=> function (){
             console.log("Oh yeah. It's all coming together.");
          }
  [b] (4) {
    [num]=> 123
    [str]=> "My name is Mykola Brozhyna" (26)
    [func]=> function (){
                 console.log("Oh yeah. It's all coming together.");
              }
    [c] (3) {
      [num]=> 123
      [str]=> "My name is Mykola Brozhyna" (26)
      [func]=> function (){
                     console.log("Oh yeah. It's all coming together.");
                  }
    }
  }
}

Show small log (default:false)

( You can also toggle log to see some small details about the result )

dump (a,false,false,true);

Console

--------------------------------
LOG: Start =>
[] (6){
   [maxNestedLevelAllowed]=> 0 
   [maxNestedLvlFound]=> 4 
   [forced_log]=> false 
   [maxStepsAllowed]=> 0 
   [stepsFound]=> 4 
   [msg]=> "" 
}
LOG: End
--------------------------------

Circular objects

( It deals with most cases of circularity )

test = function () {
   return {
      num: 123,
      str: "My name is Mykola Brozhyna",
      func: function () {
         console.log("Oh yeah. It's all coming together.");
      }
   };
};

a = new test;
a.c = a;
a.b = new test;
a.b.a = a;
a.b.c = new test;
a.b.c.d = new test;
a.b.c.d.e = a.b.c.d;

Console

[] (5){
   [num]=> 123 
   [str]=> "My name is Mykola Brozhyna" 
   [func]=> [Function] 
   [c]=> [Circular]
   [b](5){
      [num]=> 123 
      [str]=> "My name is Mykola Brozhyna" 
      [func]=> [Function] 
      [a]=> [Circular]
      [c](4){
         [num]=> 123 
         [str]=> "My name is Mykola Brozhyna" 
         [func]=> [Function] 
         [d](4){
            [num]=> 123 
            [str]=> "My name is Mykola Brozhyna" 
            [func]=> [Function] 
            [e]=> [Circular]
         }
      }
   }
}

Config

( Some configuration you can toy with)

Use vdumper.showConfig() to get config list.

vdumper.config = {
   showTypes: false, // default global setting for showing types
   showFunctions: false, // default global setting for showing function
   showLog: false, // default global setting for showing small log about last dump result
   maxNestedLevel: 0,  // max nested child level 
   print: true, // true means it will show dump result in consle
   colorsLog: true, // colors log in case there was some error (occurs when setting custom colors ) 
   bracketInNewLine: true, // brackets in new line (try it on same object to see for yourself :D)
   spacer:"   " // ( example below) (if not will be provided in the next updates)
};

( maxNestedLevel ) Consider the following example:

var id = 0;
test = function () {
   return {
     id:id++,
     str:"Abd Adwa awf awg "
   };
};

a = new test;
a.b = new test;
a.b.c = new test;

With vdumper.config.maxNestedLevel = 0 (0 = default,means there is no maxNestedLevel)

Console

[] (3){
   [id]=> 0 
   [str]=> "Abd Adwa awf awg " 
   [b](3){
      [id]=> 1 
      [str]=> "Abd Adwa awf awg " 
      [c](2){
         [id]=> 2 
         [str]=> "Abd Adwa awf awg " 
      }
   }
}

Now, lets say due to allot of information, you only want to get to b, and ignore rest. You can use vdumper.config.maxNestedLevel for that:

With vdumper.config.maxNestedLevel = 2 you get:

Console

[] (3){
   [id]=> 0 
   [str]=> "Abd Adwa awf awg " 
   [b](3){
      [id]=> 1 
      [str]=> "Abd Adwa awf awg " 
      [c](2){ // Content of `c` is ignored because it is > (maxNestedLevel = 2)
      }
   }
}

It's quite usefull when you need to filter out some information.

( bracketInNewLine ) This one is self explanatory

false

[b] (4) {
  [num]=> 123
  [str]=> 

true

[b] (4)
{
  [num]=> 123
  [str]=> 

( spacer )

By default spacer is equal to " "; so

a = new test;
a.b = new test;
a.b.c = new test;

Results in:

[] (3){
   [id]=> 0 
   [str]=> "Abd Adwa awf awg " 
   [b](3){
      [id]=> 1 
      [str]=> "Abd Adwa awf awg " 
      [c](2){
         [id]=> 2 
         [str]=> "Abd Adwa awf awg " 
      }
   }
}

Lets change spacer to spacer = "...":

vdumper.config.spacer = "...";

Result:

[] (3){
...[id]=> 0 
...[str]=> "Abd Adwa awf awg " 
...[b](3){
......[id]=> 1 
......[str]=> "Abd Adwa awf awg " 
......[c](2){
.........[id]=> 2 
.........[str]=> "Abd Adwa awf awg " 
......}
...}
}

You can toy with it to get some funny stuff.

( colorsLog: true )

In case you use custom non default color: if (true) it will yield a warning. Set it to false to turn of.

vdumper.config.colorsLog = false;

Colors

vdumper supports coloring, both: ( array / object ) and variables.

Use vdumper.colors.showList(); to get list of default colors;

Use vdumper.single.showList(); to get list for single variable;

Use vdumper.object.showList(); to get list for object;

In order to change colors we use:

let myColors = {
    key:"red",
    string:"green"
};
vdumper[`single / object`].setColors(myColors);

Or separated by space: "color bgcolor".

let myColors = {
    key:"red bgred",
    string:"bgreen green"
};
vdumper[`single / object`].setColors(myColors);

License

(The MIT License)

Copyright (c) 2019 Mykola Brozhyna

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.