1.0.0 • Published 4 years ago

@vadym.gidulian/is-vue-functional-component-prop-truthy v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

is-vue-functional-component-prop-truthy

npm peer dependency version

Checks if a property passed to Vue functional component is truthy

Usage

const isTruthy = require(...)(options);

... = isTruthy(context, propName);
  • options

    • isPropsOmitted (false by default)

      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

  • context – the component's context

  • propName – the property name

Examples

const isTruthy = require(...)();

module.exports = {
	functional: true,
	props: ['prop'],
	render(h, context) {
		const isProp = isTruthy(context, 'prop');
		...
	}
};
const isTruthy = require(...)({isPropsOmitted: true});

module.exports = {
	functional: true,
	render(h, context) {
		const isProp = isTruthy(context, 'prop');
		...
	}
};

Caveats

CodeisPropsOmitted!isPropsOmitted!isPropsOmitted, !props.includes('prop')
<component/>falsefalsefalse
<component prop/>truefalse²true³
<component prop=""/>truefalse²true³
<component prop="prop"/>truetruetrue³
<component prop="false"/>truetruetrue³
<component :prop="false"/>falsefalsetrue³
<component :prop="undefined"/>falsefalsetrue³
<component :prop="null"/>falsefalsetrue³
<component :prop="0"/>falsefalsetrue³
<component :prop="NaN"/>falsefalsetrue³
<component :prop="''"/>true¹falsetrue³
  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

1.0.0

4 years ago