jaredwray / flat-cache

A stupidly simple key/value storage using files to persist the data
MIT License
165 stars 30 forks source link

refactor: prefer let/const over var #89

Closed uncenter closed 9 months ago

uncenter commented 9 months ago

This PR switches to using the let and const keywords instead of var in the source code, as is the standard nowadays.

codecov[bot] commented 9 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (d749c00) 100.00% compared to head (741ca19) 100.00%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #89 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 3 3 Lines 290 288 -2 ========================================= - Hits 290 288 -2 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

uncenter commented 9 months ago

I'm not sure why this is the case but I tried the following, 5 of the tests failed:

// ...
- const utils = require('./utils');
+ const { writeJSON, tryParse } = require('./utils');
const del = require('./del');
- const writeJSON = utils.writeJSON;

const cache = {
  /**
   * Load a cache identified by the given Id. If the element does not exists, then initialize an empty
   * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted
    @@ -16,12 +16,12 @@ var cache = {
   * @param [cacheDir] {String} directory for the cache entry
   */
  load: function (docId, cacheDir) {
    const me = this;
    me.keyv = new Keyv();

    me.__visited = {};
    me.__persisted = {};

    me._pathToFile = cacheDir ? path.resolve(cacheDir, docId) : path.resolve(__dirname, '../.cache/', docId);

    if (fs.existsSync(me._pathToFile)) {
-      me._persisted = utils.tryParse(me._pathToFile, {});
+      me._persisted = tryParse(me._pathToFile, {});
    }
  },
// ...

EDIT: Ah. I see. I guess it can't access this.readJSON when I do that.

jaredwray commented 9 months ago

@uncenter - thanks for the update. We will be working to make this more modern code base in the new year and might move it to typescript.

uncenter commented 9 months ago

TypeScript sounds awesome! I've had a great experience with https://github.com/unjs/unbuild and there is also https://github.com/egoist/tsup.