Skip to content

Commit

Permalink
Refactor: [#52] Store tags in a Set
Browse files Browse the repository at this point in the history
* 52-tags-as-set:
  TagsSet: Convert TaskItem.tags from List to Set. Updated tests to account for the change
  • Loading branch information
roovo committed Dec 23, 2021
2 parents d76fdee + 61d1962 commit c8d24c3
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 83 deletions.
19 changes: 10 additions & 9 deletions src/MarkdownFile.elm
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module MarkdownFile exposing (MarkdownFile, decoder)

import Set exposing (Set)
import TsJson.Decode as TsDecode
import Yaml.Decode as YD


type alias MarkdownFile =
{ filePath : String
, fileDate : Maybe String
, frontMatterTags : List String
, frontMatterTags : Set String
, bodyOffset : Int
, body : String
}
Expand All @@ -25,35 +26,35 @@ decoder =
-- INTERNAL


markdownFileBuilder : String -> Maybe String -> ( List String, Int, String ) -> MarkdownFile
markdownFileBuilder : String -> Maybe String -> ( Set String, Int, String ) -> MarkdownFile
markdownFileBuilder filePath fileDate ( tags, bodyOffset, body ) =
MarkdownFile filePath fileDate tags bodyOffset body


tagsAndContentsDecoder : TsDecode.Decoder ( List String, Int, String )
tagsAndContentsDecoder : TsDecode.Decoder ( Set String, Int, String )
tagsAndContentsDecoder =
TsDecode.andThen (TsDecode.andThenInit contentDecoder) TsDecode.string


contentDecoder : String -> TsDecode.Decoder ( List String, Int, String )
contentDecoder : String -> TsDecode.Decoder ( Set String, Int, String )
contentDecoder contents =
case frontMatterAndBodyFrom contents of
( Just frontMatter, bodyOffset, Just body ) ->
case YD.fromString frontMatterDecoder frontMatter of
Ok tags ->
TsDecode.succeed ( tags, bodyOffset, body )
TsDecode.succeed ( Set.fromList tags, bodyOffset, body )

Err _ ->
TsDecode.succeed ( [], bodyOffset, body )
TsDecode.succeed ( Set.empty, bodyOffset, body )

( Nothing, _, Just _ ) ->
TsDecode.succeed ( [], 0, contents )
TsDecode.succeed ( Set.empty, 0, contents )

( Just _, _, Nothing ) ->
TsDecode.succeed ( [], 0, "" )
TsDecode.succeed ( Set.empty, 0, "" )

( Nothing, _, Nothing ) ->
TsDecode.succeed ( [], 0, "" )
TsDecode.succeed ( Set.empty, 0, "" )


frontMatterDecoder : YD.Decoder (List String)
Expand Down
5 changes: 3 additions & 2 deletions src/Page/Board.elm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import InteropPorts
import Json.Decode as JD
import SafeZipper
import Session exposing (Session)
import Set exposing (Set)
import TaskItem exposing (TaskItem, TaskItemFields)
import TimeWithZone exposing (TimeWithZone)

Expand Down Expand Up @@ -305,10 +306,10 @@ cardView timeWithZone card =
|> Tuple.pair cardId


cardTagsView : List String -> Html Msg
cardTagsView : Set String -> Html Msg
cardTagsView tags =
Html.div [ class "card-board-card-tag-area" ]
(List.map cardTagView tags)
(List.map cardTagView <| Set.toList tags)


cardTagView : String -> Html Msg
Expand Down
45 changes: 23 additions & 22 deletions src/TaskItem.elm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import List.Extra as LE
import Maybe.Extra as ME
import Parser as P exposing ((|.), (|=), Parser)
import ParserHelper exposing (isSpaceOrTab, lineEndOrEnd)
import Set exposing (Set)
import TaskPaperTag
import Time

Expand All @@ -55,11 +56,11 @@ type alias TaskItemFields =
, dueFile : Maybe Date
, dueTag : Maybe Date
, filePath : String
, frontMatterTags : List String
, frontMatterTags : Set String
, lineNumber : Int
, notes : String
, originalText : String
, tags : List String
, tags : Set String
, title : String
}

Expand All @@ -72,11 +73,11 @@ dummy =
, dueFile = Nothing
, dueTag = Nothing
, filePath = ""
, frontMatterTags = []
, frontMatterTags = Set.empty
, lineNumber = 0
, notes = ""
, originalText = ""
, tags = []
, tags = Set.empty
, title = ""
}
[]
Expand Down Expand Up @@ -178,7 +179,7 @@ hasNotes =

hasTags : TaskItem -> Bool
hasTags taskItem =
not <| List.isEmpty <| tags taskItem
not <| Set.isEmpty <| tags taskItem


