UNPKG

blended-nunjucks

Version:

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)

97 lines (95 loc) 3.54 kB
'use strict'; // A simple class system, more documentation to come 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 _inheritsLoose(t, o) { t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } var EventEmitter = require('events'); var lib = require('./lib'); function parentWrap(parent, prop) { if (typeof parent !== 'function' || typeof prop !== 'function') { return prop; } return function wrap() { // Save the current parent method var tmp = this.parent; // Set parent to the previous method, call, and restore this.parent = parent; var res = prop.apply(this, arguments); this.parent = tmp; return res; }; } function extendClass(cls, name, props) { props = props || {}; lib.keys(props).forEach(function (k) { props[k] = parentWrap(cls.prototype[k], props[k]); }); var subclass = /*#__PURE__*/function (_cls) { function subclass() { return _cls.apply(this, arguments) || this; } _inheritsLoose(subclass, _cls); return _createClass(subclass, [{ key: "typename", get: function get() { return name; } }]); }(cls); lib._assign(subclass.prototype, props); return subclass; } var Obj = /*#__PURE__*/function () { function Obj() { // Unfortunately necessary for backwards compatibility this.init.apply(this, arguments); } var _proto = Obj.prototype; _proto.init = function init() {}; Obj.extend = function extend(name, props) { if (typeof name === 'object') { props = name; name = 'anonymous'; } return extendClass(this, name, props); }; return _createClass(Obj, [{ key: "typename", get: function get() { return this.constructor.name; } }]); }(); var EmitterObj = /*#__PURE__*/function (_EventEmitter) { function EmitterObj() { var _this2; var _this; _this = _EventEmitter.call(this) || this; // Unfortunately necessary for backwards compatibility (_this2 = _this).init.apply(_this2, arguments); return _this; } _inheritsLoose(EmitterObj, _EventEmitter); var _proto2 = EmitterObj.prototype; _proto2.init = function init() {}; EmitterObj.extend = function extend(name, props) { if (typeof name === 'object') { props = name; name = 'anonymous'; } return extendClass(this, name, props); }; return _createClass(EmitterObj, [{ key: "typename", get: function get() { return this.constructor.name; } }]); }(EventEmitter); module.exports = { Obj: Obj, EmitterObj: EmitterObj };