UNPKG

3.12 kBMarkdownView Raw
1# Display Value
2
3> Converts values to a readable display string
4>
5> [![npm][npm]][npm-url]
6[![build][build]][build-url]
7[![deps][deps]][deps-url]
8[![size][size]][size-url]
9[![vulnerabilities][vulnerabilities]][vulnerabilities-url]
10[![license][license]][license-url]
11
12
13<br><a name="displayValue"></a>
14
15## displayValue(value, [settings]) ⇒ <code>string</code>
16> Designed for use in test messages, displayValue takes a javascript value and returns a human readable string representation of that value.
17>
18> Notes:
19> - finite numbers are passed through number.toLocaleString()
20> - -0 is rendered as -0
21> - 1300 is rendered as 1,300 (depending on locale)
22> - strings are wrapped in double quotes
23> - Arrays and Objects are passed through a function similar to JSON.stringify, but values are individually run through displayValue
24> - Array-like values such as arguments are handled like Arrays
25> - Object-like values such as ClientRect and DOMRect are handled like Objects
26> - Constructors will return the constructor's name
27> - Instances of non-native constructors:
28> - will return the result of .toString() if other than '[object Object]'
29> - otherwise returns '[object Name]' where Name is the constructor's name
30
31
32| Param | Type | Default | Description |
33| --- | --- | --- | --- |
34| value | <code>\*</code> | | |
35| [settings] | <code>Object</code> | | |
36| [settings.beautify] | <code>Boolean</code> | <code>false</code> | If true and value is an Array or Object then the output is rendered in multiple lines with indentation |
37
38**Example**
39``` javascript
40import displayValue from 'display-value';
41
42displayValue(-0); // '-0'
43displayValue(1300000); // '1,300,000'
44displayValue('foo'); // '"foo"'
45displayValue({x: 1}); // '{"x": 1}'
46
47displayValue(() => {}); // '() => {…}'
48displayValue(function(param) {}); // 'ƒ (param) {…}'
49displayValue(function name() {}); // 'ƒ name() {…}'
50
51displayValue(Symbol()); // 'Symbol()'
52displayValue(Symbol('name')); // 'Symbol(name)'
53
54displayValue(new CustomClass()); // '[object CustomClass]'
55
56displayValue([{x: 1}, {x: 2000}], {beautify: true});
57// '[
58// {
59// "x": 1
60// }, {
61// "x": 2,000
62// }
63// ]'
64```
65
66[npm]: https://img.shields.io/npm/v/display-value.svg
67[npm-url]: https://npmjs.com/package/display-value
68[build]: https://travis-ci.org/DarrenPaulWright/display-value.svg?branch&#x3D;master
69[build-url]: https://travis-ci.org/DarrenPaulWright/display-value
70[deps]: https://david-dm.org/DarrenPaulWright/display-value.svg
71[deps-url]: https://david-dm.org/DarrenPaulWright/display-value
72[size]: https://packagephobia.now.sh/badge?p&#x3D;display-value
73[size-url]: https://packagephobia.now.sh/result?p&#x3D;display-value
74[vulnerabilities]: https://snyk.io/test/github/DarrenPaulWright/display-value/badge.svg?targetFile&#x3D;package.json
75[vulnerabilities-url]: https://snyk.io/test/github/DarrenPaulWright/display-value?targetFile&#x3D;package.json
76[license]: https://img.shields.io/github/license/DarrenPaulWright/display-value.svg
77[license-url]: https://npmjs.com/package/display-value/LICENSE.md