@kosatyi/ejs 0.0.12 → 0.0.14

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/ejs.mjs CHANGED
@@ -12,19 +12,15 @@ defaults.path = 'views';
12
12
 
13
13
  defaults.resolver = null;
14
14
 
15
- defaults.extension = {
16
- template: 'ejs',
17
- module: 'mjs',
18
- };
15
+ defaults.extension = 'ejs';
19
16
 
20
17
  defaults.vars = {
21
18
  EXTEND: '$$$',
22
19
  BUFFER: '$$a',
23
20
  OUTPUT: '$$i',
24
21
  LAYOUT: '$$l',
25
- MACROS: '$$m',
26
- PRINT: '$$j',
27
22
  BLOCKS: '$$b',
23
+ MACRO: '$$m',
28
24
  ERROR: '$$e',
29
25
  SCOPE: '$$s',
30
26
  SAFE: '$$v',
@@ -41,7 +37,6 @@ const typeProp = function () {
41
37
  const callback = args.shift();
42
38
  return args.filter(callback).pop()
43
39
  };
44
-
45
40
  const isFunction = (v) => typeof v === 'function';
46
41
  const isString = (v) => typeof v === 'string';
47
42
  const isBoolean = (v) => typeof v === 'boolean';
@@ -112,6 +107,8 @@ const extend = (...args) => {
112
107
  }, target)
113
108
  };
114
109
 
110
+ const noop = () => {};
111
+
115
112
  const each = (object, callback) => {
116
113
  let prop;
117
114
  for (prop in object) {
@@ -160,6 +157,9 @@ const omit = (object, list) => {
160
157
  })
161
158
  };
162
159
 
160
+ const resolve$1 = (value,callback,context) =>
161
+ Promise.resolve(value).then(callback.bind(context));
162
+
163
163
  const hasProp = (object, prop) => {
164
164
  return object && object.hasOwnProperty(prop)
165
165
  };
@@ -249,14 +249,13 @@ const match = (regex, text, callback) => {
249
249
  };
250
250
  /**
251
251
  *
252
- * @param {Object} config
252
+ * @param {object} config
253
253
  * @return {function(*, *): Function}
254
254
  * @constructor
255
255
  */
