JSPM

append-type

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 28610
  • Score
    100M100P100Q141203F
  • License MIT

Stringify the value with appending its type: 10 → '10 (number)'

Package Exports

  • append-type

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (append-type) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

append-type

NPM version Bower version Build Status Coverage Status devDependencies Status

Stringify the value with appending its type: 10'10 (number)'

import appendType from 'append-type';

appendType('123'); //=> 'foo (string)'
appendType(123); //=> '123 (number)'

Installation

npm

npm install append-type

bower

bower install append-type

API

appendType(value)

value: any type
Return: String

Essentially, it returns String(value) + ' (' + typeof value + ')'.

appendType(() => {}); //=> '() => {} (function)'

When it takes null / undefined, it returns 'null' / 'undefined'.

appendType(null); //=> 'null'
appendType(undefined); //=> 'undefined'

Example

This module is useful for making TypeError error messages.

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

Copyright (c) 2016 Shinnosuke Watanabe

Licensed under the MIT License.