@kosatyi/ejs 0.0.98 → 0.0.99

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -654,6 +654,7 @@ defaults.globalHelpers = [];
654
654
  defaults.vars = {
655
655
  SCOPE: 'ejs',
656
656
  COMPONENT: 'ui',
657
+ ELEMENT: 'el',
657
658
  EXTEND: '$$e',
658
659
  BUFFER: '$$a',
659
660
  LAYOUT: '$$l',
@@ -690,7 +691,6 @@ var Cache = /*#__PURE__*/function () {
690
691
  _classCallCheck(this, Cache);
691
692
  _classPrivateFieldInitSpec(this, _enabled, true);
692
693
  _classPrivateFieldInitSpec(this, _list, {});
693
- bindContext(this, ['configure']);
694
694
  this.configure(config);
695
695
  }
696
696
  return _createClass(Cache, [{
@@ -772,7 +772,6 @@ var Compiler = /*#__PURE__*/function () {
772
772
  return "')\n".concat(value.trim(), "\n").concat(this.BUFFER, "('");
773
773
  }
774
774
  }]);
775
- bindContext(this, ['configure', 'compile']);
776
775
  this.configure(config);
777
776
  }
778
777
  return _createClass(Compiler, [{
@@ -807,7 +806,8 @@ var Compiler = /*#__PURE__*/function () {
807
806
  SCOPE = _classPrivateFieldGet2$1.SCOPE,
808
807
  SAFE = _classPrivateFieldGet2$1.SAFE,
809
808
  BUFFER = _classPrivateFieldGet2$1.BUFFER,
810
- COMPONENT = _classPrivateFieldGet2$1.COMPONENT;
809
+ COMPONENT = _classPrivateFieldGet2$1.COMPONENT,
810
+ ELEMENT = _classPrivateFieldGet2$1.ELEMENT;
811
811
  var GLOBALS = _classPrivateFieldGet2(_config$1, this).globalHelpers;
812
812
  if (_classPrivateFieldGet2(_config$1, this).rmWhitespace) {
813
813
  content = String(content).replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
@@ -830,7 +830,7 @@ var Compiler = /*#__PURE__*/function () {
830
830
  source = "".concat(BUFFER, ".start();").concat(source, "return ").concat(BUFFER, ".end();");
831
831
  source += "\n//# sourceURL=".concat(path);
832
832
  var result = null;
833
- var params = [SCOPE, COMPONENT, BUFFER, SAFE].concat(GLOBALS);
833
+ var params = [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT].concat(GLOBALS);
834
834
  try {
835
835
  result = Function.apply(null, params.concat(source));
836
836
  result.source = "(function(".concat(params.join(','), "){\n").concat(source, "\n});");
@@ -861,7 +861,6 @@ var Template = /*#__PURE__*/function () {
861
861
  assertInstanceOf(compiler, Compiler);
862
862
  _classPrivateFieldSet2(_cache$1, this, cache);
863
863
  _classPrivateFieldSet2(_compiler$1, this, compiler);
864
- bindContext(this, ['configure', 'get']);
865
864
  this.configure(config);
866
865
  }
867
866
  return _createClass(Template, [{
@@ -1023,7 +1022,7 @@ function createBuffer() {
1023
1022
  return buffer;
1024
1023
  }
1025
1024
 
1026
- var createScope = function createScope(config, methods) {
1025
+ var createContextScope = function createContextScope(config, methods) {
1027
1026
  var _Object$definePropert;
1028
1027
  var _config$vars = config.vars,
1029
1028
  BLOCKS = _config$vars.BLOCKS,
@@ -1031,9 +1030,10 @@ var createScope = function createScope(config, methods) {
1031
1030
  EXTEND = _config$vars.EXTEND,
1032
1031
  LAYOUT = _config$vars.LAYOUT,
1033
1032
  BUFFER = _config$vars.BUFFER,
1034
- COMPONENT = _config$vars.COMPONENT,
1035
1033
  SAFE = _config$vars.SAFE,
1036
- SCOPE = _config$vars.SCOPE;
1034
+ SCOPE = _config$vars.SCOPE,
1035
+ COMPONENT = _config$vars.COMPONENT,
1036
+ ELEMENT = _config$vars.ELEMENT;
1037
1037
  /**
1038
1038
  * @name ContextScope
1039
1039
  * @param data
@@ -1042,7 +1042,7 @@ var createScope = function createScope(config, methods) {
1042
1042
  function ContextScope(data) {
1043
1043
  this[BLOCKS] = {};
1044
1044
  this[MACRO] = {};
1045
- Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT]));
1045
+ Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
1046
1046
  }
1047
1047
  Object.assign(ContextScope.prototype, methods);
1048
1048
  Object.defineProperties(ContextScope.prototype, (_Object$definePropert = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, BUFFER, {
@@ -1059,6 +1059,30 @@ var createScope = function createScope(config, methods) {
1059
1059
  }), EXTEND, {
1060
1060
  value: false,
1061
1061
  writable: true
1062
+ }), "useSafeValue", {
1063
+ get: function get() {
1064
+ return safeValue;
1065
+ }
1066
+ }), "useComponent", {
1067
+ get: function get() {
1068
+ if (isFunction(this[COMPONENT])) {
1069
+ return this[COMPONENT].bind(this);
1070
+ } else {
1071
+ return function () {
1072
+ throw new Error("".concat(COMPONENT, " must be a function"));
1073
+ };
1074
+ }
1075
+ }
1076
+ }), "useElement", {
1077
+ get: function get() {
1078
+ if (isFunction(this[ELEMENT])) {
1079
+ return this[ELEMENT].bind(this);
1080
+ } else {
1081
+ return function () {
1082
+ throw new Error("".concat(ELEMENT, " must be a function"));
1083
+ };
1084
+ }
1085
+ }
1062
1086
  }), "getMacro", {
1063
1087
  value: function value() {
1064
1088
  return this[MACRO];
@@ -1067,19 +1091,7 @@ var createScope = function createScope(config, methods) {
1067
1091
  value: function value() {
1068
1092
  return this[BUFFER];
1069
1093
  }
1070
- }), "getComponent", {
1071
- value: function value() {
1072
- var context = this;
1073
- if (COMPONENT in context) {
1074
- return function () {
1075
- return context[COMPONENT].apply(context, arguments);
1076
- };
1077
- }
1078
- return function () {
1079
- console.log('%s function not defined', COMPONENT);
1080
- };
1081
- }
1082
- }), "getBlocks", {
1094
+ }), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, "getBlocks", {
1083
1095
  value: function value() {
1084
1096
  return this[BLOCKS];
1085
1097
  }
@@ -1087,7 +1099,7 @@ var createScope = function createScope(config, methods) {
1087
1099
  value: function value(_value) {
1088
1100
  this[EXTEND] = _value;
1089
1101
  }
1090
- }), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, "getExtend", {
1102
+ }), "getExtend", {
1091
1103
  value: function value() {
1092
1104
  return this[EXTEND];
1093
1105
  }
@@ -1123,30 +1135,13 @@ var createScope = function createScope(config, methods) {
1123
1135
  var buffer = this.getBuffer();
1124
1136
  var context = this;
1125
1137
  return function () {
1126
- buffer.backup();
1127
1138
  if (isFunction(callback)) {
1128
- callback.apply(context, arguments);
1139
+ buffer.backup();
1140
+ buffer(callback.apply(context, arguments));
1141
+ return buffer.restore();
1129
1142
  }
1130
- return buffer.restore();
1131
1143
  };
1132
1144
  }
1133
- }), "get", {
1134
- value: function value(name, defaults) {
1135
- var path = getPath(this, name, true);
1136
- var result = path.shift();
1137
- var prop = path.pop();
1138
- return hasProp(result, prop) ? result[prop] : defaults;
1139
- }
1140
- }), "set", {
1141
- value: function value(name, _value2) {
1142
- var path = getPath(this, name, false);
1143
- var result = path.shift();
1144
- var prop = path.pop();
1145
- if (this.getExtend() && hasProp(result, prop)) {
1146
- return result[prop];
1147
- }
1148
- return result[prop] = _value2;
1149
- }
1150
1145
  }), "macro", {
1151
1146
  value: function value(name, callback) {
1152
1147
  var list = this.getMacro();
@@ -1199,14 +1194,11 @@ var createScope = function createScope(config, methods) {
1199
1194
  var promise = this.render(path, params);
1200
1195
  this.echo(promise);
1201
1196
  }
1202
- }), "promiseResolve", {
1203
- value: function value(_value3, callback) {
1204
- return Promise.resolve(isFunction(_value3) ? this.fn(_value3)() : _value3).then(callback.bind(this));
1205
- }
1206
1197
  }), "use", {
1207
1198
  value: function value(path, namespace) {
1208
- this.echo(this.promiseResolve(this.require(path), function (exports) {
1209
- var list = this.getMacro();
1199
+ var _this2 = this;
1200
+ this.echo(Promise.resolve(this.require(path)).then(function (exports) {
1201
+ var list = _this2.getMacro();
1210
1202
  each(exports, function (macro, name) {
1211
1203
  list[[namespace, name].join('.')] = macro;
1212
1204
  });
@@ -1214,9 +1206,24 @@ var createScope = function createScope(config, methods) {
1214
1206
  }
1215
1207
  }), "async", {
1216
1208
  value: function value(promise, callback) {
1217
- this.echo(this.promiseResolve(promise, function (data) {
1218
- return this.fn(callback)(data);
1219
- }));
1209
+ this.echo(Promise.resolve(promise).then(callback));
1210
+ }
1211
+ }), "get", {
1212
+ value: function value(name, defaults) {
1213
+ var path = getPath(this, name, true);
1214
+ var result = path.shift();
1215
+ var prop = path.pop();
1216
+ return hasProp(result, prop) ? result[prop] : defaults;
1217
+ }
1218
+ }), "set", {
1219
+ value: function value(name, _value2) {
1220
+ var path = getPath(this, name, false);
1221
+ var result = path.shift();
1222
+ var prop = path.pop();
1223
+ if (this.getExtend() && hasProp(result, prop)) {
1224
+ return result[prop];
1225
+ }
1226
+ return result[prop] = _value2;
1220
1227
  }
1221
1228
  }), "each", {
1222
1229
  value: function value(object, callback) {
@@ -1224,17 +1231,19 @@ var createScope = function createScope(config, methods) {
1224
1231
  object = this.get(object, []);
1225
1232
  }
1226
1233
  each(object, callback);
1227
- }
1228
- }), "element", {
1229
- value: function value(tag, attr, content) {
1230
- return element(tag, attr, content);
1231
- }
1234
+ },
1235
+ writable: true
1232
1236
  }), "el", {
1233
1237
  value: function value(tag, attr, content) {
1234
- this.echo(this.promiseResolve(content, function (content) {
1235
- return this.element(tag, attr, content);
1238
+ content = isFunction(content) ? this.fn(content)() : content;
1239
+ this.echo(Promise.resolve(content).then(function (content) {
1240
+ return element(tag, attr, content);
1236
1241
  }));
1237
- }
1242
+ },
1243
+ writable: true
1244
+ }), _defineProperty(_Object$definePropert, "ui", {
1245
+ value: function value(layout) {},
1246
+ writable: true
1238
1247
  })));
1239
1248
  return ContextScope;
1240
1249
  };
@@ -1243,7 +1252,6 @@ var Context = /*#__PURE__*/function () {
1243
1252
  function Context(config, methods) {
1244
1253
  _classCallCheck(this, Context);
1245
1254
  _classPrivateFieldInitSpec(this, _scope, void 0);
1246
- bindContext(this, ['create', 'helpers', 'configure']);
1247
1255
  this.configure(config, methods);
1248
1256
  }
1249
1257
  return _createClass(Context, [{
@@ -1254,7 +1262,7 @@ var Context = /*#__PURE__*/function () {
1254
1262
  }, {
1255
1263
  key: "configure",
1256
1264
  value: function configure(config, methods) {
1257
- _classPrivateFieldSet2(_scope, this, createScope(config, methods));
1265
+ _classPrivateFieldSet2(_scope, this, createContextScope(config, methods));
1258
1266
  }
1259
1267
  }, {
1260
1268
  key: "helpers",
@@ -1287,10 +1295,10 @@ var EJS = /*#__PURE__*/function () {
1287
1295
  _classPrivateFieldSet2(_cache, this, new Cache(_classPrivateFieldGet2(_config, this)));
1288
1296
  _classPrivateFieldSet2(_template, this, new Template(_classPrivateFieldGet2(_config, this), _classPrivateFieldGet2(_cache, this), _classPrivateFieldGet2(_compiler, this)));
1289
1297
  //
1290
- bindContext(this, ['configure', 'create', 'render', 'context', 'preload', 'compile', 'helpers']);
1298
+ bindContext(this, ['configure', 'create', 'render', 'require', 'context', 'preload', 'compile', 'helpers']);
1291
1299
  //
1292
1300
  this.helpers({
1293
- require: _assertClassBrand(_EJS_brand, this, _require),
1301
+ require: this.require,
1294
1302
  render: this.render
1295
1303
  });
1296
1304
  }
@@ -1345,163 +1353,21 @@ var EJS = /*#__PURE__*/function () {
1345
1353
  value: function create(options) {
1346
1354
  return new this.constructor(options);
1347
1355
  }
1356
+ }, {
1357
+ key: "require",
1358
+ value: function require(name) {
1359
+ var filepath = ext(name, _classPrivateFieldGet2(_config, this).extension);
1360
+ var scope = this.context({});
1361
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, filepath, scope).then(function () {
1362
+ return scope.getMacro();
1363
+ });
1364
+ }
1348
1365
  }]);
1349
1366
  }();
1350
-
1351
- // export function EJS2(options) {
1352
- // const self = {
1353
- // config: {},
1354
- // helpers: {},
1355
- // /**
1356
- // * @type {Context}
1357
- // */
1358
- // context: null,
1359
- // /**
1360
- // * @type {Compiler}
1361
- // */
1362
- // compiler: null,
1363
- // /**
1364
- // * @type {Template}
1365
- // */
1366
- // template: null,
1367
- // /**
1368
- // * @type {Cache}
1369
- // */
1370
- // cache: null,
1371
- // }
1372
- // /**
1373
- // *
1374
- // */
1375
- // configSchema(self.config, options || {})
1376
- // self.context = useContext(self.config, self.helpers)
1377
- // self.compiler = useCompiler(self.config)
1378
- // self.cache = useCache(self.config)
1379
- // self.template = useTemplate(self.config, self.cache, self.compiler)
1380
- // /**
1381
- // *
1382
- // * @param {string} path
1383
- // * @param {ContextScope} scope
1384
- // * @return {Promise<string>}
1385
- // */
1386
- // const output = (path, scope) => {
1387
- // const { globalHelpers } = self.config
1388
- // const params = [
1389
- // scope,
1390
- // scope.getComponent(),
1391
- // scope.getBuffer(),
1392
- // safeValue,
1393
- // ].concat(
1394
- // globalHelpers
1395
- // .filter((name) => isFunction(scope[name]))
1396
- // .map((name) => scope[name].bind(scope))
1397
- // )
1398
- // return self.template
1399
- // .get(path)
1400
- // .then((callback) => callback.apply(scope, params))
1401
- // }
1402
- // /**
1403
- // *
1404
- // * @param name
1405
- // * @return {Promise<string>}
1406
- // */
1407
- // const require = (name) => {
1408
- // const filepath = ext(name, self.config.extension)
1409
- // const scope = context({})
1410
- // return output(filepath, scope).then(() => scope.getMacro())
1411
- // }
1412
- // /**
1413
- // *
1414
- // * @param {string} name
1415
- // * @param {{}} [data]
1416
- // * @return {Promise<string>}
1417
- // */
1418
- // const render = (name, data) => {
1419
- // const filepath = ext(name, self.config.extension)
1420
- // const scope = context(data)
1421
- // return output(filepath, scope).then((content) => {
1422
- // if (scope.getExtend()) {
1423
- // scope.setExtend(false)
1424
- // const layout = scope.getLayout()
1425
- // const data = scope.clone()
1426
- // return render(layout, data)
1427
- // }
1428
- // return content
1429
- // })
1430
- // }
1431
- // /**
1432
- // *
1433
- // * @param options
1434
- // * @return {{}}
1435
- // */
1436
- // const configure = (options = {}) => {
1437
- // configSchema(self.config, options || {})
1438
- // self.context.configure(self.config, self.helpers)
1439
- // self.compiler.configure(self.config)
1440
- // self.cache.configure(self.config)
1441
- // self.template.configure(self.config)
1442
- // return self.config
1443
- // }
1444
- // /**
1445
- // *
1446
- // * @param methods
1447
- // */
1448
- // const helpers = (methods) => {
1449
- // self.context.helpers(extend(self.helpers, methods))
1450
- // }
1451
- // /**
1452
- // *
1453
- // * @param list
1454
- // * @return {*}
1455
- // */
1456
- // const preload = (list) => {
1457
- // return self.cache.load(list || {})
1458
- // }
1459
- // /**
1460
- // *
1461
- // * @param options
1462
- // * @return {any}
1463
- // */
1464
- // const create = (options) => {
1465
- // return EJS(options)
1466
- // }
1467
- // /**
1468
- // *
1469
- // * @param content
1470
- // * @param path
1471
- // * @return {Function}
1472
- // */
1473
- // const compile = (content, path) => {
1474
- // return self.compiler.compile(content, path)
1475
- // }
1476
- // /**
1477
- // *
1478
- // * @param data
1479
- // * @return {ContextScope}
1480
- // */
1481
- // const context = (data = {}) => {
1482
- // return self.context.create(data)
1483
- // }
1484
- // /**
1485
- // *
1486
- // */
1487
- // helpers({ require, render })
1488
- // /**
1489
- // *
1490
- // */
1491
- // return {
1492
- // configure,
1493
- // helpers,
1494
- // preload,
1495
- // context,
1496
- // compile,
1497
- // create,
1498
- // render,
1499
- // }
1500
- // }
1501
1367
  function _output(path, scope) {
1502
1368
  var _classPrivateFieldGet2$1 = _classPrivateFieldGet2(_config, this),
1503
1369
  globalHelpers = _classPrivateFieldGet2$1.globalHelpers;
1504
- var params = [scope, scope.getComponent(), scope.getBuffer(), safeValue].concat(globalHelpers.filter(function (name) {
1370
+ var params = [scope, scope.getBuffer(), scope.useSafeValue, scope.useComponent, scope.useElement].concat(globalHelpers.filter(function (name) {
1505
1371
  return isFunction(scope[name]);
1506
1372
  }).map(function (name) {
1507
1373
  return scope[name].bind(scope);
@@ -1510,13 +1376,6 @@ function _output(path, scope) {
1510
1376
  return callback.apply(scope, params);
1511
1377
  });
1512
1378
  }
1513
- function _require(name) {
1514
- var filepath = ext(name, _classPrivateFieldGet2(_config, this).extension);
1515
- var scope = this.context({});
1516
- return _assertClassBrand(_EJS_brand, this, _output).call(this, filepath, scope).then(function () {
1517
- return scope.getMacro();
1518
- });
1519
- }
1520
1379
 
1521
1380
  var templates = {};
1522
1381
  var getOrigin = function getOrigin(url, secure) {