11import { describe , it , expect , afterAll , vi } from "vitest" ;
22import _ from "lodash" ;
3- import * as misc from "../../src/utils/misc" ;
3+ import * as Misc from "../../src/utils/misc" ;
44import { ObjectId } from "mongodb" ;
55
66describe ( "Misc Utils" , ( ) => {
@@ -32,7 +32,7 @@ describe("Misc Utils", () => {
3232 _ . each ( testCases , ( testCase , pattern ) => {
3333 const { cases, expected } = testCase ;
3434 _ . each ( cases , ( caseValue , index ) => {
35- expect ( misc . matchesAPattern ( caseValue , pattern ) ) . toBe ( expected [ index ] ) ;
35+ expect ( Misc . matchesAPattern ( caseValue , pattern ) ) . toBe ( expected [ index ] ) ;
3636 } ) ;
3737 } ) ;
3838 } ) ;
@@ -80,7 +80,7 @@ describe("Misc Utils", () => {
8080 ] ;
8181
8282 _ . each ( testCases , ( { wpm, acc, timestamp, expectedScore } ) => {
83- expect ( misc . kogascore ( wpm , acc , timestamp ) ) . toBe ( expectedScore ) ;
83+ expect ( Misc . kogascore ( wpm , acc , timestamp ) ) . toBe ( expectedScore ) ;
8484 } ) ;
8585 } ) ;
8686
@@ -109,7 +109,7 @@ describe("Misc Utils", () => {
109109 ] ;
110110
111111 _ . each ( testCases , ( { input, expected } ) => {
112- expect ( misc . identity ( input ) ) . toEqual ( expected ) ;
112+ expect ( Misc . identity ( input ) ) . toEqual ( expected ) ;
113113 } ) ;
114114 } ) ;
115115
@@ -178,7 +178,7 @@ describe("Misc Utils", () => {
178178 ] ;
179179
180180 _ . each ( testCases , ( { obj, expected } ) => {
181- expect ( misc . flattenObjectDeep ( obj ) ) . toEqual ( expected ) ;
181+ expect ( Misc . flattenObjectDeep ( obj ) ) . toEqual ( expected ) ;
182182 } ) ;
183183 } ) ;
184184
@@ -215,7 +215,7 @@ describe("Misc Utils", () => {
215215 ] ;
216216
217217 testCases . forEach ( ( { input, expected } ) => {
218- expect ( misc . sanitizeString ( input ) ) . toEqual ( expected ) ;
218+ expect ( Misc . sanitizeString ( input ) ) . toEqual ( expected ) ;
219219 } ) ;
220220 } ) ;
221221
@@ -284,7 +284,7 @@ describe("Misc Utils", () => {
284284 ] ;
285285
286286 testCases . forEach ( ( { input, output } ) => {
287- expect ( misc . getOrdinalNumberString ( input ) ) . toEqual ( output ) ;
287+ expect ( Misc . getOrdinalNumberString ( input ) ) . toEqual ( output ) ;
288288 } ) ;
289289 } ) ;
290290 it ( "formatSeconds" , ( ) => {
@@ -298,45 +298,45 @@ describe("Misc Utils", () => {
298298 expected : "1.08 minutes" ,
299299 } ,
300300 {
301- seconds : misc . HOUR_IN_SECONDS ,
301+ seconds : Misc . HOUR_IN_SECONDS ,
302302 expected : "1 hour" ,
303303 } ,
304304 {
305- seconds : misc . DAY_IN_SECONDS ,
305+ seconds : Misc . DAY_IN_SECONDS ,
306306 expected : "1 day" ,
307307 } ,
308308 {
309- seconds : misc . WEEK_IN_SECONDS ,
309+ seconds : Misc . WEEK_IN_SECONDS ,
310310 expected : "1 week" ,
311311 } ,
312312 {
313- seconds : misc . YEAR_IN_SECONDS ,
313+ seconds : Misc . YEAR_IN_SECONDS ,
314314 expected : "1 year" ,
315315 } ,
316316 {
317- seconds : 2 * misc . YEAR_IN_SECONDS ,
317+ seconds : 2 * Misc . YEAR_IN_SECONDS ,
318318 expected : "2 years" ,
319319 } ,
320320 {
321- seconds : 4 * misc . YEAR_IN_SECONDS ,
321+ seconds : 4 * Misc . YEAR_IN_SECONDS ,
322322 expected : "4 years" ,
323323 } ,
324324 {
325- seconds : 3 * misc . WEEK_IN_SECONDS ,
325+ seconds : 3 * Misc . WEEK_IN_SECONDS ,
326326 expected : "3 weeks" ,
327327 } ,
328328 {
329- seconds : misc . MONTH_IN_SECONDS * 4 ,
329+ seconds : Misc . MONTH_IN_SECONDS * 4 ,
330330 expected : "4 months" ,
331331 } ,
332332 {
333- seconds : misc . MONTH_IN_SECONDS * 11 ,
333+ seconds : Misc . MONTH_IN_SECONDS * 11 ,
334334 expected : "11 months" ,
335335 } ,
336336 ] ;
337337
338338 testCases . forEach ( ( { seconds, expected } ) => {
339- expect ( misc . formatSeconds ( seconds ) ) . toBe ( expected ) ;
339+ expect ( Misc . formatSeconds ( seconds ) ) . toBe ( expected ) ;
340340 } ) ;
341341 } ) ;
342342
@@ -347,14 +347,14 @@ describe("Misc Utils", () => {
347347 test : "test" ,
348348 number : 1 ,
349349 } ;
350- expect ( misc . replaceObjectId ( fromDatabase ) ) . toStrictEqual ( {
350+ expect ( Misc . replaceObjectId ( fromDatabase ) ) . toStrictEqual ( {
351351 _id : fromDatabase . _id . toHexString ( ) ,
352352 test : "test" ,
353353 number : 1 ,
354354 } ) ;
355355 } ) ;
356356 it ( "ignores null values" , ( ) => {
357- expect ( misc . replaceObjectId ( null ) ) . toBeNull ( ) ;
357+ expect ( Misc . replaceObjectId ( null ) ) . toBeNull ( ) ;
358358 } ) ;
359359 } ) ;
360360
@@ -371,7 +371,7 @@ describe("Misc Utils", () => {
371371 number : 2 ,
372372 } ;
373373 expect (
374- misc . replaceObjectIds ( [ fromDatabase , fromDatabase2 ] )
374+ Misc . replaceObjectIds ( [ fromDatabase , fromDatabase2 ] )
375375 ) . toStrictEqual ( [
376376 {
377377 _id : fromDatabase . _id . toHexString ( ) ,
@@ -386,7 +386,56 @@ describe("Misc Utils", () => {
386386 ] ) ;
387387 } ) ;
388388 it ( "handles undefined" , ( ) => {
389- expect ( misc . replaceObjectIds ( undefined as any ) ) . toBeUndefined ( ) ;
389+ expect ( Misc . replaceObjectIds ( undefined as any ) ) . toBeUndefined ( ) ;
390+ } ) ;
391+ } ) ;
392+
393+ describe ( "omit()" , ( ) => {
394+ it ( "should omit a single key" , ( ) => {
395+ const input = { a : 1 , b : 2 , c : 3 } ;
396+ const result = Misc . omit ( input , "b" ) ;
397+ expect ( result ) . toEqual ( { a : 1 , c : 3 } ) ;
398+ } ) ;
399+
400+ it ( "should omit multiple keys" , ( ) => {
401+ const input = { a : 1 , b : 2 , c : 3 , d : 4 } ;
402+ const result = Misc . omit ( input , "a" , "d" ) ;
403+ expect ( result ) . toEqual ( { b : 2 , c : 3 } ) ;
404+ } ) ;
405+
406+ it ( "should return the same object if no keys are omitted" , ( ) => {
407+ const input = { x : 1 , y : 2 } ;
408+ const result = Misc . omit ( input ) ;
409+ expect ( result ) . toEqual ( { x : 1 , y : 2 } ) ;
410+ } ) ;
411+
412+ it ( "should not mutate the original object" , ( ) => {
413+ const input = { foo : "bar" , baz : "qux" } ;
414+ const copy = { ...input } ;
415+ Misc . omit ( input , "baz" ) ;
416+ expect ( input ) . toEqual ( copy ) ;
417+ } ) ;
418+
419+ it ( "should ignore keys that do not exist" , ( ) => {
420+ const input = { a : 1 , b : 2 } ;
421+ const result = Misc . omit ( input , "c" as any ) ; // allow a non-existing key
422+ expect ( result ) . toEqual ( { a : 1 , b : 2 } ) ;
423+ } ) ;
424+
425+ it ( "should work with different value types" , ( ) => {
426+ const input = {
427+ str : "hello" ,
428+ num : 123 ,
429+ bool : true ,
430+ obj : { x : 1 } ,
431+ arr : [ 1 , 2 , 3 ] ,
432+ } ;
433+ const result = Misc . omit ( input , "bool" , "arr" ) ;
434+ expect ( result ) . toEqual ( {
435+ str : "hello" ,
436+ num : 123 ,
437+ obj : { x : 1 } ,
438+ } ) ;
390439 } ) ;
391440 } ) ;
392441} ) ;
0 commit comments