• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bpatrik / pigallery2 / 39111974896

18 Oct 2026 08:34AM UTC coverage: 68.7% (+0.01%) from 68.69%
39111974896

push

github

bpatrik
Fixing tests racecondition and updating test workflow

1583 of 2583 branches covered (61.29%)

Branch coverage included in aggregate %.

4 of 4 new or added lines in 1 file covered. (100.0%)

117 existing lines in 7 files now uncovered.

5601 of 7874 relevant lines covered (71.13%)

4668.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.23
/src/common/SearchQueryParser.ts
1
import {
1 ✔
2
  ANDSearchQuery,
3
  DatePatternFrequency,
4
  DatePatternSearch,
5
  DistanceSearch,
6
  NegatableSearchQuery,
7
  OrientationSearch,
8
  ORSearchQuery,
9
  RangeSearch,
10
  SearchListQuery,
11
  SearchQueryDTO,
12
  SearchQueryTypes,
13
  SomeOfSearchQuery,
14
  TextSearch,
15
  TextSearchQueryMatchTypes,
16
  TextSearchQueryTypes,
17
} from './entities/SearchQueryDTO';
18
import {Utils} from './Utils';
1 ✔
19

20
export interface QueryKeywords {
21
  days_ago: string;
22
  years_ago: string;
23
  months_ago: string;
24
  weeks_ago: string;
25
  every_year: string;
26
  every_month: string;
27
  every_week: string;
28
  lastNDays: string;
29
  sameDay: string;
30
  portrait: string;
31
  landscape: string;
32
  orientation: string;
33
  kmFrom: string;
34
  resolution: string;
35
  rating: string;
36
  personCount: string;
37
  NSomeOf: string;
38
  someOf: string;
39
  or: string;
40
  and: string;
41
  date: string;
42
  any_text: string;
43
  caption: string;
44
  directory: string;
45
  file_name: string;
46
  keyword: string;
47
  person: string;
48
  position: string;
49
}
50

51
export const defaultQueryKeywords: QueryKeywords = {
1 ✔
52
  NSomeOf: 'of',
53
  and: 'and',
54
  or: 'or',
55

56
  date: 'date',
57

58
  rating: 'rating',
59
  personCount: 'person-count',
60
  resolution: 'resolution',
61

62
  kmFrom: 'km-from',
63
  orientation: 'orientation',
64
  landscape: 'landscape',
65
  portrait: 'portrait',
66

67

68
  years_ago: '%d-years-ago',
69
  months_ago: '%d-months-ago',
70
  weeks_ago: '%d-weeks-ago',
71
  days_ago: '%d-days-ago',
72
  every_year: 'every-year',
73
  every_month: 'every-month',
74
  every_week: 'every-week',
75
  lastNDays: 'last-%d-days',
76
  sameDay: 'same-day',
77

78
  any_text: 'any-text',
79
  keyword: 'keyword',
80
  caption: 'caption',
81
  directory: 'directory',
82
  file_name: 'file-name',
83
  person: 'person',
84
  position: 'position',
85
  someOf: 'some-of',
86
};
87

