<commit_before>import method from './method'
import generateSlug from './generate-slug'

const objectToMongoUpdate = (
	obj,
	top = { $set: {}, $unset: undefined },
	path = []
) =>
	Object.keys(obj).reduce(({ $set, $unset }, key) => {
		const value = obj[key]
		const currentPath = path.concat(key)

		if (value === null) {
			$unset = $unset || {}
			$unset[currentPath.join('.')] = true
		} else if (Object.getPrototypeOf(value) === Object) {
			objectToMongoUpdate(value, { $set, $unset }, currentPath)
		} else {
			$set[currentPath.join('.')] = value
		}

		return { $set, $unset }
	}, top)

export default (collection, validate, historyCollection) => {
	const baseCreate = method(`${collection._name}.create`, function(data) {
		validate.create(data, this.userId)

		data.owner = this.userId
		data.updated = new Date()
		collection.insert(data)

		if (historyCollection) {
			historyCollection.insert({
				verb: 'add',
				date: new Date(),
				owner: this.userId,
				campaignId: data.campaignId,
				data
			})
		}

		return data<commit_msg>do okay with undefined things<commit_after>import method from './method'
import generateSlug from './generate-slug'

const objectToMongoUpdate = (
	obj,
	top = { $set: {}, $unset: undefined },
	path = []
) =>
	Object.keys(obj).reduce(({ $set, $unset }, key) => {
		const value = obj[key]
		const currentPath = path.concat(key)

		if (value === null) {
			$unset = $unset || {}
			$unset[currentPath.join('.')] = true
		} else if (value && Object.getPrototypeOf(value) === Object) {
			objectToMongoUpdate(value, { $set, $unset }, currentPath)
		} else {
			$set[currentPath.join('.')] = value
		}

		return { $set, $unset }
	}, top)

export default (collection, validate, historyCollection) => {
	const baseCreate = method(`${collection._name}.create`, function(data) {
		validate.create(data, this.userId)

		data.owner = this.userId
		data.updated = new Date()
		collection.insert(data)

		if (historyCollection) {
			historyCollection.insert({
				verb: 'add',
				date: new Date(),
				owner: this.userId,
				campaignId: data.campaignId,
				data
			})
		}

		return data
{'fraction_non_alphanumeric': 0.16643159379407615, 'fraction_numerical': 0.0, 'mean_word_length': 3.6769230769230767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2995948', 'n_tokens_mistral': 801, 'n_tokens_neox': 714, 'n_words': 164}