stringable
v0.2.4
Published
Stringify any values to got something which render well in a log
Downloads
3
Maintainers
Readme
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 stringable
How 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
value
any The value to format.formatter
function (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
data
object The data object sent to the formatter.data.value
any The value to format.data.type
string The value type from typeof operator.data.stringifiedValue
string A default and not optimal stringified version of the value.data.isInteger
boolean true if the value is an integer, else false.data.isFloat
boolean true if the value is a float, else false.data.simpleQuoteString
string If value is a string, this will be a representation of the string quotted with simple quotes.data.doubleQuoteString
string If value is a string, this will be a representation of the string quotted with double quotes.data.defaultFormatter
function The default formatter used by stringable.data.constructorName
string The name of the value constructor.data.keys
Array<string> The list of the value properties (or attributes names of the value is a DOM Node)data.functionName
string The name of the function is the value is a function.data.isAsync
boolean true if the value is an async function, else false.data.isGenerator
boolean true if the value is a generator function (using the syntax function*), else false.data.isClass
boolean true if the value is an es class, else false.
parents
Array Used internally. An array containing the owners objects of the value (if value is a property). (optional, default[]
)deepness
number Used internally. The displayed nesting indentation level. (optional, default0
)useNestTab
boolean 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