<commit_before>fs = require('fs')
path = require('path')
rimraf = require('rimraf')
fsUtils = require('../fs-utils/fs-utils')
exports.prefix = require('./data-prefix')

haltIfNoPrefix = (callback) ->
	return ->
		if not exports.prefix.get()?
			throw new Error('Did you forget to set a prefix?')
		return callback.apply(null, arguments)

constructPath = (key) ->
	prefix = exports.prefix.get()
	result = path.join(prefix, key)

	if not fsUtils.isValidPath(result)
		throw new Error('Invalid path')

	return result

exports.get = haltIfNoPrefix (key, options, callback) ->
	exports.has key, (hasKey) ->
		if not hasKey
			return callback?(null)

		keyPath = constructPath(key)
		fs.readFile(keyPath, options, callback)

exports.set = haltIfNoPrefix (key, value, options, callback) ->
	keyPath = constructPath(key)
	fs.writeFile(keyPath, value, options, callback)

exports.has = haltIfNoPrefix (key, callback) ->
	keyPath = constructPath(key)
	fs.exists(keyPath, callback)<commit_msg>Pass undefined directly on data.get when key doesn't exists<commit_after>fs = require('fs')
path = require('path')
rimraf = require('rimraf')
fsUtils = require('../fs-utils/fs-utils')
exports.prefix = require('./data-prefix')

haltIfNoPrefix = (callback) ->
	return ->
		if not exports.prefix.get()?
			throw new Error('Did you forget to set a prefix?')
		return callback.apply(null, arguments)

constructPath = (key) ->
	prefix = exports.prefix.get()
	result = path.join(prefix, key)

	if not fsUtils.isValidPath(result)
		throw new Error('Invalid path')

	return result

exports.get = haltIfNoPrefix (key, options, callback) ->
	exports.has key, (hasKey) ->
		if not hasKey

			# Pass undefined explicitly, otherwise
			# async gets confused
			return callback?(null, undefined)

		keyPath = constructPath(key)
		fs.readFile(keyPath, options, callback)

exports.set = haltIfNoPrefix (key, value, options, callback) ->
	keyPath = constructPath(key)
	fs.writeFile(keyPath, value, options, callback)

exports.has = haltIfNoPrefix (key, callback) ->
	keyPath = constructPath(key)
	fs.exists(keyPath, callback)
{'fraction_non_alphanumeric': 0.13499281953087602, 'fraction_numerical': 0.0, 'mean_word_length': 4.694822888283379, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '4360937', 'n_tokens_mistral': 688, 'n_tokens_neox': 658, 'n_words': 179}