@kosatyi/ejs 0.0.98 → 0.0.100

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/esm/index.js CHANGED
@@ -218,6 +218,7 @@ defaults.globalHelpers = [];
218
218
  defaults.vars = {
219
219
  SCOPE: 'ejs',
220
220
  COMPONENT: 'ui',
221
+ ELEMENT: 'el',
221
222
  EXTEND: '$$e',
222
223
  BUFFER: '$$a',
223
224
  LAYOUT: '$$l',
@@ -282,7 +283,6 @@ class Cache {
282
283
  #enabled = true
283
284
  #list = {}
284
285
  constructor(config) {
285
- bindContext(this, ['configure']);
286
286
  this.configure(config);
287
287
  }
288
288
  load(data) {
@@ -349,7 +349,6 @@ class Compiler {
349
349
  },
350
350
  ]
351
351
  constructor(config) {
352
- bindContext(this, ['configure', 'compile']);
353
352
  this.configure(config);
354
353
  }
355
354
  configure(config) {
@@ -392,7 +391,7 @@ class Compiler {
392
391
  );
393
392
  }
394
393
  compile(content, path) {
395
- const { SCOPE, SAFE, BUFFER, COMPONENT } = this.#config.vars;
394
+ const { SCOPE, SAFE, BUFFER, COMPONENT, ELEMENT } = this.#config.vars;
396
395
  const GLOBALS = this.#config.globalHelpers;
397
396
  if (this.#config.rmWhitespace) {
398
397
  content = String(content)
@@ -419,7 +418,7 @@ class Compiler {
419
418
  source = `${BUFFER}.start();${source}return ${BUFFER}.end();`;
420
419
  source += `\n//# sourceURL=${path}`;
421
420
  let result = null;
422
- let params = [SCOPE, COMPONENT, BUFFER, SAFE].concat(GLOBALS);
421
+ let params = [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT].concat(GLOBALS);
423
422
  try {
424
423
  result = Function.apply(null, params.concat(source));
425
424
  result.source = `(function(${params.join(',')}){\n${source}\n});`;
@@ -442,7 +441,6 @@ class Template {
442
441
  assertInstanceOf(compiler, Compiler);
443
442
  this.#cache = cache;
444
443
  this.#compiler = compiler;
445
- bindContext(this, ['configure', 'get']);
446
444
  this.configure(config);
447
445
  }
448
446
  #resolve(path) {
@@ -588,9 +586,18 @@ function createBuffer() {
588
586
  return buffer
589
587
  }
590
588
 
591
- const createScope = (config, methods) => {
592
- const { BLOCKS, MACRO, EXTEND, LAYOUT, BUFFER, COMPONENT, SAFE, SCOPE } =
593
- config.vars;
589
+ const createContextScope = (config, methods) => {
590
+ const {
591
+ BLOCKS,
592
+ MACRO,
593
+ EXTEND,
594
+ LAYOUT,
595
+ BUFFER,
596
+ SAFE,
597
+ SCOPE,
598
+ COMPONENT,
599
+ ELEMENT,
600
+ } = config.vars;
594
601
  /**
595
602
  * @name ContextScope
596
603
  * @param data
@@ -599,29 +606,59 @@ const createScope = (config, methods) => {
599
606
  function ContextScope(data) {
600
607
  this[BLOCKS] = {};
601
608
  this[MACRO] = {};
602
- Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT]));
609
+ Object.assign(
610
+ this,
611
+ omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT])
612
+ );
603
613
  }
604
-
605
614
  Object.assign(ContextScope.prototype, methods);
615
+ Object.defineProperty(ContextScope.prototype, BUFFER, {
616
+ value: createBuffer(),
617
+ });
618
+ Object.defineProperty(ContextScope.prototype, BLOCKS, {
619
+ value: {},
620
+ writable: true,
621
+ });
622
+ Object.defineProperty(ContextScope.prototype, MACRO, {
623
+ value: {},
624
+ writable: true,
625
+ });
626
+ Object.defineProperty(ContextScope.prototype, LAYOUT, {
627
+ value: false,
628
+ writable: true,
629
+ });
630
+ Object.defineProperty(ContextScope.prototype, EXTEND, {
631
+ value: false,
632
+ writable: true,
633
+ });
606
634
  Object.defineProperties(ContextScope.prototype, {
607
- [BUFFER]: {
608
- value: createBuffer(),
609
- },
610
- [BLOCKS]: {
611
- value: {},
612
- writable: true,
613
- },
614
- [MACRO]: {
615
- value: {},
616
- writable: true,
635
+ /** @type {function} */
636
+ useSafeValue: {
637
+ get: () => safeValue,
617
638
  },
618
- [LAYOUT]: {
619
- value: false,
620
- writable: true,
639
+ /** @type {function} */
640
+ useComponent: {
641
+ get() {
642
+ if (isFunction(this[COMPONENT])) {
643
+ return this[COMPONENT].bind(this)
644
+ } else {
645
+ return () => {
646
+ throw new Error(`${COMPONENT} must be a function`)
647
+ }
648
+ }
649
+ },
621
650
  },
622
- [EXTEND]: {
623
- value: false,
624
- writable: true,
651
+ /** @type {function} */
652
+ useElement: {
653
+ get() {
654
+ if (isFunction(this[ELEMENT])) {
655
+ return this[ELEMENT].bind(this)
656
+ } else {
657
+ return () => {
658
+ throw new Error(`${ELEMENT} must be a function`)
659
+ }
660
+ }
661
+ },
625
662
  },
626
663
  /** @type {()=>this[MACRO]} */
627
664
  getMacro: {
@@ -636,20 +673,6 @@ const createScope = (config, methods) => {
636
673
  },
637
674
  },
638
675
  /** @type {function} */
639
- getComponent: {
640
- value() {
641
- const context = this;
642
- if (COMPONENT in context) {
643
- return function () {
644
- return context[COMPONENT].apply(context, arguments)
645
- }
646
- }
647
- return function () {
648
- console.log('%s function not defined', COMPONENT);
649
- }
650
- },
651
- },
652
- /** @type {function} */
653
676
  getBlocks: {
654
677
  value() {
655
678
  return this[BLOCKS]
@@ -710,33 +733,12 @@ const createScope = (config, methods) => {
710
733
  const buffer = this.getBuffer();
711
734
  const context = this;
712
735
  return function () {
713
- buffer.backup();
714
736
  if (isFunction(callback)) {
715
- callback.apply(context, arguments);
737
+ buffer.backup();
738
+ buffer(callback.apply(context, arguments));
739
+ return buffer.restore()
716
740
  }
717
- return buffer.restore()
718
- }
719
- },
720
- },
721
- /** @type {function} */
722
- get: {
723
- value(name, defaults) {
724
- const path = getPath(this, name, true);
725
- const result = path.shift();
726
- const prop = path.pop();
727
- return hasProp(result, prop) ? result[prop] : defaults
728
- },
729
- },
730
- /** @type {function} */
731
- set: {
732
- value(name, value) {
733
- const path = getPath(this, name, false);
734
- const result = path.shift();
735
- const prop = path.pop();
736
- if (this.getExtend() && hasProp(result, prop)) {
737
- return result[prop]
738
741
  }
739
- return (result[prop] = value)
740
742
  },
741
743
  },
742
744
  /** @type {function} */
@@ -769,7 +771,7 @@ const createScope = (config, methods) => {
769
771
  blocks[name].push(this.fn(callback));
770
772
  if (this.getExtend()) return
771
773
  const list = Object.assign([], blocks[name]);
772
- const current = function () {
774
+ const current = () => {
773
775
  return list.shift()
774
776
  };
775
777
  const next = () => {
@@ -801,18 +803,10 @@ const createScope = (config, methods) => {
801
803
  },
802
804
  },
803
805
  /** @type {function} */
804
- promiseResolve: {
805
- value(value, callback) {
806
- return Promise.resolve(
807
- isFunction(value) ? this.fn(value)() : value
808
- ).then(callback.bind(this))
809
- },
810
- },
811
- /** @type {function} */
812
806
  use: {
813
807
  value(path, namespace) {
814
808
  this.echo(
815
- this.promiseResolve(this.require(path), function (exports) {
809
+ Promise.resolve(this.require(path)).then((exports) => {
816
810
  const list = this.getMacro();
817
811
  each(exports, function (macro, name) {
818
812
  list[[namespace, name].join('.')] = macro;
@@ -824,11 +818,28 @@ const createScope = (config, methods) => {
824
818
  /** @type {function} */
825
819
  async: {
826
820
  value(promise, callback) {
827
- this.echo(
828
- this.promiseResolve(promise, function (data) {
829
- return this.fn(callback)(data)
830
- })
831
- );
821
+ this.echo(Promise.resolve(promise).then(callback));
822
+ },
823
+ },
824
+ /** @type {function} */
825
+ get: {
826
+ value(name, defaults) {
827
+ const path = getPath(this, name, true);
828
+ const result = path.shift();
829
+ const prop = path.pop();
830
+ return hasProp(result, prop) ? result[prop] : defaults
831
+ },
832
+ },
833
+ /** @type {function} */
834
+ set: {
835
+ value(name, value) {
836
+ const path = getPath(this, name, false);
837
+ const result = path.shift();
838
+ const prop = path.pop();
839
+ if (this.getExtend() && hasProp(result, prop)) {
840
+ return result[prop]
841
+ }
842
+ return (result[prop] = value)
832
843
  },
833
844
  },
834
845
  /** @type {function} */
@@ -839,22 +850,24 @@ const createScope = (config, methods) => {
839
850
  }
840
851
  each(object, callback);
841
852
  },
842
- },
843
- /** @type {function} */
844
- element: {
845
- value(tag, attr, content) {
846
- return element(tag, attr, content)
847
- },
853
+ writable: true,
848
854
  },
849
855
  /** @type {function} */
850
856
  el: {
851
857
  value(tag, attr, content) {
858
+ content = isFunction(content) ? this.fn(content)() : content;
852
859
  this.echo(
853
- this.promiseResolve(content, function (content) {
854
- return this.element(tag, attr, content)
855
- })
860
+ Promise.resolve(content).then((content) =>
861
+ element(tag, attr, content)
862
+ )
856
863
  );
857
864
  },
865
+ writable: true,
866
+ },
867
+ /** @type {function} */
868
+ ui: {
869
+ value(layout) {},
870
+ writable: true,
858
871
  },
859
872
  });
860
873
  return ContextScope
@@ -862,20 +875,15 @@ const createScope = (config, methods) => {
862
875
 
863
876
  class Context {
864
877
  #scope
865
-
866
878
  constructor(config, methods) {
867
- bindContext(this, ['create', 'helpers', 'configure']);
868
879
  this.configure(config, methods);
869
880
  }
870
-
871
881
  create(data) {
872
882
  return new this.#scope(data)
873
883
  }
874
-
875
884
  configure(config, methods) {
876
- this.#scope = createScope(config, methods);
885
+ this.#scope = createContextScope(config, methods);
877
886
  }
878
-
879
887
  helpers(methods) {
880
888
  extend(this.#scope.prototype, methods || {});
881
889
  }
@@ -899,13 +907,14 @@ class EJS {
899
907
  'configure',
900
908
  'create',
901
909
  'render',
910
+ 'require',
902
911
  'context',
903
912
  'preload',
904
913
  'compile',
905
914
  'helpers',
906
915
  ]);
907
916
  //
908
- this.helpers({ require: this.#require, render: this.render });
917
+ this.helpers({ require: this.require, render: this.render });
909
918
  }
910
919
  configure(options) {
911
920
  configSchema(this.#config, options || {});
@@ -943,13 +952,19 @@ class EJS {
943
952
  create(options) {
944
953
  return new this.constructor(options)
945
954
  }
955
+ require(name) {
956
+ const filepath = ext(name, this.#config.extension);
957
+ const scope = this.context({});
958
+ return this.#output(filepath, scope).then(() => scope.getMacro())
959
+ }
946
960
  #output(path, scope) {
947
961
  const { globalHelpers } = this.#config;
948
962
  const params = [
949
963
  scope,
950
- scope.getComponent(),
951
964
  scope.getBuffer(),
952
- safeValue,
965
+ scope.useSafeValue,
966
+ scope.useComponent,
967
+ scope.useElement,
953
968
  ].concat(
954
969
  globalHelpers
955
970
  .filter((name) => isFunction(scope[name]))
@@ -959,164 +974,8 @@ class EJS {
959
974
  .get(path)
960
975
  .then((callback) => callback.apply(scope, params))
961
976
  }
962
- #require(name) {
963
- const filepath = ext(name, this.#config.extension);
964
- const scope = this.context({});
965
- return this.#output(filepath, scope).then(() => scope.getMacro())
966
- }
967
977
  }
968
978
 
969
- // export function EJS2(options) {
970
- // const self = {
971
- // config: {},
972
- // helpers: {},
973
- // /**
974
- // * @type {Context}
975
- // */
976
- // context: null,
977
- // /**
978
- // * @type {Compiler}
979
- // */
980
- // compiler: null,
981
- // /**
982
- // * @type {Template}
983
- // */
984
- // template: null,
985
- // /**
986
- // * @type {Cache}
987
- // */
988
- // cache: null,
989
- // }
990
- // /**
991
- // *
992
- // */
993
- // configSchema(self.config, options || {})
994
- // self.context = useContext(self.config, self.helpers)
995
- // self.compiler = useCompiler(self.config)
996
- // self.cache = useCache(self.config)
997
- // self.template = useTemplate(self.config, self.cache, self.compiler)
998
- // /**
999
- // *
1000
- // * @param {string} path
1001
- // * @param {ContextScope} scope
1002
- // * @return {Promise<string>}
1003
- // */
1004
- // const output = (path, scope) => {
1005
- // const { globalHelpers } = self.config
1006
- // const params = [
1007
- // scope,
1008
- // scope.getComponent(),
1009
- // scope.getBuffer(),
1010
- // safeValue,
1011
- // ].concat(
1012
- // globalHelpers
1013
- // .filter((name) => isFunction(scope[name]))
1014
- // .map((name) => scope[name].bind(scope))
1015
- // )
1016
- // return self.template
1017
- // .get(path)
1018
- // .then((callback) => callback.apply(scope, params))
1019
- // }
1020
- // /**
1021
- // *
1022
- // * @param name
1023
- // * @return {Promise<string>}
1024
- // */
1025
- // const require = (name) => {
1026
- // const filepath = ext(name, self.config.extension)
1027
- // const scope = context({})
1028
- // return output(filepath, scope).then(() => scope.getMacro())
1029
- // }
1030
- // /**
1031
- // *
1032
- // * @param {string} name
1033
- // * @param {{}} [data]
1034
- // * @return {Promise<string>}
1035
- // */
1036
- // const render = (name, data) => {
1037
- // const filepath = ext(name, self.config.extension)
1038
- // const scope = context(data)
1039
- // return output(filepath, scope).then((content) => {
1040
- // if (scope.getExtend()) {
1041
- // scope.setExtend(false)
1042
- // const layout = scope.getLayout()
1043
- // const data = scope.clone()
1044
- // return render(layout, data)
1045
- // }
1046
- // return content
1047
- // })
1048
- // }
1049
- // /**
1050
- // *
1051
- // * @param options
1052
- // * @return {{}}
1053
- // */
1054
- // const configure = (options = {}) => {
1055
- // configSchema(self.config, options || {})
1056
- // self.context.configure(self.config, self.helpers)
1057
- // self.compiler.configure(self.config)
1058
- // self.cache.configure(self.config)
1059
- // self.template.configure(self.config)
1060
- // return self.config
1061
- // }
1062
- // /**
1063
- // *
1064
- // * @param methods
1065
- // */
1066
- // const helpers = (methods) => {
1067
- // self.context.helpers(extend(self.helpers, methods))
1068
- // }
1069
- // /**
1070
- // *
1071
- // * @param list
1072
- // * @return {*}
1073
- // */
1074
- // const preload = (list) => {
1075
- // return self.cache.load(list || {})
1076
- // }
1077
- // /**
1078
- // *
1079
- // * @param options
1080
- // * @return {any}
1081
- // */
1082
- // const create = (options) => {
1083
- // return EJS(options)
1084
- // }
1085
- // /**
1086
- // *
1087
- // * @param content
1088
- // * @param path
1089
- // * @return {Function}
1090
- // */
1091
- // const compile = (content, path) => {
1092
- // return self.compiler.compile(content, path)
1093
- // }
1094
- // /**
1095
- // *
1096
- // * @param data
1097
- // * @return {ContextScope}
1098
- // */
1099
- // const context = (data = {}) => {
1100
- // return self.context.create(data)
1101
- // }
1102
- // /**
1103
- // *
1104
- // */
1105
- // helpers({ require, render })
1106
- // /**
1107
- // *
1108
- // */
1109
- // return {
1110
- // configure,
1111
- // helpers,
1112
- // preload,
1113
- // context,
1114
- // compile,
1115
- // create,
1116
- // render,
1117
- // }
1118
- // }
1119
-
1120
979
  function readFile(path, template) {
1121
980
  return new Promise((resolve, reject) => {
1122
981
  fs.readFile(joinPath(path, template), (error, data) => {