Skip to content

Commit 692ecdf

Browse files
authored
docs: improved and finalized jsdocs in exported functions (#74)
1 parent e09027f commit 692ecdf

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

src/crypto/sha256.ts

+21
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const W: number[] = [];
3232
export class SHA256 extends Hasher {
3333
_hash = new WordArray([...H]);
3434

35+
/**
36+
* Resets the internal state of the hash object to initial values.
37+
*/
3538
reset() {
3639
super.reset();
3740
this._hash = new WordArray([...H]);
@@ -107,6 +110,12 @@ export class SHA256 extends Hasher {
107110
H[7] = (H[7] + h) | 0;
108111
}
109112

113+
/**
114+
* Finishes the hash calculation and returns the hash as a WordArray.
115+
*
116+
* @param {string} messageUpdate - Additional message content to include in the hash.
117+
* @returns {WordArray} The finalised hash as a WordArray.
118+
*/
110119
finalize(messageUpdate: string): WordArray {
111120
super.finalize(messageUpdate);
112121

@@ -129,10 +138,22 @@ export class SHA256 extends Hasher {
129138
}
130139
}
131140

141+
/**
142+
* Calculates the SHA-256 hash of the message provided.
143+
*
144+
* @param {string} message - The message to hash.
145+
* @returns {string} The message hash as a hexadecimal string.
146+
*/
132147
export function sha256(message: string) {
133148
return new SHA256().finalize(message).toString();
134149
}
135150

151+
/**
152+
* Calculates the SHA-256 hash of the given message and encodes it in Base64.
153+
*
154+
* @param {string} message - The message to hash.
155+
* @returns {string} The base64 encoded hash of the message.
156+
*/
136157
export function sha256base64(message: string) {
137158
return new SHA256().finalize(message).toString(Base64);
138159
}

src/diff.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { objectHash, HashOptions } from "./object-hash";
22

3+
/**
4+
* Calculates the difference between two objects and returns a list of differences.
5+
*
6+
* @param {any} obj1 - The first object to compare.
7+
* @param {any} obj2 - The second object to compare.
8+
* @param {HashOptions} [opts={}] - Configuration options for hashing the objects. See {@link HashOptions}.
9+
* @returns {DiffEntry[]} An array with the differences between the two objects.
10+
*/
311
export function diff(
412
obj1: any,
513
obj2: any,

src/hash.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { sha256base64 } from "./crypto/sha256";
44
/**
55
* Hash any JS value into a string
66
* @param {object} object value to hash
7-
* @param {HashOptions} options hashing options
7+
* @param {HashOptions} options hashing options. See {@link HashOptions}.
88
* @return {string} hash value
99
* @api public
1010
*/

src/object-hash.ts

+41-11
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,73 @@
22

33
export interface HashOptions {
44
/**
5-
*
5+
* Function to determine if a key should be excluded from hashing.
6+
* @optional
7+
* @param key - The key to check for exclusion.
8+
* @returns {boolean} - Returns true to exclude the key from hashing.
69
*/
710
excludeKeys?: ((key: string) => boolean) | undefined;
11+
812
/**
9-
* hash object keys, values ignored
13+
* Specifies whether to exclude values from hashing, so that only the object keys are hashed.
14+
* @optional
1015
*/
1116
excludeValues?: boolean | undefined;
17+
1218
/**
13-
* ignore unknown object types
19+
* Specifies whether to ignore objects of unknown type (not directly serialisable) when hashing.
20+
* @optional
21+
* @default false
1422
*/
1523
ignoreUnknown?: boolean | undefined;
24+
1625
/**
17-
* optional function that replaces values before hashing
26+
* A function that replaces values before they are hashed, which can be used to customise the hashing process.
27+
* @optional
28+
* @param value - The current value to be hashed.
29+
* @returns {any} - The value to use for hashing instead.
1830
*/
1931
replacer?: ((value: any) => any) | undefined;
32+
2033
/**
21-
* consider 'name' property of functions for hashing
34+
* Specifies whether the 'name' property of functions should be taken into account when hashing.
35+
* @optional
36+
* @default false
2237
*/
2338
respectFunctionNames?: boolean | undefined;
39+
2440
/**
25-
* consider function properties when hashing
41+
* Specifies whether properties of functions should be taken into account when hashing.
42+
* @optional
43+
* @default false
2644
*/
2745
respectFunctionProperties?: boolean | undefined;
46+
2847
/**
29-
* Respect special properties (prototype, letructor) when hashing to distinguish between types
48+
* Specifies whether to include type-specific properties such as prototype or constructor in the hash to distinguish between types.
49+
* @optional
50+
* @default false
3051
*/
3152
respectType?: boolean | undefined;
53+
3254
/**
33-
* Sort all arrays before hashing
55+
* Specifies whether arrays should be sorted before hashing to ensure consistent order.
56+
* @optional
57+
* @default false
3458
*/
3559
unorderedArrays?: boolean | undefined;
60+
3661
/**
37-
* Sort `Set` and `Map` instances before hashing
62+
* Specifies whether Set and Map instances should be sorted by key before hashing to ensure consistent order.
63+
* @optional
64+
* @default true
3865
*/
3966
unorderedObjects?: boolean | undefined;
67+
4068
/**
41-
* Sort `Set` and `Map` instances before hashing
69+
* Specifies whether the elements of `Set' and keys of `Map' should be sorted before hashing to ensure consistent order.
70+
* @optional
71+
* @default false
4272
*/
4373
unorderedSets?: boolean | undefined;
4474
}
@@ -60,7 +90,7 @@ const defaults: HashOptions = Object.freeze({
6090
/**
6191
* Serialize any JS value into a stable, hashable string
6292
* @param {object} object value to hash
63-
* @param {HashOptions} options hashing options
93+
* @param {HashOptions} options hashing options. See {@link HashOptions}.
6494
* @return {string} serialized value
6595
* @api public
6696
*/

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { objectHash, HashOptions } from "./object-hash";
33
* Compare two objects using reference equality and stable deep hashing.
44
* @param {any} object1 First object
55
* @param {any} object2 Second object
6-
* @param {HashOptions} hash options
6+
* @param {HashOptions} hashOptions. Configuration options for hashing the objects. See {@link HashOptions}.
77
* @return {boolean} true if equal and false if not
88
* @api public
99
*/

0 commit comments

Comments
 (0)