What?
Object Information is a library that returns a formatted string with informarion about what an object contains.
Why?
This was developed to give better results when combined with approval tests. JSON.stringify by itself does not give enough information.
I wanted something that could handle functions, signet signatures, and errors within the output.
The 'asInformationString' method, takes an object and returns the most detailed string.
const { asInformationString } = require('object-information');
const enforcedFunction = signet.enforce(
'a:number, b:int => number',
function enforcedFunction(a, b) {
return a * b;
}
);
const testObject = {
'a String property': 'this is a string',
'an error property': new Error('this is an error'),
'a number property': 42,
'an enforced function': enforcedFunction,
'an array property': ['life', 'universe', 'everything', 42],
};
const informationString = asInformationString(testObject);
The variable 'inforamtionString' contains:
`{
"a String property": "this is a string",
"an error property": "Error: this is an error",
"a number property": 42,
"an enforced function": "Function: enforceDecorator [a:number, b:int => number]",
"an array property": [
"life",
"universe",
"everything",
42
]
}`
The method 'asBasicInformationString' removes the function signatures.
`{
"a String property": "this is a string",
"an error property": "Error: this is an error",
"a number property": 42,
"an enforced function": "Function: enforceDecorator",
"an array property": [
"life",
"universe",
"everything",
42
]
}`
The method 'asFormattedJsonString' is just a wrapper arround JSON.stringify(value, null, 4);
.
`{
"a String property": "this is a string",
"an error property": {},
"a number property": 42,
"an array property": [
"life",
"universe",
"everything",
42
]
}`
The method 'asJsonString' allows you to determine the indent.
The call asJsonString(testObject, 2);
returns:
`{
"a String property": "this is a string",
"an error property": {},
"a number property": 42,
"an array property": [
"life",
"universe",
"everything",
42
]
}`
Where as the call sJsonString(testObject);
returns:
'{"a String property":"this is a string","an error property":{},"a number property":42,"an array property":["life","universe","everything",42]}'
The method 'asGridString' takes in an array of arrays, and then prints returns it as a grid.
const data = [
[1, 2, null],
[4, undefined, 5],
[6, { property: 7 }, 8]
];
asGridString(data);
This call returns:
.......1........|.......2........|......null......
.......4........|.."undefined"...|.......5........
.......6........|.{"property":7}.|.......8........