UNPKG

@omni-door/utils

Version:

The utils of omni-door's projects

132 lines (131 loc) 5.36 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tplEngineInit = exports.tplEngineNew = exports._typeof = void 0; var vm_1 = __importDefault(require("vm")); function _typeof(ele) { if (typeof ele !== 'object') return typeof ele; if (!ele) return 'null'; // fix typeof null === 'object' problem var len = Object.prototype.toString.call(ele).length - 1; return Object.prototype.toString.call(ele).slice(8, len).toLowerCase(); } exports._typeof = _typeof; function tplEngineNew(tpls, tplName, params) { return function (envs) { var newTpls = {}; var context = __assign(__assign(__assign({}, (envs || {})), { use_strict: "'use strict';", include: function (name) { if (!name) return ''; var tplFn = newTpls[name]; return tplFn ? tplFn() : ''; }, alter: function (env, tplName, value) { if (!env || !tplName) return ''; var alter = envs[env]; if (typeof value === 'boolean') { if (alter !== value) return ''; } else if (!alter) return ''; var tplFn = newTpls[tplName]; return tplFn ? tplFn() : ''; }, alter_style: function (tplNames) { if (_typeof(tplNames) !== 'object') return ''; var realStyle = envs['style']; if (!realStyle) return ''; var tplName = tplNames[realStyle]; if (!tplName) return ''; var tplFn = newTpls[tplName]; return tplFn ? tplFn() : ''; }, _typeof: _typeof }), (params || {})); Object.keys(tpls).forEach(function (tplName) { var tpl = tpls[tplName]; newTpls[tplName] = vm_1.default.runInNewContext("\n (function () {\n return " + tpl + ";\n })\n ", context); }); var tplFn = newTpls[tplName]; return tplFn(); }; } exports.tplEngineNew = tplEngineNew; function tplEngineInit(tpls, tplName, params) { return function (envs) { var newTpls = {}; var context = __assign(__assign(__assign({}, (envs || {})), { use_strict: "'use strict';", include: function (name) { if (!name) return ''; var tplFn = newTpls[name]; return tplFn ? tplFn() : ''; }, alter: function (env, tplName, value) { if (!env || !tplName) return ''; var alter = envs[env]; if (typeof value === 'boolean') { if (alter !== value) return ''; } else if (!alter) return ''; var tplFn = newTpls[tplName]; return tplFn ? tplFn() : ''; }, alter_project_type: function (tplNames) { if (_typeof(tplNames) !== 'object') return ''; var realType = envs['project_type']; if (!realType) return ''; var tplName = tplNames[realType]; if (!tplName) return ''; var tplFn = newTpls[tplName]; return tplFn ? tplFn() : ''; }, alter_style: function (tplNames) { if (_typeof(tplNames) !== 'object') return ''; var realStyle = envs['style']; if (!realStyle) return ''; var tplName = tplNames[realStyle]; if (!tplName) return ''; var tplFn = newTpls[tplName]; return tplFn ? tplFn() : ''; }, alter_strategy: function (tplNames) { if (_typeof(tplNames) !== 'object') return ''; var realStrategy = envs['strategy']; if (!realStrategy) return ''; var tplName = tplNames[realStrategy]; if (!tplName) return ''; var tplFn = newTpls[tplName]; return tplFn ? tplFn() : ''; }, _typeof: _typeof }), (params || {})); Object.keys(tpls).forEach(function (tplName) { var tpl = tpls[tplName]; newTpls[tplName] = vm_1.default.runInNewContext("\n (function () {\n return " + tpl + ";\n })\n ", context); }); var tplFn = newTpls[tplName]; return tplFn(); }; } exports.tplEngineInit = tplEngineInit;