@kosatyi/ejs 0.0.1-alpha.1

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.js ADDED
@@ -0,0 +1,959 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define(factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ejs = factory());
5
+ })(this, (function () { 'use strict';
6
+
7
+ var path = {};
8
+
9
+ var defaults = {};
10
+ defaults["export"] = 'ejs.precompiled';
11
+ defaults.path = 'views';
12
+ defaults.resolver = null;
13
+ defaults.extension = {
14
+ template: 'ejs',
15
+ module: 'mjs'
16
+ };
17
+ defaults.vars = {
18
+ EXTEND: '$$$',
19
+ BUFFER: '$$a',
20
+ OUTPUT: '$$i',
21
+ LAYOUT: '$$l',
22
+ MACROS: '$$m',
23
+ PRINT: '$$j',
24
+ BLOCKS: '$$b',
25
+ ERROR: '$$e',
26
+ SCOPE: '$$s',
27
+ SAFE: '$$v'
28
+ };
29
+ defaults.token = {
30
+ start: '<%',
31
+ end: '%>',
32
+ regex: '([\\s\\S]+?)'
33
+ };
34
+
35
+ var typeProp = function typeProp() {
36
+ var args = [].slice.call(arguments);
37
+ var callback = args.shift();
38
+ return args.filter(callback).pop();
39
+ };
40
+ var isFunction = function isFunction(v) {
41
+ return typeof v === 'function';
42
+ };
43
+ var isString = function isString(v) {
44
+ return typeof v === 'string';
45
+ };
46
+
47
+ var isNode = new Function('try {return this===global;}catch(e){return false;}');
48
+ var symbolEntities = {
49
+ "'": "'",
50
+ '\\': '\\',
51
+ '\r': 'r',
52
+ '\n': 'n',
53
+ '\t': 't',
54
+ "\u2028": 'u2028',
55
+ "\u2029": 'u2029'
56
+ };
57
+ var htmlEntities = {
58
+ '&': '&amp;',
59
+ '<': '&lt;',
60
+ '>': '&gt;',
61
+ '"': '&quot;',
62
+ "'": '&#x27;'
63
+ };
64
+
65
+ var regexKeys = function regexKeys(obj) {
66
+ return new RegExp(['[', Object.keys(obj).join(''), ']'].join(''), 'g');
67
+ };
68
+
69
+ var htmlEntitiesMatch = regexKeys(htmlEntities);
70
+ var symbolEntitiesMatch = regexKeys(symbolEntities);
71
+ var entities = function entities() {
72
+ var string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
73
+ return ('' + string).replace(htmlEntitiesMatch, function (match) {
74
+ return htmlEntities[match];
75
+ });
76
+ };
77
+ var symbols = function symbols(string) {
78
+ return ('' + string).replace(symbolEntitiesMatch, function (match) {
79
+ return '\\' + symbolEntities[match];
80
+ });
81
+ };
82
+ var safeValue = function safeValue(value, escape, check) {
83
+ return (check = value) == null ? '' : escape ? entities(check) : check;
84
+ };
85
+ var getPath = function getPath(context, name) {
86
+ var data = context;
87
+ var chunk = name.split('.');
88
+ var prop = chunk.pop();
89
+ chunk.forEach(function (part) {
90
+ data = data[part] = data[part] || {};
91
+ });
92
+ return [data, prop];
93
+ };
94
+ var extend = function extend() {
95
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
96
+ args[_key] = arguments[_key];
97
+ }
98
+
99
+ var target = args.shift();
100
+ return args.filter(function (source) {
101
+ return source;
102
+ }).reduce(function (target, source) {
103
+ return Object.assign(target, source);
104
+ }, target);
105
+ };
106
+ var noop = function noop() {};
107
+ var each = function each(object, callback) {
108
+ var prop;
109
+
110
+ for (prop in object) {
111
+ if (hasProp(object, prop)) {
112
+ callback(object[prop], prop, object);
113
+ }
114
+ }
115
+ };
116
+ var map = function map(object, callback, context) {
117
+ var result = [];
118
+ each(object, function (value, key, object) {
119
+ var item = callback(value, key, object);
120
+
121
+ if (item !== undefined) {
122
+ result.push(item);
123
+ }
124
+ });
125
+ return result;
126
+ };
127
+ var filter = function filter(object, callback, context) {
128
+ var isArray = object instanceof Array;
129
+ var result = isArray ? [] : {};
130
+ each(object, function (value, key, object) {
131
+ var item = callback(value, key, object);
132
+
133
+ if (item !== undefined) {
134
+ if (isArray) {
135
+ result.push(item);
136
+ } else {
137
+ result[key] = item;
138
+ }
139
+ }
140
+ });
141
+ return result;
142
+ };
143
+ var omit = function omit(object, list) {
144
+ return filter(object, function (value, key) {
145
+ if (list.indexOf(key) === -1) {
146
+ return value;
147
+ }
148
+ });
149
+ };
150
+ var hasProp = function hasProp(object, prop) {
151
+ return object && object.hasOwnProperty(prop);
152
+ };
153
+
154
+ var selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
155
+ var space = ' ';
156
+ var quote = '"';
157
+ var equal = '=';
158
+ var slash = '/';
159
+ var lt = '<';
160
+ var gt = '>';
161
+ function element(tag, attrs, content) {
162
+ var result = [];
163
+ var hasClosedTag = selfClosed.indexOf(tag) === -1;
164
+ var attributes = map(attrs, function (value, key) {
165
+ if (value !== null && value !== undefined) {
166
+ return [entities(key), [quote, entities(value), quote].join('')].join(equal);
167
+ }
168
+ }).join(space);
169
+ result.push([lt, tag, space, attributes, gt].join(''));
170
+
171
+ if (content) {
172
+ result.push(content instanceof Array ? content.join('') : content);
173
+ }
174
+
175
+ if (hasClosedTag) {
176
+ result.push([lt, slash, tag, gt].join(''));
177
+ }
178
+
179
+ return result.join('');
180
+ }
181
+
182
+ var tags = [{
183
+ symbol: '-',
184
+ format: function format(value) {
185
+ return "'+\n".concat(this.SAFE, "(").concat(value, ",1)+\n'");
186
+ }
187
+ }, {
188
+ symbol: '=',
189
+ format: function format(value) {
190
+ return "'+\n".concat(this.SAFE, "(").concat(value, ")+\n'");
191
+ }
192
+ }, {
193
+ symbol: '#',
194
+ format: function format(value) {
195
+ return "'+\n/**".concat(value, "**/+\n'");
196
+ }
197
+ }, {
198
+ symbol: '',
199
+ format: function format(value) {
200
+ return "')\n".concat(value, "\n").concat(this.BUFFER, "('");
201
+ }
202
+ }];
203
+
204
+ var match = function match(regex, text, callback) {
205
+ var index = 0;
206
+ text.replace(regex, function () {
207
+ var params = [].slice.call(arguments, 0, -1);
208
+ var offset = params.pop();
209
+ var match = params.shift();
210
+ callback(params, index, offset);
211
+ index = offset + match.length;
212
+ return match;
213
+ });
214
+ };
215
+ /**
216
+ *
217
+ * @param {Object} config
218
+ * @return {function(*, *): Function}
219
+ * @constructor
220
+ */
221
+
222
+
223
+ var Compiler = function Compiler(config) {
224
+ var token = config.token;
225
+ var vars = config.vars;
226
+ var module = config.extension.module;
227
+ var matches = [];
228
+ var formats = [];
229
+ var slurp = {
230
+ match: '[ \\t]*',
231
+ start: [token.start, '_'],
232
+ end: ['_', token.end]
233
+ };
234
+ tags.forEach(function (item) {
235
+ matches.push(token.start.concat(item.symbol).concat(token.regex).concat(token.end));
236
+ formats.push(item.format.bind(vars));
237
+ });
238
+ var regex = new RegExp(matches.join('|').concat('|$'), 'g');
239
+ var slurpStart = new RegExp([slurp.match, slurp.start].join(''), 'gm');
240
+ var slurpEnd = new RegExp([slurp.end, slurp.match].join(''), 'gm');
241
+ /**
242
+ * @type Function
243
+ * @name Compile
244
+ */
245
+
246
+ return function (content, path) {
247
+ var SCOPE = vars.SCOPE,
248
+ SAFE = vars.SAFE,
249
+ BUFFER = vars.BUFFER;
250
+ var extension = path.split('.').pop();
251
+ content = content.replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
252
+ content = content.replace(slurpStart, slurp.start).replace(slurpEnd, slurp.end);
253
+
254
+ if (extension === module) {
255
+ content = [token.start, content, token.end].join('\n');
256
+ }
257
+
258
+ var source = "".concat(BUFFER, "('");
259
+ match(regex, content, function (params, index, offset) {
260
+ source += symbols(content.slice(index, offset));
261
+ params.forEach(function (value, index) {
262
+ if (value) source += formats[index](value);
263
+ });
264
+ });
265
+ source += "');";
266
+ source = "with(".concat(SCOPE, "){").concat(source, "}");
267
+ source = "".concat(BUFFER, ".start();").concat(source, "return ").concat(BUFFER, ".end();");
268
+ source += "\n//# sourceURL=".concat(path);
269
+ var result = null;
270
+
271
+ try {
272
+ result = new Function(SCOPE, BUFFER, SAFE, source);
273
+ result.source = "(function(".concat(SCOPE, ",").concat(BUFFER, ",").concat(SAFE, "){\n").concat(source, "\n})"); //result.source = result.toString()
274
+ } catch (e) {
275
+ e.filename = path;
276
+ e.source = source;
277
+ throw e;
278
+ }
279
+
280
+ return result;
281
+ };
282
+ };
283
+
284
+ var Wrapper = function Wrapper(config) {
285
+ var name = config["export"];
286
+ return function (list) {
287
+ var out = '(function(o){\n';
288
+ list.forEach(function (item) {
289
+ out += 'o[' + JSON.stringify(item.name) + ']=' + String(item.content) + '\n';
290
+ });
291
+ out += '})(window["' + name + '"] = window["' + name + '"] || {});\n';
292
+ return out;
293
+ };
294
+ };
295
+
296
+ var HttpRequest = function HttpRequest(template) {
297
+ return window.fetch(template).then(function (response) {
298
+ return response.text();
299
+ });
300
+ };
301
+
302
+ var FileSystem = function FileSystem(template) {
303
+ return new Promise(function (resolve, reject) {
304
+ path.readFile(template, function (error, data) {
305
+ if (error) {
306
+ reject(error);
307
+ } else {
308
+ resolve(data.toString());
309
+ }
310
+ });
311
+ });
312
+ };
313
+
314
+ var Watcher = function Watcher(path$1, cache) {
315
+ return path.watch('.', {
316
+ cwd: path$1
317
+ }).on('change', function (name) {
318
+ cache.remove(name);
319
+ }).on('error', function (error) {
320
+ console.log('watcher error: ' + error);
321
+ });
322
+ };
323
+
324
+ var Template = function Template(config, cache, compile) {
325
+ var path = config.path;
326
+ config.token || {};
327
+ var resolver = isFunction(config.resolver) ? config.resolver : isNode() ? FileSystem : HttpRequest;
328
+
329
+ var normalize = function normalize(template) {
330
+ template = [path, template].join('/');
331
+ template = template.replace(/\/\//g, '/');
332
+ return template;
333
+ };
334
+
335
+ var resolve = function resolve(template) {
336
+ return resolver(normalize(template));
337
+ };
338
+
339
+ var result = function result(content, template) {
340
+ cache.set(template, content);
341
+ return content;
342
+ };
343
+
344
+ var get = function get(template) {
345
+ if (cache.exist(template)) {
346
+ return cache.resolve(template);
347
+ }
348
+
349
+ var content = resolve(template).then(function (content) {
350
+ return result(compile(content, template), template);
351
+ });
352
+ return result(content, template);
353
+ };
354
+
355
+ if (config.watch && isNode()) {
356
+ Watcher(path, cache);
357
+ }
358
+
359
+ return get;
360
+ };
361
+
362
+ function _defineProperty(obj, key, value) {
363
+ if (key in obj) {
364
+ Object.defineProperty(obj, key, {
365
+ value: value,
366
+ enumerable: true,
367
+ configurable: true,
368
+ writable: true
369
+ });
370
+ } else {
371
+ obj[key] = value;
372
+ }
373
+
374
+ return obj;
375
+ }
376
+
377
+ var resolve = function resolve(list) {
378
+ return Promise.all(list).then(function (list) {
379
+ return list.join('');
380
+ });
381
+ };
382
+ /**
383
+ *
384
+ * @return {function}
385
+ */
386
+
387
+
388
+ var Buffer = function Buffer() {
389
+ var store = [],
390
+ array = [];
391
+
392
+ function buffer(value) {
393
+ array.push(value);
394
+ }
395
+
396
+ buffer.start = function () {
397
+ array = [];
398
+ };
399
+
400
+ buffer.backup = function () {
401
+ store.push(array.concat());
402
+ array = [];
403
+ };
404
+
405
+ buffer.restore = function () {
406
+ var result = array.concat();
407
+ array = store.pop();
408
+ return resolve(result);
409
+ };
410
+
411
+ buffer.end = function () {
412
+ return resolve(array);
413
+ };
414
+
415
+ return buffer;
416
+ };
417
+
418
+ /**
419
+ *
420
+ * @param {{}} instance
421
+ * @method create
422
+ */
423
+
424
+ var Component = function Component(instance) {
425
+ var defaults = extend({}, instance.props);
426
+ var create = instance.create;
427
+ return {
428
+ element: element,
429
+ create: create,
430
+ render: function render(props) {
431
+ return this.create(extend({}, defaults, props));
432
+ }
433
+ };
434
+ };
435
+
436
+ var Scope = function Scope(config, methods) {
437
+ var _Object$definePropert;
438
+
439
+ /**
440
+ *
441
+ */
442
+ var _config$vars = config.vars,
443
+ EXTEND = _config$vars.EXTEND,
444
+ MACROS = _config$vars.MACROS,
445
+ LAYOUT = _config$vars.LAYOUT,
446
+ BLOCKS = _config$vars.BLOCKS,
447
+ BUFFER = _config$vars.BUFFER;
448
+ /**
449
+ *
450
+ * @param data
451
+ * @constructor
452
+ */
453
+
454
+ function Scope() {
455
+ var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
456
+ extend(this, data);
457
+ }
458
+ /**
459
+ *
460
+ */
461
+
462
+
463
+ Object.defineProperties(Scope.prototype, (_Object$definePropert = {}, _defineProperty(_Object$definePropert, BUFFER, {
464
+ value: Buffer(),
465
+ writable: false,
466
+ configurable: false,
467
+ enumerable: false
468
+ }), _defineProperty(_Object$definePropert, BLOCKS, {
469
+ value: {},
470
+ writable: false,
471
+ configurable: false,
472
+ enumerable: false
473
+ }), _defineProperty(_Object$definePropert, EXTEND, {
474
+ value: false,
475
+ writable: true,
476
+ configurable: false,
477
+ enumerable: false
478
+ }), _defineProperty(_Object$definePropert, LAYOUT, {
479
+ value: false,
480
+ writable: true,
481
+ configurable: false,
482
+ enumerable: false
483
+ }), _defineProperty(_Object$definePropert, "setBuffer", {
484
+ value: function value(_value) {
485
+ this[BUFFER] = _value;
486
+ },
487
+ writable: false,
488
+ configurable: false
489
+ }), _defineProperty(_Object$definePropert, "getBuffer", {
490
+ value: function value() {
491
+ return this[BUFFER];
492
+ },
493
+ writable: false,
494
+ configurable: false
495
+ }), _defineProperty(_Object$definePropert, "setBlocks", {
496
+ value: function value(_value2) {
497
+ this[BLOCKS] = _value2;
498
+ },
499
+ writable: false,
500
+ configurable: false
501
+ }), _defineProperty(_Object$definePropert, "getBlocks", {
502
+ value: function value() {
503
+ return this[BLOCKS];
504
+ },
505
+ writable: false,
506
+ configurable: false
507
+ }), _defineProperty(_Object$definePropert, "setExtend", {
508
+ value: function value(_value3) {
509
+ this[EXTEND] = _value3;
510
+ },
511
+ writable: false,
512
+ configurable: false
513
+ }), _defineProperty(_Object$definePropert, "getExtend", {
514
+ value: function value() {
515
+ return this[EXTEND];
516
+ },
517
+ writable: false,
518
+ configurable: false
519
+ }), _defineProperty(_Object$definePropert, "setLayout", {
520
+ value: function value(layout) {
521
+ this[LAYOUT] = layout;
522
+ },
523
+ writable: false,
524
+ configurable: false
525
+ }), _defineProperty(_Object$definePropert, "getLayout", {
526
+ value: function value() {
527
+ return this[LAYOUT];
528
+ },
529
+ writable: false,
530
+ configurable: false
531
+ }), _Object$definePropert));
532
+
533
+ Scope.helpers = function (methods) {
534
+ extend(Scope.prototype, methods);
535
+ };
536
+ /**
537
+ * @lends Scope.prototype
538
+ */
539
+
540
+
541
+ Scope.helpers(methods);
542
+ /**
543
+ * @lends Scope.prototype
544
+ */
545
+
546
+ Scope.helpers({
547
+ /**
548
+ * @return {*}
549
+ */
550
+ clone: function clone(exclude_blocks) {
551
+ var filter = [LAYOUT, EXTEND, MACROS, BUFFER];
552
+
553
+ if (exclude_blocks === true) {
554
+ filter.push(BLOCKS);
555
+ }
556
+
557
+ return omit(this, filter);
558
+ },
559
+
560
+ /**
561
+ * Join values to output buffer
562
+ * @memberOf global
563
+ * @type Function
564
+ */
565
+ echo: function echo() {
566
+ var buffer = this.getBuffer();
567
+ var params = [].slice.call(arguments);
568
+ params.forEach(function (item) {
569
+ buffer(item);
570
+ });
571
+ },
572
+
573
+ /**
574
+ * Buffered output callback
575
+ * @type Function
576
+ * @param {Function} callback
577
+ * @param {Boolean} [echo]
578
+ * @return {Function}
579
+ */
580
+ macro: function macro(callback, echo) {
581
+ var buffer = this.getBuffer();
582
+
583
+ var macro = function () {
584
+ buffer.backup();
585
+
586
+ if (isFunction(callback)) {
587
+ callback.apply(this, arguments);
588
+ }
589
+
590
+ var result = buffer.restore();
591
+ return echo === true ? this.echo(result) : result;
592
+ }.bind(this);
593
+
594
+ macro.ctx = this;
595
+ return macro;
596
+ },
597
+
598
+ /**
599
+ * @memberOf global
600
+ * @param value
601
+ * @param callback
602
+ * @return {Promise<unknown>}
603
+ */
604
+ resolve: function resolve(value, callback) {
605
+ return Promise.resolve(value).then(callback.bind(this));
606
+ },
607
+
608
+ /**
609
+ * @memberOf global
610
+ */
611
+ async: function async(promise, callback) {
612
+ var _this = this;
613
+
614
+ this.echo(this.resolve(promise, function (data) {
615
+ return _this.macro(callback)(data);
616
+ }));
617
+ },
618
+
619
+ /**
620
+ * @memberOf global
621
+ */
622
+ node: element,
623
+
624
+ /**
625
+ * @memberOf global
626
+ */
627
+ element: function element$1(tag, attr, content) {
628
+ if (isFunction(content)) {
629
+ content = this.macro(content)();
630
+ }
631
+
632
+ this.echo(this.resolve(content, function (content) {
633
+ return element(tag, attr, content);
634
+ }));
635
+ },
636
+
637
+ /**
638
+ * @memberOf global
639
+ * @param {String} namespace
640
+ * @param {Object} instance
641
+ */
642
+ component: function component(namespace, instance) {
643
+ instance = Component(instance);
644
+ this.set(namespace, function (props) {
645
+ this.echo(instance.render(props));
646
+ }.bind(this));
647
+ },
648
+
649
+ /**
650
+ * @memberOf global
651
+ * @param name
652
+ * @param defaults
653
+ */
654
+ get: function get(name, defaults) {
655
+ var path = getPath(this, name);
656
+ var result = path.shift();
657
+ var prop = path.pop();
658
+ return hasProp(result, prop) ? result[prop] : defaults;
659
+ },
660
+
661
+ /**
662
+ * @memberOf global
663
+ * @param {String} name
664
+ * @param value
665
+ * @return
666
+ */
667
+ set: function set(name, value) {
668
+ var path = getPath(this, name);
669
+ var result = path.shift();
670
+ var prop = path.pop();
671
+
672
+ if (this.getExtend()) {
673
+ if (hasProp(result, prop)) {
674
+ return result[prop];
675
+ }
676
+ }
677
+
678
+ result[prop] = value;
679
+ },
680
+
681
+ /**
682
+ * @memberOf global
683
+ * @param name
684
+ */
685
+ call: function call(name) {
686
+ var params = [].slice.call(arguments, 1);
687
+ var path = getPath(this, name);
688
+ var result = path.shift();
689
+ var prop = path.pop();
690
+
691
+ if (isFunction(result[prop])) {
692
+ return result[prop].apply(result, params);
693
+ }
694
+ },
695
+
696
+ /**
697
+ * @memberOf global
698
+ * @param object
699
+ * @param callback
700
+ */
701
+ each: function each$1(object, callback) {
702
+ if (isString(object)) {
703
+ object = this.get(object, []);
704
+ }
705
+
706
+ each(object, callback);
707
+ },
708
+
709
+ /**
710
+ * @memberOf global
711
+ * @param {String} layout
712
+ */
713
+ extend: function extend(layout) {
714
+ this.setExtend(true);
715
+ this.setLayout(layout);
716
+ },
717
+
718
+ /**
719
+ * @memberOf global
720
+ * @param name
721
+ * @param callback
722
+ * @return {*}
723
+ */
724
+ block: function block(name, callback) {
725
+ var blocks = this.getBlocks();
726
+ var macro = this.macro(callback);
727
+
728
+ if (this.getExtend()) {
729
+ blocks[name] = macro;
730
+ } else {
731
+ var block = blocks[name];
732
+
733
+ if (block) {
734
+ var parent = function () {
735
+ this.echo(macro());
736
+ }.bind(block.ctx);
737
+
738
+ this.echo(block(parent));
739
+ } else {
740
+ this.echo(macro(noop));
741
+ }
742
+ }
743
+ },
744
+
745
+ /**
746
+ * @memberOf global
747
+ * @param {string} path
748
+ * @param {object} [data]
749
+ * @param {boolean} [cx]
750
+ */
751
+ include: function include(path, data, cx) {
752
+ var context = cx === false ? {} : this.clone(true);
753
+ var params = extend(context, data || {});
754
+ var promise = this.render(path, params);
755
+ this.echo(promise);
756
+ },
757
+
758
+ /**
759
+ * @memberOf global
760
+ * @param {string} path
761
+ */
762
+ use: function use(path) {
763
+ var scope = this;
764
+
765
+ var promise = this.require(path);
766
+
767
+ this.echo(promise);
768
+ return {
769
+ as: function as(namespace) {
770
+ promise.then(function (exports) {
771
+ scope.set(namespace, exports);
772
+ });
773
+ return this;
774
+ }
775
+ };
776
+ },
777
+
778
+ /**
779
+ * @memberOf global
780
+ * @param {string} path
781
+ */
782
+ from: function from(path) {
783
+ var scope = this;
784
+
785
+ var promise = this.require(path);
786
+
787
+ this.echo(promise);
788
+ return {
789
+ use: function use() {
790
+ var params = [].slice.call(arguments);
791
+ promise.then(function (exports) {
792
+ params.forEach(function (name) {
793
+ scope.set(name, exports[name]);
794
+ });
795
+ });
796
+ return this;
797
+ }
798
+ };
799
+ }
800
+ });
801
+ return Scope;
802
+ };
803
+
804
+ function Cache(config) {
805
+ var namespace = config["export"];
806
+ var list = {};
807
+ var cache = {
808
+ preload: function preload() {
809
+ if (isNode() === false) {
810
+ this.load(window[namespace]);
811
+ }
812
+
813
+ return this;
814
+ },
815
+ exist: function exist(key) {
816
+ return hasProp(list, key);
817
+ },
818
+ get: function get(key) {
819
+ return list[key];
820
+ },
821
+ remove: function remove(key) {
822
+ delete list[key];
823
+ },
824
+ resolve: function resolve(key) {
825
+ return Promise.resolve(this.get(key));
826
+ },
827
+ set: function set(key, value) {
828
+ list[key] = value;
829
+ return this;
830
+ },
831
+ load: function load(data) {
832
+ extend(list, data);
833
+ return this;
834
+ }
835
+ };
836
+ return cache.preload();
837
+ }
838
+
839
+ function init(options) {
840
+ /**
841
+ * @type {Object}
842
+ */
843
+ var config = {};
844
+ var _helpers = {};
845
+
846
+ var ext = function ext(path, defaults) {
847
+ var ext = path.split('.').pop();
848
+
849
+ if (ext !== defaults) {
850
+ path = [path, defaults].join('.');
851
+ }
852
+
853
+ return path;
854
+ };
855
+
856
+ var view = {
857
+ element: element,
858
+ output: function output(path, scope) {
859
+ return view.template(path).then(function (template) {
860
+ return template.call(scope, scope, scope.getBuffer(), safeValue);
861
+ });
862
+ },
863
+ render: function render(name, data) {
864
+ var filepath = ext(name, config.extension.template);
865
+ var scope = new view.scope(data);
866
+ return view.output(filepath, scope).then(function (content) {
867
+ if (scope.getExtend()) {
868
+ scope.setExtend(false);
869
+ var layout = scope.getLayout();
870
+
871
+ var _data = scope.clone();
872
+
873
+ return view.render(layout, _data);
874
+ }
875
+
876
+ return content;
877
+ });
878
+ },
879
+ require: function require(name) {
880
+ var filepath = ext(name, config.extension.module);
881
+ var scope = new view.scope({});
882
+ return view.output(filepath, scope).then(function () {
883
+ return scope.clone(true);
884
+ });
885
+ },
886
+ helpers: function helpers(methods) {
887
+ methods = methods || {};
888
+ extend(_helpers, methods);
889
+ view.scope.helpers(methods);
890
+ },
891
+ configure: function configure(options) {
892
+ config["export"] = typeProp(isString, defaults["export"], options["export"]);
893
+ config.path = typeProp(isString, defaults.path, options.path);
894
+ config.resolver = typeProp(isFunction, defaults.resolver, options.resolver);
895
+ config.extension = extend({}, defaults.extension, options.extension);
896
+ config.token = extend({}, defaults.token, options.token);
897
+ config.vars = extend({}, defaults.vars, options.vars);
898
+ view.scope = Scope(config, _helpers);
899
+ view.compile = Compiler(config);
900
+ view.wrapper = Wrapper(config);
901
+ view.cache = Cache(config);
902
+ view.template = Template(config, view.cache, view.compile);
903
+ return view;
904
+ }
905
+ };
906
+ /**
907
+ *
908
+ * @param name
909
+ * @param options
910
+ * @param callback
911
+ * @return {*}
912
+ * @private
913
+ */
914
+
915
+ view.__express = function (name, options, callback) {
916
+ if (isFunction(options)) {
917
+ callback = options;
918
+ options = {};
919
+ }
920
+
921
+ options = options || {};
922
+ var settings = options.settings || {};
923
+ var viewPath = settings['views'];
924
+ var viewOptions = settings['view options'] || {};
925
+ var filename = path.relative(viewPath, name);
926
+ viewOptions.path = viewPath;
927
+ view.configure(viewOptions);
928
+ return view.render(filename, options).then(function (content) {
929
+ callback(null, content);
930
+ })["catch"](function (error) {
931
+ callback(error);
932
+ });
933
+ };
934
+ /**
935
+ *
936
+ */
937
+
938
+
939
+ view.configure(options || {});
940
+ /**
941
+ *
942
+ */
943
+
944
+ view.helpers({
945
+ require: function require(name) {
946
+ return view.require(name, this);
947
+ },
948
+ render: function render(name, data) {
949
+ return view.render(name, data);
950
+ }
951
+ });
952
+ return view;
953
+ }
954
+
955
+ var index = init();
956
+
957
+ return index;
958
+
959
+ }));