-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor stub-components.js (#544)
- Loading branch information
1 parent
319ab1b
commit db5e07e
Showing
4 changed files
with
98 additions
and
154 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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// @flow | ||
|
||
import { throwError } from './util' | ||
import { compileToFunctions } from 'vue-template-compiler' | ||
|
||
export function validateStubOptions (stubOptions: Array<string> | Object) { | ||
if (Array.isArray(stubOptions)) { | ||
if (containsNonStringItem(stubOptions)) { | ||
throwError('each item in an options.stubs array must be a string') | ||
} | ||
} else { | ||
if (containsInvalidOptions(stubOptions)) { | ||
throwError('options.stub values must be passed a string or component') | ||
} | ||
|
||
if (necessaryCompileToFunctionsMissed(stubOptions)) { | ||
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined') | ||
} | ||
} | ||
} | ||
|
||
function containsNonStringItem (array: Array<string>): boolean { | ||
return array.some(name => typeof name !== 'string') | ||
} | ||
|
||
function necessaryCompileToFunctionsMissed (stubOptions: Object): boolean { | ||
return Object.keys(stubOptions) | ||
.map(key => stubOptions[key]) | ||
.some(stub => typeof stub === 'string') && !compileToFunctions | ||
} | ||
|
||
function containsInvalidOptions (stubOptions: Object): boolean { | ||
return Object.keys(stubOptions) | ||
.map(key => stubOptions[key]) | ||
.some(isInvalidStubOption) | ||
} | ||
|
||
function isInvalidStubOption (stub): boolean { | ||
return !['string', 'boolean'].includes(typeof stub) && !isVueComponent(stub) | ||
} | ||
|
||
function isVueComponent (cmp) { | ||
return cmp && (cmp.render || cmp.template || cmp.options) | ||
} |
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
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