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