import { MockRepoDocInfos } from "../MockRepoDocInfos" ; import { ISODateTimeStrings } from "polar-shared/src/metadata/ISODateTimeStrings" ; import { TimeDurations } from "polar-shared/src/util/TimeDurations" ; import { Sorting } from "./Sorting" ; import { Tag } from "polar-shared/src/tags/Tags" ; import { arrayStream } from "polar-shared/src/util/ArrayStreams" ; import { RepoDocInfo } from "../RepoDocInfo" ; import { assertJSON } from "../../../../web/js/test/Assertions" ; import TypeConverter = Sorting . TypeConverter ; import { IDocInfo } from "polar-shared/src/metadata/IDocInfo" ; const now = new Date ( ) ; const today = ISODateTimeStrings . create ( ) ; const yesterday = ISODateTimeStrings . create ( TimeDurations . compute ( '-1d' , now ) ) ; interface MinimalDoc { readonly title : string ; readonly tags : ReadonlyArray < Tag > ; } const toMinimalDoc = ( repoDocInfo ) : MinimalDoc => { return { title : repoDocInfo . title , tags : Object . values ( repoDocInfo . tags || { } ) } ; } ; const tagsToString = ( repoDocInfo ) => { return Object . keys ( repoDocInfo . tags || { } ) . map ( current => current . toLowerCase ( ) ) . sort ( ) . join ( ', ' ) ; } ; const converter = ( from ) => from . docInfo ; describe ( 'Sorting' , function ( ) { it ( "by tags" , function ( ) { const docs = [ MockRepoDocInfos . createDoc ( 'doc 0' , yesterday , today , [ ] , 50 ) , MockRepoDocInfos . createDoc ( 'doc 1' , yesterday , today , [ 'alice' ] , 50 ) , MockRepoDocInfos . createDoc ( 'doc 2' , yesterday , today , [ 'bob' , 'alice' ] , 50 ) , MockRepoDocInfos . createDoc ( 'doc 3' , yesterday , today , [ 'carol' ] , 50 ) , MockRepoDocInfos . createDoc ( 'doc 4' , yesterday , today , [ 'dan' ] , 50 ) , MockRepoDocInfos . createDoc ( 'doc 5' , yesterday , today , [ 'elizabeth' , 'alice' ] , 50 ) , MockRepoDocInfos . createDoc ( 'doc 6' , yesterday , today , [ 'elizabeth' ] , 50 ) , ] ; const toColumnString = ( repoDocInfo ) : string => { return Object . keys ( repoDocInfo . tags || [ ] ) . map ( current => current . toLowerCase ( ) ) . sort ( ) . join ( ', ' ) ; } ; const sorted = arrayStream ( Sorting . stableSort ( docs , Sorting . createComparator ( 'asc' , 'tags' , converter ) ) ) . map ( toColumnString ) . collect ( ) ; assertJSON ( sorted , [ "alice" , "alice, bob" , "alice, elizabeth" , "carol" , "dan" , "elizabeth" , "" , ] ) ; } ) ; } ) ;