<commit_before>var Validator = require("../controllers/ValidateObjectController.js");
var fs = require('fs');

/*	Flat File Access
 *  Use: Reads, Saves and Updates JSON objects saved in flat files.
 */

module.exports = {
	saveJSONObject: function(obj, path, callback) {
		Validator.ValidateArgs(arguments, Object, String, Function);
		fs.writeFile(path, JSON.stringify(obj, null, "\t"), callback);
	},
	loadJSONObject: function(path, callback) {
		Validator.ValidateArgs(arguments, String, Function);
		fs.readFile(path, function(err, data) {
			if(err) {
				callback(err, {});
			} else {
				try {
					var o = JSON.parse(data || "{}");
					callback(undefined, o);
				} catch(e) {
					callback(e, {});
				}
			}
		});
	},
	isPathCreated: function(path, callback) {
		Validator.ValidateArgs(arguments, String, Function);
		fs.exists(path, callback);
	},
	deleteFile: function(path, callback) {
		Validator.ValidateArgs(arguments, String, Function);
		fs.unlink(path, callback);
	}
};<commit_msg>Added noop for optional callback default values and added validation for loading/saving to *.json filepaths only.<commit_after>var Validator = require("../controllers/ValidateObjectController.js");
var fs = require('fs');
var noop = function(){};

/*	Flat File Access
 *  Use: Reads, Saves and Updates JSON objects saved in flat files.
 */

var jsonFilePathValidate = function(path) {
	var i=path.indexOf(".json");
	if(i==-1 || i!=(path.length-5)) {
		throw new Error("Path argument must me a *.json filepath.");
	}
};

module.exports = {
	saveJSONObject: function(path, obj, callback) {
		callback = callback || noop;
		Validator.ValidateArgs(arguments, String, Object, Validator.OPTIONAL);
		jsonFilePathValidate(path);
		fs.writeFile(path, JSON.stringify(obj, null, "\t"), callback);
	},
	loadJSONObject: function(path, callback) {
		callback = callback || noop;
		Validator.ValidateArgs(arguments, String, Validator.OPTIONAL);
		jsonFilePathValidate(path);
		fs.readFile(path, 'utf8', function(err, data) {
			if(err) {
				callback(err, {});
			} else {
				var o;
				var parseErr;
				try {
					o = JSON.parse(data || "{}");
				} catch(e) {
					parseErr = e;
				}
				if(parseErr) {
					callback(parseErr, {});
				} else {
					callback(undefined, o);
				}
				
			}
		});
	},
	isPathCreated: function(path, callback) {
		Validator.ValidateArgs(arguments, String, Function);
		fs.exists(path, callback);
	},
	deleteFile: function(path, callback) {
		callback = callback || noop;
		Validator.ValidateArgs(arguments, String, Validator.OPTIONAL);
		fs.unlink(path, callback);
	}
};
{'fraction_non_alphanumeric': 0.15375722543352602, 'fraction_numerical': 0.0011560693641618498, 'mean_word_length': 4.060428849902534, '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': '1055824', 'n_tokens_mistral': 936, 'n_tokens_neox': 841, 'n_words': 193}