util.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict'
  2. module.exports.isObjectProto = isObjectProto
  3. function isObjectProto (obj) {
  4. return obj === Object.prototype
  5. }
  6. const _null = {}
  7. const _undefined = {}
  8. const Bool = Boolean
  9. const Num = Number
  10. const Str = String
  11. const boolCache = {
  12. true: new Bool(true),
  13. false: new Bool(false)
  14. }
  15. const numCache = {}
  16. const strCache = {}
  17. /*
  18. * Returns a useful dispatch object for value using a process similar to
  19. * the ToObject operation specified in http://es5.github.com/#x9.9
  20. */
  21. module.exports.dispatchableObject = dispatchableObject
  22. function dispatchableObject (value) {
  23. // To shut up jshint, which doesn't let me turn off this warning.
  24. const Obj = Object
  25. if (value === null) { return _null }
  26. if (value === undefined) { return _undefined }
  27. switch (typeof value) {
  28. case 'object': return value
  29. case 'boolean': return boolCache[value]
  30. case 'number': return numCache[value] || (numCache[value] = new Num(value))
  31. case 'string': return strCache[value] || (strCache[value] = new Str(value))
  32. default: return new Obj(value)
  33. }
  34. }