<commit_before>/*global
define, ga
*/
define([
	'codemirror',
	'jsonlint',
	'js-beautify',
	'lib/minify.json',
	'javascript-syntax'
],
function(CodeMirror, jsonlint, beautify, minify) {
	'use strict';
	var doc = document,
		App = function App() {
			var form = this.form = doc.forms.main,
				query = this.query = parseQuery();

			this
				.initEditor()
				.registerEvents();

			// defines 'code' property
			Object.defineProperty(this, 'code', {
				get: function() {
					return this.editor.getValue();
				},
				set: function(v) {
					form.code.value = v;
					this.editor.setValue(v);
				}
			});

			// if json parameter is given, use it
			// URL (where JSON is located) is also allowed
			if (query.json) {
				this.code = query.json;
				this.go();
			}
		},
		fn = App.prototype;

	// registers events
	fn.registerEvents = function() {
		// when user types something, remove highlighting from "bad" line
		this.editor.on('change', function() {
			this.highlightErrorLine(null);
		}.bind(this));

		// when user submits form (eg presses "Validate" button), run "go" method
		this.form.addEventListener('submit', function(evt) {
			evt.preventDefault();
			this.go();
		}.bind(this));

		// when Ctrl-Enter is pressed, run "go" method
		doc.addEventListener('keyup', function(evt) {
			if (evt.ctrlKey && evt.keyCode == 13) {
				this.go();
		  	}
		}.bind(this));

		// expands/unexpands faq by clicking #faqButton
		$('#faqButton').addEventListener('click', function(evt) {
			evt.preventDefault();
			$('#faq').classList.toggle('expand');
		});

		// initializes Google Analytics tracking
		// when user clicks on [data-ga="blah"], call ga('send', 'pageview', '/blah');
		$$('[data-ga]').forEach(function(node) {
			node.addEventListener('click', function() {
				ga('send', 'pageview', '/' + node.getAttribute('data-ga'));
			});
		});

		return this;
	};

	// initializes CodeMirror editor
	fn.initEditor = function() {
		this.editor = CodeMirror.fromTextArea(this.form.code, {
			lineNumbers: true,
			styleActiveLine: true,
			matchBrackets: true,
			indentWithTabs: true
		});

		return this;
	};

	// the main function of this app
	fn.go = function() {
		var code = this.code;
		// if URL is given, fetch data on this URL
		if (code.indexOf('http') === 0) {
			this.fetch(code, function(resp) {
				// if fetching is OK, run validator
				this.validate(resp);
			}, function(err) {
				// if not, show error
				this.notify(false, err);
			});
		// if non-url is given, run validator
		} else {
			this.validate();
		}

		return this;
	};

	// combs JSON depending on query.reformat value
	// code argument is optional
	fn.comb = function(code) {
		code = typeof code == 'undefined' ? this.code : code;

		// if reformat==compress, use minifier
		// if reformat==no, keep code as is
		// else beautify code
		if (this.query.reformat == 'compress') {
			code = minify(code) || code;
		} else if(this.query.reformat != 'no') {
			code = beautify.js_beautify(code, {
				indent_with_tabs: true
			});
		}

		this.code = code;

		return this;
	};

	// validates JSON
	// code argument is optional
	fn.validate = function(code) {
		var lineMatches;

		this.comb(code);
		code = this.code;

		try {
			jsonlint.parse(code);
			this.notify(true, 'Valid JSON');
		} catch (e) {
			// retrieve line number from error
			lineMatches = e.message.match(/line ([0-9]*)/);

			if (lineMatches && lineMatches.length > 1) {
				this.highlightErrorLine(+lineMatches[1]-1);
			}

			this.notify(false, e);
		}

		return this;
	};

	// displays success or error message about validation status
	fn.notify = function(success, text) {
		var result = $('#result');
		$('#result-container').classList.add('shown');
		// ie10 doesn't support 2nd argument in classList.toggle
		result.classList[success ? 'add' : 'remove']('success');
		result.classList[!success ? 'add' : 'remove']('error');
		result.innerHTML = text;

		return this;
	};<commit_msg>use dirty hack to close #32<commit_after>/*global
define, ga
*/
define([
	'codemirror',
	'jsonlint',
	'js-beautify',
	'lib/minify.json',
	'javascript-syntax'
],
function(CodeMirror, jsonlint, beautify, minify) {
	'use strict';
	var doc = document,
		App = function App() {
			var form = this.form = doc.forms.main,
				query = this.query = parseQuery();

			this
				.initEditor()
				.registerEvents();

			// defines 'code' property
			Object.defineProperty(this, 'code', {
				get: function() {
					return this.editor.getValue();
				},
				set: function(v) {
					form.code.value = v;
					this.editor.setValue(v);
				}
			});

			// if json parameter is given, use it
			// URL (where JSON is located) is also allowed
			if (query.json) {
				this.code = query.json;
				this.go();
			}
		},
		fn = App.prototype;

	// registers events
	fn.registerEvents = function() {
		// when user types something, remove highlighting from "bad" line
		this.editor.on('change', function() {
			this.highlightErrorLine(null);
		}.bind(this));

		// when user submits form (eg presses "Validate" button), run "go" method
		this.form.addEventListener('submit', function(evt) {
			evt.preventDefault();
			this.go();
		}.bind(this));

		// when Ctrl-Enter is pressed, run "go" method
		doc.addEventListener('keyup', function(evt) {
			if (evt.ctrlKey && evt.keyCode == 13) {
				this.go();
		  	}
		}.bind(this));

		// expands/unexpands faq by clicking #faqButton
		$('#faqButton').addEventListener('click', function(evt) {
			evt.preventDefault();
			$('#faq').classList.toggle('expand');
		});

		// initializes Google Analytics tracking
		// when user clicks on [data-ga="blah"], call ga('send', 'pageview', '/blah');
		$$('[data-ga]').forEach(function(node) {
			node.addEventListener('click', function() {
				ga('send', 'pageview', '/' + node.getAttribute('data-ga'));
			});
		});

		return this;
	};

	// initializes CodeMirror editor
	fn.initEditor = function() {
		this.editor = CodeMirror.fromTextArea(this.form.code, {
			lineNumbers: true,
			styleActiveLine: true,
			matchBrackets: true,
			indentWithTabs: true
		});

		return this;
	};

	// the main function of this app
	fn.go = function() {
		var code = this.code;
		// if URL is given, fetch data on this URL
		if (code.indexOf('http') === 0) {
			this.fetch(code, function(resp) {
				// if fetching is OK, run validator
				this.validate(resp);
			}, function(err) {
				// if not, show error
				this.notify(false, err);
			});
		// if non-url is given, run validator
		} else {
			this.validate();
		}

		return this;
	};

	// combs JSON depending on query.reformat value
	// code argument is optional
	fn.comb = function(code) {
		code = typeof code == 'undefined' ? this.code : code;

		// if reformat==compress, use minifier
		// if reformat==no, keep code as is
		// else beautify code
		if (this.query.reformat == 'compress') {
			code = minify(code) || code;
		} else if(this.query.reformat != 'no') {
			code = beautify.js_beautify(code, {
				indent_with_tabs: true
			});
		}

		this.code = code;

		return this;
	};

	// validates JSON
	// code argument is optional
	fn.validate = function(code) {
		var lineMatches;

		this.comb(code);
		code = this.code;

		try {
			JSON.parse(code.replace(/\\/g, '\\\\'));
			this.notify(true, 'Valid JSON');
		} catch(e) {console.log(e, code);
			try {
				jsonlint.parse(code);
				this.notify(true, 'Valid JSON');
			} catch (e) {
				// retrieve line number from error
				lineMatches = e.message.match(/line ([0-9]*)/);

				if (lineMatches && lineMatches.length > 1) {
					this.highlightErrorLine(+lineMatches[1]-1);
				}

				this.notify(false, e);
			}
		}

		return this;
	};

	// displays success or error message about validation status
	fn.notify = function(success, text) {
		var result = $('#result');
		$('#result-container').classList.add('shown');
		// ie10 doesn't support 2nd argument in classList.toggle
		result.classList[success ? 'add' : 'remove']('success');
		result.classList[!success ? 'add' : 'remove']('error');
		result.innerHTML = text;

		return this;
	};
{'fraction_non_alphanumeric': 0.16511367871785315, 'fraction_numerical': 0.002981736861721953, 'mean_word_length': 3.109239407861154, 'pattern_counts': {'":': 0, '<': 3, '<?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': '52560', 'n_tokens_mistral': 3133, 'n_tokens_neox': 2676, 'n_words': 669}