| 
									
										
										
										
											2017-02-18 01:56:23 -07:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2014-04-14 15:09:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 20:11:53 -04:00
										 |  |  | const helpers = module.exports; | 
					
						
							|  |  |  | const utils = require('../../utils'); | 
					
						
							| 
									
										
										
										
											2017-11-02 08:14:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | helpers.noop = function () {}; | 
					
						
							| 
									
										
										
										
											2014-04-14 15:09:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.toMap = function (data) { | 
					
						
							| 
									
										
										
										
											2014-07-24 12:41:22 -04:00
										 |  |  | 	var map = {}; | 
					
						
							| 
									
										
										
										
											2017-02-18 01:12:18 -07:00
										 |  |  | 	for (var i = 0; i < data.length; i += 1) { | 
					
						
							| 
									
										
										
										
											2014-07-24 12:41:22 -04:00
										 |  |  | 		map[data[i]._key] = data[i]; | 
					
						
							| 
									
										
										
										
											2017-07-10 12:54:45 -04:00
										 |  |  | 		delete data[i]._key; | 
					
						
							| 
									
										
										
										
											2014-04-14 15:09:57 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-24 12:41:22 -04:00
										 |  |  | 	return map; | 
					
						
							| 
									
										
										
										
											2014-09-06 20:47:20 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-04-14 15:09:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.fieldToString = function (field) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:52:56 -07:00
										 |  |  | 	if (field === null || field === undefined) { | 
					
						
							| 
									
										
										
										
											2014-04-14 15:09:57 -04:00
										 |  |  | 		return field; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 01:52:56 -07:00
										 |  |  | 	if (typeof field !== 'string') { | 
					
						
							| 
									
										
										
										
											2014-04-14 15:09:57 -04:00
										 |  |  | 		field = field.toString(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// if there is a '.' in the field name it inserts subdocument in mongo, replace '.'s with \uff0E
 | 
					
						
							| 
									
										
										
										
											2018-12-19 11:09:19 -05:00
										 |  |  | 	return field.replace(/\./g, '\uff0E'); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | helpers.serializeData = function (data) { | 
					
						
							|  |  |  | 	const serialized = {}; | 
					
						
							|  |  |  | 	for (const field in data) { | 
					
						
							|  |  |  | 		if (data.hasOwnProperty(field) && field !== '') { | 
					
						
							|  |  |  | 			serialized[helpers.fieldToString(field)] = data[field]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return serialized; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | helpers.deserializeData = function (data) { | 
					
						
							|  |  |  | 	const deserialized = {}; | 
					
						
							|  |  |  | 	for (const field in data) { | 
					
						
							|  |  |  | 		if (data.hasOwnProperty(field)) { | 
					
						
							|  |  |  | 			deserialized[field.replace(/\uff0E/g, '.')] = data[field]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return deserialized; | 
					
						
							| 
									
										
										
										
											2014-04-14 15:09:57 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.valueToString = function (value) { | 
					
						
							| 
									
										
										
										
											2019-02-11 11:23:18 -05:00
										 |  |  | 	return String(value); | 
					
						
							| 
									
										
										
										
											2014-04-14 15:09:57 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-07-02 20:11:53 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | helpers.buildMatchQuery = function (match) { | 
					
						
							|  |  |  | 	let _match = match; | 
					
						
							|  |  |  | 	if (match.startsWith('*')) { | 
					
						
							|  |  |  | 		_match = _match.substring(1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (match.endsWith('*')) { | 
					
						
							|  |  |  | 		_match = _match.substring(0, _match.length - 1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	_match = utils.escapeRegexChars(_match); | 
					
						
							|  |  |  | 	if (!match.startsWith('*')) { | 
					
						
							|  |  |  | 		_match = '^' + _match; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!match.endsWith('*')) { | 
					
						
							|  |  |  | 		_match += '$'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return _match; | 
					
						
							|  |  |  | }; |