@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.
package/dist/cjs/index.js CHANGED
@@ -336,6 +336,7 @@ defaults.globalHelpers = [];
336
336
  defaults.vars = {
337
337
  SCOPE: 'ejs',
338
338
  COMPONENT: 'ui',
339
+ ELEMENT: 'el',
339
340
  EXTEND: '$$e',
340
341
  BUFFER: '$$a',
341
342
  LAYOUT: '$$l',
@@ -372,7 +373,6 @@ var Cache = /*#__PURE__*/function () {
372
373
  _classCallCheck(this, Cache);
373
374
  _classPrivateFieldInitSpec(this, _enabled, true);
374
375
  _classPrivateFieldInitSpec(this, _list, {});
375
- bindContext(this, ['configure']);
376
376
  this.configure(config);
377
377
  }
378
378
  return _createClass(Cache, [{
@@ -454,7 +454,6 @@ var Compiler = /*#__PURE__*/function () {
454
454
  return "')\n".concat(value.trim(), "\n").concat(this.BUFFER, "('");
455
455
  }
456
456
  }]);
457
- bindContext(this, ['configure', 'compile']);
458
457
  this.configure(config);
459
458
  }
460
459
  return _createClass(Compiler, [{
@@ -489,7 +488,8 @@ var Compiler = /*#__PURE__*/function () {
489
488
  SCOPE = _classPrivateFieldGet2$1.SCOPE,
490
489
  SAFE = _classPrivateFieldGet2$1.SAFE,
491
490
  BUFFER = _classPrivateFieldGet2$1.BUFFER,
492
- COMPONENT = _classPrivateFieldGet2$1.COMPONENT;
491
+ COMPONENT = _classPrivateFieldGet2$1.COMPONENT,
492
+ ELEMENT = _classPrivateFieldGet2$1.ELEMENT;
493
493
  var GLOBALS = _classPrivateFieldGet2(_config$1, this).globalHelpers;
494
494
  if (_classPrivateFieldGet2(_config$1, this).rmWhitespace) {
495
495
  content = String(content).replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
@@ -512,7 +512,7 @@ var Compiler = /*#__PURE__*/function () {
512
512
  source = "".concat(BUFFER, ".start();").concat(source, "return ").concat(BUFFER, ".end();");
513
513
  source += "\n//# sourceURL=".concat(path);
514
514
  var result = null;
515
- var params = [SCOPE, COMPONENT, BUFFER, SAFE].concat(GLOBALS);
515
+ var params = [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT].concat(GLOBALS);
516
516
  try {
517
517
  result = Function.apply(null, params.concat(source));
518
518
  result.source = "(function(".concat(params.join(','), "){\n").concat(source, "\n});");
@@ -543,7 +543,6 @@ var Template = /*#__PURE__*/function () {
543
543
  assertInstanceOf(compiler, Compiler);
544
544
  _classPrivateFieldSet2(_cache$1, this, cache);
545
545
  _classPrivateFieldSet2(_compiler$1, this, compiler);
546
- bindContext(this, ['configure', 'get']);
547
546
  this.configure(config);
548
547
  }
549
548
  return _createClass(Template, [{
@@ -705,7 +704,7 @@ function createBuffer() {
705
704
  return buffer;
706
705
  }
707
706
 
708
- var createScope = function createScope(config, methods) {
707
+ var createContextScope = function createContextScope(config, methods) {
709
708
  var _Object$definePropert;
710
709
  var _config$vars = config.vars,
711
710
  BLOCKS = _config$vars.BLOCKS,
@@ -713,9 +712,10 @@ var createScope = function createScope(config, methods) {
713
712
  EXTEND = _config$vars.EXTEND,
714
713
  LAYOUT = _config$vars.LAYOUT,
715
714
  BUFFER = _config$vars.BUFFER,
716
- COMPONENT = _config$vars.COMPONENT,
717
715
  SAFE = _config$vars.SAFE,
718
- SCOPE = _config$vars.SCOPE;
716
+ SCOPE = _config$vars.SCOPE,
717
+ COMPONENT = _config$vars.COMPONENT,
718
+ ELEMENT = _config$vars.ELEMENT;
719
719
  /**
720
720
  * @name ContextScope
721
721
  * @param data
@@ -724,7 +724,7 @@ var createScope = function createScope(config, methods) {
724
724
  function ContextScope(data) {
725
725
  this[BLOCKS] = {};
726
726
  this[MACRO] = {};
727
- Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT]));
727
+ Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
728
728
  }
729
729
  Object.assign(ContextScope.prototype, methods);
730
730
  Object.defineProperties(ContextScope.prototype, (_Object$definePropert = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, BUFFER, {
@@ -741,6 +741,30 @@ var createScope = function createScope(config, methods) {
741
741
  }), EXTEND, {
742
742
  value: false,
743
743
  writable: true
744
+ }), "useSafeValue", {
745
+ get: function get() {
746
+ return safeValue;
747
+ }
748
+ }), "useComponent", {
749
+ get: function get() {
750
+ if (isFunction(this[COMPONENT])) {
751
+ return this[COMPONENT].bind(this);
752
+ } else {
753
+ return function () {
754
+ throw new Error("".concat(COMPONENT, " must be a function"));
755
+ };
756
+ }
757
+ }
758
+ }), "useElement", {
759
+ get: function get() {
760
+ if (isFunction(this[ELEMENT])) {
761
+ return this[ELEMENT].bind(this);
762
+ } else {
763
+ return function () {
764
+ throw new Error("".concat(ELEMENT, " must be a function"));
765
+ };
766
+ }
767
+ }
744
768
  }), "getMacro", {
745
769
  value: function value() {
746
770
  return this[MACRO];
@@ -749,19 +773,7 @@ var createScope = function createScope(config, methods) {
749
773
  value: function value() {
750
774
  return this[BUFFER];
751
775
  }
752
- }), "getComponent", {
753
- value: function value() {
754
- var context = this;
755
- if (COMPONENT in context) {
756
- return function () {
757
- return context[COMPONENT].apply(context, arguments);
758
- };
759
- }
760
- return function () {
761
- console.log('%s function not defined', COMPONENT);
762
- };
763
- }
764
- }), "getBlocks", {
776
+ }), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, "getBlocks", {
765
777
  value: function value() {
766
778
  return this[BLOCKS];
767
779
  }
@@ -769,7 +781,7 @@ var createScope = function createScope(config, methods) {
769
781
  value: function value(_value) {
770
782
  this[EXTEND] = _value;
771
783
  }
772
- }), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, "getExtend", {
784
+ }), "getExtend", {
773
785
  value: function value() {
774
786
  return this[EXTEND];
775
787
  }
@@ -805,30 +817,13 @@ var createScope = function createScope(config, methods) {
805
817
  var buffer = this.getBuffer();
806
818
  var context = this;
807
819
  return function () {
808
- buffer.backup();
809
820
  if (isFunction(callback)) {
810
- callback.apply(context, arguments);
821
+ buffer.backup();
822
+ buffer(callback.apply(context, arguments));
823
+ return buffer.restore();
811
824
  }
812
- return buffer.restore();
813
825
  };
814
826
  }
815
- }), "get", {
816
- value: function value(name, defaults) {
817
- var path = getPath(this, name, true);
818
- var result = path.shift();
819
- var prop = path.pop();
820
- return hasProp(result, prop) ? result[prop] : defaults;
821
- }
822
- }), "set", {
823
- value: function value(name, _value2) {
824
- var path = getPath(this, name, false);
825
- var result = path.shift();
826
- var prop = path.pop();
827
- if (this.getExtend() && hasProp(result, prop)) {
828
- return result[prop];
829
- }
830
- return result[prop] = _value2;
831
- }
832
827
  }), "macro", {
833
828
  value: function value(name, callback) {
834
829
  var list = this.getMacro();
@@ -881,14 +876,11 @@ var createScope = function createScope(config, methods) {
881
876
  var promise = this.render(path, params);
882
877
  this.echo(promise);
883
878
  }
884
- }), "promiseResolve", {
885
- value: function value(_value3, callback) {
886
- return Promise.resolve(isFunction(_value3) ? this.fn(_value3)() : _value3).then(callback.bind(this));
887
- }
888
879
  }), "use", {
889
880
  value: function value(path, namespace) {
890
- this.echo(this.promiseResolve(this.require(path), function (exports) {
891
- var list = this.getMacro();
881
+ var _this2 = this;
882
+ this.echo(Promise.resolve(this.require(path)).then(function (exports) {
883
+ var list = _this2.getMacro();
892
884
  each(exports, function (macro, name) {
893
885
  list[[namespace, name].join('.')] = macro;
894
886
  });
@@ -896,9 +888,24 @@ var createScope = function createScope(config, methods) {
896
888
  }
897
889
  }), "async", {
898
890
  value: function value(promise, callback) {
899
- this.echo(this.promiseResolve(promise, function (data) {
900
- return this.fn(callback)(data);
901
- }));
891
+ this.echo(Promise.resolve(promise).then(callback));
892
+ }
893
+ }), "get", {
894
+ value: function value(name, defaults) {
895
+ var path = getPath(this, name, true);
896
+ var result = path.shift();
897
+ var prop = path.pop();
898
+ return hasProp(result, prop) ? result[prop] : defaults;
899
+ }
900
+ }), "set", {
901
+ value: function value(name, _value2) {
902
+ var path = getPath(this, name, false);
903
+ var result = path.shift();
904
+ var prop = path.pop();
905
+ if (this.getExtend() && hasProp(result, prop)) {
906
+ return result[prop];
907
+ }
908
+ return result[prop] = _value2;
902
909
  }
903
910
  }), "each", {
904
911
  value: function value(object, callback) {
@@ -906,17 +913,19 @@ var createScope = function createScope(config, methods) {
906
913
  object = this.get(object, []);
907
914
  }
908
915
  each(object, callback);
909
- }
910
- }), "element", {
911
- value: function value(tag, attr, content) {
912
- return element(tag, attr, content);
913
- }
916
+ },
917
+ writable: true
914
918
  }), "el", {
915
919
  value: function value(tag, attr, content) {
916
- this.echo(this.promiseResolve(content, function (content) {
917
- return this.element(tag, attr, content);
920
+ content = isFunction(content) ? this.fn(content)() : content;
921
+ this.echo(Promise.resolve(content).then(function (content) {
922
+ return element(tag, attr, content);
918
923
  }));
919
- }
924
+ },
925
+ writable: true
926
+ }), _defineProperty(_Object$definePropert, "ui", {
927
+ value: function value(layout) {},
928
+ writable: true
920
929
  })));
921
930
  return ContextScope;
922
931
  };
@@ -925,7 +934,6 @@ var Context = /*#__PURE__*/function () {
925
934
  function Context(config, methods) {
926
935
  _classCallCheck(this, Context);
927
936
  _classPrivateFieldInitSpec(this, _scope, void 0);
928
- bindContext(this, ['create', 'helpers', 'configure']);
929
937
  this.configure(config, methods);
930
938
  }
931
939
  return _createClass(Context, [{
@@ -936,7 +944,7 @@ var Context = /*#__PURE__*/function () {
936
944
  }, {
937
945
  key: "configure",
938
946
  value: function configure(config, methods) {
939
- _classPrivateFieldSet2(_scope, this, createScope(config, methods));
947
+ _classPrivateFieldSet2(_scope, this, createContextScope(config, methods));
940
948
  }
941
949
  }, {
942
950
  key: "helpers",
@@ -969,10 +977,10 @@ var EJS = /*#__PURE__*/function () {
969
977
  _classPrivateFieldSet2(_cache, this, new Cache(_classPrivateFieldGet2(_config, this)));
970
978
  _classPrivateFieldSet2(_template, this, new Template(_classPrivateFieldGet2(_config, this), _classPrivateFieldGet2(_cache, this), _classPrivateFieldGet2(_compiler, this)));
971
979
  //
972
- bindContext(this, ['configure', 'create', 'render', 'context', 'preload', 'compile', 'helpers']);
980
+ bindContext(this, ['configure', 'create', 'render', 'require', 'context', 'preload', 'compile', 'helpers']);
973
981
  //
974
982
  this.helpers({
975
- require: _assertClassBrand(_EJS_brand, this, _require),
983
+ require: this.require,
976
984
  render: this.render
977
985
  });
978
986
  }
@@ -1027,163 +1035,21 @@ var EJS = /*#__PURE__*/function () {
1027
1035
  value: function create(options) {
1028
1036
  return new this.constructor(options);
1029
1037
  }
1038
+ }, {
1039
+ key: "require",
1040
+ value: function require(name) {
1041
+ var filepath = ext(name, _classPrivateFieldGet2(_config, this).extension);
1042
+ var scope = this.context({});
1043
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, filepath, scope).then(function () {
1044
+ return scope.getMacro();
1045
+ });
1046
+ }
1030
1047
  }]);
1031
1048
  }();
1032
-
1033
- // export function EJS2(options) {
1034
- // const self = {
1035
- // config: {},
1036
- // helpers: {},
1037
- // /**
1038
- // * @type {Context}
1039
- // */
1040
- // context: null,
1041
- // /**
1042
- // * @type {Compiler}
1043
- // */
1044
- // compiler: null,
1045
- // /**
1046
- // * @type {Template}
1047
- // */
1048
- // template: null,
1049
- // /**
1050
- // * @type {Cache}
1051
- // */
1052
- // cache: null,
1053
- // }
1054
- // /**
1055
- // *
1056
- // */
1057
- // configSchema(self.config, options || {})
1058
- // self.context = useContext(self.config, self.helpers)
1059
- // self.compiler = useCompiler(self.config)
1060
- // self.cache = useCache(self.config)
1061
- // self.template = useTemplate(self.config, self.cache, self.compiler)
1062
- // /**
1063
- // *
1064
- // * @param {string} path
1065
- // * @param {ContextScope} scope
1066
- // * @return {Promise<string>}
1067
- // */
1068
- // const output = (path, scope) => {
1069
- // const { globalHelpers } = self.config
1070
- // const params = [
1071
- // scope,
1072
- // scope.getComponent(),
1073
- // scope.getBuffer(),
1074
- // safeValue,
1075
- // ].concat(
1076
- // globalHelpers
1077
- // .filter((name) => isFunction(scope[name]))
1078
- // .map((name) => scope[name].bind(scope))
1079
- // )
1080
- // return self.template
1081
- // .get(path)
1082
- // .then((callback) => callback.apply(scope, params))
1083
- // }
1084
- // /**
1085
- // *
1086
- // * @param name
1087
- // * @return {Promise<string>}
1088
- // */
1089
- // const require = (name) => {
1090
- // const filepath = ext(name, self.config.extension)
1091
- // const scope = context({})
1092
- // return output(filepath, scope).then(() => scope.getMacro())
1093
- // }
1094
- // /**
1095
- // *
1096
- // * @param {string} name
1097
- // * @param {{}} [data]
1098
- // * @return {Promise<string>}
1099
- // */
1100
- // const render = (name, data) => {
1101
- // const filepath = ext(name, self.config.extension)
1102
- // const scope = context(data)
1103
- // return output(filepath, scope).then((content) => {
1104
- // if (scope.getExtend()) {
1105
- // scope.setExtend(false)
1106
- // const layout = scope.getLayout()
1107
- // const data = scope.clone()
1108
- // return render(layout, data)
1109
- // }
1110
- // return content
1111
- // })
1112
- // }
1113
- // /**
1114
- // *
1115
- // * @param options
1116
- // * @return {{}}
1117
- // */
1118
- // const configure = (options = {}) => {
1119
- // configSchema(self.config, options || {})
1120
- // self.context.configure(self.config, self.helpers)
1121
- // self.compiler.configure(self.config)
1122
- // self.cache.configure(self.config)
1123
- // self.template.configure(self.config)
1124
- // return self.config
1125
- // }
1126
- // /**
1127
- // *
1128
- // * @param methods
1129
- // */
1130
- // const helpers = (methods) => {
1131
- // self.context.helpers(extend(self.helpers, methods))
1132
- // }
1133
- // /**
1134
- // *
1135
- // * @param list
1136
- // * @return {*}
1137
- // */
1138
- // const preload = (list) => {
1139
- // return self.cache.load(list || {})
1140
- // }
1141
- // /**
1142
- // *
1143
- // * @param options
1144
- // * @return {any}
1145
- // */
1146
- // const create = (options) => {
1147
- // return EJS(options)
1148
- // }
1149
- // /**
1150
- // *
1151
- // * @param content
1152
- // * @param path
1153
- // * @return {Function}
1154
- // */
1155
- // const compile = (content, path) => {
1156
- // return self.compiler.compile(content, path)
1157
- // }
1158
- // /**
1159
- // *
1160
- // * @param data
1161
- // * @return {ContextScope}
1162
- // */
1163
- // const context = (data = {}) => {
1164
- // return self.context.create(data)
1165
- // }
1166
- // /**
1167
- // *
1168
- // */
1169
- // helpers({ require, render })
1170
- // /**
1171
- // *
1172
- // */
1173
- // return {
1174
- // configure,
1175
- // helpers,
1176
- // preload,
1177
- // context,
1178
- // compile,
1179
- // create,
1180
- // render,
1181
- // }
1182
- // }
1183
1049
  function _output(path, scope) {
1184
1050
  var _classPrivateFieldGet2$1 = _classPrivateFieldGet2(_config, this),
1185
1051
  globalHelpers = _classPrivateFieldGet2$1.globalHelpers;
1186
- var params = [scope, scope.getComponent(), scope.getBuffer(), safeValue].concat(globalHelpers.filter(function (name) {
1052
+ var params = [scope, scope.getBuffer(), scope.useSafeValue, scope.useComponent, scope.useElement].concat(globalHelpers.filter(function (name) {
1187
1053
  return isFunction(scope[name]);
1188
1054
  }).map(function (name) {
1189
1055
  return scope[name].bind(scope);
@@ -1192,13 +1058,6 @@ function _output(path, scope) {
1192
1058
  return callback.apply(scope, params);
1193
1059
  });
1194
1060
  }
1195
- function _require(name) {
1196
- var filepath = ext(name, _classPrivateFieldGet2(_config, this).extension);
1197
- var scope = this.context({});
1198
- return _assertClassBrand(_EJS_brand, this, _output).call(this, filepath, scope).then(function () {
1199
- return scope.getMacro();
1200
- });
1201
- }
1202
1061
 
1203
1062
  function readFile(path, template) {
1204
1063
  return new Promise(function (resolve, reject) {