@kosatyi/ejs 0.0.62 → 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,92 +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);
446
- }
447
- configure(config) {
448
- this.list = {};
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 = {};
449
500
  if (isNode() === false) {
450
501
  this.load(global[config.export]);
451
502
  }
452
- }
453
- load(data) {
454
- extend(this.list, data);
503
+ };
504
+ this.load = function (data) {
505
+ extend(cache.list, data || {});
455
506
  return this
456
- }
457
- exist(key) {
458
- return hasProp(this.list, key)
459
- }
460
- get(key) {
461
- return this.list[key]
462
- }
463
- remove(key) {
464
- delete this.list[key];
465
- }
466
- resolve(key) {
467
- return Promise.resolve(this.get(key))
468
- }
469
- set(key, value) {
470
- 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;
471
513
  return this
472
- }
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
+ };
473
524
  }
474
525
 
475
- const selfClosed = [
476
- 'area',
477
- 'base',
478
- 'br',
479
- 'col',
480
- 'embed',
481
- 'hr',
482
- 'img',
483
- 'input',
484
- 'link',
485
- 'meta',
486
- 'param',
487
- 'source',
488
- 'track',
489
- 'wbr',
490
- ];
491
-
492
- const space = ' ';
493
- const quote = '"';
494
- const equal = '=';
495
- const slash = '/';
496
- const lt = '<';
497
- const gt = '>';
498
-
499
- const element = (tag, attrs, content) => {
500
- const result = [];
501
- const hasClosedTag = selfClosed.indexOf(tag) === -1;
502
- const attributes = map(attrs, (value, key) => {
503
- if (value !== null && value !== undefined) {
504
- return [
505
- entities(key),
506
- [quote, entities(value), quote].join(''),
507
- ].join(equal)
508
- }
509
- }).join(space);
510
- result.push([lt, tag, space, attributes, gt].join(''));
511
- if (content) {
512
- result.push(content instanceof Array ? content.join('') : content);
513
- }
514
- if (hasClosedTag) {
515
- result.push([lt, slash, tag, gt].join(''));
516
- }
517
- return result.join('')
518
- };
519
-
520
- const resolve = (list) => Promise.all(list).then((list) => list.join(''));
526
+ function resolve(list) {
527
+ return Promise.all(list || []).then((list) => list.join(''))
528
+ }
521
529
 