88
export class SearchQueryParser {
1 ✔
89
  constructor(private keywords: QueryKeywords = defaultQueryKeywords) {
287 ✔
90
  }
91

92
  public static stringifyText(
93
    text: string,
94
    matchType = TextSearchQueryMatchTypes.like
864,372 ✔
95
  ): string {
96
    if (matchType === TextSearchQueryMatchTypes.exact_match) {
864,428 ✔
97
      return '"' + text + '"';
28 ✔
98
    }
99
    if (matchType === TextSearchQueryMatchTypes.globMatch) {
864,400 ✔
100
      if (text.indexOf(' ') !== -1) {
26 ✔
101
        return '"' + text + '"';
14 ✔
102
      }
103
      return text;
12 ✔
104
    }
105
    if (text.indexOf(' ') !== -1) {
864,374 ✔
106
      return '(' + text + ')';
79 ✔
107
    }
108
    return text;
864,295 ✔
109
  }
110

111
  public static stringifyDate(time: number): string {
112
    if (!time) {
58 ✔
113
      return null;
21 ✔
114
    }
115
    const date = new Date(time);
37 ✔
116

117
    // simplify date with yeah only if its first of jan
118
    if (date.getMonth() === 0 && date.getDate() === 1) {
37 ✔
119
      return date.getFullYear().toString();
13 ✔
120
    }
121
    return this.stringifyText(date.toISOString().substring(0, 10));
24 ✔
122
  }
123

124
  public static humanToRegexpStr(str: string) {
125
    return str.replace(/%d/g, '\\d*');
555 ✔
126
  }
127

128
  /**
129
   * Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the specified date.
130
   * @param text
131
   * @private
132
   */
133
  private static parseDate(text: string): number {
134
    if (text.charAt(0) === '"' || text.charAt(0) === '(') {
25 !
135
      text = text.substring(1);
×
136
    }
137
    if (
25 !
138
      text.charAt(text.length - 1) === '"' ||
50 ✔
139
      text.charAt(text.length - 1) === ')'
140
    ) {
UNCOV
141
      text = text.substring(0, text.length - 1);
×
142
    }
143
    // it is the year only
144
    if (text.length === 4) {
25 ✔
145
      return Date.UTC(parseInt(text, 10), 0, 1, 0, 0, 0, 0);
11 ✔
146
    }
147
    let timestamp = null;
14 ✔
148
    // Parsing ISO string
149
    try {
14 ✔
150
      const parts = text.split('-').map((t) => parseInt(t, 10));
41 ✔
151
      if (parts && parts.length === 2) {
14 ✔
152
        timestamp = Date.UTC(parts[0], parts[1] - 1, 1, 0, 0, 0, 0); // Note: months are 0-based
1 ✔
153
      }
154
      if (parts && parts.length === 3) {
14 ✔
155
        timestamp = Date.UTC(parts[0], parts[1] - 1, parts[2], 0, 0, 0, 0); // Note: months are 0-based
13 ✔
156
      }
157
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
158
    } catch (e) {
159
      // ignoring errors
160
    }
161
    // If it could not parse as ISO string, try our luck with Date.parse
162
    // https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results
163
    if (timestamp === null) {
14 !
UNCOV
164
      timestamp = Date.parse(text);
×
165
    }
166
    if (isNaN(timestamp) || timestamp === null) {
14 !
UNCOV
167
      throw new Error('Cannot parse date: ' + text);
×
168
    }
169

170
    return timestamp;
14 ✔
171
  }
172

173
  public parse(str: string, implicitAND = true): SearchQueryDTO {
306 ✔
174
    str = str
423 ✔
175
      .replace(/\s\s+/g, ' ') // remove double spaces
176
      .replace(/:\s+/g, ':')
177
      .trim();
178

179

180
    const intFromRegexp = (str: string) => {
423 ✔
181
      const numSTR = new RegExp(/\d+/).exec(str);
164 ✔
182
      if (!numSTR) {
164 !
UNCOV
183
        return 0;
×
184
      }
185
      return parseInt(numSTR[0], 10);
164 ✔
186
    };
187
    if (str.charAt(0) === '(' && str.charAt(str.length - 1) === ')') {
423 ✔
188
      str = str.slice(1, str.length - 1);
7 ✔
189
    }
190
    const fistSpace = (start = 0) => {
423 ✔
191
      const bracketIn = [];
423 ✔
192
      let quotationMark = false;
423 ✔
193
      for (let i = start; i < str.length; ++i) {
423 ✔
194
        if (str.charAt(i) === '"') {
6,452 ✔
195
          quotationMark = !quotationMark;
66 ✔
196
          continue;
66 ✔
197
        }
198
        if (str.charAt(i) === '(') {
6,386 ✔
199
          bracketIn.push(i);
112 ✔
200
          continue;
112 ✔
201
        }
202
        if (str.charAt(i) === ')') {
6,274 ✔
203
          bracketIn.pop();
112 ✔
204
          continue;
112 ✔
205
        }
206

207
        if (
6,162 ✔
208
          quotationMark === false &&
16,820 ✔
209
          bracketIn.length === 0 &&
210
          str.charAt(i) === ' '
211
        ) {
212
          return i;
54 ✔
213
        }
214
      }
215
      return str.length - 1;
369 ✔
216
    };
217

218
    // tokenize
219
    const tokenEnd = fistSpace();
423 ✔
220

221
    // case-insensitive variant of str.startsWith(search, pos) so that
222
    // boolean keywords (e.g. `or` vs `OR`) are matched regardless of casing
223
    const startsWithI = (search: string, pos: number): boolean =>
423 ✔
224
      str.slice(pos, pos + search.length).toLowerCase() === search.toLowerCase();
86 ✔
225

226
    if (tokenEnd !== str.length - 1) {
423 ✔
227
      if (startsWithI(' ' + this.keywords.and, tokenEnd)) {
54 ✔
228
        const rest = this.parse(
22 ✔
229
          str.slice(tokenEnd + (' ' + this.keywords.and).length),
230
          implicitAND
231
        );
232
        return {
22 ✔
233
          type: SearchQueryTypes.AND,
234
          list: [
235
            this.parse(str.slice(0, tokenEnd), implicitAND), // trim brackets
236
            ...(rest.type === SearchQueryTypes.AND
22 ✔
237
              ? (rest as SearchListQuery).list
238
              : [rest]),
239
          ],
240
        } as ANDSearchQuery;
241
      } else if (startsWithI(' ' + this.keywords.or, tokenEnd)) {
32 ✔
242
        const rest = this.parse(
16 ✔
243
          str.slice(tokenEnd + (' ' + this.keywords.or).length),
244
          implicitAND
245
        );
246
        return {
16 ✔
247
          type: SearchQueryTypes.OR,
248
          list: [
249
            this.parse(str.slice(0, tokenEnd), implicitAND), // trim brackets
250
            ...(rest.type === SearchQueryTypes.OR
16 ✔
251
              ? (rest as SearchListQuery).list
252
              : [rest]),
253
          ],
254
        } as ORSearchQuery;
255
      } else {
256
        // Relation cannot be detected
257
        const t =
258
          implicitAND === true
16 ✔
259
            ? SearchQueryTypes.AND
260
            : SearchQueryTypes.UNKNOWN_RELATION;
261
        const rest = this.parse(str.slice(tokenEnd), implicitAND);
16 ✔
262
        return {
16 ✔
263
          type: t,
264
          list: [
265
            this.parse(str.slice(0, tokenEnd), implicitAND), // trim brackets
266
            ...(rest.type === t ? (rest as SearchListQuery).list : [rest]),
16 ✔
267
          ],
268
        } as SearchListQuery;
269
      }
270
    }
271
    if (
369 ✔
272
      str.startsWith(this.keywords.someOf + ':') ||
735 ✔
273
      new RegExp('^\\d*-' + this.keywords.NSomeOf + ':').test(str)
274
    ) {
275
      const prefix = str.startsWith(this.keywords.someOf + ':')
9 ✔
276
        ? this.keywords.someOf + ':'
277
        : new RegExp('^\\d*-' + this.keywords.NSomeOf + ':').exec(str)[0];
278
      let tmpList: SearchQueryDTO | SearchQueryDTO[] = this.parse(str.slice(prefix.length + 1, -1), false); // trim brackets
9 ✔
279

280
      const unfoldList = (q: SearchListQuery): SearchQueryDTO[] => {
9 ✔
281
        if (q.list) {
35 ✔
282
          if (q.type === SearchQueryTypes.UNKNOWN_RELATION) {
10 ✔
283
            return q.list.map((e) => unfoldList(e as SearchListQuery)).flat();  // flatten array
24 ✔
284
          } else {
285
            q.list.forEach((e) => unfoldList(e as SearchListQuery));
2 ✔
286
          }
287
        }
288
        return [q];
26 ✔
289
      };
290
      tmpList = unfoldList(tmpList as SearchListQuery);
9 ✔
291
      const ret = {
9 ✔
292
        type: SearchQueryTypes.SOME_OF,
293
        list: tmpList,
294
      } as SomeOfSearchQuery;
295
      if (new RegExp('^\\d*-' + this.keywords.NSomeOf + ':').test(str)) {
9 ✔
296
        ret.min = parseInt(new RegExp(/^\d*/).exec(str)[0], 10);
6 ✔
297
      }
298
      return ret;
9 ✔
299
    }
300

301
    const kwStartsWith = (s: string, kw: string): boolean => {
360 ✔
302
      return s.startsWith(kw + ':') || s.startsWith(kw + '!:');
399 ✔
303
    };
304

305
    const addValueRangeParser = (matcher: string, type: SearchQueryTypes): RangeSearch => {
360 ✔
306

307

308
      const value =
309
        matcher === 'date'
1,352 ✔
310
          ? '(\\d{4}(?:-\\d{1,2})?(?:-\\d{1,2})?)' // YYYY-MM or YYYY-MM-DD
311
          : '(\\d+)';                     // number
312
      /**
313
       * Matching:
314
       * rating:4..6
315
       * rating:4
316
       * rating=4
317
       * rating!>3
318
       * rating>3
319
       * rating!>=3
320
       * rating>=3
321
       * rating!<3
322
       * rating<3
323
       * rating!<=3
324
       * rating<=3
325
       */
326
      const regex = new RegExp(
1,352 ✔
327
        `^${matcher}(!?[:=]|!?[<>]=?)${value}(?:\\.\\.${value})?$`
328
      );
329

330
      const m = str.match(regex);
1,352 ✔
331
      if (!m) {
1,352 ✔
332
        return null;
1,302 ✔
333
      }
334

335
      let relation = m[1];
50 ✔
336
      const rawA = m[2];
50 ✔
337
      const rawB = m[3];
50 ✔
338

339
      const toValue =
340
        matcher === this.keywords.date
50 ✔
341
          ? (v: string) => SearchQueryParser.parseDate(v)
25 ✔
342
          : (v: string) => Number(v);
33 ✔
343

344
      const addValue =
345
        matcher === this.keywords.date
50 ✔
346
          ? (v: number, a: number) => v + a * 24 * 60 * 60 * 1000
2 ✔
347
          : (v: number, a: number) => v + a;
5 ✔
348

349

350
      const a = toValue(rawA);
50 ✔
351
      const b = rawB !== undefined ? toValue(rawB) : undefined;
50 ✔
352

353
      let negate = false;
50 ✔
354
      if (relation.startsWith('!')) {
50 ✔
355
        negate = true;
18 ✔
356
        relation = relation.slice(1);
18 ✔
357
      }
358
      const base =
359
        relation === '='
50 ✔
360
          ? {type, min: a, max: a}
361
          : relation === ':'
44 ✔
362
            ? b === undefined
9 ✔
363
              ? {type, min: a, max: a}
364
              : {type, min: a, max: b}
365
            : relation === '>='
35 ✔
366
              ? {type, min: a}
367
              : relation === '>'
19 ✔
368
                ? {type, min: addValue(a, 1)}
369
                : relation === '<='
15 ✔
370
                  ? {type, max: a}
371
                  : relation === '<'
3 !
372
                    ? {type, max: addValue(a, -1)}
373
                    : null;
374

375
      if (!base) {
50 !
UNCOV
376
        return null;
×
377
      }
378

379
      return negate ? {...base, negate: true} : base;
50 ✔
380

381
    };
382

383

384
    const range =
385
      addValueRangeParser(this.keywords.rating, SearchQueryTypes.rating) ||
360 ✔
386
      addValueRangeParser(this.keywords.personCount, SearchQueryTypes.person_count) ||
387
      addValueRangeParser(this.keywords.date, SearchQueryTypes.date) ||
388
      addValueRangeParser(this.keywords.resolution, SearchQueryTypes.resolution);
389

390
    if (range) {
360 ✔
391
      return range;
50 ✔
392
    }
393

394

395
    if (new RegExp('^\\d*-' + this.keywords.kmFrom + '!?:').test(str)) {
310 ✔
396
      let from = str.slice(
8 ✔
397
        new RegExp('^\\d*-' + this.keywords.kmFrom + '!?:').exec(str)[0].length
398
      );
399
      if (
8 ✔
400
        (from.charAt(0) === '(' && from.charAt(from.length - 1) === ')') ||
16 !
401
        (from.charAt(0) === '"' && from.charAt(from.length - 1) === '"')
402
      ) {
403
        from = from.slice(1, from.length - 1);
8 ✔
404
      }
405

406
      // Check if the from part matches coordinate pattern (number, number)
407
      const coordMatch = from.match(/^\s*(-?\d+\.?\d*)\s*,\s*(-?\d+\.?\d*)\s*$/);
8 ✔
408
      if (coordMatch) {
8 ✔
409
        // It's a coordinate pair
410
        const latitude = parseFloat(coordMatch[1]);
6 ✔
411
        const longitude = parseFloat(coordMatch[2]);
6 ✔
412
        return {
6 ✔
413
          type: SearchQueryTypes.distance,
414
          distance: intFromRegexp(str),
415
          from: {
416
            GPSData: {
417
              latitude,
418
              longitude
419
            }
420
          },
421
          // only add negate if the value is true
422
          ...(new RegExp('^\\d*-' + this.keywords.kmFrom + '!:').test(str) && {
8 ✔
423
            negate: true,
424
          }),
425
        } as DistanceSearch;
426
      }
427

428
      // If not coordinates, treat as location text
429
      return {
2 ✔
430
        type: SearchQueryTypes.distance,
431
        distance: intFromRegexp(str),
432
        from: {value: from},
433
        // only add negate if the value is true
434
        ...(new RegExp('^\\d*-' + this.keywords.kmFrom + '!:').test(str) && {
3 ✔
435
          negate: true,
436
        }),
437
      } as DistanceSearch;
438
    }
439

440
    if (str.startsWith(this.keywords.orientation + ':')) {
302 ✔
441
      return {
2 ✔
442
        type: SearchQueryTypes.orientation,
443
        landscape:
444
          str.slice((this.keywords.orientation + ':').length) ===
445
          this.keywords.landscape,
446
      } as OrientationSearch;
447
    }
448

449

450
    if (kwStartsWith(str, this.keywords.sameDay) ||
300 ✔
451
      new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.lastNDays) + '!?:').test(str)) {
452

453
      const freqStr = str.indexOf('!:') === -1 ? str.slice(str.indexOf(':') + 1) : str.slice(str.indexOf('!:') + 2);
99 ✔
454
      let freq: DatePatternFrequency = null;
99 ✔
455
      let ago;
456
      if (freqStr == this.keywords.every_week) {
99 ✔
457
        freq = DatePatternFrequency.every_week;
11 ✔
458
      } else if (freqStr == this.keywords.every_month) {
88 ✔
459
        freq = DatePatternFrequency.every_month;
11 ✔
460
      } else if (freqStr == this.keywords.every_year) {
77 ✔
461
        freq = DatePatternFrequency.every_year;
11 ✔
462
      } else if (new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.days_ago) + '$').test(freqStr)) {
66 ✔
463
        freq = DatePatternFrequency.days_ago;
22 ✔
464
        ago = intFromRegexp(freqStr);
22 ✔
465
      } else if (new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.weeks_ago) + '$').test(freqStr)) {
44 ✔
466
        freq = DatePatternFrequency.weeks_ago;
11 ✔
467
        ago = intFromRegexp(freqStr);
11 ✔
468
      } else if (new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.months_ago) + '$').test(freqStr)) {
33 ✔
469
        freq = DatePatternFrequency.months_ago;
11 ✔
470
        ago = intFromRegexp(freqStr);
11 ✔
471
      } else if (new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.years_ago) + '$').test(freqStr)) {
22 ✔
472
        freq = DatePatternFrequency.years_ago;
22 ✔
473
        ago = intFromRegexp(freqStr);
22 ✔
474
      }
