UNPKG

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