hasOneOfTheTags : List String -> TaskItem -> Bool
Expand All @@ -198,7 +199,7 @@ hasTag tagToMatch taskItem =
else
String.toLower itemTag == String.toLower tagToMatch
in
List.any matches <| tags taskItem
Set.size (Set.filter matches <| tags taskItem) > 0


hasSubtasks : TaskItem -> Bool
Expand Down Expand Up @@ -253,13 +254,12 @@ subtasks (TaskItem _ subtasks_) =
List.map (\s -> TaskItem s []) subtasks_


tags : TaskItem -> List String
tags : TaskItem -> Set String
tags ((TaskItem fields_ _) as taskItem) =
subtasks taskItem
|> List.concatMap (\(TaskItem fs _) -> fs.tags)
|> List.append fields_.tags
|> List.append fields_.frontMatterTags
|> LE.unique
|> List.map tags
|> List.foldl Set.union fields_.tags
|> Set.union fields_.frontMatterTags


tasksToToggle : String -> { a | now : Time.Posix } -> TaskItem -> List TaskItem
Expand Down Expand Up @@ -336,9 +336,10 @@ toString (TaskItem fields_ _) =

fieldTags : String
fieldTags =
if List.length fields_.tags > 0 then
if Set.size fields_.tags > 0 then
fields_.tags
|> List.map (String.append "#")
|> Set.map (String.append "#")
|> Set.toList
|> String.join " "
|> String.append " "

Expand Down Expand Up @@ -415,7 +416,7 @@ updateFilePath oldPath newPath ((TaskItem fields_ subtasks_) as taskItem) =
-- SERIALIZATION