475

476
      if (freq) {
99 ✔
477
        return {
99 ✔
478
          type: SearchQueryTypes.date_pattern,
479
          daysLength: kwStartsWith(str, this.keywords.sameDay) ? 0 : intFromRegexp(str),
99 ✔
480
          frequency: freq,
481
          ...((new RegExp('^' + SearchQueryParser.humanToRegexpStr(this.keywords.lastNDays) + '!:').test(str) ||
199 ✔
482
            str.startsWith(this.keywords.sameDay + '!:')) && {
483
            negate: true
484
          }),
485
          ...(!isNaN(ago) && {agoNumber: ago})
165 ✔
486
        } as DatePatternSearch;
487
      }
488
    }
489

490
    // parse text search
491
    const tmp = TextSearchQueryTypes.map((type) => ({
1,407 ✔
492
      key: (this.keywords as never)[SearchQueryTypes[type]] + ':',
493
      queryTemplate: {type, value: ''} as TextSearch,
494
    })).concat(
495
      TextSearchQueryTypes.map((type) => ({
1,407 ✔
496
        key: (this.keywords as never)[SearchQueryTypes[type]] + '!:',
497
        queryTemplate: {type, value: '', negate: true} as TextSearch,
498
      }))
499
    ).concat(
500
      TextSearchQueryTypes.map((type) => ({
1,407 ✔
501
        key: (this.keywords as never)[SearchQueryTypes[type]] + '~:',
502
        queryTemplate: {type, value: '', matchType: TextSearchQueryMatchTypes.globMatch} as TextSearch,
503
      }))
504
    ).concat(
505
      TextSearchQueryTypes.map((type) => ({
1,407 ✔
506
        key: (this.keywords as never)[SearchQueryTypes[type]] + '!~:',
507
        queryTemplate: {type, value: '', negate: true, matchType: TextSearchQueryMatchTypes.globMatch} as TextSearch,
508
      }))
509
    );
