<commit_before>var Filesaver, collections, safename, fs, msgErr;

// get dependencies
safename = require( './safename' );
fs = require( 'fs' );

msgErr = function ( msg ) {
	this.msg = msg;
	this.name = "Error";
}

/**
 * Filesaver constructor.
 *
 * Example:
 * 
 * ```js
 * var collections = {
 *     images: './images',
 *     books: './books'
 * }
 * var filesaver = new Filesaver( collections );
 * ```
 * 
 * @param {Object} collections Collections schema
 */

Filesaver = function (cols) {
	// Store collections in private
	collections = cols;

	// check for existing folders
	for (col in collections) {
		if (!fs.existsSync( collections[col] )){
			// create folder if not exists
			fs.mkdir( collections[col] );
		}
	}
};



/**
 * Add a new collection
 *
 * Example:
 *
 * ```js
 * filesaver.collection( 'documents', './path/to/folder', function () {
 *     // do something
 * });
 * ```
 * @param  {String}   name       name of new collection
 * @param  {Object}   collection Collection schema
 * @param  {Function} callback   no signature callback
 */

Filesaver.prototype.collection = function (name, folder, callback) {
	// create folder if not exists
	if (!fs.existsSync( folder )) {
		fs.mkdir( folder );
	}
	// add collection
	collections[name] = folder;
	// callback if exists
	if (callback){
		callback();
	}
}



/**
 * Add a new file without overwrite anyone
 *
 * Example:
 *
 * ```js
 * filesaver.add( 'images', '/path/to/temp/file.jpg', 'avatar.jpg', function (err, data) {
 *     console.log( data );
 *     // ->
 *     // filename: 'avatar_2.jpg',
 *     // filepath: './images'
 * });
 * ```
 * 
 * @param {String}   collection Name of collection to insert the file
 * @param {String}   origin     path to origin file
 * @param {String}   target     name target file
 * @param {Function} callback   Signature: error, data. Data signature:{filename, filepath}
 */

Filesaver.prototype.add = function (collection, origin, target, callback) {

	// check for valid arguments
	if (collection && origin && (typeof collection === 'string') && (typeof origin === 'string')) {

		var _this = this, rename, filename, destiny;

		/*
		 * +1 to filename sufix if file exists, else callback
		 * @param  {String} path path to check
		 */
		rename = function (path) {
			if (fs.exists( path )) {
				path = path.split( '_' );
				len = path.length;
				path[len - 1] = 1 + path[len - 1];
				checker( path.join( ), callback);
			} else {
				return _this.replace( collection, origin, path, callback );
			}
		};

		// get filename and destiny
		filename = origin.split('/').pop();
		destiny = '' + collections[collection] + '/' + filename;
		
		// check if file exists
		if (fs.exists( destiny )) {
			rename( '' + destiny + '_1', callback );
		} else {
			// write file
			return this.replace( collection, origin, destiny, callback );
		}
		
	} else {
		throw new msgErr( 'Collection or origin not valid');
	}

}



/**
 * Remove old file and then add the new one
 *
 * Example:
 * 
 * ```js
 * filesaver.swap( 'images', '/path/to/temp/file.jpg', 'avatar.jpg', function (err, data) {
 *     console.log( data );
 *     // ->
 *     // filename: 'file.jpg',
 *     // filepath: './images'
 * });
 * ```<commit_msg>added buggy renamer<commit_after>var Filesaver, collections, safename, fs, msgErr, rename;

// get dependencies
safename = require( './safename' );
fs = require( 'fs' );

msgErr = function ( msg ) {
	this.msg = msg;
	this.name = "Error";
}


/**
 * Filesaver constructor.
 *
 * Example:
 * 
 * ```js
 * var collections = {
 *     images: './images',
 *     books: './books'
 * }
 * var filesaver = new Filesaver( collections );
 * ```
 * 
 * @param {Object} collections Collections schema
 */

Filesaver = function (cols) {
	// Store collections in private
	collections = cols;

	// check for existing folders
	for (col in collections) {
		if (!fs.existsSync( collections[col] )){
			// create folder if not exists
			fs.mkdir( collections[col] );
		}
	}
};



/**
 * Add a new collection
 *
 * Example:
 *
 * ```js
 * filesaver.collection( 'documents', './path/to/folder', function () {
 *     // do something
 * });
 * ```
 * @param  {String}   name       name of new collection
 * @param  {Object}   collection Collection schema
 * @param  {Function} callback   no signature callback
 */

Filesaver.prototype.collection = function (name, folder, callback) {
	// create folder if not exists
	if (!fs.existsSync( folder )) {
		fs.mkdir( folder );
	}
	// add collection
	collections[name] = folder;
	// callback if exists
	if (callback){
		callback();
	}
}



/**
 * Add a new file without overwrite anyone
 *
 * Example:
 *
 * ```js
 * filesaver.add( 'images', '/path/to/temp/file.jpg', 'avatar.jpg', function (err, data) {
 *     console.log( data );
 *     // ->
 *     // filename: 'avatar_2.jpg',
 *     // filepath: './images'
 * });
 * ```
 * 
 * @param {String}   collection Name of collection to insert the file
 * @param {String}   origin     path to origin file
 * @param {String}   target     name target file
 * @param {Function} callback   Signature: error, data. Data signature:{filename, filepath}
 */

Filesaver.prototype.add = function (collection, origin, target, callback) {
	// check for valid arguments
	if (collection && origin && (typeof collection === 'string') && (typeof origin === 'string')) {

		var _this = this, checker, filename, destiny, rename;

		/*
		 * +1 to filename sufix if file exists, else callback
		 * @param  {String} path path to check
		 */
		checker = function (path) {
			if (fs.existsSync( '' + collections[collection] + '/' + path )) {

				path = path.split( '_' );
				len = path.length;
				path[len - 1] = 1 + path[len - 1];
				checker( path.join( '_' ), callback);

			} else {
				return _this.replace( collection, origin, path, callback );
			}
		};

		rename = function (path) {

			var splitted, name, split_, num;

			splitdot = path.split( '.' );
			name = splitdot.shift();
			split_ = name.split( '_' );
			num = split_.pop();
			num = 1 + num;
			split_.push( num );
			name = split_.join( '_' );
			splitdot.unshift( name );

			checker( splitdot.join( '.' ));
		}


		// get filename and destiny
		filename = origin.split('/').pop();
		destiny = '' + collections[collection] + '/' + target;

		checker( target, callback );
		
	} else {
		console.log('error!');
		throw new msgErr( 'Collection or origin not valid' );
	}

}



/**
 * Remove old file and then add the new one
 *
 * Example:
 * 
 * ```js
 * filesaver.swap( 'images', '/path/to/temp/file.jpg', 'avatar.jpg', function (err, data) {
 *     console.log( data );
 *     // ->
 *     // filename: 'file.jpg',
 *     // filepath: './images'
 * });
 * ```
{'fraction_non_alphanumeric': 0.16728790815565828, 'fraction_numerical': 0.0017891754882958103, 'mean_word_length': 2.774901519414744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5676451', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2119, 'n_words': 624}