41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
/**
|
|
* @file ES6-compliant shim for ToString.
|
|
* @see {@link http://www.ecma-international.org/ecma-262/6.0/#sec-tostring|7.1.12 ToString ( argument )}
|
|
* @version 1.4.2
|
|
* @author Xotic750 <Xotic750@gmail.com>
|
|
* @copyright Xotic750
|
|
* @license {@link <https://opensource.org/licenses/MIT> MIT}
|
|
* @module to-string-x
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var castString = ''.constructor;
|
|
var isSymbol = require('is-symbol');
|
|
|
|
/**
|
|
* The abstract operation ToString converts argument to a value of type String.
|
|
*
|
|
* @param {*} value - The value to convert to a string.
|
|
* @throws {TypeError} If `value` is a Symbol.
|
|
* @returns {string} The converted value.
|
|
* @example
|
|
* var $toString = require('to-string-x');
|
|
*
|
|
* $toString(); // 'undefined'
|
|
* $toString(null); // 'null'
|
|
* $toString('abc'); // 'abc'
|
|
* $toString(true); // 'true'
|
|
* $toString(Symbol('foo')); // TypeError
|
|
* $toString(Symbol.iterator); // TypeError
|
|
* $toString(Object(Symbol.iterator)); // TypeError
|
|
* $toString(Object.create(null)); // TypeError
|
|
*/
|
|
module.exports = function ToString(value) {
|
|
if (isSymbol(value)) {
|
|
throw new TypeError('Cannot convert a Symbol value to a string');
|
|
}
|
|
|
|
return castString(value);
|
|
};
|