Filter filters cheatsheets that do not match `tag(s)`
( cheatpaths []map[string]sheet.Sheet, tags []string, )
| 9 | |
| 10 | // Filter filters cheatsheets that do not match `tag(s)` |
| 11 | func Filter( |
| 12 | cheatpaths []map[string]sheet.Sheet, |
| 13 | tags []string, |
| 14 | ) []map[string]sheet.Sheet { |
| 15 | |
| 16 | // buffer a map of filtered cheatsheets |
| 17 | filtered := make([]map[string]sheet.Sheet, 0, len(cheatpaths)) |
| 18 | |
| 19 | // iterate over each cheatpath |
| 20 | for _, cheatsheets := range cheatpaths { |
| 21 | |
| 22 | // create a map of cheatsheets for each cheatpath. The filtering will be |
| 23 | // applied to each cheatpath individually. |
| 24 | pathFiltered := make(map[string]sheet.Sheet) |
| 25 | |
| 26 | // iterate over each cheatsheet that exists on each cheatpath |
| 27 | for title, sheet := range cheatsheets { |
| 28 | |
| 29 | // assume that the sheet should be kept (ie, should not be filtered) |
| 30 | keep := true |
| 31 | |
| 32 | // iterate over each tag. If the sheet does not match *all* tags, filter |
| 33 | // it out. |
| 34 | for _, tag := range tags { |
| 35 | trimmed := strings.TrimSpace(tag) |
| 36 | if trimmed == "" || !utf8.ValidString(trimmed) || !sheet.Tagged(trimmed) { |
| 37 | keep = false |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // if the sheet does match all tags, it passes the filter |
| 42 | if keep { |
| 43 | pathFiltered[title] = sheet |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // the sheets on this individual cheatpath have now been filtered. Now, |
| 48 | // store those alongside the sheets on the other cheatpaths that also made |
| 49 | // it passed the filter. |
| 50 | filtered = append(filtered, pathFiltered) |
| 51 | } |
| 52 | |
| 53 | // return the filtered cheatsheets on all paths |
| 54 | return filtered |
| 55 | } |