2.2.0 • Published 8 months ago
display-value v2.2.0
Display Value
Converts values to a readable display string
displayValue(value, settings) ⇒ string
Designed for use in test messages, displayValue takes a javascript value and returns a human readable string representation of that value.
Notes:
- finite numbers are passed through number.toLocaleString()
- -0 is rendered as -0
- 1300 is rendered as 1,300 (depending on locale)
- strings are wrapped in double quotes
- Arrays and Objects are passed through a function similar to JSON.stringify, but values are individually run through displayValue
- Array-like values such as arguments are handled like Arrays
- Object-like values such as ClientRect and DOMRect are handled like Objects
- Constructors will return the constructor's name
- Instances of non-native constructors:
- will return the result of .toString() if other than 'object Object'
- otherwise returns 'object Name' where Name is the constructor's name
Param | Type | Default | Description |
---|---|---|---|
value | * | ||
settings | Object | ||
settings.beautify | Boolean | false | If true and value is an Array or Object then the output is rendered in multiple lines with indentation |
settings.preferJson | Boolean | true | If true then keys and strings are wrapped in double quotes, similar to JSON.stringify. |
settings.preferSingleQuote | Boolean | false | If true then strings will be wrapped in single quotes. Only applicable if preferJson is false. |
Example
import displayValue from 'display-value';
displayValue(-0); // '-0'
displayValue(1300000); // '1,300,000'
displayValue('foo'); // '"foo"'
displayValue({x: 1}); // '{"x": 1}'
displayValue(() => {}); // '() => {…}'
displayValue(function(param) {}); // 'ƒ (param) {…}'
displayValue(function name() {}); // 'ƒ name() {…}'
displayValue(Symbol()); // 'Symbol()'
displayValue(Symbol('name')); // 'Symbol(name)'
displayValue(new CustomClass()); // '[object CustomClass]'
displayValue([{x: 1}, {x: 2000}], {beautify: true});
// '[
// {
// "x": 1
// }, {
// "x": 2,000
// }
// ]'
2.2.0
8 months ago
2.1.0
1 year ago
2.0.1
2 years ago
2.0.0
3 years ago
1.8.5
3 years ago
1.8.4
3 years ago
1.8.2
3 years ago
1.8.1
4 years ago
1.8.0
4 years ago
1.7.4
4 years ago
1.7.3
4 years ago
1.7.2
4 years ago
1.7.1
4 years ago
1.7.0
4 years ago
1.6.1
4 years ago
1.6.0
4 years ago
1.5.0
4 years ago
1.4.2
4 years ago
1.4.1
4 years ago
1.4.0
4 years ago
1.3.2
4 years ago
1.3.1
4 years ago
1.3.0
5 years ago
1.2.2
5 years ago
1.2.1
5 years ago
1.2.0
5 years ago
1.1.0
5 years ago
1.0.2
5 years ago
1.0.1
5 years ago
1.0.0
5 years ago