forked from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#171 - started creation of a PoC of the idea of metadata value extrac…
…tors. At a glance a low hanging fruit turned out to be far too complex to be worth it.
- Loading branch information
1 parent
b096e4c
commit 42a5f1f
Showing
4 changed files
with
154 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
getNormalizedDate_NormalizerFn_for | ||
} from "./matchers"; | ||
|
||
const DateExtractorSpecPattern1 = 'date(dd/mm/yyyy)' | ||
const DateExtractorRegex1 = new RegExp('\\d{2}/\\d{2}/\\d{4}') | ||
const DateExtractorNormalizer1 = getNormalizedDate_NormalizerFn_for('/', 0, 1, 2) | ||
const DateExtractorSpecPattern2 = 'date(mm/dd/yyyy)' | ||
const DateExtractorRegex2 = new RegExp('\\d{2}/\\d{2}/\\d{4}') | ||
const DateExtractorNormalizer2 = getNormalizedDate_NormalizerFn_for('/', 1, 0, 2) | ||
|
||
export interface MDataExtractor { | ||
(mdataValue: string): string|undefined | ||
} | ||
|
||
export interface MDataExtractorParseResult { | ||
m: MDataExtractor | ||
remainder: string | ||
} | ||
|
||
export const tryParseAsMDataExtractorSpec = (s: string): MDataExtractorParseResult|undefined => { | ||
// Simplistic initial implementation of the idea with hardcoded two extractors | ||
if (s.trim().startsWith(DateExtractorSpecPattern1)) { | ||
return { | ||
m: extractorForPattern1, | ||
remainder: s.substring(DateExtractorSpecPattern1.length).trim() | ||
} | ||
} | ||
if (s.trim().startsWith(DateExtractorSpecPattern2)) { | ||
return { | ||
m: extractorForPattern2, | ||
remainder: s.substring(DateExtractorSpecPattern2.length).trim() | ||
} | ||
} | ||
return undefined | ||
} | ||
|
||
export function extractorForPattern1(mdataValue: string): string|undefined { | ||
const hasDate = mdataValue?.match(DateExtractorRegex1) | ||
if (hasDate && hasDate[0]) { | ||
return DateExtractorNormalizer1(hasDate[0]) ?? undefined | ||
} else { | ||
return undefined | ||
} | ||
} | ||
|
||
export function extractorForPattern2(mdataValue: string): string|undefined { | ||
const hasDate = mdataValue?.match(DateExtractorRegex2) | ||
if (hasDate && hasDate[0]) { | ||
return DateExtractorNormalizer2(hasDate[0]) ?? undefined | ||
} else { | ||
return undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
extractorForPattern1 | ||
} from '../../custom-sort/mdata-extractors' | ||
|
||
describe('extractorForPattern1', () => { | ||
const params = [ | ||
// Positive | ||
['03/05/2019', '2019-05-03//'], | ||
['Created at: 03/05/2019', '2019-05-03//'], | ||
['03/05/2019 | 22:00', '2019-05-03//'], | ||
['Created at: 03/05/2019 | 22:00', '2019-05-03//'], | ||
|
||
// TODO: more positive then negative examples | ||
|
||
['13-Jan-2012', '2012-01-13//'], | ||
['3-Feb-2', '0002-02-03//'], | ||
['1-Mar-1900', '1900-03-01//'], | ||
['42-Apr-9999', '9999-04-42//'], | ||
['0-May-0', '0000-05-00//'], | ||
['21-Jun-2024', '2024-06-21//'], | ||
['7-Jul-1872', '1872-07-07//'], | ||
['15-Aug-1234', '1234-08-15//'], | ||
['1234-Sep-7777', '7777-09-1234//'], | ||
['3-Oct-2023', '2023-10-03//'], | ||
['8-Nov-2022', '2022-11-08//'], | ||
['18-Dec-2021', '2021-12-18//'], | ||
// Negative | ||
['88-Dec-2012', '2012-12-88//'], // Invalid case, Regexp on matcher in the caller should guard against this | ||
['13-JANUARY-2012', '2012-00-13//'], // Invalid case, Regexp on matcher in the caller should guard against this | ||
['1 .1', '0000-00-1 .1//'], // Invalid case, Regexp on matcher in the caller should guard against this | ||
['', '0000-00-00//'], // Invalid case, Regexp on matcher in the caller should guard against this | ||
['abc', '0000-00-abc//'], // Invalid case, Regexp on matcher in the caller should guard against this | ||
['def-abc', '0000-00-def//'], // Invalid case, Regexp on matcher in the caller should guard against this | ||
]; | ||
it.each(params)('>%s< should become %s', (s: string, out: string) => { | ||
expect(extractorForPattern1(s)).toBe(out) | ||
}) | ||
}) |