function-tree
Version:
When a function is not enough
118 lines (112 loc) • 5.92 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
import { ResolveValue } from './ResolveValue';
/*
Creates tag for targetting things with a path in Cerebral
*/
export var Tag = /*#__PURE__*/function (_ResolveValue) {
function Tag(type, getter, strings, values) {
var _this;
_classCallCheck(this, Tag);
_this = _callSuper(this, Tag);
_this.type = type;
_this.getter = getter;
_this.strings = strings;
_this.values = values;
return _this;
}
/*
Returns all tags, also nested to identify nested state dependencies
in components
*/
_inherits(Tag, _ResolveValue);
return _createClass(Tag, [{
key: "getTags",
value: function getTags() {
return [this].concat(this.getNestedTags());
}
/*
Gets the path of the tag, where nested tags are evaluated
*/
}, {
key: "getPath",
value: function getPath(context) {
var _this2 = this;
return this.strings.reduce(function (currentPath, string, idx) {
var valueTemplate = _this2.values[idx];
if (valueTemplate instanceof ResolveValue) {
return currentPath + string + valueTemplate.getValue(context);
}
return currentPath + string + (valueTemplate !== undefined ? valueTemplate : '');
}, '');
}
}, {
key: "getValue",
value: function getValue(context) {
return this.getter(this.getPath(context), context);
}
/*
Grab nested tags from the tags current path
*/
}, {
key: "getNestedTags",
value: function getNestedTags() {
var _this3 = this;
return this.strings.reduce(function (currentPaths, string, idx) {
var valueTemplate = _this3.values[idx];
if (valueTemplate instanceof Tag) {
return currentPaths.concat(valueTemplate);
}
return currentPaths;
}, []);
}
/*
Produces a string representation of the tag
*/
}, {
key: "toString",
value: function toString() {
return this.type + '`' + this.pathToString() + '`';
}
/*
Produces a string representation of the path
*/
}, {
key: "pathToString",
value: function pathToString() {
var _this4 = this;
return this.strings.reduce(function (currentPath, string, idx) {
var valueTemplate = _this4.values[idx];
if (valueTemplate instanceof Tag) {
return currentPath + string + '${' + valueTemplate.toString() + '}';
}
return currentPath + string + (valueTemplate !== undefined ? valueTemplate : '');
}, '');
}
}]);
}(ResolveValue);
export function createTemplateTag(tag, getValue) {
return function (strings) {
for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
values[_key - 1] = arguments[_key];
}
if (values.some(function (value) {
return value === undefined;
})) {
throw new Error('One of the values passed inside the tag interpolated to undefined. Please check.');
}
return new Tag(tag, getValue, strings, values);
};
}
//# sourceMappingURL=Tag.js.map