256
256
  const Compiler = (config) => {
257
257
  const token = config.token;
258
258
  const vars = config.vars;
259
- const module = config.extension.module;
260
259
  const matches = [];
261
260
  const formats = [];
262
261
  const slurp = {
@@ -277,19 +276,15 @@ const Compiler = (config) => {
277
276
  const slurpStart = new RegExp([slurp.match, slurp.start].join(''), 'gm');
278
277
  const slurpEnd = new RegExp([slurp.end, slurp.match].join(''), 'gm');
279
278
  /**
280
- * @type Function
279
+ * @type function
281
280
  * @name Compile
282
281
  */
283
282
  return function (content, path) {
284
283
  const { SCOPE, SAFE, BUFFER } = vars;
285
- const extension = path.split('.').pop();
286
284
  content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
287
285
  content = content
288
286
  .replace(slurpStart, slurp.start)
289
287
  .replace(slurpEnd, slurp.end);
290
- if (extension === module) {
291
- content = [token.start, content, token.end].join('\n');
292
- }
293
288
  let source = `${BUFFER}('`;
294
289
  match(regex, content, (params, index, offset) => {
295
290
  source += symbols(content.slice(index, offset));
@@ -298,6 +293,7 @@ const Compiler = (config) => {
298
293
  });
299
294
  });
300
295
  source += `');`;
296
+ source = `try{${source}}catch(e){console.info(e)}`;
301
297
  source = `with(${SCOPE}){${source}}`;
302
298
  source = `${BUFFER}.start();${source}return ${BUFFER}.end();`;
303
299
  source += `\n//# sourceURL=${path}`;
@@ -307,6 +303,7 @@ const Compiler = (config) => {
307
303
  result.source = `(function(${SCOPE},${BUFFER},${SAFE}){\n${source}\n})`;
308
304
  //result.source = result.toString()
309
305
  } catch (e) {
306
+ console.log(e);
310
307
  e.filename = path;
311
308
  e.source = source;
312
309
  throw e
@@ -417,275 +414,345 @@ const Buffer = () => {
417
414
  array = store.pop();
418
415
  return resolve(result)
419
416
  };
417
+ buffer.error = function(e){
418
+ throw e
419
+ };
420
420
  buffer.end = function () {
421
421
  return resolve(array)
422
422
  };
423
423
  return buffer
424
424
  };
425
425
 
426
- /**
427
- *
428
- * @param {{}} instance
429
- * @method create
430
- */
431
- const Component = (instance) => {
432
- const defaults = extend({}, instance.props);
433
- const create = instance.create;
434
- return {
435
- element,
436
- create,
437
- render(props) {
438
- return this.create(extend({}, defaults, props))
439
- },
440
- }
441
- };
442
-
443
- const Scope = (config, methods) => {
426
+ const configure = (config, methods) => {
444
427
  /**
445
428
  *
446
429
  */
447
- const { EXTEND, MACROS, LAYOUT, BLOCKS, BUFFER } = config.vars;
448
-
430
+ const { EXTEND, LAYOUT, BLOCKS, BUFFER, MACRO, SCOPE } = config.vars;
449
431
  /**
450
- *
432
+ * @memberOf global
433
+ * @name [SCOPE]
451
434
  * @param data
452
435
  * @constructor
453
436
  */
454
437
  function Scope(data = {}) {
455
- this.setBlocks({});
438
+ this.initBlocks();
439
+ this.initMacro();
456
440
  extend(this, data);
457
- this.setLayout(false);
458
- this.setExtend(false);
459
441
  }
460
442
  /**
461
- *
443
+ * @static
444
+ * @param methods
445
+ */
446
+ Scope.helpers = (methods) => {
447
+ extend(Scope.prototype, methods);
448
+ };
449
+ /**
450
+ * @static
451
+ * @param name
452
+ * @param descriptor
453
+ */
454
+ Scope.property = (name, descriptor) => {
455
+ Object.defineProperty(Scope.prototype, name, descriptor);
456
+ };
457
+ /**
458
+ * @static
459
+ * @param name
460
+ * @param method
462
461
  */
463
- Object.defineProperties(Scope.prototype, {
464
- [BUFFER]: {
465
- value: Buffer(),
462
+ Scope.method = (name, method) => {
463
+ return Scope.property(name,{
464
+ value: method,
466
465
  writable: false,
467
466
  configurable: false,
468
467
  enumerable: false
469
- },
470
- // [BLOCKS]: {
471
- // value: {},
472
- // writable: true,
473
- // configurable: false,
474
- // enumerable: false
475
- // },
476
- // [EXTEND]: {
477
- // value: false,
478
- // writable: true,
479
- // configurable: false,
480
- // enumerable: false
481
- // },
482
- // [LAYOUT]: {
483
- // value: false,
484
- // writable: true,
485
- // configurable: false,
486
- // enumerable: false
487
- // },
488
- // setBuffer: {
489
- // value(value) {
490
- // this[BUFFER] = value
491
- // },
492
- // writable: false,
493
- // configurable: false
494
- // },
495
- getBuffer: {
496
- value() {
497
- return this[BUFFER]
498
- },
499
- writable: false,
500
- configurable: false
501
- },
502
- setBlocks: {
503
- value(value) {
504
- this[BLOCKS] = value;
505
- },
506
- writable: false,
507
- configurable: false
508
- },
509
- getBlocks: {
510
- value() {
511
- return this[BLOCKS]
512
- },
513
- writable: false,
514
- configurable: false
515
- },
516
- setExtend: {
517
- value(value) {
518
- this[EXTEND] = value;
519
- },
520
- writable: false,
521
- configurable: false
522
- },
523
- getExtend: {
524
- value() {
525
- return this[EXTEND]
526
- },
527
- writable: false,
528
- configurable: false
529
- },
530
- setLayout: {
531
- value(layout) {
532
- this[LAYOUT] = layout;
533
- },
534
- writable: false,
535
- configurable: false
536
- },
537
- getLayout: {
538
- value() {
539
- return this[LAYOUT]
540
- },
541
- writable: false,
542
- configurable: false
468
+ })
469
+ };
470
+ /**
471
+ *
472
+ */
473
+ Scope.property(BUFFER, {
474
+ value: Buffer(),
475
+ writable: false,
476
+ configurable: false,
477
+ enumerable: false
478
+ });
479
+ /**
480
+ *
481
+ */
482
+ Scope.property(BLOCKS, {
483
+ value: {},
484
+ writable: true,
485
+ configurable: false,
486
+ enumerable: false
487
+ });
488
+ /**
489
+ *
490
+ */
491
+ Scope.property(MACRO, {
492
+ value: {},
493
+ writable: true,
494
+ configurable: false,
495
+ enumerable: false
496
+ });
497
+ /**
498
+ *
499
+ */
500
+ Scope.property(LAYOUT, {
501
+ value: false,
502
+ writable: true,
503
+ configurable: false,
504
+ enumerable: false
505
+ });
506
+ /**
507
+ *
508
+ */
509
+ Scope.property(EXTEND, {
510
+ value: false,
511
+ writable: true,
512
+ configurable: false,
513
+ enumerable: false
514
+ });
515
+ /**
516
+ *
517
+ */
518
+ Scope.method('initBlocks', function() {
519
+ this[BLOCKS] = {};
520
+ });
521
+ /**
522
+ *
523
+ */
524
+ Scope.method('initMacro', function() {
525
+ this[MACRO] = {};
526
+ });
527
+ /**
528
+ *
529
+ */
530
+ Scope.method('getMacro', function() {
531
+ return this[MACRO]
532
+ });
533
+ /**
534
+ *
535
+ */
536
+ Scope.method('getBuffer', function() {
537
+ return this[BUFFER]
538
+ });
539
+ /**
540
+ *
541
+ */
542
+ Scope.method('getBlocks', function() {
543
+ return this[BLOCKS]
544
+ });
545
+ /**
546
+ *
547
+ */
548
+ Scope.method('setExtend', function(value) {
549
+ this[EXTEND] = value;
550
+ });
551
+ /**
552
+ *
553
+ */
554
+ Scope.method('getExtend', function() {
555
+ return this[EXTEND]
556
+ });
557
+ /**
558
+ *
559
+ */
560
+ Scope.method('setLayout', function(layout) {
561
+ this[LAYOUT] = layout;
562
+ });
563
+ /**
564
+ *
565
+ */
566
+ Scope.method('getLayout', function() {
567
+ return this[LAYOUT]
568
+ });
569
+ /**
570
+ *
571
+ */
572
+ Scope.method('clone', function(exclude_blocks) {
573
+ const filter = [LAYOUT, EXTEND, BUFFER];
574
+ if (exclude_blocks === true) {
575
+ filter.push(BLOCKS);
543
576
  }
577
+ return omit(this, filter)
544
578
  });
545
-
546
- Scope.helpers = (methods) => {
547
- extend(Scope.prototype, methods);
548
- };
549
-
550
579
  /**
551
- * @lends Scope.prototype
580
+ * @methodOf global
581
+ * @function extend
582
+ * @param layout
552
583
  */
553
- Scope.helpers(methods);
584
+ Scope.method('extend', function(layout) {
585
+ this.setExtend(true);
586
+ this.setLayout(layout);
587
+ });
554
588
  /**
555
- * @lends Scope.prototype
589
+ * @memberOf global
590
+ * @function echo
556
591
  */
557
- Scope.helpers({
558
- /**
559
- * @return {*}
560
- */
561
- clone(exclude_blocks) {
562
- const filter = [LAYOUT, EXTEND, MACROS, BUFFER];
563
- if (exclude_blocks === true) {
564
- filter.push(BLOCKS);
592
+ Scope.method('echo', function() {
593
+ const buffer = this.getBuffer();
594
+ const params = [].slice.call(arguments);
595
+ params.forEach(function(item) {
596
+ buffer(item);
597
+ });
598
+ });
599
+ /**
600
+ * @memberOf global
601
+ * @function fn
602
+ * @param callback
603
+ */
604
+ Scope.method('fn', function(callback) {
605
+ const buffer = this.getBuffer();
606
+ const context = this;
607
+ return function() {
608
+ buffer.backup();
609
+ if (isFunction(callback)) {
610
+ callback.apply(context, arguments);
565
611
  }
566
- return omit(this, filter)
567
- },
568
- /**
569
- * Join values to output buffer
570
- * @memberOf global
571
- * @type Function
572
- */
573
- echo() {
574
- const buffer = this.getBuffer();
575
- const params = [].slice.call(arguments);
576
- params.forEach(function(item) {
577
- buffer(item);
578
- });
579
- },
580
- /**
581
- * Buffered output callback
582
- * @type Function
583
- * @param {Function} callback
584
- * @param {Boolean} [echo]
585
- * @return {Function}
586
- */
587
- macro(callback, echo) {
588
- const buffer = this.getBuffer();
589
- const macro = function() {
590
- buffer.backup();
591
- if (isFunction(callback)) {
592
- callback.apply(this, arguments);
612
+ return buffer.restore()
613
+ }
614
+ });
615
+ /**
616
+ * @memberOf global
617
+ * @function get
618
+ * @param name
619
+ * @param [defaults]
620
+ */
621
+ Scope.method('get', function(name,defaults) {
622
+ const path = getPath(this, name);
623
+ const result = path.shift();
624
+ const prop = path.pop();
625
+ return hasProp(result, prop) ? result[prop] : defaults
626
+ });
627
+ /**
628
+ * @memberOf global
629
+ * @function set
630
+ * @param name
631
+ * @param value
632
+ */
633
+ Scope.method('set', function(name,value) {
634
+ const path = getPath(this, name);
635
+ const result = path.shift();
636
+ const prop = path.pop();
637
+ if (this.getExtend() && hasProp(result, prop)) {
638
+ return result[prop]
639
+ }
640
+ return result[prop] = value
641
+ });
642
+ /**
643
+ * @memberOf global
644
+ * @function macro
645
+ * @param name
646
+ * @param callback
647
+ */
648
+ Scope.method('macro', function(name, callback) {
649
+ const list = this.getMacro();
650
+ const macro = this.fn(callback);
651
+ const context = this;
652
+ list[name] = function() {
653
+ return context.echo(macro.apply(undefined, arguments))
654
+ };
655
+ });
656
+ /**
657
+ * @memberOf global
658
+ * @function call
659
+ * @param name
660
+ * @param {...*} args
661
+ */
662
+ Scope.method('call', function(name) {
663
+ const list = this.getMacro();
664
+ const macro = list[name];
665
+ const params = [].slice.call(arguments, 1);
666
+ if (isFunction(macro)) {
667
+ return macro.apply(macro, params)
668
+ }
669
+ });
670
+ /**
671
+ * @memberOf global
672
+ * @function block
673
+ * @param name
674
+ * @param callback
675
+ */
676
+ Scope.method('block',function(name,callback){
677
+ const blocks = this.getBlocks();
678
+ blocks[name] = blocks[name] || [];
679
+ blocks[name].push(this.fn(callback));
680
+ if (this.getExtend()) return
681
+ const list = Object.assign([], blocks[name]);
682
+ const current = function() {
683
+ return list.shift()
684
+ };
685
+ const next = () => {
686
+ const parent = current();
687
+ if (parent) {
688
+ return () => {
689
+ this.echo(parent(next()));
593
690
  }
594
- const result = buffer.restore();
595
- return echo === true ? this.echo(result) : result
596
- }.bind(this);
597
- macro.__context = this;
598
- macro.__source = callback;
599
- macro.__layout = this.getLayout();
600
- return macro
601
- },
602
- /**
603
- * @memberOf global
604
- * @param value
605
- * @param callback
606
- * @return {Promise<unknown>}
607
- */
608
- resolve(value, callback) {
609
- return Promise.resolve(value).then(callback.bind(this))
610
- },
611
- /**
612
- * @memberOf global
613
- */
614
- async(promise, callback) {
615
- this.echo(
616
- this.resolve(promise, (data) => this.macro(callback)(data))
617
- );
618
- },
619
- /**
620
- * @memberOf global
621
- */
622
- node: element,
691
+ } else {
692
+ return noop
693
+ }
694
+ };
695
+ this.echo(current()(next()));
696
+ });
697
+ /**
698
+ * @memberOf global
699
+ * @function include
700
+ * @param path
701
+ * @param [data]
702
+ * @param [cx]
703
+ */
704
+ Scope.method('include',function(path, data, cx){
705
+ const context = cx === false ? {} : this.clone(true);
706
+ const params = extend(context, data || {});
707
+ const promise = this.render(path, params);
708
+ this.echo(promise);
709
+ });
710
+ /**
711
+ * @memberOf global
712
+ * @function use
713
+ * @param path
714
+ * @param namespace
715
+ */
716
+ Scope.method('use',function(path, namespace){
717
+ const promise = this.require(path);
718
+ this.echo(resolve$1(promise,function(exports){
719
+ const list = this.getMacro();
720
+ each(exports, function(macro, name) {
721
+ list[[namespace, name].join('.')] = macro;
722
+ });
723
+ },this));
724
+ });
725
+ /**
726
+ * @memberOf global
727
+ * @function async
728
+ * @param promise
729
+ * @param callback
730
+ */
731
+ Scope.method('async',function(promise,callback){
732
+ this.echo(
733
+ resolve$1(promise, function(data) {
734
+ return this.fn(callback)(data)
735
+ }, this)
736
+ );
737
+ });
738
+ Scope.helpers(methods);
739
+ Scope.helpers({
623
740
  /**
624
741
  * @memberOf global
742
+ * @param tag
743
+ * @param attr
744
+ * @param content
625
745
  */
626
- element(tag, attr, content) {
746
+ el(tag, attr, content) {
627
747
  if (isFunction(content)) {
628
- content = this.macro(content)();
748
+ content = this.fn(content)();
629
749
  }
630
750
  this.echo(
631
- this.resolve(content, (content) => element(tag, attr, content))
632
- );
633
- },
634
- /**
635
- * @memberOf global
636
- * @param {String} namespace
637
- * @param {Object} instance
638
- */
639
- component(namespace, instance) {
640
- instance = Component(instance);
641
- this.set(
642
- namespace,
643
- function(props) {
644
- this.echo(instance.render(props));
645
- }.bind(this)
751
+ resolve$1(content, function(content) {
752
+ return element(tag, attr, content)
753
+ }, this)
646
754
  );
647
755
  },
648
- /**
649
- * @memberOf global
650
- * @param name
651
- * @param defaults
652
- */
653
- get(name, defaults) {
654
- const path = getPath(this, name);
655
- const result = path.shift();
656
- const prop = path.pop();
657
- return hasProp(result, prop) ? result[prop] : defaults
658
- },
659
- /**
660
- * @memberOf global
661
- * @param {String} name
662
- * @param value
663
- * @return
664
- */
665
- set(name, value) {
666
- const path = getPath(this, name);
667
- const result = path.shift();
668
- const prop = path.pop();
669
- if (this.getExtend()) {
670
- if (hasProp(result, prop)) {
671
- return result[prop]
672
- }
673
- }
674
- result[prop] = value;
675
- },
676
- /**
677
- * @memberOf global
678
- * @param name
679
- */
680
- call(name) {
681
- const params = [].slice.call(arguments, 1);
682
- const path = getPath(this, name);
683
- const result = path.shift();
684
- const prop = path.pop();
685
- if (isFunction(result[prop])) {
686
- return result[prop].apply(result, params)
687
- }
688
- },
689
756
  /**
690
757
  * @memberOf global
691
758
  * @param object
@@ -696,89 +763,6 @@ const Scope = (config, methods) => {
696
763
  object = this.get(object, []);
697
764
  }
698
765
  each(object, callback);
699
- },
700
- /**
701
- * @memberOf global
702
- * @param {String} layout
703
- */
704
- extend(layout) {
705
- this.setExtend(true);
706
- this.setLayout(layout);
707
- },
708
- /**
709
- * @memberOf global
710
- * @param name
711
- * @param callback
712
- * @return {*}
713
- */
714
- block(name, callback) {
715
- const blocks = this.getBlocks();
716
- blocks[name] = blocks[name] || [];
717
- blocks[name].push(this.macro(callback));
718
- if (this.getExtend() === false) {
719
- const list = blocks[name];
720
- const callback = function(){
721
- const parent = list.shift();
722
- if( parent ) {
723
- const context = parent.__context;
724
- return function(){
725
- context.echo(parent(callback()));
726
- }
727
- } else {
728
- return function(){}
729
- }
730
- };
731
- this.echo(list.shift()(callback()));
732
- }
733
- },
734
- /**
735
- * @memberOf global
736
- * @param {string} path
737
- * @param {object} [data]
738
- * @param {boolean} [cx]
739
- */
740
- include(path, data, cx) {
741
- const context = cx === false ? {} : this.clone(true);
742
- const params = extend(context, data || {});
743
- const promise = this.render(path, params);
744
- this.echo(promise);
745
- },
746
- /**
747
- * @memberOf global
748
- * @param {string} path
749
- */
750
- use(path) {
751
- const scope = this;
752
- const promise = this.require(path);
753
- this.echo(promise);
754
- return {
755
- as(namespace) {
756
- promise.then((exports) => {
757
- scope.set(namespace, exports);
758
- });
759
- return this
760
- }
761
- }
762
- },
763
- /**
764
- * @memberOf global
765
- * @param {string} path
766
- */
767
- from(path) {
768
- const scope = this;
769
- const promise = this.require(path);
770
- this.echo(promise);
771
- return {
772
- use() {
773
- const params = [].slice.call(arguments);
774
- promise.then((exports) => {
775
- params.forEach((name) => {
776
- scope.set(name, exports[name]);
777
- });
778
- });
779
- return this
780
- }
781
- }
782
766
  }
783
767
  });
784
768
  return Scope
@@ -839,7 +823,7 @@ function init(options) {
839
823
  })
840
824
  },
841
825
  render(name, data) {
842
- const filepath = ext(name, config.extension.template);
826
+ const filepath = ext(name, config.extension);
843
827
  const scope = new view.scope(data);
844
828
  return view.output(filepath, scope).then((content) => {
845
829
  if (scope.getExtend()) {
@@ -852,10 +836,10 @@ function init(options) {
852
836
  })
853
837
  },
854
838
  require(name) {
855
- const filepath = ext(name, config.extension.module);
839
+ const filepath = ext(name, config.extension);
856
840
  const scope = new view.scope({});
857
841
  return view.output(filepath, scope).then(() => {
858
- return scope.clone(true)
842
+ return scope.getMacro()
859
843
  })
860
844
  },
861
845
  helpers(methods) {
@@ -871,10 +855,14 @@ function init(options) {
871
855
  defaults.resolver,
872
856
  options.resolver
873
857
  );
874
- config.extension = extend({}, defaults.extension, options.extension);
858
+ config.extension = typeProp(
859
+ isString,
860
+ defaults.extension,
861
+ options.extension
862
+ );
875
863
  config.token = extend({}, defaults.token, options.token);
876
864
  config.vars = extend({}, defaults.vars, options.vars);
877
- view.scope = Scope(config, helpers);
865
+ view.scope = configure(config, helpers);
878
866
  view.compile = Compiler(config);
879
867
  view.wrapper = Wrapper(config);
880
868
  view.cache = Cache(config);