stringable v0.2.4
Stringable
Stringify any values to got something which render well in a log
Introduction
Why ?
You need to format as string a value that you ignore the type. However, in JS, some value doesn't have nice stringified version, and even sometimes, trying to stringify a value can throw an error. For example, if you try to stringify a Symbol using a string template like that:
const aSymbol = Symbol('a'); console.log(`${aSymbol}`);You will get an error like: "Cannot convert a Symbol value to a string". To avoid issues like this one, Stringable converts any value in a string which aims to be readable, verbose, and useful in dev mode.
How to install ?
npm i stringableHow to use ?
const stringable = require('stringable');
const loggableObject = stringable({ firstName: 'Will', lastName: 'Wheeler', age: 13 });
console.log(loggableObject);
// (object => {
// firstName: (string => 'Will'),
// lastName: (string => 'Wheeler'),
// age: (number: integer => 13)
// })This is a basic example. You can use stringable with any value like functions, array, Symbols and even Node instance from the DOM API. Some value however, maybe worth to have a better and/or more specific formatted version. Don't hesitate to open issues.
Eventually, if you don't like the default formatting of some values, you can create a variant of stringable passing a custom formatter function to it (see the documentation below). The formatter will receive some data about the value to format and just have to return the formated value as a string.
Documentation
Table of Contents
stringable
The function you get when requiring the stringable module. Formats a value.
Parameters
valueany The value to format.formatterfunction (data: object): string The formatter to use in order to format the value. To create a custom one, look at the defaultFormatter documentation to see which data it will receive. (optional, defaultdefaultFormatter)
Returns string The value formatted using the formatter.
defaultFormatter
Can't be required directly. The formatter used by default when using stringable. It formats a value from data provided by stringable.
Parameters
dataobject The data object sent to the formatter.data.valueany The value to format.data.typestring The value type from typeof operator.data.stringifiedValuestring A default and not optimal stringified version of the value.data.isIntegerboolean true if the value is an integer, else false.data.isFloatboolean true if the value is a float, else false.data.simpleQuoteStringstring If value is a string, this will be a representation of the string quotted with simple quotes.data.doubleQuoteStringstring If value is a string, this will be a representation of the string quotted with double quotes.data.defaultFormatterfunction The default formatter used by stringable.data.constructorNamestring The name of the value constructor.data.keysArray<string> The list of the value properties (or attributes names of the value is a DOM Node)data.functionNamestring The name of the function is the value is a function.data.isAsyncboolean true if the value is an async function, else false.data.isGeneratorboolean true if the value is a generator function (using the syntax function*), else false.data.isClassboolean true if the value is an es class, else false.
parentsArray Used internally. An array containing the owners objects of the value (if value is a property). (optional, default[])deepnessnumber Used internally. The displayed nesting indentation level. (optional, default0)useNestTabboolean Used internally. If the nesting indentation must be used at this level. (optional, defaulttrue)
Returns string The formatted value as a string.
License
stringable is released under MIT. Copyright (c) 2017-present Alexis Tessier