<commit_before>const SEARCH_API = 'https://api.reddit.com/search.json?sort=top&t=all&limit=100&q=url:';
const INFO_API = 'https://reddit.com/api/info.json?url=';

function findOnReddit(url, useCache = true, exact = true) {
	let query = encodeURIComponent(url);
	let results = search(query, useCache, exact);
	results.then(res => cachePosts(query, res, exact)).catch(ignoreRejection);
	return results;
}

function search(query, useCache = true, exact = true) {
	let requestUrl = `${exact ? INFO_API : SEARCH_API}${query}`;
	if (!useCache) {
		return makeApiRequest(requestUrl).then(res => res.data.children);
	}
	return searchCache(query).then(cache => {
		let data = cache[query] || {};
		let key = exact ? 'exact' : 'nonExact';
		let otherResults = data[exact ? 'nonExact' : 'exact'];
		return checkCacheValidity(data, key).then(isValid => {
			if (isValid) {
				posts = data[key].posts;
				if (otherResults) {
					posts.other = otherResults.posts.length;
				}
				return posts;
			} else {
				let res = makeApiRequest(requestUrl).then(res => res.data.children);
				if (otherResults) {
					res.then(posts => {
						posts.other = otherResults.posts.length;
						return posts;
					});
				}
				return res;
			}
		});
	});
}

function makeApiRequest(url) {
	console.log('cache miss');
	return new Promise((resolve, reject) => {
		$.get(url).done(resolve).fail(reject);
	});
}

function cachePosts(query, posts, exact) {
	let key = exact ? 'exact' : 'nonExact';
	searchCache(query).then(c => {
		let objectToStore = {};
		let data = c[query] || {};
		data[key] = {
			posts: posts,
			time: Date.now()
		};
		objectToStore[query] = data;
		return cache(objectToStore);
	});
}

function checkCacheValidity(cache, key) {
	if (!cache.hasOwnProperty(key)) {
		return Promise.resolve(false);
	}
	let data = cache[key];
	if (!(data.time && data.posts)) {
		return Promise.resolve(false);
	}
	let diff = Date.now() - data.time;
	let query = { cache: { period: DEFAULT_CACHE_PERIOD_MINS } };
	return getOptions(query).then(opts => diff < +(opts.cache.period) * 60e3);
}
<commit_msg>ignore protocol when doing a non-exact search<commit_after>const SEARCH_API = 'https://api.reddit.com/search.json?sort=top&t=all&limit=100&q=url:';
const INFO_API = 'https://reddit.com/api/info.json?url=';

function findOnReddit(url, useCache = true, exact = true) {
	if (!exact) {
		url = url.replace(/^https?\:\/\//i, "")
	}
	let query = encodeURIComponent(url);
	let results = search(query, useCache, exact);
	results.then(res => cachePosts(query, res, exact)).catch(ignoreRejection);
	return results;
}

function search(query, useCache = true, exact = true) {
	let requestUrl = `${exact ? INFO_API : SEARCH_API}${query}`;
	if (!useCache) {
		return makeApiRequest(requestUrl).then(res => res.data.children);
	}
	return searchCache(query).then(cache => {
		let data = cache[query] || {};
		let key = exact ? 'exact' : 'nonExact';
		let otherResults = data[exact ? 'nonExact' : 'exact'];
		return checkCacheValidity(data, key).then(isValid => {
			if (isValid) {
				posts = data[key].posts;
				if (otherResults) {
					posts.other = otherResults.posts.length;
				}
				return posts;
			} else {
				let res = makeApiRequest(requestUrl).then(res => res.data.children);
				if (otherResults) {
					res.then(posts => {
						posts.other = otherResults.posts.length;
						return posts;
					});
				}
				return res;
			}
		});
	});
}

function makeApiRequest(url) {
	console.log('cache miss');
	return new Promise((resolve, reject) => {
		$.get(url).done(resolve).fail(reject);
	});
}

function cachePosts(query, posts, exact) {
	let key = exact ? 'exact' : 'nonExact';
	searchCache(query).then(c => {
		let objectToStore = {};
		let data = c[query] || {};
		data[key] = {
			posts: posts,
			time: Date.now()
		};
		objectToStore[query] = data;
		return cache(objectToStore);
	});
}

function checkCacheValidity(cache, key) {
	if (!cache.hasOwnProperty(key)) {
		return Promise.resolve(false);
	}
	let data = cache[key];
	if (!(data.time && data.posts)) {
		return Promise.resolve(false);
	}
	let diff = Date.now() - data.time;
	let query = { cache: { period: DEFAULT_CACHE_PERIOD_MINS } };
	return getOptions(query).then(opts => diff < +(opts.cache.period) * 60e3);
}

{'fraction_non_alphanumeric': 0.1712875939849624, 'fraction_numerical': 0.002819548872180451, 'mean_word_length': 3.9615384615384617, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 4, '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': '2159973', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1486, 'n_words': 302}