all files / node-csgo-parser/lib/ miscHelper.js

100% Statements 39/39
100% Branches 14/14
100% Functions 7/7
100% Lines 39/39
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                      6453× 6453× 20305693× 6133×                             6491× 6491× 6428×   6491×                                               3190× 3190× 3190× 470651× 1424×     3190× 1766×                           2489× 2489× 2489× 2489× 2562× 132×     2489×                 1347×                 1346× 1346×          
'use strict';
 
/**
 * Prototype Object
 * Normalized search in keys (Valve seems to like put different case type between lang file and schema).
 * @this {Object} Object to get value of
 * @param {String} Unnormalized value 
 * @return {Object} Data.
 * @private
 */
Object.defineProperty(Object.prototype, 'getValue', {
    value: function (prop) {
        var self = this;
        for (var key in self) {
            if (key.toLowerCase() === prop.toLowerCase()) {
                return self[key];
            }
            
        }
    }
});
 
/**
 * Prototype String
 * Remove # on i18n key for lang search.
 * @this {String} i18n key
 * @return {String} i18n key without #.
 * @private
 */
Object.defineProperty(String.prototype, 'prepareLang', {
	value: function() {
		var self=this;
		if (self.charAt(0) === '#') {
			self=self.slice(1);
		}
		return self;
	}
});
 
/**
 * Prototype Array
 * HashTable light implementation, only push once. If object already present, do nothing
 * @this {Array} HashTable Array
 * @private
 */
Object.defineProperty(Array.prototype, 'pushUnique', {
	value: function(value) {
		var self=this;
		var isPresent = false;
		for (var key in self) {
			if (value === self[key]) {
				isPresent = true;
			}
		}
		if (!isPresent) {
			self.push(value);
		}
	}
});
 
/**
 * Prototype Array
 * HashTable light implementation, only push once named object. If object already present, do nothing
 * @this {Array} HashTable Array
 * @private
 */
Object.defineProperty(Array.prototype, 'pushUniqueNamedObject', {
	value: function(value) {
		var self=this;
		var isPresent = false;
		for (var key in self) {
			if (value.name === self[key].name) {
				isPresent = true;
			}
		}
		if (!isPresent) {
			self.push(value);
		}
	}
});
 
/**
 * Prototype String
 * Split itself on space to check if value is present
 * @this {String} String to check on split
 * @param {String} value Value to check in this
 * @return {boolean} true if present, false otherwise
 * @private
 */
Object.defineProperty(String.prototype, 'containsOnSpaceSplit', {
	value: function(value) {
		var self=this;
		var splitArray=self.split(' ');
		var isPresent = false;
		for (var key in splitArray) {
			if (splitArray[key] === value) {
				isPresent = true;
			}
		}
		return isPresent;
	}
});
 
/**
 * Generate a timer
 * @return {Array} Returns the current high-resolution real time in a [seconds, nanoseconds] tuple Array. 
 * @private
 */
exports.generateTimer = function(){
    return process.hrtime();
};
 
/**
 * Get the result diff timer
 * @param {Array} timer to diff with
 * @return {Float} Diff. 
 * @private
 */
exports.resultTimer = function(timer){
    var diff = process.hrtime(timer);
    return ((diff[0]*1e9+diff[1])*1e-9).toFixed(4);
};