@kosatyi/ejs 0.0.65 → 0.0.66

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
@@ -95,8 +95,10 @@ const ext = (path, defaults) => {
95
95
  return path
96
96
  };
97
97
 
98
- const extend = (...args) => {
99
- const target = args.shift();
98
+ /**
99
+ * @type {<T extends {}, U, V, W,Y>(T,U,V,W,Y)=> T & U & V & W & Y}
100
+ */
101
+ const extend = (target, ...args) => {
100
102
  return args
101
103
  .filter((source) => source)
102
104
  .reduce((target, source) => Object.assign(target, source), target)
@@ -506,7 +508,7 @@ function resolve(list) {
506
508
  return Promise.all(list || []).then((list) => list.join(''))
507
509
  }
508
510
 
509
- const createBuffer = function () {
511
+ function createBuffer() {
510
512
  let store = [],
511
513
  array = [];
512
514
  function buffer(value) {
@@ -531,11 +533,10 @@ const createBuffer = function () {
531
533
  return resolve(array)
532
534
  };
533
535
  return buffer
534
- };
536
+ }
535
537
 
536
538
  function Context(config) {
537
539
  if (instanceOf(this, Context) === false) return new Context(config)
538
-
539
540
  this.configure = function (config, methods) {
540
541
  const { BLOCKS, MACRO, EXTEND, LAYOUT, BUFFER, COMPONENT } = config.vars;
541
542
 
@@ -545,6 +546,7 @@ function Context(config) {
545
546
 
546
547
  this.helpers = function (methods) {
547
548
  extend(Scope.prototype, methods || {});
549
+ console.log('helpers', Scope.prototype);
548
550
  };
549
551
 
550
552
  function Scope(data) {
@@ -552,218 +554,319 @@ function Context(config) {
552
554
  this[MACRO] = {};
553
555
  extend(this, data || {});
554
556
  }
555
- Scope.prototype = extend({}, methods || {});
556
- Scope.method = Scope.define = function (
557
- name,
558
- value,
559
- writable,
560
- configurable,
561
- enumerable
562
- ) {
563
- Object.defineProperty(Scope.prototype, name, {
564
- value: value,
565
- writable: writable || false,
566
- configurable: configurable || false,
567
- enumerable: enumerable || false,
568
- });
569
- };
570
-
571
- Scope.define(BUFFER, createBuffer());
572
- Scope.define(BLOCKS, {}, true);
573
- Scope.define(MACRO, {}, true);
574
- Scope.define(LAYOUT, false, true);
575
- Scope.define(EXTEND, false, true);
576
-
577
- Scope.method('getMacro', function () {
578
- return this[MACRO]
579
- });
580
-
581
- Scope.method('getBuffer', function () {
582
- return this[BUFFER]
583
- });
584
-
585
- Scope.method('getComponent', function () {
586
- const context = this;
587
- if (COMPONENT in context) {
588
- return function () {
589
- return context[COMPONENT].apply(context, arguments)
590
- }
591
- }
592
- return function () {
593
- console.log('%s function not defined', COMPONENT);
594
- }
595
- });
596
-
597
- Scope.method('getBlocks', function () {
598
- return this[BLOCKS]
599
- });
600
-
601
- Scope.method('setExtend', function (value) {
602
- this[EXTEND] = value;
603
- });
604
-
605
- Scope.method('getExtend', function () {
606
- return this[EXTEND]
607
- });
608
-
609
- Scope.method('setLayout', function (layout) {
610
- this[LAYOUT] = layout;
611
- });
612
-
613
- Scope.method('getLayout', function () {
614
- return this[LAYOUT]
615
- });
616
-
617
- Scope.method('clone', function (exclude_blocks) {
618
- const filter = [LAYOUT, EXTEND, BUFFER];
619
- if (exclude_blocks === true) {
620
- filter.push(BLOCKS);
621
- }
622
- return omit(this, filter)
623
- });
624
-
625
- Scope.method('extend', function (layout) {
626
- this.setExtend(true);
627
- this.setLayout(layout);
628
- });
629
-
630
- Scope.method('echo', function () {
631
- const buffer = this.getBuffer();
632
- const params = [].slice.call(arguments);
633
- params.forEach(buffer);
634
- });
635
-
636
- Scope.method('fn', function (callback) {
637
- const buffer = this.getBuffer();
638
- const context = this;
639
- return function () {
640
- buffer.backup();
641
- if (isFunction(callback)) {
642
- callback.apply(context, arguments);
643
- }
644
- return buffer.restore()
645
- }
646
- });
647
-
648
- Scope.method('get', function (name, defaults) {
649
- const path = getPath(this, name, true);
650
- const result = path.shift();
651
- const prop = path.pop();
652
- return hasProp(result, prop) ? result[prop] : defaults
653
- });
654
-
655
- Scope.method('set', function (name, value) {
656
- const path = getPath(this, name, false);
657
- const result = path.shift();
658
- const prop = path.pop();
659
- if (this.getExtend() && hasProp(result, prop)) {
660
- return result[prop]
661
- }
662
- return (result[prop] = value)
663
- });
664
557
 
665
- Scope.method('macro', function (name, callback) {
666
- const list = this.getMacro();
667
- const macro = this.fn(callback);
668
- const context = this;
669
- list[name] = function () {
670
- return context.echo(macro.apply(undefined, arguments))
671
- };
672
- });
673
-
674
- Scope.method('call', function (name) {
675
- const list = this.getMacro();
676
- const macro = list[name];
677
- const params = [].slice.call(arguments, 1);
678
- if (isFunction(macro)) {
679
- return macro.apply(macro, params)
680
- }
681
- });
682
-
683
- Scope.method('block', function (name, callback) {
684
- const blocks = this.getBlocks();
685
- blocks[name] = blocks[name] || [];
686
- blocks[name].push(this.fn(callback));
687
- if (this.getExtend()) return
688
- const list = Object.assign([], blocks[name]);
689
- const current = function () {
690
- return list.shift()
691
- };
692
- const next = () => {
693
- const parent = current();
694
- if (parent) {
695
- return () => {
696
- this.echo(parent(next()));
558
+ Scope.prototype = extend({}, methods || {});
559
+ Object.defineProperties(Scope.prototype, {
560
+ [BUFFER]: {
561
+ value: createBuffer(),
562
+ writable: true,
563
+ configurable: false,
564
+ enumerable: false,
565
+ },
566
+ [BLOCKS]: {
567
+ value: {},
568
+ writable: true,
569
+ configurable: false,
570
+ enumerable: false,
571
+ },
572
+ [MACRO]: {
573
+ value: {},
574
+ writable: true,
575
+ configurable: false,
576
+ enumerable: false,
577
+ },
578
+ [LAYOUT]: {
579
+ value: false,
580
+ writable: true,
581
+ configurable: false,
582
+ enumerable: false,
583
+ },
584
+ [EXTEND]: {
585
+ value: false,
586
+ writable: true,
587
+ configurable: false,
588
+ enumerable: false,
589
+ },
590
+ getMacro: {
591
+ value() {
592
+ return this[MACRO]
593
+ },
594
+ writable: false,
595
+ configurable: false,
596
+ enumerable: false,
597
+ },
598
+ getBuffer: {
599
+ value() {
600
+ return this[BUFFER]
601
+ },
602
+ writable: false,
603
+ configurable: false,
604
+ enumerable: false,
605
+ },
606
+ getComponent: {
607
+ value() {
608
+ const context = this;
609
+ if (COMPONENT in context) {
610
+ return function () {
611
+ return context[COMPONENT].apply(context, arguments)
612
+ }
697
613
  }
698
- } else {
699
- return noop
700
- }
701
- };
702
- this.echo(current()(next()));
703
- });
704
-
705
- Scope.method('include', function (path, data, cx) {
706
- const context = cx === false ? {} : this.clone(true);
707
- const params = extend(context, data || {});
708
- const promise = this.render(path, params);
709
- this.echo(promise);
710
- });
711
-
712
- Scope.method('use', function (path, namespace) {
713
- const promise = this.require(path);
714
- this.echo(
715
- resolve$1(
716
- promise,
717
- function (exports) {
718
- const list = this.getMacro();
719
- each(exports, function (macro, name) {
720
- list[[namespace, name].join('.')] = macro;
721
- });
722
- },
723
- this
724
- )
725
- );
726
- });
727
-
728
- Scope.method('async', function (promise, callback) {
729
- this.echo(
730
- resolve$1(
731
- promise,
732
- function (data) {
733
- return this.fn(callback)(data)
734
- },
735
- this
736
- )
737
- );
738
- });
739
-
740
- Scope.method('node', function (tag, attr, content) {
741
- return element(tag, attr, content)
742
- });
743
-
744
- Scope.method('el', function (tag, attr, content) {
745
- if (isFunction(content)) {
746
- content = this.fn(content)();
747
- }
748
- this.echo(
749
- resolve$1(
750
- content,
751
- function (content) {
752
- return element(tag, attr, content)
753
- },
754
- this
755
- )
756
- );
757
- });
758
-
759
- Scope.method('each', function (object, callback) {
760
- if (isString(object)) {
761
- object = this.get(object, []);
762
- }
763
- each(object, callback);
614
+ return function () {
615
+ console.log('%s function not defined', COMPONENT);
616
+ }
617
+ },
618
+ writable: false,
619
+ configurable: false,
620
+ enumerable: false,
621
+ },
622
+ getBlocks: {
623
+ value() {
624
+ return this[BLOCKS]
625
+ },
626
+ writable: false,
627
+ configurable: false,
628
+ enumerable: false,
629
+ },
630
+ setExtend: {
631
+ value(value) {
632
+ this[EXTEND] = value;
633
+ },
634
+ writable: false,
635
+ configurable: false,
636
+ enumerable: false,
637
+ },
638
+ getExtend: {
639
+ value() {
640
+ return this[EXTEND]
641
+ },
642
+ writable: false,
643
+ configurable: false,
644
+ enumerable: false,
645
+ },
646
+ setLayout: {
647
+ value(layout) {
648
+ this[LAYOUT] = layout;
649
+ },
650
+ writable: false,
651
+ configurable: false,
652
+ enumerable: false,
653
+ },
654
+ getLayout: {
655
+ value() {
656
+ return this[LAYOUT]
657
+ },
658
+ writable: false,
659
+ configurable: false,
660
+ enumerable: false,
661
+ },
662
+ clone: {
663
+ value(exclude_blocks) {
664
+ const filter = [LAYOUT, EXTEND, BUFFER];
665
+ if (exclude_blocks === true) {
666
+ filter.push(BLOCKS);
667
+ }
668
+ return omit(this, filter)
669
+ },
670
+ writable: false,
671
+ configurable: false,
672
+ enumerable: false,
673
+ },
674
+ extend: {
675
+ value(layout) {
676
+ this.setExtend(true);
677
+ this.setLayout(layout);
678
+ },
679
+ writable: false,
680
+ configurable: false,
681
+ enumerable: false,
682
+ },
683
+ echo: {
684
+ value(layout) {
685
+ const buffer = this.getBuffer();
686
+ const params = [].slice.call(arguments);
687
+ params.forEach(buffer);
688
+ },
689
+ writable: false,
690
+ configurable: false,
691
+ enumerable: false,
692
+ },
693
+ fn: {
694
+ value(callback) {
695
+ const buffer = this.getBuffer();
696
+ const context = this;
697
+ return function () {
698
+ buffer.backup();
699
+ if (isFunction(callback)) {
700
+ callback.apply(context, arguments);
701
+ }
702
+ return buffer.restore()
703
+ }
704
+ },
705
+ writable: false,
706
+ configurable: false,
707
+ enumerable: false,
708
+ },
709
+ get: {
710
+ value(name, defaults) {
711
+ const path = getPath(this, name, true);
712
+ const result = path.shift();
713
+ const prop = path.pop();
714
+ return hasProp(result, prop) ? result[prop] : defaults
715
+ },
716
+ writable: false,
717
+ configurable: false,
718
+ enumerable: false,
719
+ },
720
+ set: {
721
+ value(name, value) {
722
+ const path = getPath(this, name, false);
723
+ const result = path.shift();
724
+ const prop = path.pop();
725
+ if (this.getExtend() && hasProp(result, prop)) {
726
+ return result[prop]
727
+ }
728
+ return (result[prop] = value)
729
+ },
730
+ writable: false,
731
+ configurable: false,
732
+ enumerable: false,
733
+ },
734
+ macro: {
735
+ value(name, callback) {
736
+ const list = this.getMacro();
737
+ const macro = this.fn(callback);
738
+ const context = this;
739
+ list[name] = function () {
740
+ return context.echo(macro.apply(undefined, arguments))
741
+ };
742
+ },
743
+ writable: false,
744
+ configurable: false,
745
+ enumerable: false,
746
+ },
747
+ call: {
748
+ value(name) {
749
+ const list = this.getMacro();
750
+ const macro = list[name];
751
+ const params = [].slice.call(arguments, 1);
752
+ if (isFunction(macro)) {
753
+ return macro.apply(macro, params)
754
+ }
755
+ },
756
+ writable: false,
757
+ configurable: false,
758
+ enumerable: false,
759
+ },
760
+ block: {
761
+ value(name, callback) {
762
+ const blocks = this.getBlocks();
763
+ blocks[name] = blocks[name] || [];
764
+ blocks[name].push(this.fn(callback));
765
+ if (this.getExtend()) return
766
+ const list = Object.assign([], blocks[name]);
767
+ const current = function () {
768
+ return list.shift()
769
+ };
770
+ const next = () => {
771
+ const parent = current();
772
+ if (parent) {
773
+ return () => {
774
+ this.echo(parent(next()));
775
+ }
776
+ } else {
777
+ return noop
778
+ }
779
+ };
780
+ this.echo(current()(next()));
781
+ },
782
+ writable: false,
783
+ configurable: false,
784
+ enumerable: false,
785
+ },
786
+ include: {
787
+ value(path, data, cx) {
788
+ const context = cx === false ? {} : this.clone(true);
789
+ const params = extend(context, data || {});
790
+ const promise = this.render(path, params);
791
+ this.echo(promise);
792
+ },
793
+ writable: false,
794
+ configurable: false,
795
+ enumerable: false,
796
+ },
797
+ use: {
798
+ value(path, namespace) {
799
+ const promise = this.require(path);
800
+ this.echo(
801
+ resolve$1(
802
+ promise,
803
+ function (exports) {
804
+ const list = this.getMacro();
805
+ each(exports, function (macro, name) {
806
+ list[[namespace, name].join('.')] = macro;
807
+ });
808
+ },
809
+ this
810
+ )
811
+ );
812
+ },
813
+ writable: false,
814
+ configurable: false,
815
+ enumerable: false,
816
+ },
817
+ async: {
818
+ value(promise, callback) {
819
+ this.echo(
820
+ resolve$1(
821
+ promise,
822
+ function (data) {
823
+ return this.fn(callback)(data)
824
+ },
825
+ this
826
+ )
827
+ );
828
+ },
829
+ writable: false,
830
+ configurable: false,
831
+ enumerable: false,
832
+ },
833
+ each: {
834
+ value: function (object, callback) {
835
+ if (isString(object)) {
836
+ object = this.get(object, []);
837
+ }
838
+ each(object, callback);
839
+ },
840
+ writable: false,
841
+ configurable: false,
842
+ enumerable: false,
843
+ },
844
+ element: {
845
+ value(tag, attr, content) {
846
+ return element(tag, attr, content)
847
+ },
848
+ },
849
+ el: {
850
+ value(tag, attr, content) {
851
+ if (isFunction(content)) {
852
+ content = this.fn(content)();
853
+ }
854
+ this.echo(
855
+ resolve$1(
856
+ content,
857
+ function (content) {
858
+ return this.element(tag, attr, content)
859
+ },
860
+ this
861
+ )
862
+ );
863
+ },
864
+ writable: false,
865
+ configurable: false,
866
+ enumerable: false,
867
+ },
764
868
  });
765
869
  };
766
-
767
870
  this.configure(config);
768
871
  }
769
872
 
@@ -794,7 +897,6 @@ function EJS(options) {
794
897
  return scope.getMacro()
795
898
  })
796
899
  };
797
-
798
900
  const render = function (name, data) {
799
901
  const filepath = ext(name, config.extension);
800
902
  const scope = context.create(data);
@@ -808,7 +910,6 @@ function EJS(options) {
808
910
  return content
809
911
  })
810
912
  };
811
-
812
913
  this.configure = function (options) {
813
914
  options = options || {};
814
915
  configSchema(config, options);
@@ -818,32 +919,26 @@ function EJS(options) {
818
919
  template.configure(config);
819
920
  return config
820
921
  };
821
-
822
922
  this.render = function (name, data) {
823
923
  return render(name, data)
824
924
  };
825
-
826
925
  this.helpers = function (methods) {
827
- context.helpers(Object.assign(scope, methods || {}));
926
+ context.helpers(extend(scope, methods));
828
927
  };
829
-
830
928
  this.preload = function (list) {
831
929
  return cache.load(list || {})
832
930
  };
833
-
834
931
  this.create = function (options) {
835
932
  return new EJS(options)
836
933
  };
837
-
838
934
  this.compile = function (content, path) {
839
935
  return compiler.compile(content, path)
840
936
  };
841
-
842
937
  this.context = function (data) {
843
938
  return context.create(data)
844
939
  };
845
-
846
940
  this.helpers({ require, render });
941
+ return this
847
942
  }
848
943
 
849
944
  const ejs = new EJS();