Home Reference Source

stdlib/obj/keyvalues.js

"use strict";

Object.defineProperty(exports, "__esModule", {
	value: true
});

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; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * Wrapper class for interop with Source Engine
 * 
 * Usage:<br>
 * ```let a = new KeyValues();```<br>
 * ```a["color"] = new Color();```<br>
 * ```b["userData"] = new KeyValues();```<br>
 * ```b["userData"]["foo"] = "bar";```
 */
var KeyValues = exports.KeyValues = function () {
	function KeyValues() {
		_classCallCheck(this, KeyValues);
	}

	_createClass(KeyValues, [{
		key: "_serialize",


		/**
   * Serializes this KeyValues into a format supported by the Source Engine
   * @param {string} name - The key name of this KeyValues
   * @type {string}
   */
		value: function _serialize(name) {
			if (!name) throw new Error("name parameter not set");

			var result = "\"" + name + "\"\n{\n";

			var keys = Object.keys(this);
			for (var i = 0; i < keys.length; i++) {
				var key = keys[i];
				var value = this[key];

				if (value instanceof KeyValues) {
					result += value._serialize(key);
				} else if (value instanceof Color) {
					result += "\"" + key + "\" \"" + value.r + " " + value.g + " " + value.b + " " + value.a + "\"\n";
				} else {
					result += "\"" + key + "\" \"" + value + "\"\n";
				}
			}

			result += "}\n";

			return result;
		}

		/**
   * Creates a new construction context
   */

	}], [{
		key: "__startContext",
		value: function __startContext() {
			if (KeyValues.__current !== undefined) {
				throw new Error("Another KeyValues construction is busy, unable to start a second context.");
			}
			KeyValues.__current = [new KeyValues()];
		}

		/**
   * Destroys the current context and returns the constructed KeyValues
   * @type {KeyValues}
   */

	}, {
		key: "__stopContext",
		value: function __stopContext() {
			if (KeyValues.__current === undefined) {
				throw new Error("No context started, please create a new one using __startContent");
			}

			if (KeyValues.__current.length != 1) {
				throw new Error("Too many values on the stack, please pop until the root node");
			}

			var result = KeyValues.__current[0];
			KeyValues.__current = undefined;
			return result;
		}

		/**
   * Creates a new child node and pushes it on the construction stack
   * This is now the current active node
   * @param {string} name
   */

	}, {
		key: "__pushNode",
		value: function __pushNode(name) {
			var node = new KeyValues();
			var cur = KeyValues.__current[KeyValues.__current.length - 1];
			cur[name] = node;
			KeyValues.__current.push(node);
		}

		/**
   * Pops a node from the construction stack
   */

	}, {
		key: "__popNode",
		value: function __popNode() {
			KeyValues.__current.pop();
		}

		/**
   * Sets a value for a given key
   * @param {string} name
   * @param value
   */

	}, {
		key: "__setValue",
		value: function __setValue(name, value) {
			var cur = KeyValues.__current[KeyValues.__current.length - 1];
			cur[name] = value;
		}
	}, {
		key: "fromObject",
		value: function fromObject(object) {
			if (!object) throw new Error("Object property not set");

			var result = new KeyValues();

			var keys = Object.keys(object);
			for (var i = 0; i < keys.length; i++) {
				var k = keys[i];
				var prop = object[k];

				if (prop instanceof Object) result[k] = KeyValues.fromObject(k, prop);else result[k] = prop;
			}

			return result;
		}
	}]);

	return KeyValues;
}();