@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.
@@ -209,6 +209,7 @@ defaults.globalHelpers = [];
209
209
  defaults.vars = {
210
210
  SCOPE: 'ejs',
211
211
  COMPONENT: 'ui',
212
+ ELEMENT: 'el',
212
213
  EXTEND: '$$e',
213
214
  BUFFER: '$$a',
214
215
  LAYOUT: '$$l',
@@ -273,7 +274,6 @@ class Cache {
273
274
  #enabled = true
274
275
  #list = {}
275
276
  constructor(config) {
276
- bindContext(this, ['configure']);
277
277
  this.configure(config);
278
278
  }
279
279
  load(data) {
@@ -340,7 +340,6 @@ class Compiler {
340
340
  },
341
341
  ]
342
342
  constructor(config) {
343
- bindContext(this, ['configure', 'compile']);
344
343
  this.configure(config);
345
344
  }
346
345
  configure(config) {
@@ -383,7 +382,7 @@ class Compiler {
383
382
  );
384
383
  }
385
384
  compile(content, path) {
386
- const { SCOPE, SAFE, BUFFER, COMPONENT } = this.#config.vars;
385
+ const { SCOPE, SAFE, BUFFER, COMPONENT, ELEMENT } = this.#config.vars;
387
386
  const GLOBALS = this.#config.globalHelpers;
388
387
  if (this.#config.rmWhitespace) {
389
388
  content = String(content)
@@ -410,7 +409,7 @@ class Compiler {
410
409
  source = `${BUFFER}.start();${source}return ${BUFFER}.end();`;
411
410
  source += `\n//# sourceURL=${path}`;
412
411
  let result = null;
413
- let params = [SCOPE, COMPONENT, BUFFER, SAFE].concat(GLOBALS);
412
+ let params = [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT].concat(GLOBALS);
414
413
  try {
415
414
  result = Function.apply(null, params.concat(source));
416
415
  result.source = `(function(${params.join(',')}){\n${source}\n});`;
@@ -433,7 +432,6 @@ class Template {
433
432
  assertInstanceOf(compiler, Compiler);
434
433
  this.#cache = cache;
435
434
  this.#compiler = compiler;
436
- bindContext(this, ['configure', 'get']);
437
435
  this.configure(config);
438
436
  }
439
437
  #resolve(path) {
@@ -579,9 +577,18 @@ function createBuffer() {
579
577
  return buffer
580
578
  }
581
579
 
582
- const createScope = (config, methods) => {
583
- const { BLOCKS, MACRO, EXTEND, LAYOUT, BUFFER, COMPONENT, SAFE, SCOPE } =
584
- config.vars;
580
+ const createContextScope = (config, methods) => {
581
+ const {
582
+ BLOCKS,
583
+ MACRO,
584
+ EXTEND,
585
+ LAYOUT,
586
+ BUFFER,
587
+ SAFE,
588
+ SCOPE,
589
+ COMPONENT,
590
+ ELEMENT,
591
+ } = config.vars;
585
592
  /**
586
593
  * @name ContextScope
587
594
  * @param data
@@ -590,9 +597,11 @@ const createScope = (config, methods) => {
590
597
  function ContextScope(data) {
591
598
  this[BLOCKS] = {};
592
599
  this[MACRO] = {};
593
- Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT]));
600
+ Object.assign(
601
+ this,
602
+ omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT])
603
+ );
594
604
  }
595
-
596
605
  Object.assign(ContextScope.prototype, methods);
597
606
  Object.defineProperties(ContextScope.prototype, {
598
607
  [BUFFER]: {
@@ -614,6 +623,34 @@ const createScope = (config, methods) => {
614
623
  value: false,
615
624
  writable: true,
616
625
  },
626
+ /** @type {function} */
627
+ useSafeValue: {
628
+ get: () => safeValue,
629
+ },
630
+ /** @type {function} */
631
+ useComponent: {
632
+ get() {
633
+ if (isFunction(this[COMPONENT])) {
634
+ return this[COMPONENT].bind(this)
635
+ } else {
636
+ return () => {
637
+ throw new Error(`${COMPONENT} must be a function`)
638
+ }
639
+ }
640
+ },
641
+ },
642
+ /** @type {function} */
643
+ useElement: {
644
+ get() {
645
+ if (isFunction(this[ELEMENT])) {
646
+ return this[ELEMENT].bind(this)
647
+ } else {
648
+ return () => {
649
+ throw new Error(`${ELEMENT} must be a function`)
650
+ }
651
+ }
652
+ },
653
+ },
617
654
  /** @type {()=>this[MACRO]} */
618
655
  getMacro: {
619
656
  value() {
@@ -627,20 +664,6 @@ const createScope = (config, methods) => {
627
664
  },
628
665
  },
629
666
  /** @type {function} */
630
- getComponent: {
631
- value() {
632
- const context = this;
633
- if (COMPONENT in context) {
634
- return function () {
635
- return context[COMPONENT].apply(context, arguments)
636
- }
637
- }
638
- return function () {
639
- console.log('%s function not defined', COMPONENT);
640
- }
641
- },
642
- },
643
- /** @type {function} */
644
667
  getBlocks: {
645
668
  value() {
646
669
  return this[BLOCKS]
@@ -701,33 +724,12 @@ const createScope = (config, methods) => {
701
724
  const buffer = this.getBuffer();
702
725
  const context = this;
703
726
  return function () {
704
- buffer.backup();
705
727
  if (isFunction(callback)) {
706
- callback.apply(context, arguments);
728
+ buffer.backup();
729
+ buffer(callback.apply(context, arguments));
730
+ return buffer.restore()
707
731
  }
708
- return buffer.restore()
709
- }
710
- },
711
- },
712
- /** @type {function} */
713
- get: {
714
- value(name, defaults) {
715
- const path = getPath(this, name, true);
716
- const result = path.shift();
717
- const prop = path.pop();
718
- return hasProp(result, prop) ? result[prop] : defaults
719
- },
720
- },
721
- /** @type {function} */
722
- set: {
723
- value(name, value) {
724
- const path = getPath(this, name, false);
725
- const result = path.shift();
726
- const prop = path.pop();
727
- if (this.getExtend() && hasProp(result, prop)) {
728
- return result[prop]
729
732
  }
730
- return (result[prop] = value)
731
733
  },
732
734
  },
733
735
  /** @type {function} */
@@ -760,7 +762,7 @@ const createScope = (config, methods) => {
760
762
  blocks[name].push(this.fn(callback));
761
763
  if (this.getExtend()) return
762
764
  const list = Object.assign([], blocks[name]);
763
- const current = function () {
765
+ const current = () => {
764
766
  return list.shift()
765
767
  };
766
768
  const next = () => {
@@ -792,18 +794,10 @@ const createScope = (config, methods) => {
792
794
  },
793
795
  },
794
796
  /** @type {function} */
795
- promiseResolve: {
796
- value(value, callback) {
797
- return Promise.resolve(
798
- isFunction(value) ? this.fn(value)() : value
799
- ).then(callback.bind(this))
800
- },
801
- },
802
- /** @type {function} */
803
797
  use: {
804
798
  value(path, namespace) {
805
799
  this.echo(
806
- this.promiseResolve(this.require(path), function (exports) {
800
+ Promise.resolve(this.require(path)).then((exports) => {
807
801
  const list = this.getMacro();
808
802
  each(exports, function (macro, name) {
809
803
  list[[namespace, name].join('.')] = macro;
@@ -815,11 +809,28 @@ const createScope = (config, methods) => {
815
809
  /** @type {function} */
816
810
  async: {
817
811
  value(promise, callback) {
818
- this.echo(
819
- this.promiseResolve(promise, function (data) {
820
- return this.fn(callback)(data)
821
- })
822
- );
812
+ this.echo(Promise.resolve(promise).then(callback));
813
+ },
814
+ },
815
+ /** @type {function} */
816
+ get: {
817
+ value(name, defaults) {
818
+ const path = getPath(this, name, true);
819
+ const result = path.shift();
820
+ const prop = path.pop();
821
+ return hasProp(result, prop) ? result[prop] : defaults
822
+ },
823
+ },
824
+ /** @type {function} */
825
+ set: {
826
+ value(name, value) {
827
+ const path = getPath(this, name, false);
828
+ const result = path.shift();
829
+ const prop = path.pop();
830
+ if (this.getExtend() && hasProp(result, prop)) {
831
+ return result[prop]
832
+ }
833
+ return (result[prop] = value)
823
834
  },
824
835
  },
825
836
  /** @type {function} */
@@ -830,22 +841,24 @@ const createScope = (config, methods) => {
830
841
  }
831
842
  each(object, callback);
832
843
  },
833
- },
834
- /** @type {function} */
835
- element: {
836
- value(tag, attr, content) {
837
- return element(tag, attr, content)
838
- },
844
+ writable: true,
839
845
  },
840
846
  /** @type {function} */
841
847
  el: {
842
848
  value(tag, attr, content) {
849
+ content = isFunction(content) ? this.fn(content)() : content;
843
850
  this.echo(
844
- this.promiseResolve(content, function (content) {
845
- return this.element(tag, attr, content)
846
- })
851
+ Promise.resolve(content).then((content) =>
852
+ element(tag, attr, content)
853
+ )
847
854
  );
848
855
  },
856
+ writable: true,
857
+ },
858
+ /** @type {function} */
859
+ ui: {
860
+ value(layout) {},
861
+ writable: true,
849
862
  },
850
863
  });
851
864
  return ContextScope
@@ -853,20 +866,15 @@ const createScope = (config, methods) => {
853
866
 
854
867
  class Context {
855
868
  #scope
856
-
857
869
  constructor(config, methods) {
858
- bindContext(this, ['create', 'helpers', 'configure']);
859
870
  this.configure(config, methods);
860
871
  }
861
-
862
872
  create(data) {
863
873
  return new this.#scope(data)
864
874
  }
865
-
866
875
  configure(config, methods) {
867
- this.#scope = createScope(config, methods);
876
+ this.#scope = createContextScope(config, methods);
868
877
  }
869
-
870
878
  helpers(methods) {
871
879
  extend(this.#scope.prototype, methods || {});
872
880
  }
@@ -890,13 +898,14 @@ class EJS {
890
898
  'configure',
891
899
  'create',
892
900
  'render',
901
+ 'require',
893
902
  'context',
894
903
  'preload',
895
904
  'compile',
896
905
  'helpers',
897
906
  ]);
898
907
  //
899
- this.helpers({ require: this.#require, render: this.render });
908
+ this.helpers({ require: this.require, render: this.render });
900
909
  }
901
910
  configure(options) {
902
911
  configSchema(this.#config, options || {});
@@ -934,13 +943,19 @@ class EJS {
934
943
  create(options) {
935
944
  return new this.constructor(options)
936
945
  }
946
+ require(name) {
947
+ const filepath = ext(name, this.#config.extension);
948
+ const scope = this.context({});
949
+ return this.#output(filepath, scope).then(() => scope.getMacro())
950
+ }
937
951
  #output(path, scope) {
938
952
  const { globalHelpers } = this.#config;
939
953
  const params = [
940
954
  scope,
941
- scope.getComponent(),
942
955
  scope.getBuffer(),
943
- safeValue,
956
+ scope.useSafeValue,
957
+ scope.useComponent,
958
+ scope.useElement,
944
959
  ].concat(
945
960
  globalHelpers
946
961
  .filter((name) => isFunction(scope[name]))
@@ -950,164 +965,8 @@ class EJS {
950
965
  .get(path)
951
966
  .then((callback) => callback.apply(scope, params))
952
967
  }
953
- #require(name) {
954
- const filepath = ext(name, this.#config.extension);
955
- const scope = this.context({});
956
- return this.#output(filepath, scope).then(() => scope.getMacro())
957
- }
958
968
  }
959
969
 
960
- // export function EJS2(options) {
961
- // const self = {
962
- // config: {},
963
- // helpers: {},
964
- // /**
965
- // * @type {Context}
966
- // */
967
- // context: null,
968
- // /**
969
- // * @type {Compiler}
970
- // */
971
- // compiler: null,
972
- // /**
973
- // * @type {Template}
974
- // */
975
- // template: null,
976
- // /**
977
- // * @type {Cache}
978
- // */
979
- // cache: null,
980
- // }
981
- // /**
982
- // *
983
- // */
984
- // configSchema(self.config, options || {})
985
- // self.context = useContext(self.config, self.helpers)
986
- // self.compiler = useCompiler(self.config)
987
- // self.cache = useCache(self.config)
988
- // self.template = useTemplate(self.config, self.cache, self.compiler)
989
- // /**
990
- // *
991
- // * @param {string} path
992
- // * @param {ContextScope} scope
993
- // * @return {Promise<string>}
994
- // */
995
- // const output = (path, scope) => {
996
- // const { globalHelpers } = self.config
997
- // const params = [
998
- // scope,
999
- // scope.getComponent(),
1000
- // scope.getBuffer(),
1001
- // safeValue,
1002
- // ].concat(
1003
- // globalHelpers
1004
- // .filter((name) => isFunction(scope[name]))
1005
- // .map((name) => scope[name].bind(scope))
1006
- // )
1007
- // return self.template
1008
- // .get(path)
1009
- // .then((callback) => callback.apply(scope, params))
1010
- // }
1011
- // /**
1012
- // *
1013
- // * @param name
1014
- // * @return {Promise<string>}
1015
- // */
1016
- // const require = (name) => {
1017
- // const filepath = ext(name, self.config.extension)
1018
- // const scope = context({})
1019
- // return output(filepath, scope).then(() => scope.getMacro())
1020
- // }
1021
- // /**
1022
- // *
1023
- // * @param {string} name
1024
- // * @param {{}} [data]
1025
- // * @return {Promise<string>}
1026
- // */
1027
- // const render = (name, data) => {
1028
- // const filepath = ext(name, self.config.extension)
1029
- // const scope = context(data)
1030
- // return output(filepath, scope).then((content) => {
1031
- // if (scope.getExtend()) {
1032
- // scope.setExtend(false)
1033
- // const layout = scope.getLayout()
1034
- // const data = scope.clone()
1035
- // return render(layout, data)
1036
- // }
1037
- // return content
1038
- // })
1039
- // }
1040
- // /**
1041
- // *
1042
- // * @param options
1043
- // * @return {{}}
1044
- // */
1045
- // const configure = (options = {}) => {
1046
- // configSchema(self.config, options || {})
1047
- // self.context.configure(self.config, self.helpers)
1048
- // self.compiler.configure(self.config)
1049
- // self.cache.configure(self.config)
1050
- // self.template.configure(self.config)
1051
- // return self.config
1052
- // }
1053
- // /**
1054
- // *
1055
- // * @param methods
1056
- // */
1057
- // const helpers = (methods) => {
1058
- // self.context.helpers(extend(self.helpers, methods))
1059
- // }
1060
- // /**
1061
- // *
1062
- // * @param list
1063
- // * @return {*}
1064
- // */
1065
- // const preload = (list) => {
1066
- // return self.cache.load(list || {})
1067
- // }
1068
- // /**
1069
- // *
1070
- // * @param options
1071
- // * @return {any}
1072
- // */
1073
- // const create = (options) => {
1074
- // return EJS(options)
1075
- // }
1076
- // /**
1077
- // *
1078
- // * @param content
1079
- // * @param path
1080
- // * @return {Function}
1081
- // */
1082
- // const compile = (content, path) => {
1083
- // return self.compiler.compile(content, path)
1084
- // }
1085
- // /**
1086
- // *
1087
- // * @param data
1088
- // * @return {ContextScope}
1089
- // */
1090
- // const context = (data = {}) => {
1091
- // return self.context.create(data)
1092
- // }
1093
- // /**
1094
- // *
1095
- // */
1096
- // helpers({ require, render })
1097
- // /**
1098
- // *
1099
- // */
1100
- // return {
1101
- // configure,
1102
- // helpers,
1103
- // preload,
1104
- // context,
1105
- // compile,
1106
- // create,
1107
- // render,
1108
- // }
1109
- // }
1110
-
1111
970
  const templates = {};
1112
971
 
1113
972
  const getOrigin = (url, secure) => {