https://github.com/shinnn/append-type
Stringify the value with appending its type: 10 → '10 (number)'
https://github.com/shinnn/append-type
error-messages javascript stringify type
Last synced: 9 months ago
JSON representation
Stringify the value with appending its type: 10 → '10 (number)'
- Host: GitHub
- URL: https://github.com/shinnn/append-type
- Owner: shinnn
- License: mit-0
- Created: 2016-10-14T11:02:25.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-01-22T12:21:17.000Z (over 7 years ago)
- Last Synced: 2025-08-09T01:35:05.696Z (9 months ago)
- Topics: error-messages, javascript, stringify, type
- Language: JavaScript
- Size: 69.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# append-type
[](https://www.npmjs.com/package/append-type)
[](https://travis-ci.com/shinnn/append-type)
[](https://coveralls.io/r/shinnn/append-type)
Stringify the value with appending its [type](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof): `10` → `'10 (number)'`
```javascript
import appendType from 'append-type';
appendType('123'); //=> '123 (string)'
appendType(123); //=> '123 (number)'
```
## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install append-type
```
## API
```javascript
import appendType from 'append-type';
```
### appendType(*value*)
*value*: any type
Return: `string`
Essentially, it returns `String(value) + ' (' + typeof value + ')'`.
```javascript
appendType(() => {}); //=> '() => {} (function)'
```
When it takes `null` / `undefined`, it returns `'null'` / `'undefined'`.
```javascript
appendType(null); //=> 'null'
appendType(undefined); //=> 'undefined'
```
## Example
This module is useful for making `TypeError` error messages.
```javascript
function reverse(v) {
if (typeof v !== 'boolean') {
throw new TypeError(`Expected a Boolean value, but got ${appendType(v)}.`);
}
return !v;
};
reverse(1); //=> TypeError: Expected a Boolean value, but got 1 (number).
```
## License
[MIT No Attribution](./LICENSE) © 2019 Shinnosuke Watanabe