@aspectus/vue-utils
Version:
Vue related utilities that makes development easier.
453 lines (388 loc) • 12.1 kB
JavaScript
/*!
* vue-utils v0.10.21
* (c) 2023 Alex Tkachenko
* Released under the MIT License.
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var propsMergerFunction = _interopDefault(require('@vue/babel-helper-vue-jsx-merge-props'));
function mergeContext() {
for (var _len = arguments.length, attrs = new Array(_len), _key = 0; _key < _len; _key++) {
attrs[_key] = arguments[_key];
}
return propsMergerFunction(attrs);
}
function renderSlim(nodes, h) {
var tag = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'div';
var data = arguments.length > 3 ? arguments[3] : undefined;
if (nodes.length === 0) {
return null;
}
if (nodes.length === 1) {
return nodes[0];
}
return h(tag, data, nodes);
}
/* eslint-disable import/prefer-default-export, no-underscore-dangle */
function resolveInjection(key, vm) {
var source = vm;
while (source) {
if (source._provided && Object.prototype.hasOwnProperty.call(source._provided, key)) {
return source._provided[key];
}
source = source.$parent;
}
return null;
}
/* eslint-disable import/prefer-default-export, no-underscore-dangle */
var getComponentOptions = function getComponentOptions(Component) {
return typeof Component === 'function' ? Component.options : Component;
};
var makeF = function makeF(render, props, name) {
var rest = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
return Object.assign(rest, {
name: name,
functional: true,
props: props,
render: render
});
};
/* eslint-disable prefer-object-spread, no-param-reassign, no-unused-expressions */
var normalizeProps = function normalizeProps(props) {
if (!props) {
return {};
}
if (Array.isArray(props)) {
var result = {};
props.forEach(function (key) {
if (typeof key === 'string') {
result[key] = {};
}
});
return result;
}
return Object.assign({}, props);
};
var mergeMixinProps = function mergeMixinProps(mixins) {
var initial = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (!mixins || !mixins.length) {
return initial;
}
return mixins.reduce(function (result, mixin) {
var props = Object.assign({}, mergeMixinProps(mixin.mixins, result), normalizeProps(mixin.props));
return Object.assign({}, result, props);
}, initial);
};
var getProps = function getProps(Component) {
var options = getComponentOptions(Component);
var props = normalizeProps(options.props);
var mixinProps = mergeMixinProps(options.mixins);
return Object.assign({}, mixinProps, props);
};
var VALUE_TAG_MAP = {
input: true,
textarea: true,
option: true,
select: true,
progress: true
};
var DOM_PROP_ATTRS = ['value', 'selected', 'checked', 'muted'];
var mustBeDOM = function mustBeDOM(tag, type, attr) {
return attr === 'value' && VALUE_TAG_MAP[tag] && type !== 'button' || attr === 'selected' && tag === 'option' || attr === 'checked' && tag === 'input' || attr === 'muted' && tag === 'video';
};
function internalPropsMagicTransform(tag, context) {
var g = function g(name) {
return context.props && context.props[name] || context.attrs && context.attrs[name];
};
var tp = g('type');
var hasDom = !!context.domProps;
DOM_PROP_ATTRS.forEach(function (attr) {
if (mustBeDOM(tag, tp, attr)) {
if (hasDom && typeof context.domProps[attr] !== 'undefined') {
return;
}
context.domProps = context.domProps || {};
context.domProps[attr] = g(attr);
context.props && delete context.props[attr];
context.attrs && delete context.attrs[attr];
}
});
return context;
}
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _isPlaceholder(a) {
return a != null && _typeof(a) === 'object' && a['@@functional/placeholder'] === true;
}
var _isPlaceholder_1 = _isPlaceholder;
/**
* Optimized internal one-arity curry function.
*
* @private
* @category Function
* @param {Function} fn The function to curry.
* @return {Function} The curried function.
*/
function _curry1(fn) {
return function f1(a) {
if (arguments.length === 0 || _isPlaceholder_1(a)) {
return f1;
} else {
return fn.apply(this, arguments);
}
};
}
var _curry1_1 = _curry1;
function _arity(n, fn) {
/* eslint-disable no-unused-vars */
switch (n) {
case 0:
return function () {
return fn.apply(this, arguments);
};
case 1:
return function (a0) {
return fn.apply(this, arguments);
};
case 2:
return function (a0, a1) {
return fn.apply(this, arguments);
};
case 3:
return function (a0, a1, a2) {
return fn.apply(this, arguments);
};
case 4:
return function (a0, a1, a2, a3) {
return fn.apply(this, arguments);
};
case 5:
return function (a0, a1, a2, a3, a4) {
return fn.apply(this, arguments);
};
case 6:
return function (a0, a1, a2, a3, a4, a5) {
return fn.apply(this, arguments);
};
case 7:
return function (a0, a1, a2, a3, a4, a5, a6) {
return fn.apply(this, arguments);
};
case 8:
return function (a0, a1, a2, a3, a4, a5, a6, a7) {
return fn.apply(this, arguments);
};
case 9:
return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
return fn.apply(this, arguments);
};
case 10:
return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
return fn.apply(this, arguments);
};
default:
throw new Error('First argument to _arity must be a non-negative integer no greater than ten');
}
}
var _arity_1 = _arity;
/**
* Optimized internal two-arity curry function.
*
* @private
* @category Function
* @param {Function} fn The function to curry.
* @return {Function} The curried function.
*/
function _curry2(fn) {
return function f2(a, b) {
switch (arguments.length) {
case 0:
return f2;
case 1:
return _isPlaceholder_1(a) ? f2 : _curry1_1(function (_b) {
return fn(a, _b);
});
default:
return _isPlaceholder_1(a) && _isPlaceholder_1(b) ? f2 : _isPlaceholder_1(a) ? _curry1_1(function (_a) {
return fn(_a, b);
}) : _isPlaceholder_1(b) ? _curry1_1(function (_b) {
return fn(a, _b);
}) : fn(a, b);
}
};
}
var _curry2_1 = _curry2;
/**
* Internal curryN function.
*
* @private
* @category Function
* @param {Number} length The arity of the curried function.
* @param {Array} received An array of arguments received thus far.
* @param {Function} fn The function to curry.
* @return {Function} The curried function.
*/
function _curryN(length, received, fn) {
return function () {
var combined = [];
var argsIdx = 0;
var left = length;
var combinedIdx = 0;
while (combinedIdx < received.length || argsIdx < arguments.length) {
var result;
if (combinedIdx < received.length && (!_isPlaceholder_1(received[combinedIdx]) || argsIdx >= arguments.length)) {
result = received[combinedIdx];
} else {
result = arguments[argsIdx];
argsIdx += 1;
}
combined[combinedIdx] = result;
if (!_isPlaceholder_1(result)) {
left -= 1;
}
combinedIdx += 1;
}
return left <= 0 ? fn.apply(this, combined) : _arity_1(left, _curryN(length, combined, fn));
};
}
var _curryN_1 = _curryN;
/**
* Returns a curried equivalent of the provided function, with the specified
* arity. The curried function has two unusual capabilities. First, its
* arguments needn't be provided one at a time. If `g` is `R.curryN(3, f)`, the
* following are equivalent:
*
* - `g(1)(2)(3)`
* - `g(1)(2, 3)`
* - `g(1, 2)(3)`
* - `g(1, 2, 3)`
*
* Secondly, the special placeholder value [`R.__`](#__) may be used to specify
* "gaps", allowing partial application of any combination of arguments,
* regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
* the following are equivalent:
*
* - `g(1, 2, 3)`
* - `g(_, 2, 3)(1)`
* - `g(_, _, 3)(1)(2)`
* - `g(_, _, 3)(1, 2)`
* - `g(_, 2)(1)(3)`
* - `g(_, 2)(1, 3)`
* - `g(_, 2)(_, 3)(1)`
*
* @func
* @memberOf R
* @since v0.5.0
* @category Function
* @sig Number -> (* -> a) -> (* -> a)
* @param {Number} length The arity for the returned function.
* @param {Function} fn The function to curry.
* @return {Function} A new, curried function.
* @see R.curry
* @example
*
* const sumArgs = (...args) => R.sum(args);
*
* const curriedAddFourNumbers = R.curryN(4, sumArgs);
* const f = curriedAddFourNumbers(1, 2);
* const g = f(3);
* g(4); //=> 10
*/
var curryN =
/*#__PURE__*/
_curry2_1(function curryN(length, fn) {
if (length === 1) {
return _curry1_1(fn);
}
return _arity_1(length, _curryN_1(length, [], fn));
});
var curryN_1 = curryN;
/**
* Returns a curried equivalent of the provided function. The curried function
* has two unusual capabilities. First, its arguments needn't be provided one
* at a time. If `f` is a ternary function and `g` is `R.curry(f)`, the
* following are equivalent:
*
* - `g(1)(2)(3)`
* - `g(1)(2, 3)`
* - `g(1, 2)(3)`
* - `g(1, 2, 3)`
*
* Secondly, the special placeholder value [`R.__`](#__) may be used to specify
* "gaps", allowing partial application of any combination of arguments,
* regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
* the following are equivalent:
*
* - `g(1, 2, 3)`
* - `g(_, 2, 3)(1)`
* - `g(_, _, 3)(1)(2)`
* - `g(_, _, 3)(1, 2)`
* - `g(_, 2)(1)(3)`
* - `g(_, 2)(1, 3)`
* - `g(_, 2)(_, 3)(1)`
*
* @func
* @memberOf R
* @since v0.1.0
* @category Function
* @sig (* -> a) -> (* -> a)
* @param {Function} fn The function to curry.
* @return {Function} A new, curried function.
* @see R.curryN, R.partial
* @example
*
* const addFourNumbers = (a, b, c, d) => a + b + c + d;
*
* const curriedAddFourNumbers = R.curry(addFourNumbers);
* const f = curriedAddFourNumbers(1, 2);
* const g = f(3);
* g(4); //=> 10
*/
var curry =
/*#__PURE__*/
_curry1_1(function curry(fn) {
return curryN_1(fn.length, fn);
});
var curry_1 = curry;
/* eslint-disable import/prefer-default-export, no-underscore-dangle, prefer-object-spread */
var withDefaultProps = curry_1(function (defaults, Component) {
return makeF(function (h, context) {
return h(Component, mergeContext(context.data, {
attrs: context.props
}), context.children);
}, Object.keys(defaults).reduce(function (acc, key) {
acc[key] = {
default: defaults[key]
};
return acc;
}, getProps(Component)));
});
/* eslint-disable import/prefer-default-export */
function isEmpty(value) {
return value === null || value === '' || typeof value === 'undefined' || Array.isArray(value) && value.length === 0 || Object(value) === value && Object.keys(value).length === 0;
}
exports.DOM_PROP_ATTRS = DOM_PROP_ATTRS;
exports.VALUE_TAG_MAP = VALUE_TAG_MAP;
exports.getComponentOptions = getComponentOptions;
exports.getProps = getProps;
exports.internalPropsMagicTransform = internalPropsMagicTransform;
exports.isEmpty = isEmpty;
exports.makeF = makeF;
exports.mergeContext = mergeContext;
exports.mergeMixinProps = mergeMixinProps;
exports.mustBeDOM = mustBeDOM;
exports.normalizeProps = normalizeProps;
exports.renderSlim = renderSlim;
exports.resolveInjection = resolveInjection;
exports.withDefaultProps = withDefaultProps;