Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): refactored hash method into util #470

Merged
merged 1 commit into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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