npmtest-docxtemplater (v0.0.2)

Code coverage report for node-npmtest-docxtemplater/node_modules/docxtemplater/js/scope-manager.js

Statements: 29.33% (22 / 75)      Branches: 15.63% (5 / 32)      Functions: 30.77% (4 / 13)      Lines: 15.25% (9 / 59)      Ignored: none     

All files » node-npmtest-docxtemplater/node_modules/docxtemplater/js/ » scope-manager.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127    6   1   1     1 1               1                                                                                                                                                                                           1     1                   1  
"use strict";
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
var Errors = require("./errors");
 
// This class responsibility is to manage the scope
var ScopeManager = function () {
	function ScopeManager(options) {
		_classCallCheck(this, ScopeManager);
 
		this.scopePath = options.scopePath;
		this.scopeList = options.scopeList;
		this.parser = options.parser;
	}
 
	_createClass(ScopeManager, [{
		key: "loopOver",
		value: function loopOver(tag, callback, inverted) {
			inverted = inverted || false;
			return this.loopOverValue(this.getValue(tag), callback, inverted);
		}
	}, {
		key: "functorIfInverted",
		value: function functorIfInverted(inverted, functor, value) {
			if (inverted) {
				functor(value);
			}
		}
	}, {
		key: "isValueFalsy",
		value: function isValueFalsy(value, type) {
			return value == null || !value || type === "[object Array]" && value.length === 0;
		}
	}, {
		key: "loopOverValue",
		value: function loopOverValue(value, functor, inverted) {
			var type = Object.prototype.toString.call(value);
			var currentValue = this.scopeList[this.num];
			if (this.isValueFalsy(value, type)) {
				return this.functorIfInverted(inverted, functor, currentValue);
			}
			if (type === "[object Array]") {
				for (var i = 0, scope; i < value.length; i++) {
					scope = value[i];
					this.functorIfInverted(!inverted, functor, scope);
				}
				return;
			}
			if (type === "[object Object]") {
				return this.functorIfInverted(!inverted, functor, value);
			}
			if (value === true) {
				return this.functorIfInverted(!inverted, functor, currentValue);
			}
		}
	}, {
		key: "getValue",
		value: function getValue(tag, num) {
			// search in the scopes (in reverse order) and keep the first defined value
			this.num = num == null ? this.scopeList.length - 1 : num;
			var err = void 0;
			var parser = void 0;
			var result = void 0;
			var scope = this.scopeList[this.num];
			try {
				parser = this.parser(tag);
			} catch (error) {
				err = new Errors.XTScopeParserError("Scope parser compilation failed");
				err.properties = {
					id: "scopeparser_compilation_failed",
					tag: tag,
					explanation: "The scope parser for the tag " + tag + " failed to compile",
					rootError: error
				};
				throw err;
			}
			try {
				result = parser.get(scope, { num: this.num, scopeList: this.scopeList });
			} catch (error) {
				err = new Errors.XTScopeParserError("Scope parser execution failed");
				err.properties = {
					id: "scopeparser_execution_failed",
					explanation: "The scope parser for the tag " + tag + " failed to execute",
					scope: scope,
					tag: tag,
					rootError: error
				};
				throw err;
			}
			if (result == null && this.num > 0) {
				return this.getValue(tag, this.num - 1);
			}
			return result;
		}
	}, {
		key: "createSubScopeManager",
		value: function createSubScopeManager(scope, tag) {
			var options = {
				scopePath: this.scopePath.slice(0),
				scopeList: this.scopeList.slice(0)
			};
 
			options.parser = this.parser;
			options.scopeList = this.scopeList.concat(scope);
			options.scopePath = this.scopePath.concat(tag);
			return new ScopeManager(options);
		}
	}]);
 
	return ScopeManager;
}();
 
ScopeManager.createBaseScopeManager = function (_ref) {
	var parser = _ref.parser,
	    tags = _ref.tags;
 
	var options = { parser: parser, tags: tags };
	options.scopePath = [];
	options.scopeList = [tags];
	return new ScopeManager(options);
};
 
module.exports = ScopeManager;