Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 27 days 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 (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-22T12:21:17.000Z (almost 6 years ago)
- Last Synced: 2024-04-24T23:00:47.597Z (7 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
[![npm version](https://img.shields.io/npm/v/append-type.svg)](https://www.npmjs.com/package/append-type)
[![Build Status](https://travis-ci.com/shinnn/append-type.svg?branch=master)](https://travis-ci.com/shinnn/append-type)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/append-type.svg)](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