parser : String -> Maybe String -> List String -> Int -> Parser TaskItem
parser : String -> Maybe String -> Set String -> Int -> Parser TaskItem
parser pathToFile fileDate frontMatterTags bodyOffset =
(P.succeed taskItemFieldsBuilder
|= P.getOffset
Expand All @@ -436,7 +437,7 @@ parser pathToFile fileDate frontMatterTags bodyOffset =
|> P.andThen (addAnySubtasksAndNotes pathToFile fileDate frontMatterTags bodyOffset)


taskItemFieldsBuilder : Int -> Int -> String -> List String -> Int -> Int -> Completion -> Maybe Date -> List Content -> Int -> String -> TaskItemFields
taskItemFieldsBuilder : Int -> Int -> String -> Set String -> Int -> Int -> Completion -> Maybe Date -> List Content -> Int -> String -> TaskItemFields
taskItemFieldsBuilder startOffset startColumn path frontMatterTags bodyOffset row completion_ dueFromFile contents endOffset source =
let
sourceText : String
Expand All @@ -457,16 +458,16 @@ taskItemFieldsBuilder startOffset startColumn path frontMatterTags bodyOffset ro
contents
|> List.foldr extractDueDate Nothing

obsidianTags : List String
obsidianTags : Set String
obsidianTags =
contents
|> List.foldr extractTag []
|> List.foldr extractTag Set.empty

extractTag : Content -> List String -> List String
extractTag : Content -> Set String -> Set String
extractTag content ts =
case content of
ObsidianTag t ->
t :: ts
Set.insert t ts

_ ->
ts
Expand Down Expand Up @@ -607,7 +608,7 @@ fileDateParser fileDate =
|> P.succeed


addAnySubtasksAndNotes : String -> Maybe String -> List String -> Int -> TaskItemFields -> Parser TaskItem
addAnySubtasksAndNotes : String -> Maybe String -> Set String -> Int -> TaskItemFields -> Parser TaskItem
addAnySubtasksAndNotes pathToFile fileDate frontMatterTags bodyOffset fields_ =
let
buildTaskItem : List IndentedItem -> Parser TaskItem
Expand Down Expand Up @@ -646,7 +647,7 @@ addAnySubtasksAndNotes pathToFile fileDate frontMatterTags bodyOffset fields_ =
|> P.andThen buildTaskItem


indentedItemParser : String -> Maybe String -> List String -> Int -> Parser IndentedItem
indentedItemParser : String -> Maybe String -> Set String -> Int -> Parser IndentedItem
indentedItemParser pathToFile fileDate frontMatterTags bodyOffset =
P.oneOf
[ subTaskParser pathToFile fileDate frontMatterTags bodyOffset
Expand All @@ -660,7 +661,7 @@ notesParser =
|= ParserHelper.anyLineParser


subTaskParser : String -> Maybe String -> List String -> Int -> Parser IndentedItem
subTaskParser : String -> Maybe String -> Set String -> Int -> Parser IndentedItem
subTaskParser pathToFile fileDate frontMatterTags bodyOffset =
P.succeed taskItemFieldsBuilder
|= P.getOffset
Expand Down
5 changes: 3 additions & 2 deletions src/TaskList.elm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import List.Extra as LE
import MarkdownFile exposing (MarkdownFile)
import Parser as P exposing (Parser)
import ParserHelper exposing (anyLineParser)
import Set exposing (Set)
import TaskItem exposing (TaskItem)


Expand All @@ -42,7 +43,7 @@ empty =
-- PARSING


parser : String -> Maybe String -> List String -> Int -> Parser TaskList
parser : String -> Maybe String -> Set String -> Int -> Parser TaskList
parser filePath fileDate frontMatterTags bodyOffset =
P.loop [] (taskItemsHelp filePath fileDate frontMatterTags bodyOffset)
|> P.map (\ts -> TaskList ts)
Expand Down Expand Up @@ -163,7 +164,7 @@ itemsNotFromFile pathToFile taskItems =
|> List.filter (\t -> not (TaskItem.isFromFile pathToFile t))


taskItemsHelp : String -> Maybe String -> List String -> Int -> List TaskItem -> Parser (P.Step (List TaskItem) (List TaskItem))
taskItemsHelp : String -> Maybe String -> Set String -> Int -> List TaskItem -> Parser (P.Step (List TaskItem) (List TaskItem))
taskItemsHelp filePath fileDate frontMatterTags bodyOffset revTaskItems =
P.oneOf
[ P.backtrackable
Expand Down
5 changes: 3 additions & 2 deletions tests/CardTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Expect
import Helpers.TaskHelpers as TaskHelpers
import Helpers.TaskItemHelpers as TaskItemHelpers
import Parser
import Set
import TaskItem exposing (TaskItem)
import Test exposing (..)
import Time
Expand Down Expand Up @@ -138,7 +139,7 @@ markdownWithIds =
- [ ] bar
more notes
"""
|> Parser.run (TaskItem.parser "file" Nothing [] 0)
|> Parser.run (TaskItem.parser "file" Nothing (Set.fromList []) 0)
|> Result.toMaybe
|> Maybe.map (Card.fromTaskItem "prefix")
|> Maybe.map Card.markdownWithIds
Expand Down Expand Up @@ -212,5 +213,5 @@ now =
taskItem : Maybe TaskItem
taskItem =
"- [ ] foo"
|> Parser.run (TaskItem.parser "taskItemPath" Nothing [] 0)
|> Parser.run (TaskItem.parser "taskItemPath" Nothing (Set.fromList []) 0)
|> Result.toMaybe
5 changes: 3 additions & 2 deletions tests/Helpers/TaskItemHelpers.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ module Helpers.TaskItemHelpers exposing
)

import Parser exposing (Parser)
import Set
import TaskItem exposing (TaskItem)


basicParser : Parser TaskItem
basicParser =
TaskItem.parser "" Nothing [] 0
TaskItem.parser "" Nothing Set.empty 0


exampleTaskItem : String -> String -> TaskItem
exampleTaskItem markdown path =
Parser.run (TaskItem.parser path Nothing [] 0) markdown
Parser.run (TaskItem.parser path Nothing Set.empty 0) markdown
|> Result.withDefault TaskItem.dummy
5 changes: 3 additions & 2 deletions tests/Helpers/TaskListHelpers.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ module Helpers.TaskListHelpers exposing

import Helpers.DateTimeHelpers as DateTimeHelpers
import Parser exposing (Parser)
import Set
import TaskList exposing (TaskList)


basicParser : Parser TaskList
basicParser =
TaskList.parser "" Nothing [] 0
TaskList.parser "" Nothing Set.empty 0


parsedTasks : ( String, Maybe String, String ) -> TaskList
parsedTasks ( p, d, ts ) =
Parser.run (TaskList.parser p d [] 0) ts
Parser.run (TaskList.parser p d Set.empty 0) ts
|> Result.withDefault TaskList.empty


Expand Down
5 changes: 3 additions & 2 deletions tests/InteropDefinitionsTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Helpers.DecodeHelpers as DecodeHelpers
import Helpers.FilterHelpers as FilterHelpers
import InteropDefinitions exposing (interop)
import Semver
import Set
import Test exposing (..)
import TsJson.Encode as TsEncode

Expand Down Expand Up @@ -183,7 +184,7 @@ toElmTests =
InteropDefinitions.FileAdded
{ filePath = "a path"
, fileDate = Just "a date"
, frontMatterTags = [ "a_tag" ]
, frontMatterTags = Set.fromList [ "a_tag" ]
, bodyOffset = 3
, body = "some contents"
}
Expand All @@ -210,7 +211,7 @@ toElmTests =
InteropDefinitions.FileUpdated
{ filePath = "a path"
, fileDate = Just "a date"
, frontMatterTags = [ "a_tag" ]
, frontMatterTags = Set.fromList [ "a_tag" ]
, bodyOffset = 3
, body = "some contents"
}
Expand Down
Loading

0 comments on commit c8d24c3

Please sign in to comment.