Browse Source

feat(core): add generateHash utility fn

pull/3475/head
mehmet-erim 6 years ago
parent
commit
202fa9995e
  1. 10
      npm/ng-packs/packages/core/src/lib/tests/generator-utils.spec.ts
  2. 17
      npm/ng-packs/packages/core/src/lib/utils/generator-utils.ts

10
npm/ng-packs/packages/core/src/lib/tests/generator-utils.spec.ts

@ -0,0 +1,10 @@
import { generateHash } from '../utils';
describe('GeneratorUtils', () => {
describe('#generateHash', () => {
test('should generate a hash', async () => {
const hash = generateHash('some content \n with second line');
expect(hash).toBe(1112440527);
});
});
});

17
npm/ng-packs/packages/core/src/lib/utils/generator-utils.ts

@ -1,6 +1,19 @@
// tslint:disable: no-bitwise
export function uuid(a?: any): string {
return a
? // tslint:disable-next-line: no-bitwise
(a ^ ((Math.random() * 16) >> (a / 4))).toString(16)
? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16)
: ('' + 1e7 + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, uuid);
}
export function generateHash(value: string): number {
let hashed = 0;
let charCode: number;
for (let i = 0; i < value.length; i++) {
charCode = value.charCodeAt(i);
hashed = (hashed << 5) - hashed + charCode;
hashed |= 0;
}
return hashed;
}

Loading…
Cancel
Save