1 | import _ from '../wrap/lodash'
|
2 | import stringifyArguments from '../stringify/arguments'
|
3 |
|
4 | export default config =>
|
5 | (...matcherArgs) =>
|
6 | _.tap({
|
7 | __name: nameFor(config, matcherArgs),
|
8 | __matches (actualArg) {
|
9 | return config.matches(matcherArgs, actualArg)
|
10 | }
|
11 | }, (matcherInstance) => {
|
12 | matcherInstance.__matches.afterSatisfaction = (actualArg) => {
|
13 | _.invoke(config, 'afterSatisfaction', matcherArgs, actualArg)
|
14 | }
|
15 | _.invoke(config, 'onCreate', matcherInstance, matcherArgs)
|
16 | })
|
17 |
|
18 | var nameFor = (config, matcherArgs) => {
|
19 | if (_.isFunction(config.name)) {
|
20 | return config.name(matcherArgs)
|
21 | } else if (config.name != null) {
|
22 | return `${config.name}(${stringifyArguments(matcherArgs)})`
|
23 | } else {
|
24 | return `[Matcher for (${stringifyArguments(matcherArgs)})]`
|
25 | }
|
26 | }
|