UNPKG

1.87 kBJavaScriptView Raw
1import { getter } from 'property-expr';
2var prefixes = {
3 context: '$',
4 value: '.'
5};
6
7var Reference = /*#__PURE__*/function () {
8 function Reference(key, options) {
9 if (options === void 0) {
10 options = {};
11 }
12
13 if (typeof key !== 'string') throw new TypeError('ref must be a string, got: ' + key);
14 this.key = key.trim();
15 if (key === '') throw new TypeError('ref must be a non-empty string');
16 this.isContext = this.key[0] === prefixes.context;
17 this.isValue = this.key[0] === prefixes.value;
18 this.isSibling = !this.isContext && !this.isValue;
19 var prefix = this.isContext ? prefixes.context : this.isValue ? prefixes.value : '';
20 this.path = this.key.slice(prefix.length);
21 this.getter = this.path && getter(this.path, true);
22 this.map = options.map;
23 }
24
25 var _proto = Reference.prototype;
26
27 _proto.getValue = function getValue(value, parent, context) {
28 var result = this.isContext ? context : this.isValue ? value : parent;
29 if (this.getter) result = this.getter(result || {});
30 if (this.map) result = this.map(result);
31 return result;
32 }
33 /**
34 *
35 * @param {*} value
36 * @param {Object} options
37 * @param {Object=} options.context
38 * @param {Object=} options.parent
39 */
40 ;
41
42 _proto.cast = function cast(value, options) {
43 return this.getValue(value, options == null ? void 0 : options.parent, options == null ? void 0 : options.context);
44 };
45
46 _proto.resolve = function resolve() {
47 return this;
48 };
49
50 _proto.describe = function describe() {
51 return {
52 type: 'ref',
53 key: this.key
54 };
55 };
56
57 _proto.toString = function toString() {
58 return "Ref(" + this.key + ")";
59 };
60
61 Reference.isRef = function isRef(value) {
62 return value && value.__isYupRef;
63 };
64
65 return Reference;
66}();
67
68export { Reference as default };
69Reference.prototype.__isYupRef = true;
\No newline at end of file