UNPKG

1.68 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _propertyExpr = require("property-expr");
7
8var validateName = function validateName(d) {
9 if (typeof d !== 'string') throw new TypeError("ref's must be strings, got: " + d);
10};
11
12var Reference =
13/*#__PURE__*/
14function () {
15 Reference.isRef = function isRef(value) {
16 return !!(value && (value.__isYupRef || value instanceof Reference));
17 };
18
19 var _proto = Reference.prototype;
20
21 _proto.toString = function toString() {
22 return "Ref(" + this.key + ")";
23 };
24
25 function Reference(key, mapFn, options) {
26 if (options === void 0) {
27 options = {};
28 }
29
30 validateName(key);
31 var prefix = options.contextPrefix || '$';
32
33 if (typeof key === 'function') {
34 key = '.';
35 }
36
37 this.key = key.trim();
38 this.prefix = prefix;
39 this.isContext = this.key.indexOf(prefix) === 0;
40 this.isSelf = this.key === '.';
41 this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key;
42 this._get = (0, _propertyExpr.getter)(this.path, true);
43
44 this.map = mapFn || function (value) {
45 return value;
46 };
47 }
48
49 _proto.resolve = function resolve() {
50 return this;
51 };
52
53 _proto.cast = function cast(value, _ref) {
54 var parent = _ref.parent,
55 context = _ref.context;
56 return this.getValue(parent, context);
57 };
58
59 _proto.getValue = function getValue(parent, context) {
60 var isContext = this.isContext;
61
62 var value = this._get(isContext ? context : parent || context || {});
63
64 return this.map(value);
65 };
66
67 return Reference;
68}();
69
70exports.default = Reference;
71Reference.prototype.__isYupRef = true;
72module.exports = exports["default"];
\No newline at end of file