UNPKG

@marcelohmdias/vue-typed

Version:

The easiest way to define types in Vue props

232 lines (208 loc) 4.3 kB
/*! * @marcelohmdias/vue-typed v0.2.0 * (c) Marcelo H M Dias <marcelohmdias@gmail.com> (https://github.com/marcelohmdias) * Released under the MIT License. */ 'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var Typed = /*#__PURE__*/ function () { function Typed() { _classCallCheck(this, Typed); this.props = {}; } /** * Create a new instance of Typed * * @readonly * @static * @type {TypedInstance} * @memberof Typed */ _createClass(Typed, [{ key: "def", /** * Returns a new Prop setting * * @type {PropOptions<any>} * @memberof Typed */ value: function def() { return this.props; } /** * Assigns a new default value to Prop * * @param {PropDefault<T>} value * @memberof Typed */ }, { key: "with", value: function _with(value) { this.props["default"] = function () { return value; }; return this; } /** * Defines a new type for Prop * * @readonly * @type {TypedInstance} * @memberof Typed */ }, { key: "type", value: function type(_type) { this.props.type = _type; return this; } /** * Assigns a validator to Prop * * @param {PropValidator<T>} validator * @memberof Typed */ }, { key: "check", value: function check(validator) { this.props.validator = validator; return this; } }, { key: "required", /** * Set Prop as required * * @readonly * @type {TypedInstance} * @memberof Typed */ get: function get() { this.props.required = true; return this; } /** * Set Prop type to Array * * @readonly * @type {TypedInstance} * @memberof Typed */ }, { key: "array", get: function get() { return this.type(Array); } /** * Set Prop type to Boolean * * @readonly * @type {TypedInstance} * @memberof Typed */ }, { key: "bool", get: function get() { return this.type(Boolean); } /** * Set Prop type to Date * * @readonly * @type {TypedInstance} * @memberof Typed */ }, { key: "date", get: function get() { return this.type(Date); } /** * Set Prop type to Function * * @readonly * @type {TypedInstance} * @memberof Typed */ }, { key: "func", get: function get() { return this.type(Function); } /** * Set Prop type to Number * * @readonly * @type {TypedInstance} * @memberof Typed */ }, { key: "num", get: function get() { return this.type(Number); } /** * Set Prop type to Object * * @readonly * @type {TypedInstance} * @memberof Typed */ }, { key: "obj", get: function get() { return this.type(Object); } /** * Set Prop type to String * * @readonly * @type {TypedInstance} * @memberof Typed */ }, { key: "str", get: function get() { return this.type(String); } /** * Set Prop type to Symbol * * @readonly * @type {TypedInstance} * @memberof Typed */ }, { key: "sym", get: function get() { return this.type(Symbol); } }], [{ key: "is", get: function get() { return new Typed(); } }]); return Typed; }(); module.exports = Typed;