Skip to content

Commit

Permalink
Merge pull request #188 from sasjs/special-missings
Browse files Browse the repository at this point in the history
Added isSpecialMissing function to be used in Adapter and DataController
  • Loading branch information
allanbowe authored Apr 21, 2022
2 parents 1ae9f6d + 7c3474a commit 2450ccb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export {
InputValidator,
Choice
} from './readAndValidateInput'

export { isSpecialMissing } from './validators'
21 changes: 21 additions & 0 deletions src/input/validators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
choiceValidator,
confirmationValidator,
isSpecialMissing,
urlValidator
} from './validators'

Expand Down Expand Up @@ -100,3 +101,23 @@ describe('choiceValidator', () => {
)
})
})

describe('Special Missing check', () => {
it('should accept special missings', () => {
expect(isSpecialMissing('s')).toBeTrue()
expect(isSpecialMissing('.')).toBeTrue()
expect(isSpecialMissing('.s')).toBeTrue()
expect(isSpecialMissing('_')).toBeTrue()
expect(isSpecialMissing('._')).toBeTrue()
})

it('should reject non special missings', () => {
expect(isSpecialMissing('ss')).toBeFalse()
expect(isSpecialMissing('..')).toBeFalse()
expect(isSpecialMissing('__')).toBeFalse()
expect(isSpecialMissing('23')).toBeFalse()
expect(isSpecialMissing(23)).toBeFalse()
expect(isSpecialMissing(null)).toBeFalse()
expect(isSpecialMissing(undefined)).toBeFalse()
})
})
10 changes: 10 additions & 0 deletions src/input/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ export const choiceValidator = (
numberOfChoices: number,
errorMessage: string
) => (value > 0 && value <= numberOfChoices) || errorMessage

// This regex will match special missing
// `a-z` or `_` or `.` or '.[a-z]'
export const isSpecialMissing = (value: any) => {
// This regex can't cover the case with `..` two dots, so if we receive it that is false special missing
if (typeof value === 'string' && value.includes('..')) return false

const regex = new RegExp('^(\\.)?[a-z_.]{1}$', 'i')
return regex.test(value)
}

0 comments on commit 2450ccb

Please sign in to comment.