index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. var isValue = require("type/value/is")
  3. , isPlainFunction = require("type/plain-function/is")
  4. , assign = require("es5-ext/object/assign")
  5. , normalizeOpts = require("es5-ext/object/normalize-options")
  6. , contains = require("es5-ext/string/#/contains");
  7. var d = (module.exports = function (dscr, value/*, options*/) {
  8. var c, e, w, options, desc;
  9. if (arguments.length < 2 || typeof dscr !== "string") {
  10. options = value;
  11. value = dscr;
  12. dscr = null;
  13. } else {
  14. options = arguments[2];
  15. }
  16. if (isValue(dscr)) {
  17. c = contains.call(dscr, "c");
  18. e = contains.call(dscr, "e");
  19. w = contains.call(dscr, "w");
  20. } else {
  21. c = w = true;
  22. e = false;
  23. }
  24. desc = { value: value, configurable: c, enumerable: e, writable: w };
  25. return !options ? desc : assign(normalizeOpts(options), desc);
  26. });
  27. d.gs = function (dscr, get, set/*, options*/) {
  28. var c, e, options, desc;
  29. if (typeof dscr !== "string") {
  30. options = set;
  31. set = get;
  32. get = dscr;
  33. dscr = null;
  34. } else {
  35. options = arguments[3];
  36. }
  37. if (!isValue(get)) {
  38. get = undefined;
  39. } else if (!isPlainFunction(get)) {
  40. options = get;
  41. get = set = undefined;
  42. } else if (!isValue(set)) {
  43. set = undefined;
  44. } else if (!isPlainFunction(set)) {
  45. options = set;
  46. set = undefined;
  47. }
  48. if (isValue(dscr)) {
  49. c = contains.call(dscr, "c");
  50. e = contains.call(dscr, "e");
  51. } else {
  52. c = true;
  53. e = false;
  54. }
  55. desc = { get: get, set: set, configurable: c, enumerable: e };
  56. return !options ? desc : assign(normalizeOpts(options), desc);
  57. };