@@ -78,6 +78,10 @@ export function checkedAllCases(_witness: never): never {
7878 throw new Error ( 'not all cases checked' )
7979}
8080
81+ /**
82+ * Returns its argument unchanged. Useful for generic contexts where a transformation
83+ * function is expected but no transformation is needed.
84+ */
8185export function identity < T > ( x : T ) : T {
8286 return x
8387}
@@ -111,7 +115,8 @@ export function uniqWith<T>(array: T[], compare: (l: T, r: T) => number): T[] {
111115}
112116
113117/**
114- * https://stackoverflow.com/a/52171480
118+ * Computes a hash value for an array of numbers using a fast non-cryptographic hash algorithm.
119+ * Based on implementation from https://stackoverflow.com/a/52171480
115120 */
116121export function hashNums ( nums : number [ ] , seed = 0 ) : number {
117122 let h1 = 0xdeadbeef ^ seed , h2 = 0x41c6ce57 ^ seed
@@ -128,6 +133,10 @@ export function hashNums(nums: number[], seed = 0): number {
128133 return 4294967296 * ( 2097151 & h2 ) + ( h1 >>> 0 )
129134}
130135
136+ /**
137+ * Computes a hash value for a string using a fast non-cryptographic hash algorithm.
138+ * Uses the same algorithm as hashNums but operates on character codes.
139+ */
131140export function hashStr ( str : string , seed = 0 ) : number {
132141 let h1 = 0xdeadbeef ^ seed , h2 = 0x41c6ce57 ^ seed
133142 for ( let i = 0 ; i < str . length ; i ++ ) {
@@ -143,10 +152,17 @@ export function hashStr(str: string, seed = 0): number {
143152 return 4294967296 * ( 2097151 & h2 ) + ( h1 >>> 0 )
144153}
145154
155+ /**
156+ * Performs bitwise XOR operation on two numbers.
157+ */
146158export function xor ( a : number , b : number ) : number {
147159 return a ^ b
148160}
149161
162+ /**
163+ * Finds the element with the minimum score according to the provided scoring function.
164+ * Returns undefined if the iterable is empty.
165+ */
150166export function minBy < T > ( iterable : Iterable < T > , scoreOf : ( item : T ) => number ) : T | undefined {
151167 let minItem = undefined
152168 let minScore = Infinity
@@ -160,6 +176,17 @@ export function minBy<T>(iterable: Iterable<T>, scoreOf: (item: T) => number): T
160176 return minItem
161177}
162178
179+ /**
180+ * Calculates the sum of all numbers in the array.
181+ */
163182export function sum ( items : number [ ] ) {
164183 return items . reduce ( ( a , b ) => a + b , 0 )
165184}
185+
186+ /**
187+ * Type guard that checks if an unknown value is one of the elements in the provided array.
188+ * Returns true if the item is found in the array, with proper TypeScript type narrowing.
189+ */
190+ export function isOneOf < T > ( item : unknown , array : T [ ] ) : item is T {
191+ return ( array as unknown [ ] ) . includes ( item )
192+ }
0 commit comments