@kosatyi/ejs 0.0.61 → 0.0.63

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
@@ -1,5 +1,5 @@
1
- import fs from 'fs';
2
1
  import path from 'path';
2
+ import fs from 'fs';
3
3
 
4
4
  const typeProp = function () {
5
5
  const args = [].slice.call(arguments);
@@ -58,32 +58,36 @@ const symbols = (string) => {
58
58
  )
59
59
  };
60
60
 
61
- const safeValue = (value, escape, check) => {
62
- return (check = value) == null ? '' : escape ? entities(check) : check
61
+ const safeValue = (value, escape) => {
62
+ const check = value;
63
+ return check == null ? '' : escape === true ? entities(check) : check
64
+ };
65
+
66
+ const instanceOf = (object, instance) => {
67
+ return object instanceof instance
63
68
  };
64
69
 
65
70
  const getPath = (context, name, strict) => {
66
71
  let data = context;
67
72
  let chunks = String(name).split('.');
68
73
  let prop = chunks.pop();
69
- for (const part of chunks) {
74
+ for (let i = 0; i < chunks.length; i++) {
75
+ const part = chunks[i];
76
+ if (isFunction(data['toJSON'])) {
77
+ data = data.toJSON();
78
+ }
70
79
  if (strict && data.hasOwnProperty(part) === false) {
71
80
  data = {};
72
81
  break
73
82
  }
74
83
  data = data[part] = data[part] || {};
75
84
  }
85
+ if (isFunction(data['toJSON'])) {
86
+ data = data.toJSON();
87
+ }
76
88
  return [data, prop]
77
89
  };
78
90
 
79
- const bindContext = (object, context, methods = []) => {
80
- methods.forEach((name) => {
81
- if (name in object) {
82
- object[name] = object[name].bind(context);
83
- }
84
- });
85
- };
86
-
87
91
  const ext = (path, defaults) => {
88
92
  const ext = path.split('.').pop();
89
93
  if (ext !== defaults) {
@@ -96,9 +100,7 @@ const extend = (...args) => {
96
100
  const target = args.shift();
97
101
  return args
98
102
  .filter((source) => source)
99
- .reduce((target, source) => {
100
- return Object.assign(target, source)
101
- }, target)
103
+ .reduce((target, source) => Object.assign(target, source), target)
102
104
  };
103
105
 
104
106
  const noop = () => {};
@@ -193,6 +195,51 @@ defaults.token = {
193
195
  regex: '([\\s\\S]+?)',
194
196
  };
195
197
 
198
+ const selfClosed = [
199
+ 'area',
200
+ 'base',
201
+ 'br',
202
+ 'col',
203
+ 'embed',
204
+ 'hr',
205
+ 'img',
206
+ 'input',
207
+ 'link',
208
+ 'meta',
209
+ 'param',
210
+ 'source',
211
+ 'track',
212
+ 'wbr',
213
+ ];
214
+
215
+ const space = ' ';
216
+ const quote = '"';
217
+ const equal = '=';
218
+ const slash = '/';
219
+ const lt = '<';
220
+ const gt = '>';
221
+
222
+ const element = (tag, attrs, content) => {
223
+ const result = [];
224
+ const hasClosedTag = selfClosed.indexOf(tag) === -1;
225
+ const attributes = map(attrs, (value, key) => {
226
+ if (value !== null && value !== undefined) {
227
+ return [
228
+ entities(key),
229
+ [quote, entities(value), quote].join(''),
230
+ ].join(equal)
231
+ }
232
+ }).join(space);
233
+ result.push([lt, tag, space, attributes, gt].join(''));
234
+ if (content) {
235
+ result.push(content instanceof Array ? content.join('') : content);
236
+ }
237
+ if (hasClosedTag) {
238
+ result.push([lt, slash, tag, gt].join(''));
239
+ }
240
+ return result.join('')
241
+ };
242
+
196
243
  const configSchema = (config, options) => {
197
244
  extend(config, {
198
245
  path: typeProp(isString, defaults.path, config.path, options.path),
@@ -262,60 +309,63 @@ const fileSystem = (path, template) => {
262
309
  })
263
310
  };
264
311
 
265
- class Template {
266
- constructor(config, cache, compiler) {
267
- this.cache = cache;
268
- this.watcher = {
269
- unwatch() {},
270
- on() {},
271
- };
272
- this.compiler = compiler;
273
- this.configure(config);
274
- }
275
- configure(config) {
276
- this.path = config.path;
277
- this.chokidar = config.chokidar;
278
- this.resolver = isFunction(config.resolver)
279
- ? config.resolver
280
- : isNode()
281
- ? fileSystem
282
- : httpRequest;
283
- if (config.watch && isNode()) {
284
- if (this.watcher) {
285
- this.watcher.unwatch('.');
286
- }
287
- if (this.chokidar) {
288
- this.watcher = this.chokidar
289
- .watch('.', { cwd: this.path })
290
- .on('change', (name) => {
291
- this.cache.remove(name);
292
- });
293
- }
294
- }
295
- }
296
- resolve(template) {
297
- return this.resolver(this.path, template)
298
- }
299
- result(template, content) {
300
- this.cache.set(template, content);
312
+ const fileResolver = (resolver) => {
313
+ return isFunction(resolver) ? resolver : isNode() ? fileSystem : httpRequest
314
+ };
315
+
316
+ function Template(config, cache, compiler) {
317
+ if (instanceOf(this, Template) === false)
318
+ return new Template(config, cache, compiler)
319
+
320
+ const template = {};
321
+
322
+ const result = function (template, content) {
323
+ cache.set(template, content);
301
324
  return content
302
- }
303
- compile(content, template) {
325
+ };
326
+
327
+ const resolve = function (path) {
328
+ return template.resolver(template.path, path)
329
+ };
330
+
331
+ const compile = function (content, template) {
304
332
  if (isFunction(content)) {
305
333
  return content
306
334
  } else {
307
- return this.compiler.compile(content, template)
335
+ return compiler.compile(content, template)
308
336
  }
309
- }
310
- get(template) {
311
- if (this.cache.exist(template)) {
312
- return this.cache.resolve(template)
337
+ };
338
+
339
+ const watcher = function (config) {
340
+ if (template.watcher) {
341
+ template.watcher.unwatch('.');
313
342
  }
314
- const content = this.resolve(template).then((content) =>
315
- this.result(template, this.compile(content, template))
316
- );
317
- return this.result(template, content)
318
- }
343
+ if (config.watch && config.chokidar && isNode()) {
344
+ return config.chokidar
345
+ .watch('.', { cwd: template.path })
346
+ .on('change', (name) => {
347
+ cache.remove(name);
348
+ })
349
+ }
350
+ };
351
+ this.configure = function (config) {
352
+ template.path = config.path;
353
+ template.chokidar = config.chokidar;
354
+ template.resolver = fileResolver(config.resolver);
355
+ template.watcher = watcher(config);
356
+ };
357
+ this.get = function (template) {
358
+ if (cache.exist(template)) {
359
+ return cache.resolve(template)
360
+ }
361
+ return result(
362
+ template,
363
+ resolve(template).then((content) =>
364
+ result(template, compile(content, template))
365
+ )
366
+ )
367
+ };
368
+ this.configure(config);
319
369
  }
320
370
 
321
371
  const tagList = [
@@ -345,7 +395,7 @@ const tagList = [
345
395
  },
346
396
  ];
347
397
 
348
- const matchTokens = (regex, text, callback) => {
398
+ function matchTokens(regex, text, callback) {
349
399
  let index = 0;
350
400
  text.replace(regex, function () {
351
401
  const params = [].slice.call(arguments, 0, -1);
@@ -355,71 +405,70 @@ const matchTokens = (regex, text, callback) => {
355
405
  index = offset + match.length;
356
406
  return match
357
407
  });
358
- };
408
+ }
359
409
 
360
- class Compiler {
361
- constructor(config) {
362
- this.configure(config);
363
- }
410
+ function Compiler(config) {
411
+ if (instanceOf(this, Compiler) === false) return new Compiler(config)
364
412
 
365
- configure(config) {
366
- this.withObject = config.withObject;
367
- this.rmWhitespace = config.rmWhitespace;
368
- this.token = config.token;
369
- this.vars = config.vars;
370
- this.matches = [];
371
- this.formats = [];
372
- this.slurp = {
413
+ const compiler = {};
414
+
415
+ this.configure = function (config) {
416
+ compiler.withObject = config.withObject;
417
+ compiler.rmWhitespace = config.rmWhitespace;
418
+ compiler.token = config.token;
419
+ compiler.vars = config.vars;
420
+ compiler.matches = [];
421
+ compiler.formats = [];
422
+ compiler.slurp = {
373
423
  match: '[ \\t]*',
374
- start: [this.token.start, '_'],
375
- end: ['_', this.token.end],
424
+ start: [compiler.token.start, '_'],
425
+ end: ['_', compiler.token.end],
376
426
  };
377
427
  tagList.forEach((item) => {
378
- this.matches.push(
379
- this.token.start
428
+ compiler.matches.push(
429
+ compiler.token.start
380
430
  .concat(item.symbol)
381
- .concat(this.token.regex)
382
- .concat(this.token.end)
431
+ .concat(compiler.token.regex)
432
+ .concat(compiler.token.end)
383
433
  );
384
- this.formats.push(item.format.bind(this.vars));
434
+ compiler.formats.push(item.format.bind(compiler.vars));
385
435
  });
386
- this.regex = new RegExp(this.matches.join('|').concat('|$'), 'g');
387
- this.slurpStart = new RegExp(
388
- [this.slurp.match, this.slurp.start.join('')].join(''),
436
+ compiler.regex = new RegExp(
437
+ compiler.matches.join('|').concat('|$'),
438
+ 'g'
439
+ );
440
+ compiler.slurpStart = new RegExp(
441
+ [compiler.slurp.match, compiler.slurp.start.join('')].join(''),
389
442
  'gm'
390
443
  );
391
- this.slurpEnd = new RegExp(
392
- [this.slurp.end.join(''), this.slurp.match].join(''),
444
+ compiler.slurpEnd = new RegExp(
445
+ [compiler.slurp.end.join(''), compiler.slurp.match].join(''),
393
446
  'gm'
394
447
  );
395
- }
396
-
397
- truncate(value) {
398
- return value && value.replace(/^(?:\r\n|\r|\n)/, '')
399
- }
448
+ };
400
449
 
401
- compile(content, path) {
402
- const { SCOPE, SAFE, BUFFER, COMPONENT } = this.vars;
403
- if (this.rmWhitespace) {
450
+ this.compile = function (content, path) {
451
+ const { SCOPE, SAFE, BUFFER, COMPONENT } = compiler.vars;
452
+ if (compiler.rmWhitespace) {
404
453
  content = content
405
454
  .replace(/[\r\n]+/g, '\n')
406
455
  .replace(/^\s+|\s+$/gm, '');
407
456
  }
408
457
  content = content
409
- .replace(this.slurpStart, this.token.start)
410
- .replace(this.slurpEnd, this.token.end);
458
+ .replace(compiler.slurpStart, compiler.token.start)
459
+ .replace(compiler.slurpEnd, compiler.token.end);
411
460
  let source = `${BUFFER}('`;
412
- matchTokens(this.regex, content, (params, index, offset) => {
461
+ matchTokens(compiler.regex, content, (params, index, offset) => {
413
462
  source += symbols(content.slice(index, offset));
414
463
  params.forEach((value, index) => {
415
464
  if (value) {
416
- source += this.formats[index](value);
465
+ source += compiler.formats[index](value);
417
466
  }
418
467
  });
419
468
  });
420
469
  source += `');`;
421
470
  source = `try{${source}}catch(e){console.info(e)}`;
422
- if (this.withObject) {
471
+ if (compiler.withObject) {
423
472
  source = `with(${SCOPE}){${source}}`;
424
473
  }
425
474
  source = `${BUFFER}.start();${source}return ${BUFFER}.end();`;
@@ -434,93 +483,51 @@ class Compiler {
434
483
  throw e
435
484
  }
436
485
  return result
437
- }
486
+ };
487
+
488
+ this.configure(config);
438
489
  }
439
490
 
440
491
  const global = typeof globalThis !== 'undefined' ? globalThis : window || self;
441
492
 
442
- class Cache {
443
- list = {}
444
- constructor(config) {
445
- this.configure(config);
493
+ function Cache(config) {
494
+ if (instanceOf(this, Cache) === false) return new Cache()
495
+ const cache = {
496
+ list: {},
497
+ };
498
+ this.configure = function (config) {
499
+ cache.list = {};
446
500
  if (isNode() === false) {
447
- this.load(global[this.namespace]);
501
+ this.load(global[config.export]);
448
502
  }
449
- }
450
- configure(config) {
451
- this.list = {};
452
- this.namespace = config.export;
453
- }
454
- load(data) {
455
- extend(this.list, data);
503
+ };
504
+ this.load = function (data) {
505
+ extend(cache.list, data || {});
456
506
  return this
457
- }
458
- exist(key) {
459
- return hasProp(this.list, key)
460
- }
461
- get(key) {
462
- return this.list[key]
463
- }
464
- remove(key) {
465
- delete this.list[key];
466
- }
467
- resolve(key) {
468
- return Promise.resolve(this.get(key))
469
- }
470
- set(key, value) {
471
- this.list[key] = value;
507
+ };
508
+ this.get = function (key) {
509
+ return cache.list[key]
510
+ };
511
+ this.set = function (key, value) {
512
+ cache.list[key] = value;
472
513
  return this
473
- }
514
+ };
515
+ this.resolve = function (key) {
516
+ return Promise.resolve(this.get(key))
517
+ };
518
+ this.remove = function (key) {
519
+ delete cache.list[key];
520
+ };
521
+ this.exist = function (key) {
522
+ return hasProp(cache.list, key)
523
+ };
474
524
  }
475
525
 
476
- const selfClosed = [
477
- 'area',
478
- 'base',
479
- 'br',
480
- 'col',
481
- 'embed',
482
- 'hr',
483
- 'img',
484
- 'input',
485
- 'link',
486
- 'meta',
487
- 'param',
488
- 'source',
489
- 'track',
490
- 'wbr',
491
- ];
492
-
493
- const space = ' ';
494
- const quote = '"';
495
- const equal = '=';
496
- const slash = '/';
497
- const lt = '<';
498
- const gt = '>';
499
-
500
- const element = (tag, attrs, content) => {
501
- const result = [];
502
- const hasClosedTag = selfClosed.indexOf(tag) === -1;
503
- const attributes = map(attrs, (value, key) => {
504
- if (value !== null && value !== undefined) {
505
- return [
506
- entities(key),
507
- [quote, entities(value), quote].join(''),
508
- ].join(equal)
509
- }
510
- }).join(space);
511
- result.push([lt, tag, space, attributes, gt].join(''));
512
- if (content) {
513
- result.push(content instanceof Array ? content.join('') : content);
514
- }
515
- if (hasClosedTag) {
516
- result.push([lt, slash, tag, gt].join(''));
517
- }
518
- return result.join('')
519
- };
520
-
521
- const resolve = (list) => Promise.all(list).then((list) => list.join(''));
526
+ function resolve(list) {
527
+ return Promise.all(list || []).then((list) => list.join(''))
528
+ }
522
529
 
523
- const createBuffer = () => {
530
+ const createBuffer = function () {
524
531
  let store = [],
525
532
  array = [];
526
533
  function buffer(value) {
@@ -547,64 +554,47 @@ const createBuffer = () => {
547
554
  return buffer
548
555
  };
549
556
 
550
- class Context {
551
- constructor(config) {
552
- this.configure(config);
553
- }
554
- configure(config, methods) {
555
- const { EXTEND, LAYOUT, BLOCKS, BUFFER, MACRO, COMPONENT } = config.vars;
556
- this.create = (data) => {
557
+ function Context(config) {
558
+ if (instanceOf(this, Context) === false) return new Context(config)
559
+
560
+ this.configure = function (config, methods) {
561
+ const { BLOCKS, MACRO, EXTEND, LAYOUT, BUFFER, COMPONENT } = config.vars;
562
+
563
+ this.create = function (data) {
557
564
  return new Scope(data)
558
565
  };
559
- this.helpers = (methods) => {
560
- extend(Scope.prototype, methods);
566
+
567
+ this.helpers = function (methods) {
568
+ extend(Scope.prototype, methods || {});
561
569
  };
562
- function Scope(data = {}) {
563
- this.initBlocks();
564
- this.initMacro();
565
- extend(this, data);
570
+
571
+ function Scope(data) {
572
+ this[BLOCKS] = {};
573
+ this[MACRO] = {};
574
+ extend(this, data || {});
566
575
  }
567
576
  Scope.prototype = extend({}, methods || {});
568
- /**
569
- * @param {string} name
570
- * @param {*} value
571
- * @param {boolean} [writable]
572
- * @param {boolean} [configurable]
573
- * @param {boolean} [enumerable]
574
- */
575
- Scope.define = Scope.method = (
577
+ Scope.method = Scope.define = function (
576
578
  name,
577
579
  value,
578
580
  writable,
579
581
  configurable,
580
582
  enumerable
581
- ) => {
582
- return Object.defineProperty(Scope.prototype, name, {
583
+ ) {
584
+ Object.defineProperty(Scope.prototype, name, {
583
585
  value: value,
584
586
  writable: writable || false,
585
587
  configurable: configurable || false,
586
588
  enumerable: enumerable || false,
587
- })
589
+ });
588
590
  };
589
591
 
590
592
  Scope.define(BUFFER, createBuffer());
591
-
592
593
  Scope.define(BLOCKS, {}, true);
593
-
594
594
  Scope.define(MACRO, {}, true);
595
-
596
595
  Scope.define(LAYOUT, false, true);
597
-
598
596
  Scope.define(EXTEND, false, true);
599
597
 
600
- Scope.method('initBlocks', function () {
601
- this[BLOCKS] = {};
602
- });
603
-
604
- Scope.method('initMacro', function () {
605
- this[MACRO] = {};
606
- });
607
-
608
598
  Scope.method('getMacro', function () {
609
599
  return this[MACRO]
610
600
  });
@@ -793,42 +783,26 @@ class Context {
793
783
  }
794
784
  each(object, callback);
795
785
  });
796
- }
786
+ };
787
+
788
+ this.configure(config);
797
789
  }
798
790
 
799
- class EJS {
800
- export = [
801
- 'cache',
802
- 'render',
803
- 'require',
804
- 'helpers',
805
- 'configure',
806
- 'preload',
807
- 'compile',
808
- 'create',
809
- '__express',
810
- ]
811
- constructor(options = {}) {
812
- this.config = {};
813
- this.scope = {};
814
- configSchema(this.config, options);
815
- bindContext(this, this, this.export);
816
- this.context = new Context(this.config);
817
- this.compiler = new Compiler(this.config);
818
- this.cache = new Cache(this.config);
819
- this.template = new Template(this.config, this.cache, this.compiler);
820
- this.helpers({ require: this.require, render: this.render });
821
- }
822
- configure(options = {}) {
823
- configSchema(this.config, options);
824
- this.context.configure(this.config, this.scope);
825
- this.compiler.configure(this.config);
826
- this.cache.configure(this.config);
827
- this.template.configure(this.config);
828
- return this.config
829
- }
830
- output(path, scope) {
831
- return this.template.get(path).then(function (callback) {
791
+ function EJS(options) {
792
+ if (instanceOf(this, EJS) === false) return new EJS(options)
793
+
794
+ const scope = {};
795
+ const config = {};
796
+
797
+ configSchema(config, options || {});
798
+
799
+ const context = new Context(config);
800
+ const compiler = new Compiler(config);
801
+ const cache = new Cache();
802
+ const template = new Template(config, cache, compiler);
803
+
804
+ const output = function (path, scope) {
805
+ return template.get(path).then(function (callback) {
832
806
  return callback.call(
833
807
  scope,
834
808
  scope,
@@ -837,80 +811,97 @@ class EJS {
837
811
  safeValue
838
812
  )
839
813
  })
840
- }
841
- render(name, data) {
842
- const filepath = ext(name, this.config.extension);
843
- const scope = this.context.create(data);
844
- return this.output(filepath, scope).then((content) => {
814
+ };
815
+
816
+ const require = function (name) {
817
+ const filepath = ext(name, config.extension);
818
+ const scope = context.create({});
819
+ return output(filepath, scope).then(() => {
820
+ return scope.getMacro()
821
+ })
822
+ };
823
+
824
+ const render = function (name, data) {
825
+ const filepath = ext(name, config.extension);
826
+ const scope = context.create(data);
827
+ return output(filepath, scope).then((content) => {
845
828
  if (scope.getExtend()) {
846
829
  scope.setExtend(false);
847
830
  const layout = scope.getLayout();
848
831
  const data = scope.clone();
849
- return this.render(layout, data)
832
+ return render(layout, data)
850
833
  }
851
834
  return content
852
835
  })
853
- }
854
- require(name) {
855
- const filepath = ext(name, this.config.extension);
856
- const scope = this.context.create({});
857
- return this.output(filepath, scope).then(() => {
858
- return scope.getMacro()
859
- })
860
- }
861
- create(options = {}) {
862
- return new EJS(options)
863
- }
864
- helpers(methods = {}) {
865
- this.context.helpers(extend(this.scope, methods));
866
- }
867
- preload(list = {}) {
868
- return this.cache.load(list)
869
- }
870
- compile(content, path) {
871
- return this.compiler.compile(content, path)
872
- }
873
- __express(name, options, callback) {
874
- if (isFunction(options)) {
875
- callback = options;
876
- options = {};
877
- }
836
+ };
837
+
838
+ this.configure = function (options) {
878
839
  options = options || {};
879
- const settings = extend({}, options.settings);
880
- const viewPath = typeProp(isString, defaults.path, settings['views']);
881
- const viewCache = typeProp(
882
- isBoolean,
883
- defaults.cache,
884
- settings['view cache']
885
- );
886
- const viewOptions = extend({}, settings['view options']);
887
- const filename = path.relative(viewPath, name);
888
- viewOptions.path = viewPath;
889
- viewOptions.cache = viewCache;
890
- this.configure(viewOptions);
891
- return this.render(filename, options)
892
- .then((content) => {
893
- callback(null, content);
894
- })
895
- .catch((error) => {
896
- callback(error);
897
- })
898
- }
840
+ configSchema(config, options);
841
+ context.configure(config, scope);
842
+ compiler.configure(config);
843
+ cache.configure(config);
844
+ template.configure(config);
845
+ return config
846
+ };
847
+
848
+ this.render = function (name, data) {
849
+ return render(name, data)
850
+ };
851
+
852
+ this.helpers = function (methods) {
853
+ context.helpers(Object.assign(scope, methods || {}));
854
+ };
855
+
856
+ this.preload = function (list) {
857
+ return cache.load(list || {})
858
+ };
859
+
860
+ this.create = function (options) {
861
+ return new EJS(options)
862
+ };
863
+
864
+ this.compile = function (content, path) {
865
+ return compiler.compile(content, path)
866
+ };
867
+
868
+ this.context = function (data) {
869
+ return context.create(data)
870
+ };
871
+
872
+ this.helpers({ require, render });
873
+ }
874
+
875
+ const ejs = new EJS();
876
+
877
+ const { render, context, compile, helpers, preload, configure: configure$1, create } =
878
+ ejs;
879
+
880
+ function __express(name, options, callback) {
881
+ if (isFunction(options)) {
882
+ callback = options;
883
+ options = {};
884
+ }
885
+ options = options || {};
886
+ const settings = extend({}, options.settings);
887
+ const viewPath = typeProp(isString, defaults.path, settings['views']);
888
+ const viewCache = typeProp(
889
+ isBoolean,
890
+ defaults.cache,
891
+ settings['view cache']
892
+ );
893
+ const viewOptions = extend({}, settings['view options']);
894
+ const filename = path.relative(viewPath, name);
895
+ viewOptions.path = viewPath;
896
+ viewOptions.cache = viewCache;
897
+ configure(viewOptions);
898
+ return this.render(filename, options)
899
+ .then((content) => {
900
+ callback(null, content);
901
+ })
902
+ .catch((error) => {
903
+ callback(error);
904
+ })
899
905
  }
900
906
 
901
- const ejs = new EJS({
902
- /** defaults options **/
903
- });
904
-
905
- const {
906
- __express,
907
- render,
908
- context,
909
- compile,
910
- helpers,
911
- preload,
912
- configure,
913
- create,
914
- } = ejs;
915
-
916
- export { EJS, __express, compile, configure, context, create, element, helpers, preload, render, safeValue };
907
+ export { EJS, __express, compile, configure$1 as configure, context, create, element, helpers, preload, render, safeValue };