<commit_before>/**
 * @(#)hive.Pagination 2013.03.21
 *
 * Copyright NHN Corporation.
 * Released under the MIT license
 * 
 * http://hive.dev.naver.com/license
 */
// Render pagination in the given target HTML element.
// Usage: Pagiation.updatePagination(target, totalPages);
// For more details, see docs/technical/pagination.md
/**
 * TODO: 무한 스크롤 구현을 할 게 아니라면
 * 굳이 페이징 링크를 굳이 동적으로 만들어야 할까? 개선 검토 필요
 */
hive.Pagination = (function(window, document) {
	
	var htRegEx = {};
	
	/**
	 * getQuery
	 * @param {String} url
	 */
	function getQuery(url){
		var parser = document.createElement('a');
			parser.href = url;
			
		return parser.search;
	}

	/**
	 * valueFromQuery
	 * @param {String} key
	 * @param {String} query
	 */
	function valueFromQuery(key, query) {
		htRegEx[key] = htRegEx[key] || new RegExp('(^|&|\\?)' + key + '=([^&]+)'); 
		var result = htRegEx[key].exec(query);

		return (result) ? result[2]: null;
	}

	/**
	 * urlWithQuery
	 * @param {String} url
	 * @param {String} query
	 */
	function urlWithQuery(url, query) {
		var parser = document.createElement('a');
			parser.href = url;
			parser.search = '?' + query;
			
		return parser.href;
	}

	/**
	 * urlWithPageNum
	 * @param {String} url
	 * @param {Number} pageNum
	 * @param {String} paramNameForPage
	 */
	function urlWithPageNum(url, pageNum, paramNameForPage) {
		var query = getQuery(url);
		var regex = new RegExp('(^|&|\\?)' + paramNameForPage + '=[^&]+');
		var result = regex.exec(query);
		query = query.replace(regex, result[1] + paramNameForPage + '=' + pageNum);
		
		return urlWithQuery(url, query);
	}

	/**
	 * validateOptions
	 */
	function validateOptions(options) {
		if (!Number.isFinite(options.current)) {
			throw new Error("options.current is not valid: " + options.current);
		}
	}

	/**
	 * window.updatePagination
	 */
	window.updatePagination = function(target, totalPages, options) {
		if (totalPages <= 0){
			return;
		}

		var target = $(target);
		var linkToPrev, linkToNext, urlToPrevPage, urlToNextPage;
		var options = options || {};
		
		options.url = options.url || document.URL;
		options.firstPage = options.firstPage || 1;
		options.hasPrev = (typeof options.hasPrev == "undefined") ? options.current > options.firstPage : options.hasPrev;
		options.hasNext = (typeof options.hasNext == "undefined") ? options.current < totalPages : options.hasNext;

		var paramNameForPage = options.paramNameForPage || 'pageNum';<commit_msg>pagination: Fix js error if url has no page param.<commit_after>/**
 * @(#)hive.Pagination 2013.03.21
 *
 * Copyright NHN Corporation.
 * Released under the MIT license
 * 
 * http://hive.dev.naver.com/license
 */
// Render pagination in the given target HTML element.
// Usage: Pagiation.updatePagination(target, totalPages);
// For more details, see docs/technical/pagination.md
/**
 * TODO: 무한 스크롤 구현을 할 게 아니라면
 * 굳이 페이징 링크를 굳이 동적으로 만들어야 할까? 개선 검토 필요
 */
hive.Pagination = (function(window, document) {
	
	var htRegEx = {};
	
	/**
	 * getQuery
	 * @param {String} url
	 */
	function getQuery(url){
		var parser = document.createElement('a');
			parser.href = url;
			
		return parser.search;
	}

	/**
	 * valueFromQuery
	 * @param {String} key
	 * @param {String} query
	 */
	function valueFromQuery(key, query) {
		htRegEx[key] = htRegEx[key] || new RegExp('(^|&|\\?)' + key + '=([^&]+)'); 
		var result = htRegEx[key].exec(query);

		return (result) ? result[2]: null;
	}

	/**
	 * urlWithQuery
	 * @param {String} url
	 * @param {String} query
	 */
	function urlWithQuery(url, query) {
		var parser = document.createElement('a');
			parser.href = url;
			parser.search = '?' + query;
			
		return parser.href;
	}

	/**
	 * urlWithPageNum
     *
     * Create a url whose query has a paramNameForPage parameter whose value is
     * pageNum.
     *
	 * @param {String} url
	 * @param {Number} pageNum
	 * @param {String} paramNameForPage
	 */
	function urlWithPageNum(url, pageNum, paramNameForPage) {
		var query = getQuery(url);
		var regex = new RegExp('(^|&|\\?)' + paramNameForPage + '=[^&]+');
		var result = regex.exec(query);
        if (result) {
            // if paramNameForPage parameter already exists, update it.
            query = query.replace(regex, result[1] + paramNameForPage + '=' + pageNum);
        } else {
            // if not add new one.
            query = query + '&' + paramNameForPage + '=' + pageNum;
        }

		return urlWithQuery(url, query);
	}

	/**
	 * validateOptions
	 */
	function validateOptions(options) {
		if (!Number.isFinite(options.current)) {
			throw new Error("options.current is not valid: " + options.current);
		}
	}

	/**
	 * window.updatePagination
	 */
	window.updatePagination = function(target, totalPages, options) {
		if (totalPages <= 0){
			return;
		}

		var target = $(target);
		var linkToPrev, linkToNext, urlToPrevPage, urlToNextPage;
		var options = options || {};
		
		options.url = options.url || document.URL;
		options.firstPage = options.firstPage || 1;
		options.hasPrev = (typeof options.hasPrev == "undefined") ? options.current > options.firstPage : options.hasPrev;
		options.hasNext = (typeof options.hasNext == "undefined") ? options.current < totalPages : options.hasNext;

		var paramNameForPage = options.paramNameForPage || 'pageNum';
{'fraction_non_alphanumeric': 0.15273829827553534, 'fraction_numerical': 0.0045480386583285955, 'mean_word_length': 3.3440329218106997, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '3007776', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1843, 'n_words': 426}