@xyo-network/hash 2.84.5 → 2.84.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,6 +30,11 @@ export class PayloadHasher<T extends EmptyObject = EmptyObject> extends ObjectWr
30
30
  return (await this.hashPairs(objs)).find(([_, objHash]) => objHash === hash)?.[0]
31
31
  }
32
32
 
33
+ /**
34
+ * Asynchronously hashes a payload
35
+ * @param obj A payload
36
+ * @returns The payload hash
37
+ */
33
38
  static async hashAsync<T extends EmptyObject>(obj: T): Promise<Hash> {
34
39
  if (PayloadHasher.allowSubtle) {
35
40
  try {
@@ -51,7 +56,7 @@ export class PayloadHasher<T extends EmptyObject = EmptyObject> extends ObjectWr
51
56
  const stringToHash = this.stringifyHashFields(obj)
52
57
  try {
53
58
  return asHash(await sha256(stringToHash), true)
54
- } catch (ex) {
59
+ } catch {
55
60
  this.wasmSupport.allowWasm = false
56
61
  }
57
62
  }
@@ -62,25 +67,40 @@ export class PayloadHasher<T extends EmptyObject = EmptyObject> extends ObjectWr
62
67
  return sortFields(removeEmptyFields(deepOmitUnderscoreFields(obj)))
63
68
  }
64
69
 
70
+ /**
71
+ * Creates an array of payload/hash tuples based on the payloads passed in
72
+ * @param objs Any array of payloads
73
+ * @returns An array of payload/hash tuples
74
+ */
65
75
  static async hashPairs<T extends EmptyObject>(objs: T[]): Promise<[T, Hash][]> {
66
76
  return await Promise.all(objs.map<Promise<[T, string]>>(async (obj) => [obj, await PayloadHasher.hashAsync(obj)]))
67
77
  }
68
78
 
79
+ /**
80
+ * Synchronously hashes a payload
81
+ * @param obj A payload
82
+ * @returns The payload hash
83
+ */
69
84
  static hashSync<T extends EmptyObject>(obj: T): Hash {
70
85
  return asHash(shajs('sha256').update(this.stringifyHashFields(obj)).digest().toString('hex'), true)
71
86
  }
72
87
 
88
+ /**
89
+ * Creates an array of payload hashes based on the payloads passed in
90
+ * @param objs Any array of payloads
91
+ * @returns An array of payload hashes
92
+ */
73
93
  static async hashes<T extends EmptyObject>(objs: T[]): Promise<Hash[]> {
74
94
  return await Promise.all(objs.map((obj) => this.hashAsync(obj)))
75
95
  }
76
96
 
77
- /** @function jsonPayload Returns a clone of the payload that is JSON safe */
78
- static jsonPayload<T extends EmptyObject>(
79
- /** @param payload The payload to process */
80
- payload: T,
81
- /** @param meta Keeps underscore (meta) fields if set to true */
82
- meta = false,
83
- ): T {
97
+ /**
98
+ * Returns a clone of the payload that is JSON safe
99
+ * @param obj A payload
100
+ * @param meta Keeps underscore (meta) fields if set to true
101
+ * @returns Returns a clone of the payload that is JSON safe
102
+ */
103
+ static jsonPayload<T extends EmptyObject>(payload: T, meta = false): T {
84
104
  return sortFields(removeEmptyFields(meta ? payload : deepOmitUnderscoreFields(payload)))
85
105
  }
86
106
 
@@ -88,10 +108,19 @@ export class PayloadHasher<T extends EmptyObject = EmptyObject> extends ObjectWr
88
108
  return JSON.stringify(this.hashFields(obj))
89
109
  }
90
110
 
111
+ /**
112
+ * Creates an object map of payload hashes to payloads based on the payloads passed in
113
+ * @param objs Any array of payloads
114
+ * @returns A map of hashes to payloads
115
+ */
91
116
  static async toMap<T extends EmptyObject>(objs: T[]): Promise<Record<Hash, T>> {
92
- const result: Record<string, T> = {}
93
- await Promise.all(objs.map(async (obj) => (result[await PayloadHasher.hashAsync(obj)] = obj)))
94
- return result
117
+ return Object.fromEntries(
118
+ await Promise.all(
119
+ objs.map(async (obj) => {
120
+ return [await PayloadHasher.hashAsync(obj), obj]
121
+ }),
122
+ ),
123
+ )
95
124
  }
96
125
 
97
126
  async hashAsync(): Promise<Hash> {
@@ -102,11 +131,12 @@ export class PayloadHasher<T extends EmptyObject = EmptyObject> extends ObjectWr
102
131
  return PayloadHasher.hashSync(this.obj)
103
132
  }
104
133
 
105
- /** @function jsonPayload Returns a clone of the payload that is JSON safe */
106
- jsonPayload(
107
- /** @param meta Keeps underscore (meta) fields if set to true */
108
- meta = false,
109
- ): T {
134
+ /**
135
+ * Returns a clone of the payload that is JSON safe
136
+ * @param meta Keeps underscore (meta) fields if set to true
137
+ * @returns Returns a clone of the payload that is JSON safe
138
+ */
139
+ jsonPayload(meta = false): T {
110
140
  return PayloadHasher.jsonPayload(this.obj, meta)
111
141
  }
112
142
  }
package/src/model.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /** @deprecated use @xylabs/hex instead */
2
- const hexRegex = /[0-9a-f]+/i
2
+ const hexRegex = /[\da-f]+/i
3
3
 
4
4
  /** @deprecated use @xylabs/hex instead */
5
5
  export type Hex = string
@@ -5,12 +5,12 @@ export const removeEmptyFields = <T extends EmptyObject>(obj: T): T => {
5
5
  if (obj === null || Array.isArray(obj)) return obj
6
6
 
7
7
  const newObject: AnyObject = {}
8
- Object.entries(obj).forEach(([key, value]) => {
8
+ for (const [key, value] of Object.entries(obj)) {
9
9
  if (typeOf(value) === 'object') {
10
10
  newObject[key] = removeEmptyFields(value as Record<string, unknown>)
11
11
  } else if (value !== undefined) {
12
12
  newObject[key] = value
13
13
  }
14
- })
14
+ }
15
15
  return newObject as T
16
16
  }
package/src/sortFields.ts CHANGED
@@ -8,8 +8,8 @@ const subSort = (value: unknown) => {
8
8
  export const sortFields = <T extends EmptyObject>(obj: T) => {
9
9
  const result: AnyObject = {}
10
10
  const keys = Object.keys(obj) as (keyof T)[]
11
- keys.sort().forEach((key: keyof T) => {
11
+ for (const key of keys.sort()) {
12
12
  result[key] = subSort(obj[key])
13
- })
13
+ }
14
14
  return result as T
15
15
  }