File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * TODO: refactor validators to classes, putting needed meta info on instance.
3+ * Note that direct function comparison (Validator[0] === minDate) doesn't work when code
4+ * is transpiled
5+ * @param {String } name - a name like minDate, maxDate, minMaxDate
6+ * @param {Function } fn - the validator function to execute provided in [fn, param, config]
7+ * @param {Function } requiredSignature - arguments needed to execute fn without failing
8+ * @returns {Boolean } - whether the validator (name) is applied
9+ */
10+ export function isValidatorApplied ( name , fn , requiredSignature ) {
11+ let result ;
12+ try {
13+ result = Object . keys ( fn ( new Date ( ) , requiredSignature ) ) [ 0 ] === name ;
14+ } catch ( e ) {
15+ result = false ;
16+ }
17+ return result ;
18+ }
Original file line number Diff line number Diff line change 1+ import { expect } from '@open-wc/testing' ;
2+ import { isValidatorApplied } from '../src/isValidatorApplied.js' ;
3+
4+ describe ( 'isValidatorApplied' , ( ) => {
5+ it ( `checks if validator (provided name string) is applied` , async ( ) => {
6+ const myValFn = ( val , param ) => ( { myValFn : param === 'x' } ) ;
7+ const myOtherValFn = ( val , param ) => ( { myOtherValFn : param === 'x' } ) ;
8+
9+ expect ( isValidatorApplied ( 'myValFn' , myValFn , 'x' ) ) . to . equal ( true ) ;
10+ expect ( isValidatorApplied ( 'myValFn' , myValFn , 'y' ) ) . to . equal ( true ) ;
11+
12+ expect ( isValidatorApplied ( 'myValFn' , myOtherValFn , 'x' ) ) . to . equal ( false ) ;
13+ expect ( isValidatorApplied ( 'myValFn' , myOtherValFn , 'y' ) ) . to . equal ( false ) ;
14+ } ) ;
15+ } ) ;
You can’t perform that action at this time.
0 commit comments