All files / Nodejs/lib reference.js

62.33% Statements 48/77
58.75% Branches 47/80
62.5% Functions 10/16
63.15% Lines 48/76

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176  1x   1322x 478x         1322x                   1322x 1322x   1322x 8x 16x             1322x 935x 935x 935x       1x 1314x     1314x     1x 1303x     1303x       1x 8x 8x 8x                         1x                             1x 1x   1x   1288x 1288x     1314x                       237x               237x     237x 237x   237x 237x 18x     237x 12x     237x       237x 237x 237x 471x 471x 384x     237x   237x                                                                  
"use strict";
var Ref = function(data, opts) {
 
	if (typeof opts === "string") {
		opts = {
			type: opts,
			forcedType: opts
		}
	}
	this.opts = opts = Object.assign({
		type: "queue",
		types: {
			"queue": "queue",
			"system": "system",
			"bot": "bot"
		},
		legacyMode: false
	}, opts);
 
	this.type = this.opts.forcedType || (this.opts.types[data.type] || this.opts.type).toLowerCase();
	this.id = (data.id == undefined ? data : data.id).toString().replace(/^[seb]_/, "");
 
	if (typeof data === "object") {
		Object.keys(data).map(key => {
			Iif (key != "type" && key != "id") {
				this[key] = data[key];
			}
		});
	}
 
	let parts;
	if (typeof data === "string" && (parts = data.match(/(system|queue|bot):(.*)/i))) {
		var t = parts[1].toLowerCase();
		this.type = this.opts.forcedType || this.opts.types[t] || t;
		this.id = parts[2];
	}
};
 
Ref.new = function(data, opts) {
	Iif (data instanceof Ref) {
		return data;
	} else {
		return new Ref(data, opts);
	}
};
Ref.prototype.toString = function() {
	Iif (this.opts.legacyMode && this.type === "queue") {
		return `${this.id}`;
	} else {
		return `${this.type}:${this.id}`;
	}
};
 
Ref.prototype.queue = function(subQueue) {
	subQueue = subQueue && ("." + subQueue) || "";
	Eif (this.type === "queue") {
		return new Ref({
			id: `${this.id}${subQueue}`,
			type: "queue"
		});
	} else {
		var regex = new RegExp(`^${this.type}\.`);
		return new Ref({
			id: `${this.type}.${this.id.replace(regex, "")}${subQueue}`,
			type: "queue"
		});
	}
}
 
Ref.prototype.owner = function() {
	if (this.type === "queue") {
		//let a = this.id.match(/^(bot|system)\.(.*?)(?:\.|$)/);
		let a = this.id.match(/^(bot|system)\.(.*?)(?:\.(.*))?$/);
		if (a) {
			return new Ref({
				id: a[2],
				type: a[1],
				queue: a[3] || undefined
			});
		}
	}
	return null;
};
 
Ref.prototype.refId = Ref.prototype.toString;
Ref.prototype.asQueue = Ref.prototype.queue;
 
module.exports = {
	refId: function(data, opts) {
		var obj = this.ref(data, opts);
		return obj && obj.toString();
	},
	ref: function(data, opts) {
		return data && Ref.new(data, opts);
	},
	botRefId: function(data, opts) {
		var obj = this.botRef(data, opts);
		return obj && obj.toString();
	},
	botRef: function(data, opts) {
		return data && Ref.new(data, Object.assign({
			type: "bot"
		}, opts));
	},
	fixBotReferences: function(bot, opts) {
		opts = Object.assign({
			checkpoints: false,
			source: true,
			destination: true,
			system: true,
			id: true
		}, opts);
 
		Iif (!bot) {
			return bot;
		}
		Eif (opts.id) {
			bot.id = this.refId(bot.id, "bot");
		}
		var settings = bot.lambda && bot.lambda.settings && bot.lambda.settings[0] || {};
		if (opts.source && settings.source) {
			settings.source = this.refId(settings.source)
		}
 
		if (opts.destination && settings.destination) {
			settings.destination = this.refId(settings.destination);
		}
 
		Iif (opts.system && bot.system) {
			bot.system = this.ref(bot.system, "system")
		}
 
		Eif (opts.checkpoints) {
			var checkpoints = {};
			Object.keys(bot.checkpoints).map(type => {
				var obj = checkpoints[type] = {};
				Object.keys(bot.checkpoints[type]).map(id => {
					obj[this.refId(id)] = bot.checkpoints[type][id];
				});
			});
			bot.checkpoints = checkpoints;
		}
		return bot;
	},
	fixSystemReferences: function(system, opts) {
		opts = Object.assign({
			checksums: true,
			crons: true,
			id: true
		}, opts);
 
		if (opts.id) {
			system.id = this.refId(system.id, "system");
		}
 
		if (opts.crons && system.crons) {
			system.crons = system.crons.map(id => this.botRefId(id));
		}
 
		if (opts.checksums && system.checksums) {
			var checksums = {};
			Object.keys(system.checksums).map(id => {
				var refId = this.botRefId(id);
				var obj = system.checksums[id] || {};
				checksums[refId] = Object.assign({}, obj, {
					bot_id: refId,
					system: this.refId(obj.system, "system")
				});
			})
			system.checksums = checksums;
		}
 
		return system;
	}
};