Enforce tags to be lowercase

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/14/head
trivernis 3 years ago
parent 1df5c9e8ed
commit 27a5432176
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -9,6 +9,7 @@ use crate::error::RepoResult;
/// Parses a normalized tag into its two components of namespace and tag /// Parses a normalized tag into its two components of namespace and tag
pub fn parse_namespace_and_tag(norm_tag: String) -> (Option<String>, String) { pub fn parse_namespace_and_tag(norm_tag: String) -> (Option<String>, String) {
norm_tag norm_tag
.to_lowercase()
.split_once(':') .split_once(':')
.map(|(n, t)| (Some(n.trim().to_string()), t.trim().to_string())) .map(|(n, t)| (Some(n.trim().to_string()), t.trim().to_string()))
.unwrap_or((None, norm_tag.trim().to_string())) .unwrap_or((None, norm_tag.trim().to_string()))

@ -7,7 +7,7 @@ pub struct NamespaceDto {
impl NamespaceDto { impl NamespaceDto {
pub(crate) fn new(model: namespace::Model) -> Self { pub(crate) fn new(model: namespace::Model) -> Self {
Self {model} Self { model }
} }
pub fn id(&self) -> i64 { pub fn id(&self) -> i64 {

@ -1,4 +1,5 @@
import {FileStatus, FilterExpression, FilterQuery, PropertyQuery, ValueComparator} from "../api-types/files"; import {FileStatus, FilterExpression, FilterQuery, PropertyQuery, ValueComparator} from "../api-types/files";
import {normalizeTag} from "../../app/utils/tag-utils";
export type Comparator = "Less" | "Equal" | "Greater" | "Between"; export type Comparator = "Less" | "Equal" | "Greater" | "Between";
export type PropertyType = export type PropertyType =
@ -14,7 +15,7 @@ export type PropertyType =
export class FilterQueryBuilder { export class FilterQueryBuilder {
public static tag(tag: string, negate: boolean): FilterQuery { public static tag(tag: string, negate: boolean): FilterQuery {
return { Tag: { tag, negate } }; return { Tag: { tag: normalizeTag(tag), negate } };
} }
public static status(status: FileStatus): FilterQuery { public static status(status: FileStatus): FilterQuery {
@ -74,7 +75,7 @@ export class FilterQueryBuilder {
} }
public static buildFilterFromString(filterStr: string): FilterQuery | undefined { public static buildFilterFromString(filterStr: string): FilterQuery | undefined {
filterStr = filterStr.trim(); filterStr = filterStr.trim().toLowerCase();
if (filterStr.startsWith(".")) { if (filterStr.startsWith(".")) {
const cleanFilter = filterStr.replace(/^\./, ""); const cleanFilter = filterStr.replace(/^\./, "");

@ -6,6 +6,7 @@ import {FilterExpression, FilterQuery} from "../../../../../api/api-types/files"
import {debounceTime, map, startWith} from "rxjs/operators"; import {debounceTime, map, startWith} from "rxjs/operators";
import {compareSearchResults} from "../../../../utils/compare-utils"; import {compareSearchResults} from "../../../../utils/compare-utils";
import {FilterQueryBuilder} from "../../../../../api/models/FilterQueryBuilder"; import {FilterQueryBuilder} from "../../../../../api/models/FilterQueryBuilder";
import {normalizeTag} from "../../../../utils/tag-utils";
type AutocompleteEntry = { type AutocompleteEntry = {
value: string, value: string,
@ -122,11 +123,11 @@ export class FilterInputComponent implements OnChanges {
} }
private filterAutosuggestFilters(filterValue: string): AutocompleteEntry[] { private filterAutosuggestFilters(filterValue: string): AutocompleteEntry[] {
const queryParts = filterValue.split(/\s+or\s+/gi); const queryParts = filterValue.toLowerCase().split(/\s+or\s+/gi);
const latestQuery = queryParts[queryParts.length - 1]; const latestQuery = queryParts[queryParts.length - 1];
const trimmedValue = latestQuery.trim(); const trimmedValue = latestQuery.trim();
let isNegation = trimmedValue.startsWith("-"); let isNegation = trimmedValue.startsWith("-");
const cleanValue = trimmedValue.replace(/^-/, ""); const cleanValue = normalizeTag(trimmedValue.replace(/^-/, ""));
const autosuggestTags = this.tagsForAutocomplete.filter(t => t.includes(cleanValue)).map(t => isNegation ? "-" + t : t); const autosuggestTags = this.tagsForAutocomplete.filter(t => t.includes(cleanValue)).map(t => isNegation ? "-" + t : t);
let propertyQuerySuggestions: string[] = []; let propertyQuerySuggestions: string[] = [];

@ -5,6 +5,7 @@ import {MatAutocompleteSelectedEvent} from "@angular/material/autocomplete";
import {Observable} from "rxjs"; import {Observable} from "rxjs";
import {debounceTime, map, startWith} from "rxjs/operators"; import {debounceTime, map, startWith} from "rxjs/operators";
import {compareSearchResults} from "../../../../utils/compare-utils"; import {compareSearchResults} from "../../../../utils/compare-utils";
import {normalizeTag} from "../../../../utils/tag-utils";
@Component({ @Component({
selector: "app-tag-input", selector: "app-tag-input",
@ -35,24 +36,6 @@ export class TagInputComponent implements OnChanges {
); );
} }
/**
* Normalizes the tag by removing whitespaces
* @param {string} tag
* @returns {string}
* @private
*/
private static normalizeTag(tag: string): string {
let normalizedTag = tag.trim();
let parts = normalizedTag.split(":");
if (parts.length > 1) {
const namespace = parts.shift()!.trim();
const name = parts.join(":").trim();
return namespace + ":" + name;
} else {
return normalizedTag;
}
}
ngOnChanges(changes: SimpleChanges): void { ngOnChanges(changes: SimpleChanges): void {
if (changes["availableTags"]) { if (changes["availableTags"]) {
@ -72,7 +55,7 @@ export class TagInputComponent implements OnChanges {
} }
private addTag(value: string) { private addTag(value: string) {
const tag = TagInputComponent.normalizeTag(value); const tag = normalizeTag(value);
if (tag.length > 0 && (this.allowInvalid || this.checkTagValid(tag))) { if (tag.length > 0 && (this.allowInvalid || this.checkTagValid(tag))) {
this.tagAdded.emit(tag); this.tagAdded.emit(tag);
this.formControl.setValue(""); this.formControl.setValue("");
@ -81,7 +64,7 @@ export class TagInputComponent implements OnChanges {
} }
private filterSuggestionTag(tag: string) { private filterSuggestionTag(tag: string) {
let normalizedTag = TagInputComponent.normalizeTag(tag); let normalizedTag = normalizeTag(tag);
const negated = normalizedTag.startsWith("-") && this.allowNegation; const negated = normalizedTag.startsWith("-") && this.allowNegation;
normalizedTag = this.allowNegation ? normalizedTag.replace( normalizedTag = this.allowNegation ? normalizedTag.replace(
/^-/, /^-/,
@ -100,7 +83,7 @@ export class TagInputComponent implements OnChanges {
.slice(0, 50); .slice(0, 50);
if (containsWildcard) { if (containsWildcard) {
autocompleteTags.unshift(TagInputComponent.normalizeTag(tag)); autocompleteTags.unshift(normalizeTag(tag));
} }
return autocompleteTags; return autocompleteTags;

@ -0,0 +1,18 @@
/**
* Normalizes the tag by removing whitespaces and enforcing lowercase
* @param {string} tag
* @returns {string}
* @private
*/
export function normalizeTag(tag: string): string {
let normalizedTag = tag.trim().toLowerCase();
let parts = normalizedTag.split(":");
if (parts.length > 1) {
const namespace = parts.shift()!.trim();
const name = parts.join(":").trim();
return namespace + ":" + name;
} else {
return normalizedTag;
}
}
Loading…
Cancel
Save