Objects.stringify()
Syntax
export interface StringifyOptions {
truncateContents?: boolean | number;
typesOnly?: boolean;
primitiveTypesOnly?: boolean;
hideClasses?: boolean;
useToString?: boolean;
omitQuotes?: boolean;
replacer?: (value: unknown) => string | null;
}
function stringify(value: unknown, options?: StringifyOptions): string
Converts an arbitrary value to a string.
Parameters | |
value
|
A value of any type. No value will throw an error. The resulting string depends on the type of value. By default, primitives are printed as-is, arrays are printed in square bracket notation, class instances call toString() if a manual override exists, and all other objects print in curly bracket notation. |
options
Optional
|
An object containing any of the below options. These allow you to customize the stringifying behavior. |
options.truncateContents
Optional
|
Whether the contents of arrays and objects should be truncated and
replaced with an ellipsis ( Default is |
options.typesOnly
Optional
|
If true, stringify will only print the types, not their values.
For instance, the value This property overrides Default is |
options.primitiveTypesOnly
Optional
|
Like This property is overridden by Default is |
options.hideClasses
Optional
|
By default if a non-primitive (that is, an This property is overridden by Default is |
options.useToString
Optional
|
If true, this function uses a type’s This property is overridden by Default is |
options.omitQuotes
Optional
|
By default, this function adds double quotes to either side of natural strings. This property removes those quotes. Default is |
options.replacer
Optional
|
If specified, this callback will be called for every value that is to be converted to string (i.e. the main value, all properties, and all array elements). If the function returns a string, that string is used instead of the
default string conversion of |
Example
import { Objects } from 'potence';
class CustomClass {
prop = 5
}
Objects.stringify('hello'); // -> "hello"
Objects.stringify('hello', { omitQuotes: true }); // -> hello
Objects.stringify('hello', { typesOnly: true }); // -> string
Objects.stringify([1, 2, 3]); // -> [1, 2, 3]
Objects.stringify({ prop: 'hello' }); // -> { prop: "hello" }
Objects.stringify(new CustomClass()); // -> CustomClass { prop: 5 }
Objects.stringify(() => 5); // -> () => 5
Objects.stringify([1, 2, 3], { truncateContents: true });
// -> [...]
Objects.stringify({ prop: '5' }, { truncateContents: true });
// -> { ... }
Objects.stringify([1, 2, 3], { truncateContents: 2 });
// -> [1, 2, ...]
Objects.stringify([1, 2, 3], { typesOnly: true });
// -> Array
Objects.stringify([1, 2, 3], { primitiveTypesOnly: true });
// -> [number, number, number]
Objects.stringify(new CustomClass(), { hideClasses: true });
// -> { prop: 5 }
// Multi-line functions are always truncated:
Objects.stringify(() => {
return 5;
});
// -> () => { ... }
function replacer(item: unknown): string {
if (typeof item === 'number') {
return item.toString(2);
}
return null;
}
Objects.stringify([1, 2, 3, 'no-change'], { replacer });
// -> [1, 10, 11, "no-change"]
Remarks
This function differs from
JSON.stringify()
in that the strings generated by this function are not guaranteed to be valid
JSON. Instead of stripping out all types, this function attempts to keep
its output as close to the original data type as possible. For instance, this
function returns a valid string representation of functions (whereas
JSON.stringify()
strips functions out completely) and prints the class name
of custom classes where appropriate.
While you should always strive to create your own overrides of toString()
for
stringifications that are shown to the end user in some way, this function can
be very useful for debug outputs. For instance, the assert
module heavily
utilizes this function to generate expressive failure messages.
All strings returned by this function are single-line.