feat: GUI to create smart views (#1997)

This commit is contained in:
Aman Harwara
2022-11-14 19:40:00 +05:30
committed by GitHub
parent 1c23bc1747
commit f656185c16
28 changed files with 1032 additions and 78 deletions

View File

@@ -15,8 +15,7 @@ export interface PredicateJsonForm {
export const AllPredicateCompoundOperators = ['and', 'or'] as const
export type PredicateCompoundOperator = typeof AllPredicateCompoundOperators[number]
export const AllPredicateOperators = [
...AllPredicateCompoundOperators,
export const AllNonCompoundPredicateOperators = [
'!=',
'=',
'<',
@@ -30,6 +29,8 @@ export const AllPredicateOperators = [
'includes',
] as const
export const AllPredicateOperators = [...AllPredicateCompoundOperators, ...AllNonCompoundPredicateOperators] as const
export type PredicateOperator = typeof AllPredicateOperators[number]
export type SureValue = number | number[] | string[] | string | Date | boolean | false | ''

View File

@@ -23,6 +23,10 @@ export function valueMatchesTargetValue(
value = value.toLowerCase()
}
if (value instanceof Date && typeof targetValue === 'string') {
targetValue = new Date(targetValue)
}
if (operator === 'not') {
return !valueMatchesTargetValue(value, '=', targetValue)
}

View File

@@ -429,6 +429,36 @@ describe('predicates', () => {
it('hours ago value', () => {
expect(new Predicate('updated_at', '>', '1.hours.ago').matchesItem(item)).toEqual(true)
})
it('nonmatching hours ago value', () => {
expect(new Predicate('updated_at', '<', '1.hours.ago').matchesItem(item)).toEqual(false)
})
it('months ago value', () => {
expect(new Predicate('updated_at', '>', '1.months.ago').matchesItem(item)).toEqual(true)
})
it('nonmatching months ago value', () => {
expect(new Predicate('updated_at', '<', '1.months.ago').matchesItem(item)).toEqual(false)
})
it('years ago value', () => {
expect(new Predicate('updated_at', '>', '1.years.ago').matchesItem(item)).toEqual(true)
})
it('nonmatching years ago value', () => {
expect(new Predicate('updated_at', '<', '1.years.ago').matchesItem(item)).toEqual(false)
})
it('string date value', () => {
item = createItem({}, new Date('01/01/2022'))
expect(new Predicate('updated_at', '<', '01/02/2022').matchesItem(item)).toEqual(true)
})
it('nonmatching string date value', () => {
item = createItem({}, new Date('01/01/2022'))
expect(new Predicate('updated_at', '>', '01/02/2022').matchesItem(item)).toEqual(false)
})
})
describe('nonexistent properties', () => {

View File

@@ -0,0 +1,25 @@
import { dateFromDSLDateString } from './Utils'
describe('Predicate Utils', () => {
describe('dateFromDSLDateString', () => {
it('should return a date object with the correct day', () => {
const date = dateFromDSLDateString('1.days.ago')
expect(date.getDate()).toEqual(new Date().getDate() - 1)
})
it('should return a date object with the correct hour', () => {
const date = dateFromDSLDateString('1.hours.ago')
expect(date.getHours()).toEqual(new Date().getHours() - 1)
})
it('should return a date object with the correct month', () => {
const date = dateFromDSLDateString('1.months.ago')
expect(date.getMonth()).toEqual(new Date().getMonth() - 1)
})
it('should return a date object with the correct year', () => {
const date = dateFromDSLDateString('1.years.ago')
expect(date.getFullYear()).toEqual(new Date().getFullYear() - 1)
})
})
})

View File

@@ -10,6 +10,10 @@ export function dateFromDSLDateString(string: string): Date {
date.setDate(date.getDate() - offset)
} else if (unit === 'hours') {
date.setHours(date.getHours() - offset)
} else if (unit === 'months') {
date.setMonth(date.getMonth() - offset)
} else if (unit === 'years') {
date.setFullYear(date.getFullYear() - offset)
}
return date
}