mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-10 10:53:07 +02:00
#362 - Case sensitive + insensitive option
This commit is contained in:
@@ -1,15 +1,51 @@
|
||||
import { WhenClause } from './../models/PanelSettings';
|
||||
import { Field, WhenOperator } from "../models";
|
||||
import { IMetadata } from "../panelWebView/components/Metadata";
|
||||
|
||||
|
||||
/**
|
||||
* Validate the field its "when" clause
|
||||
* @param field
|
||||
* @param parent
|
||||
* @returns
|
||||
*/
|
||||
export const fieldWhenClause = (field: Field, parent: IMetadata): boolean => {
|
||||
const when = field.when;
|
||||
if (!when) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const whenValue = parent[when.fieldRef];
|
||||
let whenValue = parent[when.fieldRef];
|
||||
if (when.caseSensitive || typeof when.caseSensitive === "undefined") {
|
||||
return caseSensitive(when, field, whenValue);
|
||||
} else {
|
||||
return caseInsensitive(when, field, whenValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Case sensitive checks
|
||||
* @param when
|
||||
* @param field
|
||||
* @param whenValue
|
||||
* @returns
|
||||
*/
|
||||
const caseInsensitive = (when: WhenClause, field: Field, whenValue: string | IMetadata | string[] | null) => {
|
||||
whenValue = lowerValue(whenValue);
|
||||
|
||||
const whenClone = Object.assign({}, when);
|
||||
whenClone.value = lowerValue(whenClone.value);
|
||||
|
||||
return caseSensitive(whenClone, field, whenValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Case insensitive checks
|
||||
* @param when
|
||||
* @param field
|
||||
* @param whenValue
|
||||
* @returns
|
||||
*/
|
||||
const caseSensitive = (when: WhenClause, field: Field, whenValue: string | IMetadata | string[] | null) => {
|
||||
switch (when.operator) {
|
||||
case WhenOperator.equals:
|
||||
if (whenValue !== when.value) {
|
||||
@@ -66,4 +102,25 @@ export const fieldWhenClause = (field: Field, parent: IMetadata): boolean => {
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lower the value(s)
|
||||
* @param value
|
||||
* @returns
|
||||
*/
|
||||
const lowerValue = (value: string | string[] | any) => {
|
||||
if (typeof value === "string") {
|
||||
value = value.toLowerCase();
|
||||
} else if (value instanceof Array) {
|
||||
value = value.map(crntValue => {
|
||||
if (typeof crntValue === "string") {
|
||||
return crntValue.toLowerCase();
|
||||
}
|
||||
|
||||
return crntValue;
|
||||
});
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
Reference in New Issue
Block a user