vtils
Version:
一个面向业务的 JavaScript/TypeScript 实用程序库。
65 lines (64 loc) • 2.18 kB
JavaScript
import { getter } from 'property-expr';
var prefixes = {
context: '$',
value: '.',
rootValue: '@'
};
var Reference = /*#__PURE__*/function () {
function Reference(key, options) {
if (options === void 0) {
options = {};
}
if (typeof options === 'function') {
options = {
map: options
};
}
if (typeof key !== 'string') throw new TypeError('ref must be a string, got: ' + key);
this.key = key.trim();
if (key === '') throw new TypeError('ref must be a non-empty string');
this.isContext = this.key[0] === prefixes.context;
this.isValue = this.key[0] === prefixes.value;
this.isRootValue = this.key[0] === prefixes.rootValue;
this.isSibling = !this.isContext && !this.isValue && !this.isRootValue;
var prefix = this.isContext ? prefixes.context : this.isValue ? prefixes.value : this.isRootValue ? prefixes.rootValue : '';
this.path = this.key.slice(prefix.length);
this.getter = this.path && getter(this.path, true);
this.map = options.map;
}
var _proto = Reference.prototype;
_proto.getValue = function getValue(value, parent, context, rootValue) {
var result = this.isContext ? context : this.isValue ? value : this.isRootValue ? rootValue : parent;
if (this.getter) result = this.getter(result || {});
if (this.map) result = this.map(result);
return result;
}
/**
*
* @param {*} value
* @param {Object} options
* @param {Object=} options.context
* @param {Object=} options.parent
*/;
_proto.cast = function cast(value, options) {
return this.getValue(value, options == null ? void 0 : options.parent, options == null ? void 0 : options.context, options == null ? void 0 : options.rootValue);
};
_proto.resolve = function resolve() {
return this;
};
_proto.describe = function describe() {
return {
type: 'ref',
key: this.key
};
};
_proto.toString = function toString() {
return "Ref(" + this.key + ")";
};
Reference.isRef = function isRef(value) {
return value && value.__isYupRef;
};
return Reference;
}();
export { Reference as default };
Reference.prototype.__isYupRef = true;