522
- const createBuffer = () => {
530
+ const createBuffer = function () {
523
531
  let store = [],
524
532
  array = [];
525
533
  function buffer(value) {
@@ -546,64 +554,47 @@ const createBuffer = () => {
546
554
  return buffer
547
555
  };
548
556
 
549
- class Context {
550
- constructor(config) {
551
- this.configure(config);
552
- }
553
- configure(config, methods) {
554
- const { EXTEND, LAYOUT, BLOCKS, BUFFER, MACRO, COMPONENT } = config.vars;
555
- 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) {
556
564
  return new Scope(data)
557
565
  };
558
- this.helpers = (methods) => {
559
- extend(Scope.prototype, methods);
566
+
567
+ this.helpers = function (methods) {
568
+ extend(Scope.prototype, methods || {});
560
569
  };
561
- function Scope(data = {}) {
562
- this.initBlocks();
563
- this.initMacro();
564
- extend(this, data);
570
+
571
+ function Scope(data) {
572
+ this[BLOCKS] = {};
573
+ this[MACRO] = {};
574
+ extend(this, data || {});
565
575
  }
566
576
  Scope.prototype = extend({}, methods || {});
567
- /**
568
- * @param {string} name
569
- * @param {*} value
570
- * @param {boolean} [writable]
571
- * @param {boolean} [configurable]
572
- * @param {boolean} [enumerable]
573
- */
574
- Scope.define = Scope.method = (
577
+ Scope.method = Scope.define = function (
575
578
  name,
576
579
  value,
577
580
  writable,
578
581
  configurable,
579
582
  enumerable
580
- ) => {
581
- return Object.defineProperty(Scope.prototype, name, {
583
+ ) {
584
+ Object.defineProperty(Scope.prototype, name, {
582
585
  value: value,
583
586
  writable: writable || false,
584
587
  configurable: configurable || false,
585
588
  enumerable: enumerable || false,
586
- })
589
+ });
587
590
  };
588
591
 
589
592
  Scope.define(BUFFER, createBuffer());
590
-
591
593
  Scope.define(BLOCKS, {}, true);
592
-
593
594
  Scope.define(MACRO, {}, true);
594
-
595
595
  Scope.define(LAYOUT, false, true);
596
-
597
596
  Scope.define(EXTEND, false, true);
598
597
 
599
- Scope.method('initBlocks', function () {
600
- this[BLOCKS] = {};
601
- });
602
-
603
- Scope.method('initMacro', function () {
604
- this[MACRO] = {};
605
- });
606
-
607
598
  Scope.method('getMacro', function () {
608
599
  return this[MACRO]
609
600
  });
@@ -792,42 +783,26 @@ class Context {
792
783
  }
793
784
  each(object, callback);
794
785
  });
795
- }
786
+ };
787
+
788
+ this.configure(config);
796
789
  }
797
790
 
798
- class EJS {
799
- export = [
800
- 'cache',
801
- 'render',
802
- 'require',
803
- 'helpers',
804
- 'configure',
805
- 'preload',
806
- 'compile',
807
- 'create',
808
- '__express',
809
- ]
810
- constructor(options = {}) {
811
- this.config = {};
812
- this.scope = {};
813
- configSchema(this.config, options);
814
- bindContext(this, this, this.export);
815
- this.context = new Context(this.config);
816
- this.compiler = new Compiler(this.config);
817
- this.cache = new Cache(this.config);
818
- this.template = new Template(this.config, this.cache, this.compiler);
819
- this.helpers({ require: this.require, render: this.render });
820
- }
821
- configure(options = {}) {
822
- configSchema(this.config, options);
823
- this.context.configure(this.config, this.scope);
824
- this.compiler.configure(this.config);
825
- this.cache.configure(this.config);
826
- this.template.configure(this.config);
827
- return this.config
828
- }
829
- output(path, scope) {
830
- 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) {
831
806
  return callback.call(
832
807
  scope,
833
808
  scope,
@@ -836,80 +811,97 @@ class EJS {
836
811
  safeValue
837
812
  )
838
813
  })
839
- }
840
- render(name, data) {
841
- const filepath = ext(name, this.config.extension);
842
- const scope = this.context.create(data);
843
- 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) => {
844
828
  if (scope.getExtend()) {
845
829
  scope.setExtend(false);
846
830
  const layout = scope.getLayout();
847
831
  const data = scope.clone();
848
- return this.render(layout, data)
832
+ return render(layout, data)
849
833
  }
850
834
  return content
851
835
  })
852
- }
853
- require(name) {
854
- const filepath = ext(name, this.config.extension);
855
- const scope = this.context.create({});
856
- return this.output(filepath, scope).then(() => {
857
- return scope.getMacro()
858
- })
859
- }
860
- create(options = {}) {
861
- return new EJS(options)
862
- }
863
- helpers(methods = {}) {
864
- this.context.helpers(extend(this.scope, methods));
865
- }
866
- preload(list = {}) {
867
- return this.cache.load(list)
868
- }
869
- compile(content, path) {
870
- return this.compiler.compile(content, path)
871
- }
872
- __express(name, options, callback) {
873
- if (isFunction(options)) {
874
- callback = options;
875
- options = {};
876
- }
836
+ };
837
+
838
+ this.configure = function (options) {
877
839
  options = options || {};
878
- const settings = extend({}, options.settings);
879
- const viewPath = typeProp(isString, defaults.path, settings['views']);
880
- const viewCache = typeProp(
881
- isBoolean,
882
- defaults.cache,
883
- settings['view cache']
884
- );
885
- const viewOptions = extend({}, settings['view options']);
886
- const filename = path.relative(viewPath, name);
887
- viewOptions.path = viewPath;
888
- viewOptions.cache = viewCache;
889
- this.configure(viewOptions);
890
- return this.render(filename, options)
891
- .then((content) => {
892
- callback(null, content);
893
- })
894
- .catch((error) => {
895
- callback(error);
896
- })
897
- }
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
+ })
898
905
  }
899
906
 
900
- const ejs = new EJS({
901
- /** defaults options **/
902
- });
903
-
904
- const {
905
- __express,
906
- render,
907
- context,
908
- compile,
909
- helpers,
910
- preload,
911
- configure,
912
- create,
913
- } = ejs;
914
-
915
- 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 };