heyujie
2021-05-24 4885600ecc369aa2e30a65de8dd7a410f13c34df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"use strict";
 
var isValue  = require("../value/is")
  , isObject = require("../object/is");
 
var objectToString = Object.prototype.toString;
 
module.exports = function (value) {
    if (!isValue(value)) return null;
    if (isObject(value)) {
        // Reject Object.prototype.toString coercion
        var valueToString = value.toString;
        if (typeof valueToString !== "function") return null;
        if (valueToString === objectToString) return null;
        // Note: It can be object coming from other realm, still as there's no ES3 and CSP compliant
        // way to resolve its realm's Object.prototype.toString it's left as not addressed edge case
    }
    try {
        return "" + value; // Ensure implicit coercion
    } catch (error) {
        return null;
    }
};