@kosatyi/ejs 0.0.13 → 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,276 +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
-
461
442
  /**
462
- *
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
463
453
  */
464
- Object.defineProperties(Scope.prototype, {
465
- [BUFFER]: {
466
- value: Buffer(),
454
+ Scope.property = (name, descriptor) => {
455
+ Object.defineProperty(Scope.prototype, name, descriptor);
456
+ };
457
+ /**
458
+ * @static
459
+ * @param name
460
+ * @param method
461
+ */
462
+ Scope.method = (name, method) => {
463
+ return Scope.property(name,{
464
+ value: method,
467
465
  writable: false,
468
466
  configurable: false,
469
467
  enumerable: false
470
- },
471
- // [BLOCKS]: {
472
- // value: {},
473
- // writable: true,
474
- // configurable: false,
475
- // enumerable: false
476
- // },
477
- // [EXTEND]: {
478
- // value: false,
479
- // writable: true,
480
- // configurable: false,
481
- // enumerable: false
482
- // },
483
- // [LAYOUT]: {
484
- // value: false,
485
- // writable: true,
486
- // configurable: false,
487
- // enumerable: false
488
- // },
489
- // setBuffer: {
490
- // value(value) {
491
- // this[BUFFER] = value
492
- // },
493
- // writable: false,
494
- // configurable: false
495
- // },
496
- getBuffer: {
497
- value() {
498
- return this[BUFFER]
499
- },
500
- writable: false,
501
- configurable: false
502
- },
503
- setBlocks: {
504
- value(value) {
505
- this[BLOCKS] = value;
506
- },
507
- writable: false,
508
- configurable: false
509
- },
510
- getBlocks: {
511
- value() {
512
- return this[BLOCKS]
513
- },
514
- writable: false,
515
- configurable: false
516
- },
517
- setExtend: {
518
- value(value) {
519
- this[EXTEND] = value;
520
- },
521
- writable: false,
522
- configurable: false
523
- },
524
- getExtend: {
525
- value() {
526
- return this[EXTEND]
527
- },
528
- writable: false,
529
- configurable: false
530
- },
531
- setLayout: {
532
- value(layout) {
533
- this[LAYOUT] = layout;
534
- },
535
- writable: false,
536
- configurable: false
537
- },
538
- getLayout: {
539
- value() {
540
- return this[LAYOUT]
541
- },
542
- writable: false,
543
- 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);
544
576
  }
577
+ return omit(this, filter)
545
578
  });
546
-
547
- Scope.helpers = (methods) => {
548
- extend(Scope.prototype, methods);
549
- };
550
-
551
579
  /**
552
- * @lends Scope.prototype
580
+ * @methodOf global
581
+ * @function extend
582
+ * @param layout
553
583
  */
554
- Scope.helpers(methods);
584
+ Scope.method('extend', function(layout) {
585
+ this.setExtend(true);
586
+ this.setLayout(layout);
587
+ });
555
588
  /**
556
- * @lends Scope.prototype
589
+ * @memberOf global
590
+ * @function echo
557
591
  */
558
- Scope.helpers({
559
- /**
560
- * @return {*}
561
- */
562
- clone(exclude_blocks) {
563
- const filter = [LAYOUT, EXTEND, MACROS, BUFFER];
564
- if (exclude_blocks === true) {
565
- 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);
566
611
  }
567
- return omit(this, filter)
568
- },
569
- /**
570
- * Join values to output buffer
571
- * @memberOf global
572
- * @type Function
573
- */
574
- echo() {
575
- const buffer = this.getBuffer();
576
- const params = [].slice.call(arguments);
577
- params.forEach(function(item) {
578
- buffer(item);
579
- });
580
- },
581
- /**
582
- * Buffered output callback
583
- * @type Function
584
- * @param {Function} callback
585
- * @param {Boolean} [echo]
586
- * @return {Function}
587
- */
588
- macro(callback, echo) {
589
- const buffer = this.getBuffer();
590
- const macro = function() {
591
- buffer.backup();
592
- if (isFunction(callback)) {
593
- 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()));
594
690
  }
595
- const result = buffer.restore();
596
- return echo === true ? this.echo(result) : result
597
- }.bind(this);
598
- macro.__context = this;
599
- macro.__source = callback;
600
- macro.__layout = this.getLayout();
601
- return macro
602
- },
603
- /**
604
- * @memberOf global
605
- * @param value
606
- * @param callback
607
- * @return {Promise<unknown>}
608
- */
609
- resolve(value, callback) {
610
- return Promise.resolve(value).then(callback.bind(this))
611
- },
612
- /**
613
- * @memberOf global
614
- */
615
- async(promise, callback) {
616
- this.echo(
617
- this.resolve(promise, (data) => this.macro(callback)(data))
618
- );
619
- },
620
- /**
621
- * @memberOf global
622
- */
623
- 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({
624
740
  /**
625
741
  * @memberOf global
742
+ * @param tag
743
+ * @param attr
744
+ * @param content
626
745
  */
627
- element(tag, attr, content) {
746
+ el(tag, attr, content) {
628
747
  if (isFunction(content)) {
629
- content = this.macro(content)();
748
+ content = this.fn(content)();
630
749
  }
631
750
  this.echo(
632
- this.resolve(content, (content) => element(tag, attr, content))
633
- );
634
- },
635
- /**
636
- * @memberOf global
637
- * @param {String} namespace
638
- * @param {Object} instance
639
- */
640
- component(namespace, instance) {
641
- instance = Component(instance);
642
- this.set(
643
- namespace,
644
- function(props) {
645
- this.echo(instance.render(props));
646
- }.bind(this)
751
+ resolve$1(content, function(content) {
752
+ return element(tag, attr, content)
753
+ }, this)
647
754
  );
648
755
  },
649
- /**
650
- * @memberOf global
651
- * @param name
652
- * @param defaults
653
- */
654
- get(name, defaults) {
655
- const path = getPath(this, name);
656
- const result = path.shift();
657
- const prop = path.pop();
658
- return hasProp(result, prop) ? result[prop] : defaults
659
- },
660
- /**
661
- * @memberOf global
662
- * @param {String} name
663
- * @param value
664
- * @return
665
- */
666
- set(name, value) {
667
- const path = getPath(this, name);
668
- const result = path.shift();
669
- const prop = path.pop();
670
- if (this.getExtend()) {
671
- if (hasProp(result, prop)) {
672
- return result[prop]
673
- }
674
- }
675
- result[prop] = value;
676
- },
677
- /**
678
- * @memberOf global
679
- * @param name
680
- */
681
- call(name) {
682
- const params = [].slice.call(arguments, 1);
683
- const path = getPath(this, name);
684
- const result = path.shift();
685
- const prop = path.pop();
686
- if (isFunction(result[prop])) {
687
- return result[prop].apply(result, params)
688
- }
689
- },
690
756
  /**
691
757
  * @memberOf global
692
758
  * @param object
@@ -697,91 +763,6 @@ const Scope = (config, methods) => {
697
763
  object = this.get(object, []);
698
764
  }
699
765
  each(object, callback);
700
- },
701
- /**
702
- * @memberOf global
703
- * @param {String} layout
704
- */
705
- extend(layout) {
706
- this.setExtend(true);
707
- this.setLayout(layout);
708
- },
709
- /**
710
- * @memberOf global
711
- * @param name
712
- * @param callback
713
- * @return {*}
714
- */
715
- block(name, callback) {
716
- const blocks = this.getBlocks();
717
- blocks[name] = blocks[name] || [];
718
- blocks[name].push(this.macro(callback));
719
- if (this.getExtend()) return
720
- const current = function(){
721
- return blocks[name].shift()
722
- };
723
- const next = function() {
724
- const parent = current();
725
- if (parent) {
726
- const context = parent.__context;
727
- return function() {
728
- context.echo(parent(next()));
729
- }
730
- } else {
731
- return function() {
732
- }
733
- }
734
- };
735
- this.echo(current()(next()));
736
- },
737
- /**
738
- * @memberOf global
739
- * @param {string} path
740
- * @param {object} [data]
741
- * @param {boolean} [cx]
742
- */
743
- include(path, data, cx) {
744
- const context = cx === false ? {} : this.clone(true);
745
- const params = extend(context, data || {});
746
- const promise = this.render(path, params);
747
- this.echo(promise);
748
- },
749
- /**
750
- * @memberOf global
751
- * @param {string} path
752
- */
753
- use(path) {
754
- const scope = this;
755
- const promise = this.require(path);
756
- this.echo(promise);
757
- return {
758
- as(namespace) {
759
- promise.then((exports) => {
760
- scope.set(namespace, exports);
761
- });
762
- return this
763
- }
764
- }
765
- },
766
- /**
767
- * @memberOf global
768
- * @param {string} path
769
- */
770
- from(path) {
771
- const scope = this;
772
- const promise = this.require(path);
773
- this.echo(promise);
774
- return {
775
- use() {
776
- const params = [].slice.call(arguments);
777
- promise.then((exports) => {
778
- params.forEach((name) => {
779
- scope.set(name, exports[name]);
780
- });
781
- });
782
- return this
783
- }
784
- }
785
766
  }
786
767
  });
787
768
  return Scope
@@ -842,7 +823,7 @@ function init(options) {
842
823
  })
843
824
  },
844
825
  render(name, data) {
845
- const filepath = ext(name, config.extension.template);
826
+ const filepath = ext(name, config.extension);
846
827
  const scope = new view.scope(data);
847
828
  return view.output(filepath, scope).then((content) => {
848
829
  if (scope.getExtend()) {
@@ -855,10 +836,10 @@ function init(options) {
855
836
  })
856
837
  },
857
838
  require(name) {
858
- const filepath = ext(name, config.extension.module);
839
+ const filepath = ext(name, config.extension);
859
840
  const scope = new view.scope({});
860
841
  return view.output(filepath, scope).then(() => {
861
- return scope.clone(true)
842
+ return scope.getMacro()
862
843
  })
863
844
  },
864
845
  helpers(methods) {
@@ -874,10 +855,14 @@ function init(options) {
874
855
  defaults.resolver,
875
856
  options.resolver
876
857
  );
877
- config.extension = extend({}, defaults.extension, options.extension);
858
+ config.extension = typeProp(
859
+ isString,
860
+ defaults.extension,
861
+ options.extension
862
+ );
878
863
  config.token = extend({}, defaults.token, options.token);
879
864
  config.vars = extend({}, defaults.vars, options.vars);
880
- view.scope = Scope(config, helpers);
865
+ view.scope = configure(config, helpers);
881
866
  view.compile = Compiler(config);
882
867
  view.wrapper = Wrapper(config);
883
868
  view.cache = Cache(config);