Change ImageRun keys to be based on image data content (#2681)

Co-authored-by: Dolan <dolan_miu@hotmail.com>
This commit is contained in:
mustache1up
2024-10-10 21:13:51 -03:00
committed by GitHub
parent 021f1b0c4d
commit e86dbd3398
6 changed files with 102 additions and 31 deletions

View File

@ -8,6 +8,7 @@ import {
convertMillimetersToTwip,
docPropertiesUniqueNumericIdGen,
uniqueId,
hashedId,
uniqueNumericIdCreator,
uniqueUuid,
} from "./convenience-functions";
@ -72,6 +73,26 @@ describe("Utility", () => {
});
});
describe("#hashedId", () => {
it("should generate a hex string", () => {
expect(hashedId("")).to.equal("da39a3ee5e6b4b0d3255bfef95601890afd80709");
});
it("should work with string, Uint8Array, Buffer and ArrayBuffer", () => {
const stringInput = "DATA";
const uint8ArrayInput = new Uint8Array(new TextEncoder().encode(stringInput));
const bufferInput = Buffer.from(uint8ArrayInput);
const arrayBufferInput = uint8ArrayInput.buffer;
const expectedHash = "580393f5a94fb469585f5dd2a6859a4aab899f37";
expect(hashedId(stringInput)).to.equal(expectedHash);
expect(hashedId(uint8ArrayInput)).to.equal(expectedHash);
expect(hashedId(bufferInput)).to.equal(expectedHash);
expect(hashedId(arrayBufferInput)).to.equal(expectedHash);
});
});
describe("#uniqueUuid", () => {
it("should generate a unique pseudorandom ID", () => {
expect(uniqueUuid()).to.not.be.empty;

View File

@ -1,4 +1,5 @@
import { nanoid, customAlphabet } from "nanoid/non-secure";
import hash from "hash.js";
// Twip - twentieths of a point
export const convertMillimetersToTwip = (millimeters: number): number => Math.floor((millimeters / 25.4) * 72 * 20);
@ -24,6 +25,12 @@ export const bookmarkUniqueNumericIdGen = (): UniqueNumericIdCreator => uniqueNu
export const uniqueId = (): string => nanoid().toLowerCase();
export const hashedId = (data: Buffer | string | Uint8Array | ArrayBuffer): string =>
hash
.sha1()
.update(data instanceof ArrayBuffer ? new Uint8Array(data) : data)
.digest("hex");
const generateUuidPart = (count: number): string => customAlphabet("1234567890abcdef", count)();
export const uniqueUuid = (): string =>
`${generateUuidPart(8)}-${generateUuidPart(4)}-${generateUuidPart(4)}-${generateUuidPart(4)}-${generateUuidPart(12)}`;