mirror of
https://github.com/xuthus83/pigallery2.git
synced 2025-01-14 14:43:17 +08:00
improving date parsing in SearchQueryParser.ts
This commit is contained in:
parent
76ab010b7d
commit
0ee5dacf70
@ -264,7 +264,7 @@ export class MetadataLoader {
|
|||||||
// and keep the minimum amount only
|
// and keep the minimum amount only
|
||||||
const exif = ExifReader.load(data);
|
const exif = ExifReader.load(data);
|
||||||
if (exif.Rating) {
|
if (exif.Rating) {
|
||||||
metadata.rating = parseInt(exif.Rating.value, 10) as any;
|
metadata.rating = parseInt(exif.Rating.value, 10) as 0 | 1 | 2 | 3 | 4 | 5;
|
||||||
if (metadata.rating < 0) {
|
if (metadata.rating < 0) {
|
||||||
metadata.rating = 0;
|
metadata.rating = 0;
|
||||||
}
|
}
|
||||||
@ -287,7 +287,7 @@ export class MetadataLoader {
|
|||||||
const orientation = parseInt(
|
const orientation = parseInt(
|
||||||
exif.Orientation.value as any,
|
exif.Orientation.value as any,
|
||||||
10
|
10
|
||||||
) as any;
|
) as number;
|
||||||
if (OrientationTypes.BOTTOM_LEFT < orientation) {
|
if (OrientationTypes.BOTTOM_LEFT < orientation) {
|
||||||
// noinspection JSSuspiciousNameCombination
|
// noinspection JSSuspiciousNameCombination
|
||||||
const height = metadata.size.width;
|
const height = metadata.size.width;
|
||||||
|
@ -19,7 +19,7 @@ import {
|
|||||||
TextSearchQueryTypes,
|
TextSearchQueryTypes,
|
||||||
ToDateSearch,
|
ToDateSearch,
|
||||||
} from './entities/SearchQueryDTO';
|
} from './entities/SearchQueryDTO';
|
||||||
import { Utils } from './Utils';
|
import {Utils} from './Utils';
|
||||||
|
|
||||||
export interface QueryKeywords {
|
export interface QueryKeywords {
|
||||||
portrait: string;
|
portrait: string;
|
||||||
@ -72,7 +72,8 @@ export const defaultQueryKeywords: QueryKeywords = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export class SearchQueryParser {
|
export class SearchQueryParser {
|
||||||
constructor(private keywords: QueryKeywords = defaultQueryKeywords) {}
|
constructor(private keywords: QueryKeywords = defaultQueryKeywords) {
|
||||||
|
}
|
||||||
|
|
||||||
public static stringifyText(
|
public static stringifyText(
|
||||||
text: string,
|
text: string,
|
||||||
@ -115,8 +116,11 @@ export class SearchQueryParser {
|
|||||||
// Parsing ISO string
|
// Parsing ISO string
|
||||||
try {
|
try {
|
||||||
const parts = text.split('-').map((t) => parseInt(t, 10));
|
const parts = text.split('-').map((t) => parseInt(t, 10));
|
||||||
|
if (parts && parts.length === 2) {
|
||||||
|
timestamp = Date.UTC(parts[0], parts[1] - 1, 1, 0, 0, 0, 0); // Note: months are 0-based
|
||||||
|
}
|
||||||
if (parts && parts.length === 3) {
|
if (parts && parts.length === 3) {
|
||||||
timestamp = Date.UTC(parts[0], parts[1] - 1, parts[2]); // Note: months are 0-based
|
timestamp = Date.UTC(parts[0], parts[1] - 1, parts[2], 0, 0, 0, 0); // Note: months are 0-based
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// ignoring errors
|
// ignoring errors
|
||||||
@ -230,7 +234,7 @@ export class SearchQueryParser {
|
|||||||
const unfoldList = (q: SearchListQuery): SearchQueryDTO[] => {
|
const unfoldList = (q: SearchListQuery): SearchQueryDTO[] => {
|
||||||
if (q.list) {
|
if (q.list) {
|
||||||
if (q.type === SearchQueryTypes.UNKNOWN_RELATION) {
|
if (q.type === SearchQueryTypes.UNKNOWN_RELATION) {
|
||||||
return q.list.map((e) => unfoldList(e as SearchListQuery)).flat() // flatten array
|
return q.list.map((e) => unfoldList(e as SearchListQuery)).flat(); // flatten array
|
||||||
} else {
|
} else {
|
||||||
q.list.forEach((e) => unfoldList(e as SearchListQuery));
|
q.list.forEach((e) => unfoldList(e as SearchListQuery));
|
||||||
}
|
}
|
||||||
@ -256,14 +260,14 @@ export class SearchQueryParser {
|
|||||||
return {
|
return {
|
||||||
type: SearchQueryTypes.from_date,
|
type: SearchQueryTypes.from_date,
|
||||||
value: SearchQueryParser.parseDate(str.substring(str.indexOf(':') + 1)),
|
value: SearchQueryParser.parseDate(str.substring(str.indexOf(':') + 1)),
|
||||||
...(str.startsWith(this.keywords.from + '!:') && { negate: true }), // only add if the value is true
|
...(str.startsWith(this.keywords.from + '!:') && {negate: true}), // only add if the value is true
|
||||||
} as FromDateSearch;
|
} as FromDateSearch;
|
||||||
}
|
}
|
||||||
if (kwStartsWith(str, this.keywords.to)) {
|
if (kwStartsWith(str, this.keywords.to)) {
|
||||||
return {
|
return {
|
||||||
type: SearchQueryTypes.to_date,
|
type: SearchQueryTypes.to_date,
|
||||||
value: SearchQueryParser.parseDate(str.substring(str.indexOf(':') + 1)),
|
value: SearchQueryParser.parseDate(str.substring(str.indexOf(':') + 1)),
|
||||||
...(str.startsWith(this.keywords.to + '!:') && { negate: true }), // only add if the value is true
|
...(str.startsWith(this.keywords.to + '!:') && {negate: true}), // only add if the value is true
|
||||||
} as ToDateSearch;
|
} as ToDateSearch;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,14 +275,14 @@ export class SearchQueryParser {
|
|||||||
return {
|
return {
|
||||||
type: SearchQueryTypes.min_rating,
|
type: SearchQueryTypes.min_rating,
|
||||||
value: parseInt(str.substring(str.indexOf(':') + 1), 10),
|
value: parseInt(str.substring(str.indexOf(':') + 1), 10),
|
||||||
...(str.startsWith(this.keywords.minRating + '!:') && { negate: true }), // only add if the value is true
|
...(str.startsWith(this.keywords.minRating + '!:') && {negate: true}), // only add if the value is true
|
||||||
} as MinRatingSearch;
|
} as MinRatingSearch;
|
||||||
}
|
}
|
||||||
if (kwStartsWith(str, this.keywords.maxRating)) {
|
if (kwStartsWith(str, this.keywords.maxRating)) {
|
||||||
return {
|
return {
|
||||||
type: SearchQueryTypes.max_rating,
|
type: SearchQueryTypes.max_rating,
|
||||||
value: parseInt(str.substring(str.indexOf(':') + 1), 10),
|
value: parseInt(str.substring(str.indexOf(':') + 1), 10),
|
||||||
...(str.startsWith(this.keywords.maxRating + '!:') && { negate: true }), // only add if the value is true
|
...(str.startsWith(this.keywords.maxRating + '!:') && {negate: true}), // only add if the value is true
|
||||||
} as MaxRatingSearch;
|
} as MaxRatingSearch;
|
||||||
}
|
}
|
||||||
if (kwStartsWith(str, this.keywords.minResolution)) {
|
if (kwStartsWith(str, this.keywords.minResolution)) {
|
||||||
@ -312,7 +316,7 @@ export class SearchQueryParser {
|
|||||||
return {
|
return {
|
||||||
type: SearchQueryTypes.distance,
|
type: SearchQueryTypes.distance,
|
||||||
distance: parseInt(new RegExp(/^\d*/).exec(str)[0], 10),
|
distance: parseInt(new RegExp(/^\d*/).exec(str)[0], 10),
|
||||||
from: { text: from },
|
from: {text: from},
|
||||||
...(new RegExp('^\\d*-' + this.keywords.kmFrom + '!:').test(str) && {
|
...(new RegExp('^\\d*-' + this.keywords.kmFrom + '!:').test(str) && {
|
||||||
negate: true,
|
negate: true,
|
||||||
}), // only add if the value is true
|
}), // only add if the value is true
|
||||||
@ -331,11 +335,11 @@ export class SearchQueryParser {
|
|||||||
// parse text search
|
// parse text search
|
||||||
const tmp = TextSearchQueryTypes.map((type) => ({
|
const tmp = TextSearchQueryTypes.map((type) => ({
|
||||||
key: (this.keywords as any)[SearchQueryTypes[type]] + ':',
|
key: (this.keywords as any)[SearchQueryTypes[type]] + ':',
|
||||||
queryTemplate: { type, text: '' } as TextSearch,
|
queryTemplate: {type, text: ''} as TextSearch,
|
||||||
})).concat(
|
})).concat(
|
||||||
TextSearchQueryTypes.map((type) => ({
|
TextSearchQueryTypes.map((type) => ({
|
||||||
key: (this.keywords as any)[SearchQueryTypes[type]] + '!:',
|
key: (this.keywords as any)[SearchQueryTypes[type]] + '!:',
|
||||||
queryTemplate: { type, text: '', negate: true } as TextSearch,
|
queryTemplate: {type, text: '', negate: true} as TextSearch,
|
||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
for (const typeTmp of tmp) {
|
for (const typeTmp of tmp) {
|
||||||
@ -361,7 +365,7 @@ export class SearchQueryParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { type: SearchQueryTypes.any_text, text: str } as TextSearch;
|
return {type: SearchQueryTypes.any_text, text: str} as TextSearch;
|
||||||
}
|
}
|
||||||
|
|
||||||
public stringify(query: SearchQueryDTO): string {
|
public stringify(query: SearchQueryDTO): string {
|
||||||
|
@ -68,8 +68,20 @@ describe('SearchQueryParser', () => {
|
|||||||
check({type: SearchQueryTypes.to_date, value: (Date.UTC(2020, 1, 1)), negate: true} as ToDateSearch);
|
check({type: SearchQueryTypes.to_date, value: (Date.UTC(2020, 1, 1)), negate: true} as ToDateSearch);
|
||||||
|
|
||||||
const parser = new SearchQueryParser(defaultQueryKeywords);
|
const parser = new SearchQueryParser(defaultQueryKeywords);
|
||||||
|
|
||||||
|
let query: RangeSearch = ({type: SearchQueryTypes.from_date, value: (Date.UTC(2020, 1, 4))} as FromDateSearch);
|
||||||
|
expect(parser.parse(defaultQueryKeywords.from + ':' + '2020-02-04'))
|
||||||
|
.to.deep.equals(query, parser.stringify(query));
|
||||||
|
|
||||||
|
expect(parser.parse(defaultQueryKeywords.from + ':' + '2020-2-4'))
|
||||||
|
.to.deep.equals(query, parser.stringify(query));
|
||||||
|
|
||||||
|
query = ({type: SearchQueryTypes.from_date, value: (Date.UTC(2020, 1, 1))} as FromDateSearch);
|
||||||
|
expect(parser.parse(defaultQueryKeywords.from + ':' + (new Date(query.value)).getFullYear() + '-' + '02'))
|
||||||
|
.to.deep.equals(query, parser.stringify(query));
|
||||||
|
|
||||||
// test if date gets simplified on 1st of Jan.
|
// test if date gets simplified on 1st of Jan.
|
||||||
let query: RangeSearch = {type: SearchQueryTypes.to_date, value: (Date.UTC(2020, 0, 1))} as ToDateSearch;
|
query = {type: SearchQueryTypes.to_date, value: (Date.UTC(2020, 0, 1))} as ToDateSearch;
|
||||||
expect(parser.parse(defaultQueryKeywords.to + ':' + (new Date(query.value)).getFullYear()))
|
expect(parser.parse(defaultQueryKeywords.to + ':' + (new Date(query.value)).getFullYear()))
|
||||||
.to.deep.equals(query, parser.stringify(query));
|
.to.deep.equals(query, parser.stringify(query));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user