Skip to content

Commit

Permalink
refactor: use map instead of object for internal cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Mar 10, 2024
1 parent 1c6c911 commit 6f50da5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class LicensePlugin {
// This is a cache storing a directory path to associated package.
// This is an improvement to avoid looking for package information for
// already scanned directory.
this._cache = {};
this._cache = new Map();
}

/**
Expand Down Expand Up @@ -160,8 +160,8 @@ class LicensePlugin {

while (dir && dir !== this._cwd && !scannedDirs.includes(dir)) {
// Try the cache.
if (_.has(this._cache, dir)) {
pkg = this._cache[dir];
if (this._cache.has(dir)) {
pkg = this._cache.get(dir);
if (pkg) {
this.debug(`found package.json in cache (package: ${pkg.name})`);
this.addDependency(pkg);
Expand Down Expand Up @@ -222,7 +222,7 @@ class LicensePlugin {

// Update the cache
scannedDirs.forEach((scannedDir) => {
this._cache[scannedDir] = pkg;
this._cache.set(scannedDir, pkg);
});
}

Expand Down
23 changes: 10 additions & 13 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,9 @@ describe('LicensePlugin', () => {
'fake-package': fakePackage,
});

expect(plugin._cache).toEqual({
[path.join(__dirname, 'fixtures', 'fake-package-1', 'src')]: pkg,
[path.join(__dirname, 'fixtures', 'fake-package-1')]: pkg,
});
expect(plugin._cache.size).toBe(2);
expect(plugin._cache.get(path.join(__dirname, 'fixtures', 'fake-package-1', 'src'))).toEqual(pkg);
expect(plugin._cache.get(path.join(__dirname, 'fixtures', 'fake-package-1'))).toEqual(pkg);
});

it('should load pkg and put null without package', () => {
Expand All @@ -342,9 +341,8 @@ describe('LicensePlugin', () => {
plugin.scanDependency(id);

expect(plugin._dependencies).toEqual({});
expect(plugin._cache).toEqual({
[path.normalize(path.join(__dirname, '..', 'src'))]: null,
});
expect(plugin._cache.size).toBe(1);
expect(plugin._cache.get(path.normalize(path.join(__dirname, '..', 'src')))).toBeNull();
});

it('should try to load pkg without leading NULL character ', () => {
Expand All @@ -359,19 +357,18 @@ describe('LicensePlugin', () => {
'fake-package': fakePackage,
});

expect(plugin._cache).toEqual({
[path.join(__dirname, 'fixtures', 'fake-package-1', 'src')]: pkg,
[path.join(__dirname, 'fixtures', 'fake-package-1')]: pkg,
});
expect(plugin._cache.size).toBe(2);
expect(plugin._cache.get(path.join(__dirname, 'fixtures', 'fake-package-1', 'src'))).toEqual(pkg);
expect(plugin._cache.get(path.join(__dirname, 'fixtures', 'fake-package-1'))).toEqual(pkg);
});

it('should load pkg and use the cache if available', () => {
const existsSync = spyOn(fs, 'existsSync').and.callThrough();
const pkgPath = path.join(__dirname, 'fixtures', 'fake-package-1');
const id = path.join(pkgPath, 'src', 'index.js');

plugin._cache[path.join(pkgPath, 'src')] = null;
plugin._cache[pkgPath] = null;
plugin._cache.set(path.join(pkgPath, 'src'), null);
plugin._cache.set(pkgPath, null);

plugin.scanDependency(id);

Expand Down

0 comments on commit 6f50da5

Please sign in to comment.