Commit 0ec599b2 authored by Vadym Gidulian's avatar Vadym Gidulian

Initial commit

parents
# Dependency directories
node_modules/
# Logs
logs/
*.log
npm-debug.log*
# Optional npm cache directory
.npm/
# Optional REPL history
.node_repl_history/
MIT License
Copyright (c) 2018 Vadym Gidulian
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# is-vue-functional-component-prop-truthy
Checks if a property passed to Vue functional component is truthy
## Usage
```js
const isTruthy = require(...)(options);
... = isTruthy(context, propName);
```
- `options`
- `isPropsOmitted`
> Note: in versions before 2.3.0, the `props` option is required if you wish to accept props in a functional component. In 2.3.0+ you can omit the `props` option and all attributes found on the component node will be implicitly extracted as props.
>
> — [Vue.js Guide](https://vuejs.org/v2/guide/render-function.html#Functional-Components)
- `context` – the component's context
- `propName` – the property name
### Examples
```js
const isTruthy = require(...)();
module.exports = {
functional: true,
props: ['prop'],
render(h, context) {
const isProp = isTruthy(context, 'prop');
...
}
};
```
```js
const isTruthy = require(...)({isPropsOmitted: true});
module.exports = {
functional: true,
render(h, context) {
const isProp = isTruthy(context, 'prop');
...
}
};
```
## Caveats
|Code |`isPropsOmitted`|`!isPropsOmitted`|`!isPropsOmitted`, `!props.includes('prop')`|
|--------------------------------|----------------|-----------------|--------------------------------------------|
|`<component/>` |`false` |`false` |`false` |
|`<component prop/>` |`true` |`false`² |`true`³ |
|`<component prop=""/>` |`true` |`false`² |`true`³ |
|`<component prop="prop"/>` |`true` |`true` |`true`³ |
|`<component prop="false"/>` |`true` |`true` |`true`³ |
|`<component :prop="false"/>` |`false` |`false` |`true`³ |
|`<component :prop="undefined"/>`|`false` |`false` |`true`³ |
|`<component :prop="null"/>` |`false` |`false` |`true`³ |
|`<component :prop="0"/>` |`false` |`false` |`true`³ |
|`<component :prop="NaN"/>` |`false` |`false` |`true`³ |
|`<component :prop="''"/>` |`true`¹ |`false` |`true`³ |
1. `:prop="''"` considered the same as `prop=""`
2. `prop=""` considered the same as `:prop="''"`
3. Since `prop` is not declared as prop, it will be resolved to `true` as attribute, because of its presence
'use strict';
module.exports = function (options) {
options = Object.assign({}, {isPropsOmitted: false}, options);
return function (context, name) {
if (options.isPropsOmitted) return (name in context.props) && (typeof context.props[name] === 'string');
if (name in context.props) {
return !!context.props[name];
}
return context.data.attrs && (name in context.data.attrs);
};
};
{
"name": "@gviagroup/is-vue-functional-component-prop-truthy",
"version": "1.0.0",
"description": "Checks if a property passed to Vue functional component is truthy",
"license": "MIT",
"main": "index.js",
"peerDependencies": {
"vue": ">=2"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment