<commit_before>(function($) {
	var originalAjaxFunction = $.ajax;

	var defaults = {
		type: "sessionStorage", //or localStorage
		expires: null, //optional, must be datetime object, not neccessery if type is sessionStorage
		key: null //optional, must be string
	};

	var _type, _expires, _key;

	var _prefix = "csc_";

	function _createCacheKey(url, postData) {
		if (typeof url === "string") {
			var result = _prefix + url;
			if (!!postData) {
				$.each(Object.keys(postData), function (index, item) {
					result += "_" + item + "-" + postData[item];
				});
			}

			return result.replace(" ", "");
		} else {
			return null;
		}
	};

	function _setCache(responseData) {
		sessionStorage.setItem(_key, JSON.stringify(responseData));
		return true;
	};

	function _getCache() {
		var cacheData = JSON.parse(sessionStorage.getItem(_key));
		return cacheData;
	}

	function _removeCachefunction (url, postData) {
		if (!!url) {
			var cacheKey = _createCacheKey(url, postData);
			sessionStorage.removeItem(cacheKey);
		}
	}

	$.ajax = function(settings) {
		console.log(settings);
		console.log("ajax called");
		//if clientSideCache defined, do the things
		if(typeof settings.clientSideCache === "object") {
			console.log("cache func start");
			//set type *must be string
			if(typeof settings.clientSideCache.type === "string") {
				_type = settings.clientSideCache.type === "localStorage" ? "localStorage" : "sessionStorage";

				//if type is localStorage, control the expire date
				if(settings.clientSideCache.type === "localStorage") {
					//set expires *must be date object
					if(typeof settings.clientSideCache.expires === "object") {
						_expires = settings.clientSideCache.expires;
					} else {
						_expires = defaults.expires; //set default
					}
					//end of set expires
				}
				//end of if type is localStorage, control the expire date
			} else {
				_type = "sessionStorage";
			};
			//end of set type

			//set key
			if(typeof settings.clientSideCache.key === "string") {
				_key = _prefix + settings.clientSideCache.key; //add prefix to user defined key
			} else {
				_key = _createCacheKey(settings.url, settings.data);
			}
			//end of set key

			//if ajax has clientSideCache data, override beforeSend and error functions, else make ajax request and set reponse to cache data
			var cacheData = _getCache();
			console.log("cacheData:");
			console.log(cacheData);
			if(!!cacheData) {
				//control ajax error
				//not needed
				// var _originalError;
				// if(typeof settings.error === "function") {
				// 	_originalError = settings.error;
				// }
				// settings.error = function (jqXHR, textStatus, errorThrown) {
				// 	console.log("its error");
				// 	if(textStatus === "abort") {
				// 		console.log("aborted because cache");
				// 		jqXHR.statusText = "OK";
				// 		jqXHR.status = 200;
				// 		jqXHR.readyState = 4;
				// 		jqXHR.settings.success(cacheData, "OK", jqXHR);
				// 		return;
				// 	}
				//
				// 	_originalError(jqXHR, textStatus, errorThrown);
				// }
				//end of control ajax error

				//control ajax cache on beforeSend
				var _originalBeforeSend;
				if(typeof settings.beforeSend === "function") {
					_originalBeforeSend = settings.beforeSend;
				}

				settings.beforeSend = function(jqXHR, settings) {
					_originalBeforeSend(jqXHR, settings);
					jqXHR["settings"] = settings;
					jqXHR["responseJSON"] = cacheData;
					jqXHR["responseText"] = JSON.stringify(cacheData);
					settings.success(cacheData, "OK", jqXHR);
					console.log("jqXHR aborted");
					jqXHR.abort();
				}
				//end of control ajax cache on beforeSend

			} else {
				var _originalSuccess;
				if(typeof settings.success === "function") {
					_originalSuccess = settings.success;
				}
				settings.success = function (data, testStatus, jqxhr) {
					_setCache(data);
					_originalSuccess(data, testStatus, jqxhr);
				}
			}

		}
		//end of if clientSideCache defined, do the things

		//do jquery ajax things
		var result = originalAjaxFunction(settings);
		//end of do jquery ajax things
	}

	//this function is clean all sessionStorage or localStorage decided with options parameter
	$.cleanAjaxClientSideCache = function(options) {
		console.log("clean cache taken");
		return true;
	};

	//this function can get cache from sessionStorage or localStorage with options parameter
	$.getAjaxClientSideCache = function(options) {
		console.log("get cache taken");
		return true;
	};

}(jQuery));
<commit_msg>getAjaxClientSideCache and cleanAjaxClientSideCache methods implemented.<commit_after>(function ($) {
	var originalAjaxFunction = $.ajax;

	$.ajaxClientSideCacheTypeEnum = {
		sS: 'sessionStorage',
		lS: 'localStorage'
	};

	var defaults = {
		type: $.ajaxClientSideCacheTypeEnum.sS, //or localStorage
		expires: null, //optional, must be datetime object, not necessary if type is sessionStorage
		key: null //optional, must be string
	};

	var _type, _expires, _key;

	var _prefix = "csc_";

	function _createCacheKey(url, postData) {
		if (typeof url === "string") {
			var result = _prefix + url;
			if (!!postData) {
				$.each(Object.keys(postData), function (index, item) {
					result += "_" + item + "-" + postData[item];
				});
			}

			return result.replace(" ", "");
		} else {
			return null;
		}
	};

	function _setCache(responseData) {
		if (_type === $.ajaxClientSideCacheTypeEnum.sS) {
			sessionStorage.setItem(_key, JSON.stringify(responseData));
		} else {
			localStorage.setItem(_key, JSON.stringify(responseData));
		}
		return true;
	};

	function _getCache() {
		return _type === $.ajaxClientSideCacheTypeEnum.sS
			? JSON.parse(sessionStorage.getItem(_key))
			: JSON.parse(localStorage.getItem(_key));
	}

	function _removeCache(url, postData) {
		var cacheKey;
		if (!!url) {
			cacheKey = _createCacheKey(url, postData);
			_type === $.ajaxClientSideCacheTypeEnum.sS
				? sessionStorage.removeItem(cacheKey)
				: localStorage.removeItem(cacheKey);
		}
	}

	$.ajax = function (settings) {
		console.log(settings);
		console.log("ajax called");
		//if clientSideCache defined, do the things
		if (typeof settings.clientSideCache === "object") {
			console.log("cache func start");
			//set type *must be string
			if (typeof settings.clientSideCache.type === "string") {
				_type = settings.clientSideCache.type === "localStorage" ? "localStorage" : "sessionStorage";

				//if type is localStorage, control the expire date
				if (settings.clientSideCache.type === $.ajaxClientSideCacheTypeEnum.lS) {
					//set expires *must be date object
					if (typeof settings.clientSideCache.expires === "object") {
						_expires = settings.clientSideCache.expires;
					} else {
						_expires = defaults.expires; //set default
					}
					//end of set expires
				}
				//end of if type is localStorage, control the expire date
			} else {
				_type = $.ajaxClientSideCacheTypeEnum.sS;
			};
			//end of set type

			//set key
			if (typeof settings.clientSideCache.key === "string") {
				_key = _prefix + settings.clientSideCache.key; //add prefix to user defined key
			} else {
				_key = _createCacheKey(settings.url, settings.data);
			}
			//end of set key

			//if ajax has clientSideCache data, override beforeSend and error functions, else make ajax request and set reponse to cache data
			var cacheData = _getCache();
			console.log("cacheData:");
			console.log(cacheData);
			if (!!cacheData) {
				//control ajax cache on beforeSend
				var _originalBeforeSend;
				if (typeof settings.beforeSend === "function") {
					_originalBeforeSend = settings.beforeSend;
				}

				settings.beforeSend = function (jqXHR, settings) {
					_originalBeforeSend(jqXHR, settings);
					jqXHR["settings"] = settings;
					jqXHR["responseJSON"] = cacheData;
					jqXHR["responseText"] = JSON.stringify(cacheData);
					settings.success(cacheData, "OK", jqXHR);
					console.log("jqXHR aborted");
					jqXHR.abort();
				}
				//end of control ajax cache on beforeSend

			} else {
				var _originalSuccess;
				if (typeof settings.success === "function") {
					_originalSuccess = settings.success;
				}
				settings.success = function (data, testStatus, jqxhr) {
					_setCache(data);
					_originalSuccess(data, testStatus, jqxhr);
				}
			}

		}
		//end of if clientSideCache defined, do the things

		//do jquery ajax things
		var result = originalAjaxFunction(settings);
		//end of do jquery ajax things
	}

	//this function is clean all sessionStorage or localStorage decided with options parameter
	$.cleanAjaxClientSideCache = function (options) {
		console.log("clean cache taken");
		$.each(options.type === $.ajaxClientSideCacheTypeEnum.sS
			? sessionStorage
			: localStorage,
			function (key) {
				if (key.substr(0, _prefix.length) === _prefix) {
					(options.type === $.ajaxClientSideCacheTypeEnum.sS
						? sessionStorage
						: localStorage).removeItem(key);
				}
			});
		return true;
	};

	//this function can get cache from sessionStorage or localStorage with options parameter
	$.getAjaxClientSideCache = function (options) {
		console.log("get cache taken");
		if (!options.key) {
			return null;
		}
		return JSON.parse((options.type === $.ajaxClientSideCacheTypeEnum.sS
			? sessionStorage
			: localStorage).getItem(options.key));
	};
}(jQuery));

{'fraction_non_alphanumeric': 0.12770306616460464, 'fraction_numerical': 0.0005379236148466917, 'mean_word_length': 3.3237209302325583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1667169', 'n_tokens_mistral': 3485, 'n_tokens_neox': 2846, 'n_words': 739}