diff --git a/npm/ng-packs/packages/core/src/lib/tests/generator-utils.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/generator-utils.spec.ts new file mode 100644 index 0000000000..e09be3fb4f --- /dev/null +++ b/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); + }); + }); +}); diff --git a/npm/ng-packs/packages/core/src/lib/utils/generator-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/generator-utils.ts index 4256489efa..cc88e216d6 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/generator-utils.ts +++ b/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; +}