510
    for (const typeTmp of tmp) {
201 ✔
511
      if (str.startsWith(typeTmp.key)) {
3,172 ✔
512
        const ret: TextSearch = Utils.clone(typeTmp.queryTemplate);
110 ✔
513
        // exact match
514
        if (
110 ✔
515
          str.charAt(typeTmp.key.length) === '"' &&
140 ✔
516
          str.charAt(str.length - 1) === '"'
517
        ) {
518
          ret.value = str.slice(typeTmp.key.length + 1, str.length - 1);
30 ✔
519
          if (ret.matchType !== TextSearchQueryMatchTypes.globMatch) {
30 ✔
520
            ret.matchType = TextSearchQueryMatchTypes.exact_match;
21 ✔
521
          }
522
          // like match
523
        } else if (
80 ✔
524
          str.charAt(typeTmp.key.length) === '(' &&
131 ✔
525
          str.charAt(str.length - 1) === ')'
526
        ) {
527
          if (ret.matchType === TextSearchQueryMatchTypes.globMatch) {
51 !
UNCOV
528
            throw new Error('Parentheses groups are not supported for globMatch');
×
529
          }
530
          ret.value = str.slice(typeTmp.key.length + 1, str.length - 1);
51 ✔
531
        } else {
532
          ret.value = str.slice(typeTmp.key.length);
29 ✔
533
        }
534
        return ret;
110 ✔
535
      }
536
    }
