-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): expose fine-grained API for subject-case
- Loading branch information
Showing
1 changed file
with
18 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,32 @@ | ||
import ensureCase from '../library/ensure-case'; | ||
import message from '../library/message'; | ||
|
||
const negated = when => when === 'never'; | ||
|
||
export default (parsed, when, value) => { | ||
const {subject} = parsed; | ||
|
||
if (!subject) { | ||
if (typeof subject !== 'string') { | ||
return [true]; | ||
} | ||
|
||
const negated = when === 'never'; | ||
const checks = (Array.isArray(value) ? value : [value]).map(check => { | ||
if (typeof check === 'string') { | ||
return { | ||
when: 'always', | ||
case: check | ||
}; | ||
} | ||
return check; | ||
}); | ||
|
||
const result = checks.every(check => { | ||
const r = ensureCase(subject, check.case); | ||
return negated(check.when) ? !r : r; | ||
}); | ||
|
||
const result = ensureCase(subject, value); | ||
return [ | ||
negated ? !result : result, | ||
negated(when) ? !result : result, | ||
message([`subject must`, negated ? `not` : null, `be ${value}`]) | ||
]; | ||
}; |