Skip to content

Commit

Permalink
don't match whole group but only relevant subpart of tag regex
Browse files Browse the repository at this point in the history
Signed-off-by: GRBurst <[email protected]>
  • Loading branch information
GRBurst committed May 25, 2024
1 parent 2ae0481 commit b1643f3
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 10 deletions.
3 changes: 1 addition & 2 deletions frontend/src/components/CustomFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Button, Flex, Input, Space } from "antd"
import type { SearchProps } from "antd/es/input/Search"

import { TagFilter, TagFilterSimple } from "../models/TagFilter"
import { replaceTagCaptureGroup } from '../utils/hn'
import { AppConfig } from '../utils/config'

interface CustomTagFilterProps {
Expand All @@ -19,7 +18,7 @@ const CustomTagFilter = ({ onTagAdd }: CustomTagFilterProps) => {
if (tagName !== undefined && tagName != "") {
const newFlags = (tagPatternFlags !== undefined && tagPatternFlags != "") ? tagPattern : "gmi"
const newTag = (tagPattern !== undefined && tagPattern != "") ? TagFilter({ name: tagName, pattern: RegExp(tagPattern, newFlags) }) : TagFilterSimple(tagName)
onTagAdd(AppConfig.tagFilters.custom.sectionName, replaceTagCaptureGroup(newTag))
onTagAdd(AppConfig.tagFilters.custom.sectionName, newTag)
setTagName("")
setTagPattern("")
setTagPatternFlags("")
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/FilterableJobList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const getHighlightedText = (
r1.name.length < r2.name.length ? 1 : -1
);
const patterns = sorted.map((p) => p.pattern.source).join("|");
const parts = text.split(RegExp(`(${patterns})`, "gim"));
const parts = text.split(RegExp(patterns, "gim"));

console.debug(patterns);
console.debug(parts);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/models/TagFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface TagFilter {
}

export const TagFilter = Data.case<TagFilter>()
export const TagFilterSimple = (tagName: string) => TagFilter({name: tagName, pattern: RegExp(`(?:^|\\b|\\s)(?:${tagName})(?:$|\\b|\\s)`, "gmi")})
export const TagFilterSimple = (tagName: string) => TagFilter({name: tagName, pattern: RegExp(`(^|\\b|\\s)(${tagName})($|\\b|\\s)`, "gmi")})

export const tagFilterToString = (tag: TagFilter): string => JSON.stringify({
name: tag.name,
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/utils/hn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ const filterByRegex = (haystack: string | undefined, patterns: RegExp[]): boolea
.reduce<boolean>((acc, pattern) => acc && (haystack !== undefined && haystack.search(pattern) > -1), true)
}

const replaceTagCaptureGroup = (tag: TagFilter) => TagFilter({name: tag.name, pattern: RegExp(tag.pattern.source.replace(/(\()([^(:?)].*\))/, "(:?$2"), tag.pattern.flags)})

const itemFilter = (items: Item[], tagFilters: TagFilter[], searchFilter: string | undefined = undefined, parentFilter: number | undefined = undefined, userFilter: string | undefined = undefined, filterFlagged: boolean = true) => {
console.debug("items: ", items)
try {
Expand Down Expand Up @@ -81,4 +79,4 @@ const itemFilter = (items: Item[], tagFilters: TagFilter[], searchFilter: string

const flatFilters = (filters: Map<string, TagFilters>): TagFilter[] => Array.from(filters.values()).map(filterSet => Array.from(filterSet)).flat()

export { filterByRegex, filterByRegexAny, flatFilters, getItemFromId, getItemsFromIds, getItemsFromQueryId, getItemsFromQueryIds, getKidItemsFromIds, itemFilter, replaceTagCaptureGroup };
export { filterByRegex, filterByRegexAny, flatFilters, getItemFromId, getItemsFromIds, getItemsFromQueryId, getItemsFromQueryIds, getKidItemsFromIds, itemFilter };
6 changes: 3 additions & 3 deletions frontend/src/utils/predefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const technologies = HashSet.fromIterable([
TagFilterSimple("Ruby on Rails"),
TagFilterSimple("iOS"),
TagFilterSimple("Android"),
TagFilter({name: "C++", pattern: RegExp(/(?:^|\b|\s)(?:C\+\+)(?:$|\W|\s)/, "gmi")}),
TagFilter({name: "C++", pattern: RegExp(/(^|\b|\s)(C\+\+)($|\W|\s)/, "gmi")}),
TagFilterSimple("React Native"),
TagFilterSimple("Rust"),
TagFilterSimple("Angular"),
Expand All @@ -23,11 +23,11 @@ export const technologies = HashSet.fromIterable([
TagFilterSimple(".NET"),
TagFilterSimple("C#"),
TagFilterSimple("Kotlin"),
TagFilter({name: "Scala", pattern: RegExp(/(?:^|\b|\s)(?:Scala[0-9]{0,1})(?:$|\b|\s)/, "gmi")}),
TagFilter({name: "Scala", pattern: RegExp(/(^|\b|\s)(Scala[0-9]{0,1})($|\b|\s)/, "gmi")}),
TagFilterSimple("Kafka"),
TagFilterSimple("Swift"),
TagFilterSimple("Elixir"),
TagFilter({name: "C", pattern: RegExp(/(?:^|\b|\s)(?:C)(?:$|\b|\s|^\+|^#)/, "gm")}),
TagFilter({name: "C", pattern: RegExp(/(^|\b|\s)(C)($|\b|\s|^\+|^#)/, "gm")}),
TagFilterSimple("Next.js"),
TagFilterSimple("Clojure"),
TagFilterSimple("Tensorflow"),
Expand Down

0 comments on commit b1643f3

Please sign in to comment.