537

538
    return {type: SearchQueryTypes.any_text, value: str} as TextSearch;
91 ✔
539
  }
540

541
  public stringify(query: SearchQueryDTO): string {
542
    const ret = this.stringifyOneEntry(query);
509 ✔
543
    if (ret.charAt(0) === '(' && ret.charAt(ret.length - 1) === ')') {
509 ✔
544
      return ret.slice(1, ret.length - 1);
50 ✔
545
    }
546
    return ret;
459 ✔
547
  }
548

549
  private stringifyOneEntry(query: SearchQueryDTO): string {
550
    if (!query || !query.type) {
1,374,247 !
UNCOV
551
      return '';
×
552
    }
553
    const negateSign = (query as NegatableSearchQuery).negate === true ? '!' : '';
1,374,247 ✔
554
    const colon = negateSign + ':';
1,374,247 ✔
555
    const isGlob = TextSearchQueryTypes.includes(query.type) && (query as TextSearch).matchType === TextSearchQueryMatchTypes.globMatch;
1,374,247 ✔
556
    const op = negateSign + (isGlob ? '~:' : ':');
1,374,247 ✔
557
    switch (query.type) {
1,374,247 !
558
      case SearchQueryTypes.AND:
559
        return (
295,983 ✔
560
          '(' +
561
          (query as SearchListQuery).list
562
            .map((q) => this.stringifyOneEntry(q))
635,529 ✔
563
            .join(' ' + this.keywords.and + ' ') +
564
          ')'
565
        );
566

567
      case SearchQueryTypes.OR:
568
        return (
213,541 ✔
569
          '(' +
570
          (query as SearchListQuery).list
571
            .map((q) => this.stringifyOneEntry(q))
737,830 ✔
572
            .join(' ' + this.keywords.or + ' ') +
573
          ')'
574
        );
575

576
      case SearchQueryTypes.SOME_OF:
577
        if ((query as SomeOfSearchQuery).min) {
32 ✔
578
          return (
27 ✔
579
            (query as SomeOfSearchQuery).min +
580
            '-' +
581
            this.keywords.NSomeOf +
582
            ':(' +
583
            (query as SearchListQuery).list
584
              .map((q) => this.stringifyOneEntry(q))
368 ✔
585
              .join(' ') +
586
            ')'
587
          );
588
        }
589
        return (
5 ✔
590
          this.keywords.someOf +
591
          ':(' +
592
          (query as SearchListQuery).list
593
            .map((q) => this.stringifyOneEntry(q))
11 ✔
594
            .join(' ') +
595
          ')'
596
        );
597

598

599
      case SearchQueryTypes.date:
600
      case SearchQueryTypes.rating:
601
      case SearchQueryTypes.resolution:
602
      case SearchQueryTypes.person_count: {
603
        const dq = query as unknown as RangeSearch;
75 ✔
604
        let kw = this.keywords.date;
75 ✔
605
        if (dq.type == SearchQueryTypes.rating) {
75 ✔
606
          kw = this.keywords.rating;
22 ✔
607
        }
608
        if (dq.type == SearchQueryTypes.resolution) {
75 ✔
609
          kw = this.keywords.resolution;
8 ✔
610
        }
611
        if (dq.type == SearchQueryTypes.person_count) {
75 ✔
612
          kw = this.keywords.personCount;
16 ✔
613
        }
614
        let minStr = '' + dq.min;
75 ✔
615
        let maxStr = '' + dq.max;
75 ✔
616
        if (dq.type == SearchQueryTypes.date) {
75 ✔
617
          minStr = SearchQueryParser.stringifyDate(dq.min);
29 ✔
618
          maxStr = SearchQueryParser.stringifyDate(dq.max);
29 ✔
619
        }
620

621
        if (isNaN(dq.min) && isNaN(dq.max)) {
75 !
UNCOV
622
          return '';
×
623
        }
624
        if (dq.min == dq.max) {
75 ✔
625
          return (
9 ✔
626
            kw +
627
            negateSign +
628
            '=' +
629
            minStr
630
          );
631
        } else if (!isNaN(dq.min) && !isNaN(dq.max)) {
66 ✔
632
          return (
13 ✔
633
            kw +
634
            negateSign +
635
            ':' +
636
            minStr +
637
            '..' +
638
            maxStr
639
          );
640
        } else if (!isNaN(dq.min)) {
53 ✔
641
          return (
29 ✔
642
            kw +
643
            negateSign +
644
            '>=' +
645
            minStr);
646
        }
647
        return (
24 ✔
648
          kw +
649
          negateSign +
650
          '<=' +
651
          maxStr);
652
      }
653

654
      case SearchQueryTypes.distance: {
655
        const distanceQuery = query as DistanceSearch;
10 ✔
656
        const value = distanceQuery.from.value;
10 ✔
657
        const coords = distanceQuery.from.GPSData;
10 ✔
658

659
        let locationStr = '';
10 ✔
660
        if (value) {
10 ✔
661
          // If we have location text, use that
662
          locationStr = value;
4 ✔
663
        } else if (coords && coords.latitude != null && coords.longitude != null) {
6 ✔
664
          // If we only have coordinates, use them
665
          locationStr = `${coords.latitude.toFixed(6)}, ${coords.longitude.toFixed(6)}`;
6 ✔
666
        }
667

668
        // Add brackets if the location string contains spaces
669
        if (locationStr.indexOf(' ') !== -1) {
10 ✔
670
          locationStr = `(${locationStr})`;
10 ✔
671
        }
672

673
        return `${distanceQuery.distance}-${this.keywords.kmFrom}${colon}${locationStr}`;
10 ✔
674
      }
675

676
      case SearchQueryTypes.orientation:
677
        return (
4 ✔
678
          this.keywords.orientation +
679
          ':' +
680
          ((query as OrientationSearch).landscape
4 ✔
681
            ? this.keywords.landscape
682
            : this.keywords.portrait)
683
        );
684
      case SearchQueryTypes.date_pattern: {
685
        const q = (query as DatePatternSearch);
198 ✔
686
        q.daysLength = q.daysLength || 0;
198 ✔
687
        let strBuilder = '';
198 ✔
688
        if (q.daysLength <= 0) {
198 ✔
689
          strBuilder += this.keywords.sameDay;
18 ✔
690
        } else {
691
          strBuilder += this.keywords.lastNDays.replace(/%d/g, q.daysLength.toString());
180 ✔
692
        }
693
        if (q.negate === true) {
198 ✔
694
          strBuilder += '!';
22 ✔
695
        }
696
        strBuilder += ':';
198 ✔
697
        switch (q.frequency) {
198 ✔
698
          case DatePatternFrequency.every_week:
699
            strBuilder += this.keywords.every_week;
22 ✔
700
            break;
22 ✔
701
          case DatePatternFrequency.every_month:
702
            strBuilder += this.keywords.every_month;
22 ✔
703
            break;
22 ✔
704
          case DatePatternFrequency.every_year:
705
            strBuilder += this.keywords.every_year;
22 ✔
706
            break;
22 ✔
707
          case DatePatternFrequency.days_ago:
708
            strBuilder += this.keywords.days_ago.replace(/%d/g, (q.agoNumber || 0).toString());
44 ✔
709
            break;
44 ✔
710
          case DatePatternFrequency.weeks_ago:
711
            strBuilder += this.keywords.weeks_ago.replace(/%d/g, (q.agoNumber || 0).toString());
22 !
712
            break;
22 ✔
713
          case DatePatternFrequency.months_ago:
714
            strBuilder += this.keywords.months_ago.replace(/%d/g, (q.agoNumber || 0).toString());
22 !
715
            break;
22 ✔
716
          case DatePatternFrequency.years_ago:
717
            strBuilder += this.keywords.years_ago.replace(/%d/g, (q.agoNumber || 0).toString());
44 !
718
            break;
44 ✔
719
        }
720
        return strBuilder;
198 ✔
721
      }
722
      case SearchQueryTypes.any_text:
723
        if (!(query as TextSearch).negate &&
104 ✔
724
          (typeof (query as TextSearch).matchType === 'undefined' ||
725
            (query as TextSearch).matchType === TextSearchQueryMatchTypes.like) &&
726
          !/\s/.test((query as TextSearch).value)) {
727
          return SearchQueryParser.stringifyText(
92 ✔
728
            (query as TextSearch).value,
729
            (query as TextSearch).matchType
730
          );
731
        } else {
732
          return (
12 ✔
733
            (this.keywords as never)[SearchQueryTypes[query.type]] +
734
            op +
735
            SearchQueryParser.stringifyText(
736
              (query as TextSearch).value,
737
              (query as TextSearch).matchType
738
            )
739
          );
740
        }
741

742
      case SearchQueryTypes.person:
743
      case SearchQueryTypes.position:
744
      case SearchQueryTypes.keyword:
745
      case SearchQueryTypes.caption:
746
      case SearchQueryTypes.file_name:
747
      case SearchQueryTypes.directory:
748
        if (!(query as TextSearch).value) {
864,300 !
UNCOV
749
          return '';
×
750
        }
751
        return (
864,300 ✔
752
          (this.keywords as never)[SearchQueryTypes[query.type]] +
753
          op +
754
          SearchQueryParser.stringifyText(
755
            (query as TextSearch).value,
756
            (query as TextSearch).matchType
757
          )
758
        );
759

760
      default:
UNCOV
761
        throw new Error('Unknown type: ' + query.type);
×
762
    }
763
  }
764
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc