@skyscanner/backpack-web 36.2.0 → 36.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,7 @@ import { addCalendarGridTransition } from "./BpkCalendarGridTransition";
22
22
  import BpkCalendarWeek from "./BpkCalendarWeek";
23
23
  import { CALENDAR_SELECTION_TYPE } from "./custom-proptypes";
24
24
  import { addMonths, formatIsoDate, getCalendarMonthWeeks, isSameMonth } from "./date-utils";
25
+ import { memoize } from "./utils";
25
26
  import STYLES from "./BpkCalendarGrid.module.css";
26
27
 
27
28
  // This should be imported after `./BpkCalendarGrid.module.css`.
@@ -107,7 +108,7 @@ class BpkCalendarGrid extends Component {
107
108
  dates: dates,
108
109
  onDateClick: onDateClick,
109
110
  onDateKeyDown: onDateKeyDown,
110
- formatDateFull: formatDateFull,
111
+ formatDateFull: memoize(formatDateFull),
111
112
  DateComponent: DateComponent,
112
113
  dateModifiers: dateModifiers,
113
114
  preventKeyboardFocus: preventKeyboardFocus,
@@ -18,6 +18,7 @@
18
18
 
19
19
  import { cssModules } from "../../bpk-react-utils";
20
20
  import { CALENDAR_SELECTION_TYPE } from "./custom-proptypes";
21
+ import { memoize } from "./utils";
21
22
  import STYLES from "./BpkCalendar.module.css";
22
23
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
23
24
  const getClassName = cssModules(STYLES);
@@ -82,7 +83,7 @@ const composeCalendar = (Nav, GridHeader, Grid, CalendarDate) => {
82
83
  className: classNames.join(' '),
83
84
  children: [Nav && /*#__PURE__*/_jsx(Nav, {
84
85
  changeMonthLabel: changeMonthLabel,
85
- formatMonth: formatMonth,
86
+ formatMonth: memoize(formatMonth),
86
87
  id: `${id}__bpk_calendar_nav`,
87
88
  maxDate: maxDate,
88
89
  minDate: minDate,
@@ -105,7 +106,7 @@ const composeCalendar = (Nav, GridHeader, Grid, CalendarDate) => {
105
106
  dateModifiers: dateModifiers,
106
107
  daysOfWeek: daysOfWeek,
107
108
  formatDateFull: formatDateFull,
108
- formatMonth: formatMonth,
109
+ formatMonth: memoize(formatMonth),
109
110
  month: month,
110
111
  onDateClick: onDateClick,
111
112
  onDateKeyDown: onDateKeyDown,
@@ -6,3 +6,38 @@ export declare const getTransformStyles: (transformValue: string) => {
6
6
  WebkitTransform: string;
7
7
  };
8
8
  export declare const isTransitionEndSupported: () => boolean;
9
+ /**
10
+ * Memoizes a given function to optimize performance by caching its results based on input arguments.
11
+ *
12
+ * @template T - The type of the function to be memoized. Must be a function.
13
+ * @param {T} fn - The function to be memoized.
14
+ * @returns {T} - The memoized version of the input function. Subsequent calls with the same arguments
15
+ * will return the cached result, avoiding redundant computations.
16
+ *
17
+ * @example
18
+ * // Simple usage:
19
+ * const add = (a: number, b: number) => a + b;
20
+ * const memoizedAdd = memoize(add);
21
+ *
22
+ * console.log(memoizedAdd(1, 2)); // Computes and caches result: 3
23
+ * console.log(memoizedAdd(1, 2)); // Fetches from cache: 3
24
+ *
25
+ * @example
26
+ * // With a more complex function:
27
+ * const slowFunction = (num: number) => {
28
+ * console.log('Computing...');
29
+ * return num * 2;
30
+ * };
31
+ * const memoizedSlowFunction = memoize(slowFunction);
32
+ *
33
+ * console.log(memoizedSlowFunction(5)); // Logs "Computing...", returns 10
34
+ * console.log(memoizedSlowFunction(5)); // Fetches from cache: 10
35
+ *
36
+ * @remarks
37
+ * - This implementation uses a `Map` object and `JSON.stringify` to create cache keys.
38
+ * - Functions with non-primitive or cyclic arguments may not work as expected due to
39
+ * `JSON.stringify` limitations.
40
+ * - The cache is stored in memory and will grow indefinitely unless manually managed or
41
+ * the function goes out of scope.
42
+ */
43
+ export declare function memoize<T extends (...args: any[]) => any>(fn: T): T;
@@ -45,4 +45,52 @@ export const getTransformStyles = transformValue => {
45
45
  WebkitTransform: transform
46
46
  };
47
47
  };
48
- export const isTransitionEndSupported = () => !!(typeof window !== 'undefined' && 'TransitionEvent' in window);
48
+ export const isTransitionEndSupported = () => !!(typeof window !== 'undefined' && 'TransitionEvent' in window);
49
+
50
+ /**
51
+ * Memoizes a given function to optimize performance by caching its results based on input arguments.
52
+ *
53
+ * @template T - The type of the function to be memoized. Must be a function.
54
+ * @param {T} fn - The function to be memoized.
55
+ * @returns {T} - The memoized version of the input function. Subsequent calls with the same arguments
56
+ * will return the cached result, avoiding redundant computations.
57
+ *
58
+ * @example
59
+ * // Simple usage:
60
+ * const add = (a: number, b: number) => a + b;
61
+ * const memoizedAdd = memoize(add);
62
+ *
63
+ * console.log(memoizedAdd(1, 2)); // Computes and caches result: 3
64
+ * console.log(memoizedAdd(1, 2)); // Fetches from cache: 3
65
+ *
66
+ * @example
67
+ * // With a more complex function:
68
+ * const slowFunction = (num: number) => {
69
+ * console.log('Computing...');
70
+ * return num * 2;
71
+ * };
72
+ * const memoizedSlowFunction = memoize(slowFunction);
73
+ *
74
+ * console.log(memoizedSlowFunction(5)); // Logs "Computing...", returns 10
75
+ * console.log(memoizedSlowFunction(5)); // Fetches from cache: 10
76
+ *
77
+ * @remarks
78
+ * - This implementation uses a `Map` object and `JSON.stringify` to create cache keys.
79
+ * - Functions with non-primitive or cyclic arguments may not work as expected due to
80
+ * `JSON.stringify` limitations.
81
+ * - The cache is stored in memory and will grow indefinitely unless manually managed or
82
+ * the function goes out of scope.
83
+ */
84
+ export function memoize(fn) {
85
+ const cache = new Map();
86
+ function memoizedFunction(...args) {
87
+ const key = JSON.stringify(args);
88
+ if (cache.has(key)) {
89
+ return cache.get(key);
90
+ }
91
+ const result = fn(...args);
92
+ cache.set(key, result);
93
+ return result;
94
+ }
95
+ return memoizedFunction;
96
+ }
@@ -18,4 +18,4 @@
18
18
 
19
19
  /* stylelint-disable at-rule-disallowed-list */
20
20
 
21
- @import '~@skyscanner/bpk-foundations-web/tokens/base.default';
21
+ @import '@skyscanner/bpk-foundations-web/tokens/base.default';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skyscanner/backpack-web",
3
- "version": "36.2.0",
3
+ "version": "36.4.0",
4
4
  "description": "Backpack Design System web library",
5
5
  "repository": {
6
6
  "type": "git",