index.js 759 B

12345678910111213141516171819202122232425262728293031
  1. /*!
  2. * define-property <https://github.com/jonschlinkert/define-property>
  3. *
  4. * Copyright (c) 2015, 2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. var isDescriptor = require('is-descriptor');
  9. module.exports = function defineProperty(obj, prop, val) {
  10. if (typeof obj !== 'object' && typeof obj !== 'function') {
  11. throw new TypeError('expected an object or function.');
  12. }
  13. if (typeof prop !== 'string') {
  14. throw new TypeError('expected `prop` to be a string.');
  15. }
  16. if (isDescriptor(val) && ('set' in val || 'get' in val)) {
  17. return Object.defineProperty(obj, prop, val);
  18. }
  19. return Object.defineProperty(obj, prop, {
  20. configurable: true,
  21. enumerable: false,
  22. writable: true,
  23. value: val
  24. });
  25. };