setDefaultOptions.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. exports.setDefaultOptions = setDefaultOptions;
  3. var _index = require("./_lib/defaultOptions.js");
  4. /**
  5. * @name setDefaultOptions
  6. * @category Common Helpers
  7. * @summary Set default options including locale.
  8. * @pure false
  9. *
  10. * @description
  11. * Sets the defaults for
  12. * `options.locale`, `options.weekStartsOn` and `options.firstWeekContainsDate`
  13. * arguments for all functions.
  14. *
  15. * @param options - An object with options
  16. *
  17. * @example
  18. * // Set global locale:
  19. * import { es } from 'date-fns/locale'
  20. * setDefaultOptions({ locale: es })
  21. * const result = format(new Date(2014, 8, 2), 'PPPP')
  22. * //=> 'martes, 2 de septiembre de 2014'
  23. *
  24. * @example
  25. * // Start of the week for 2 September 2014:
  26. * const result = startOfWeek(new Date(2014, 8, 2))
  27. * //=> Sun Aug 31 2014 00:00:00
  28. *
  29. * @example
  30. * // Start of the week for 2 September 2014,
  31. * // when we set that week starts on Monday by default:
  32. * setDefaultOptions({ weekStartsOn: 1 })
  33. * const result = startOfWeek(new Date(2014, 8, 2))
  34. * //=> Mon Sep 01 2014 00:00:00
  35. *
  36. * @example
  37. * // Manually set options take priority over default options:
  38. * setDefaultOptions({ weekStartsOn: 1 })
  39. * const result = startOfWeek(new Date(2014, 8, 2), { weekStartsOn: 0 })
  40. * //=> Sun Aug 31 2014 00:00:00
  41. *
  42. * @example
  43. * // Remove the option by setting it to `undefined`:
  44. * setDefaultOptions({ weekStartsOn: 1 })
  45. * setDefaultOptions({ weekStartsOn: undefined })
  46. * const result = startOfWeek(new Date(2014, 8, 2))
  47. * //=> Sun Aug 31 2014 00:00:00
  48. */
  49. function setDefaultOptions(options) {
  50. const result = {};
  51. const defaultOptions = (0, _index.getDefaultOptions)();
  52. for (const property in defaultOptions) {
  53. if (Object.prototype.hasOwnProperty.call(defaultOptions, property)) {
  54. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- I challange you to fix the type
  55. result[property] = defaultOptions[property];
  56. }
  57. }
  58. for (const property in options) {
  59. if (Object.prototype.hasOwnProperty.call(options, property)) {
  60. if (options[property] === undefined) {
  61. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- I challange you to fix the type
  62. delete result[property];
  63. } else {
  64. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- I challange you to fix the type
  65. result[property] = options[property];
  66. }
  67. }
  68. }
  69. (0, _index.setDefaultOptions)(result);
  70. }