Skip to content

Commit

Permalink
chore(test): refactored hash method into util (#470)
Browse files Browse the repository at this point in the history
moved the hashing util out of the business
logic and added test coverage.

Fixes N/A
  • Loading branch information
codymikol authored Jan 30, 2021
1 parent ea3dabe commit b044404
Show file tree
Hide file tree
Showing 6 changed files with 8,728 additions and 14 deletions.
14 changes: 2 additions & 12 deletions lib/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const fs = require('fs');
const glob = require('glob');
const minimatch = require('minimatch');

const { hash } = require('./utils/hash');

const { KarmaWebpackController } = require('./KarmaWebpackController');

const controller = new KarmaWebpackController();
Expand Down Expand Up @@ -37,18 +39,6 @@ function registerExtraWebpackFiles(config, _controller) {
});
}

/**
* Simple hash function by bryc
* https://gist.github.com/iperelivskiy/4110988#gistcomment-2697447
*/
function hash(s) {
let h = 0xdeadbeef;
for (let i = 0; i < s.length; i++) {
h = Math.imul(h ^ s.charCodeAt(i), 2654435761); // eslint-disable-line no-bitwise
}
return (h ^ (h >>> 16)) >>> 0; // eslint-disable-line no-bitwise
}

function getPathKey(filePath, withExtension = false) {
const pathParts = path.parse(filePath);
const key = `${pathParts.name}.${hash(filePath)}`;
Expand Down
13 changes: 13 additions & 0 deletions lib/utils/hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Simple hash function by bryc
* https://gist.github.com/iperelivskiy/4110988#gistcomment-2697447
*/
function hash(s) {
let h = 0xdeadbeef;
for (let i = 0; i < s.length; i++) {
h = Math.imul(h ^ s.charCodeAt(i), 2654435761); // eslint-disable-line no-bitwise
}
return (h ^ (h >>> 16)) >>> 0; // eslint-disable-line no-bitwise
}

module.exports = { hash };
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {
KarmaWebpackController,
defaultWebpackOptions,
} = require('../lib/KarmaWebpackController');
} = require('../../lib/KarmaWebpackController');

describe('KarmaWebpackController', () => {
it('applies the default webpackOptions', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const path = require('path');

const { registerExtraWebpackFiles } = require('../lib/karma-webpack');
const { registerExtraWebpackFiles } = require('../../lib/karma-webpack');

jest.mock('fs');

Expand Down
7 changes: 7 additions & 0 deletions test/unit/util/hash.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { hash } = require('../../../lib/utils/hash');

describe('HashService', () => {
it('should be able to reproducibly hash a string into a number', () => {
expect(hash('test')).toBe(3311036531);
});
});
Loading

0 comments on commit b044404

Please sign in to comment.