|
| 1 | +import { |
| 2 | + getNormalizedDate_NormalizerFn_for |
| 3 | +} from "./matchers"; |
| 4 | +import {NormalizerFn} from "./custom-sort-types"; |
| 5 | + |
| 6 | +export interface MDataMatcher { |
| 7 | + (mdataValue: string): boolean |
| 8 | +} |
| 9 | + |
| 10 | +export interface MDataMatcherFactory { |
| 11 | + (specsMatch: string|RegExpMatchArray): MDataMatcher |
| 12 | +} |
| 13 | + |
| 14 | +interface ValueMatcherSpec { |
| 15 | + specPattern: string|RegExp, |
| 16 | + valueMatcherFnFactory: MDataMatcherFactory |
| 17 | + unitTestsId: string |
| 18 | +} |
| 19 | + |
| 20 | +export interface MDataMatcherParseResult { |
| 21 | + m: MDataMatcher |
| 22 | + remainder: string |
| 23 | +} |
| 24 | + |
| 25 | +const VALUE_MATCHER_REGEX = /value\(([^)]+)\)/ |
| 26 | +function getPlainValueMatcherFn(specsMatch: RegExpMatchArray) { |
| 27 | + const EXACT_VALUE_IDX = 1 // Related to the spec regexp |
| 28 | + const expectedValue = specsMatch[EXACT_VALUE_IDX].trim() |
| 29 | + return (mdataValue: string): boolean => { |
| 30 | + return mdataValue === expectedValue |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +const RANGE_MATCHER_REGEX = /range([[(])([^,]*),([^)\]]*)([)\]])/ |
| 35 | +/* |
| 36 | + range(aaa,bbb) |
| 37 | + range[aaa,bbb) |
| 38 | + range(, x) |
| 39 | + range( y, ] |
| 40 | + */ |
| 41 | +enum RangeEdgeType { INCLUSIVE, EXCLUSIVE} |
| 42 | +function getRangeMatcherFn(specsMatch: RegExpMatchArray) { |
| 43 | + const RANGE_START_TYPE_IDX = 1 |
| 44 | + const RANGE_START_IDX = 2 |
| 45 | + const RANGE_END_IDX = 3 |
| 46 | + const RANGE_END_TYPE_IDX = 4 |
| 47 | + const rangeStartType: RangeEdgeType = specsMatch[RANGE_END_TYPE_IDX] === '(' ? RangeEdgeType.EXCLUSIVE : RangeEdgeType.INCLUSIVE |
| 48 | + const rangeStartValue: string = specsMatch[RANGE_START_IDX].trim() |
| 49 | + const rangeEndValue: string = specsMatch[RANGE_END_IDX].trim() |
| 50 | + const rangeEndType: RangeEdgeType = specsMatch[RANGE_END_TYPE_IDX] === ')' ? RangeEdgeType.EXCLUSIVE : RangeEdgeType.INCLUSIVE |
| 51 | + return (mdataValue: string): boolean => { |
| 52 | + let rangeStartMatched = true |
| 53 | + if (rangeStartValue) { |
| 54 | + if (rangeStartType === RangeEdgeType.INCLUSIVE) { |
| 55 | + rangeStartMatched = mdataValue >= rangeStartValue |
| 56 | + } else { |
| 57 | + rangeStartMatched = mdataValue > rangeStartValue |
| 58 | + } |
| 59 | + } |
| 60 | + let rangeEndMatched = true |
| 61 | + if (rangeEndValue) { |
| 62 | + if (rangeEndType === RangeEdgeType.INCLUSIVE) { |
| 63 | + rangeEndMatched = mdataValue <= rangeEndValue |
| 64 | + } else { |
| 65 | + rangeEndMatched = mdataValue < rangeEndValue |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return rangeStartMatched && rangeEndMatched |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +const ValueMatchers: ValueMatcherSpec[] = [ |
| 74 | + { specPattern: VALUE_MATCHER_REGEX, |
| 75 | + valueMatcherFnFactory: getPlainValueMatcherFn, |
| 76 | + unitTestsId: 'value' |
| 77 | + }, { |
| 78 | + specPattern: RANGE_MATCHER_REGEX, |
| 79 | + valueMatcherFnFactory: getRangeMatcherFn, |
| 80 | + unitTestsId: 'range' |
| 81 | + }, { |
| 82 | + specPattern: 'any-value', // Artificially added for testing purposes |
| 83 | + valueMatcherFnFactory: () => (s: string) => true, |
| 84 | + unitTestsId: 'any-value-explicit' |
| 85 | + } |
| 86 | +] |
| 87 | + |
| 88 | +export const tryParseAsMDataMatcherSpec = (s: string): MDataMatcherParseResult|undefined => { |
| 89 | + // Simplistic initial implementation of the idea, not closing the way to more complex implementations |
| 90 | + for (const matcherSpec of ValueMatchers) { |
| 91 | + if ('string' === typeof matcherSpec.specPattern && s.trim().startsWith(matcherSpec.specPattern)) { |
| 92 | + return { |
| 93 | + m: matcherSpec.valueMatcherFnFactory(matcherSpec.specPattern), |
| 94 | + remainder: s.substring(matcherSpec.specPattern.length).trim() |
| 95 | + } |
| 96 | + } else { // regexp |
| 97 | + const match = s.match(matcherSpec.specPattern) |
| 98 | + if (match) { |
| 99 | + return { |
| 100 | + m: matcherSpec.valueMatcherFnFactory(match), |
| 101 | + remainder: s.substring(match[0].length).trim() |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return undefined |
| 107 | +} |
| 108 | + |
| 109 | +export const _unitTests = { |
| 110 | + matcherFn_value: ValueMatchers.find((it) => it.unitTestsId === 'value'), |
| 111 | + matcherFn_range: ValueMatchers.find((it) => it.unitTestsId === 'range'), |
| 112 | + matcherFn_anyValue: ValueMatchers.find((it) => it.unitTestsId === 'any-value-explicit'), |
| 113 | +} |
0 commit comments