prr.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*!
  2. * prr
  3. * (c) 2013 Rod Vagg <rod@vagg.org>
  4. * https://github.com/rvagg/prr
  5. * License: MIT
  6. */
  7. (function (name, context, definition) {
  8. if (typeof module != 'undefined' && module.exports)
  9. module.exports = definition()
  10. else
  11. context[name] = definition()
  12. })('prr', this, function() {
  13. var setProperty = typeof Object.defineProperty == 'function'
  14. ? function (obj, key, options) {
  15. Object.defineProperty(obj, key, options)
  16. return obj
  17. }
  18. : function (obj, key, options) { // < es5
  19. obj[key] = options.value
  20. return obj
  21. }
  22. , makeOptions = function (value, options) {
  23. var oo = typeof options == 'object'
  24. , os = !oo && typeof options == 'string'
  25. , op = function (p) {
  26. return oo
  27. ? !!options[p]
  28. : os
  29. ? options.indexOf(p[0]) > -1
  30. : false
  31. }
  32. return {
  33. enumerable : op('enumerable')
  34. , configurable : op('configurable')
  35. , writable : op('writable')
  36. , value : value
  37. }
  38. }
  39. , prr = function (obj, key, value, options) {
  40. var k
  41. options = makeOptions(value, options)
  42. if (typeof key == 'object') {
  43. for (k in key) {
  44. if (Object.hasOwnProperty.call(key, k)) {
  45. options.value = key[k]
  46. setProperty(obj, k, options)
  47. }
  48. }
  49. return obj
  50. }
  51. return setProperty(obj, key, options)
  52. }
  53. return prr
  54. })