You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

coerce.js 721B

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