MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / matchDateNotePattern

Function matchDateNotePattern

packages/app-core/src/lib/vault-layout.ts:336–396  ·  view source on GitHub ↗
(pattern: string, text: string)

Source from the content-addressed store, hash-verified

334}
335
336function matchDateNotePattern(pattern: string, text: string): DateNotePatternMatch | null {
337 const captures: Array<{ token: DateNotePatternToken; index: number }> = []
338 let captureIndex = 1
339 const source = parseDateNotePattern(pattern)
340 .map((part) => {
341 if (part.kind === 'literal') return escapeRegex(part.value)
342 switch (part.value) {
343 case 'yyyy':
344 captures.push({ token: part.value, index: captureIndex++ })
345 return '(\\d{4})'
346 case 'yy':
347 captures.push({ token: part.value, index: captureIndex++ })
348 return '(\\d{2})'
349 case 'MM':
350 case 'M':
351 case 'dd':
352 case 'd':
353 case 'ww':
354 case 'w':
355 captures.push({ token: part.value, index: captureIndex++ })
356 return '(\\d{1,2})'
357 case 'MMMM':
358 case 'MMM':
359 case 'EEEE':
360 case 'EEE':
361 return '[^/]+'
362 }
363 })
364 .join('')
365 const re = new RegExp(`^${source}$`)
366 const m = re.exec(text)
367 if (!m) return null
368
369 const out: DateNotePatternMatch = {}
370 for (const capture of captures) {
371 const raw = m[capture.index]
372 if (!raw) continue
373 const value = Number(raw)
374 switch (capture.token) {
375 case 'yyyy':
376 if (!mergeCapture(out, 'year', value)) return null
377 break
378 case 'yy':
379 if (!mergeCapture(out, 'year', readTwoDigitYear(raw))) return null
380 break
381 case 'MM':
382 case 'M':
383 if (!mergeCapture(out, 'month', value)) return null
384 break
385 case 'dd':
386 case 'd':
387 if (!mergeCapture(out, 'day', value)) return null
388 break
389 case 'ww':
390 case 'w':
391 if (!mergeCapture(out, 'week', value)) return null
392 break
393 }

Callers 2

matchDirectoryPatternFunction · 0.85
patternNumericsFunction · 0.85

Calls 4

parseDateNotePatternFunction · 0.85
mergeCaptureFunction · 0.85
readTwoDigitYearFunction · 0.85
escapeRegexFunction · 0.70

Tested by

no test coverage detected