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