Skip to content
Better variable inspector for JavaScript
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
docs/readme
src
test
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.ignore
.mailmap
.npmignore
.npmrc
.nvmrc
.prettierignore
.prettierrc
.travis.yml
.yarnrc
CHANGELOG.md
LICENSE
README.md
_config.yml
package.json
yarn.lock

README.md

consono

Consono

Better variable inspector for JavaScript.

Version Build Status License: MIT

Consono on github.io 🡆

Motivation and differences from other libraries

  • Light and dark themes for terminal output
  • Can print to terminal or return formatted and colored string for later use
  • Configurable coloring of variables
  • Turn on/off output colorization
  • Clear terminal before output and / or exit after it
  • Configurable indent - tabs vs spaces
  • Availability to set depth for object inspection
  • Configurable max items for array, map, object, set
  • Limit string length when printing for better readability
  • Inspect string length vs string chars
  • Inspect items count for collection-like variables array, map, object, set
  • Actually can inspect arguments, set and map
  • Can print function names or mark them as anonymous
  • Handles circular references
  • And so on and so forth…

Installation

npm install consono

Include

Default is function for printing variable.

const consono = require("consono").default;

Require multiple items: function, constructor, options object, theme objects.

const {
  Consono,
  consono,
  options,
  THEME_DARK
  THEME_LIGHT,
} = require("consono");

Import as ECMAScript module.

import { consono } from "consono/es";

Options

const { Consono } = require("consono");
const options = {
  clear: true,
  quotesEnd: ``,
  quotesStart: ``,
  stringMaxLength: 54,
};
const theme = "light"; // default is "dark"
const consono = new Consono(options, theme);
consono.log("Cleared before output. Different quotes. And cut to 54!");
// string • "Cleared before output. Different quotes. And cut to 54"
// (length=55, shown=54)
const { Consono } = require("consono");
const theme = {
  argument: [253, 151, 31],
  boolean: [174, 129, 255],
  comment: [117, 113, 94],
  keyword: [249, 38, 114],
  name: [230, 219, 116],
  number: [174, 129, 255],
  plain: [255, 255, 255],
  property: [102, 217, 239],
  string: [166, 226, 46],
};
const consono = new Consono(null, theme);
consono.log("Themed");
const { Consono, options } = require("consono");
options.colorize = false;
const consono = new Consono(options);
consono.log("Text without colorization");
const { consono } = require("consono");
console.debug(
  consono("Outputs a message only at the debug log level.", false)
);

Instance

const consono = Consono.factory(options, theme);
consono("This is log function with your own options");

Log function

const { consono } = require("consono");
const map = new Map();
map.add("key", true);
consono(map);

Return string with variable description.

const variableAsString = consono({}, false);

or

const variableAsString = consono({},  { console: false });
const defaultOptions = {
  // Maximum number of elements in array to show
  arrayMaxElements: 99,
  // Assign symbol
  assignSymbol: "",
  // Clear console before output
  clear: false,
  // Colorize the output
  colorize: true,
  // Return string or output to console
  console: true,
  // Default depth of object
  depth: 20,
  // Exit on completion
  exit: false,
  // Print indentation
  indent: "ˑˑ",
  // Maximum number of entries in map to show
  mapMaxEntries: 99,
  // Maximum number of properties in object to show
  objectMaxProps: 99,
  // Quote start
  quotesEnd: `"`,
  // Quote end
  quotesStart: `"`,
  // Maximum number of values in set to show
  setMaxValues: 99,
  // Maximum length of string to show
  stringMaxLength: 360,
};
consono("Some variable", defaultOptions);

All possible outputs - try it.

Examples

consono(undefined);
consono(null);

consono

consono(true);
consono(false);

consono

consono(Infinity);
consono(Number.NEGATIVE_INFINITY);
consono(NaN);
consono(1);
consono(1.5);
consono(BigInt(9007199254740991));

consono

consono(new Date());

consono

consono("Hello, world 😀😁😂🤣😃😄😅😆😉😊", { stringMaxLength: 17 });
consono(Symbol("🌈"));

consono

consono(/[0-9]+/);
consono(/\w+/giu);

consono

consono(function() {});
consono(function helloWorld() {});
consono(() => {});

consono

consono(new Promise(() => {}));
consono(async function helloWorld() {});
consono(async () => {});
consono(function* gen() { yield 1; });

consono

consono([1, 2, 3]);
consono(Int8Array.from([1, 2, 3]));

consono

consono(new ArrayBuffer(8));
consono(new SharedArrayBuffer(16));

consono

consono(new Set(["a", true, { prop: 1 }]));
consono(new Map([["first", "a"], [true, "b"]]));
consono(new WeakMap());
consono(new WeakSet());

consono

consono({});

class MyClass {}
const myClass = new MyClass();
myClass.deeper = new MyClass();
consono(myClass);

consono

consono(new Error("Standard error"));
consono(new EvalError("Unable to run this code"));
consono(new RangeError("Must be less than 10 and greater than 0"));
consono(new ReferenceError("This is error from try/catch"));
consono(new SyntaxError("Not a source code"));
consono(new TypeError("Value is not of the expected type"));

consono

(function(a, b) { consono(arguments); })(true, false);

consono

consono(global || globalThis, { objectMaxProps: 3 });

consono

I need help with:

  • Writing documentation
  • Theming console output
  • Writing tests
You can’t perform that action at this time.