• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yoriiis / step-manager / 6523848256

15 Oct 2023 11:47AM UTC coverage: 99.355%. Remained the same
6523848256

push

github

web-flow
Update manager.js

103 of 104 branches covered (0.0%)

Branch coverage included in aggregate %.

205 of 206 relevant lines covered (99.51%)

6.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.3
/src/cache-manager.js
1
export default class CacheManager {
2
        /**
3
         * @param {options}
4
         */
5
        constructor(options) {
6
                const userOptions = options || {};
10✔
7
                const defaultOptions = {
10✔
8
                        cacheMethod: 'sessionStorage',
9
                        keyBrowserStorage: 'stepManager'
10
                };
11

12
                // Merge default options with user options
13
                this.options = Object.assign(defaultOptions, userOptions);
10✔
14
        }
15

16
        /**
17
         * Get step datas from the cache
18
         *
19
         * @param {Array} filters Filter the request by route
20
         *
21
         * @returns {Object} Datas from the cache
22
         */
23
        getDatasFromCache(filters) {
24
                let datasToReturn = null;
3✔
25

26
                // Retrieve the data in the cache with the correct key
27
                // Cache key is composed by profile id and a static name
28
                const datas =
29
                        window[this.options.cacheMethod].getItem(this.options.keyBrowserStorage) || null;
3✔
30

31
                if (datas !== null) {
3✔
32
                        // Datas are stringify, parse them
33
                        const datasFormatted = JSON.parse(datas);
2✔
34

35
                        // Check if datas must be filtered
36
                        datasToReturn = Array.isArray(filters)
2✔
37
                                ? this.filterDatas(filters, datasFormatted)
38
                                : datasFormatted;
39
                }
40
                return datasToReturn;
3✔
41
        }
42

43
        /**
44
         * Filter datas from cache by keys
45
         *
46
         * @param {Array} filters Filters list
47
         * @param {Object} datas Datas from browser storage
48
         *
49
         * @returns {Object} Datas filtered by keys
50
         */
51
        filterDatas(filters, datas) {
52
                let datasToReturn = null;
4✔
53

54
                // Loop on all route filters and extract selected routes datas
55
                const validKeys = Object.keys(datas).filter((key) => filters.includes(key));
12✔
56

57
                if (validKeys.length) {
4✔
58
                        datasToReturn = {};
2✔
59
                        validKeys.map((key) => (datasToReturn[key] = datas[key]));
3✔
60
                }
61

62
                return datasToReturn;
4✔
63
        }
64

65
        /**
66
         * Set step datas to the cache
67
         *
68
         * @param {String} id Current step id
69
         * @param {Object} datas Datas of the step
70
         */
71
        setDatasToCache({ id, datas }) {
72
                let datasFromCache = this.getDatasFromCache();
2✔
73

74
                // First time
75
                if (!datasFromCache) {
2✔
76
                        datasFromCache = {};
1✔
77
                }
78
                if (!datasFromCache[id]) {
2!
79
                        datasFromCache[id] = {};
2✔
80
                }
81
                datasFromCache[id].datas = datas;
2✔
82

83
                window[this.options.cacheMethod].setItem(
2✔
84
                        `${this.options.keyBrowserStorage}`,
85
                        JSON.stringify(datasFromCache)
86
                );
87
        }
88

89
        /**
90
         * Remove datas from the cache
91
         * Used only when all steps are completed
92
         */
93
        removeDatasFromCache() {
94
                window[this.options.cacheMethod].removeItem(`${this.options.keyBrowserStorage}`);
1✔
95
        }
96
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc