API Docs for: 2.5.3
Show:

File: src/Query/Query.js

(function(glesea) {
	/**
		Traverses through a structure by following a list of keys/indexs
		
			var s  = { a: [1, 2, { b: 'ccc' } ] },
				s2 = { a: { b: 'ccc2' } };
			
			var o  = glesea.traverseWithQuery(s, ['a', 2, 'b']),
				o2 = glesea.traverseWithQuery(s2, 'a.b');
			
			// o  ==> 'ccc'
			// o2 ==> 'ccc2'
		
		@method traverseWithQuery
		@for glesea
		
		@param {Object} struct The structure to traverse through
		@param {Array} query An array of keys/indexs, which are followed in order
		
		@return Value of the query, or undefined if it wasn't there
		@for glesea 
	**/
	glesea.traverseWithQuery = function(struct, query) {
		var ret = struct,
			i;

		query = Array.isArray(query) ? query : query.split('.');

		for (i = 0; i < query.length - 1; i++) {
			if (ret === undefined) {
				return undefined;
			}

			ret = ret[query[i]];
		}

		if (ret === undefined) {
			return undefined;
		}

		return ret[query[i]];
	};


	/**
		Traverses through a structure by following a list of keys/indexs
		
			var s  = { a: [10, 19, { b: 'ccc' } ] },
				s2 = { a: { b: 'ccc2' } };
			
			glesea.setWithQuery(s, ['a', 1], 'new val');
			glesea.setWithQuery(s2, 'a.c', 'new val2');
			
			// s  ==> { a: [ 10, 'new val', { b: 'ccc' } ] }
			// s2 ==> { a: { b: 'ccc2', c: 'new val2' } }
		
		@method setWithQuery
		@for glesea
		
		@param {Object} struct The structure to traverse through
		@param {Array} query An array of keys/indexs, which are followed in order
		@param {Any} val The value which will be assigned to the structure's query value
		
		@return Nothing
	**/
	glesea.setWithQuery = function(struct, query, val) {
		var ret = struct,
			i;

		query = Array.isArray(query) ? query : query.split('.');

		for (i = 0; i < query.length - 1; i++) {
			if (ret === undefined) {
				ret = {};
			}

			if (ret[query[i]] === undefined) {
				ret[query[i]] = {};
			}

			ret = ret[query[i]];
		}

		if (ret === undefined) {
			ret = {};
		}
		
		ret[query[i]] = val;
	};
})(glesea);