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