<commit_before>// Returns fragment generator, where each returned fragment emits selected indexed data for
// specified object

'use strict';

var aFrom        = require('es5-ext/array/from')
  , deferred     = require('deferred')
  , ensureSet    = require('es6-set/valid-set')
  , ensureDriver = require('dbjs-persistence/ensure')
  , Fragment     = require('data-fragment')

  , isArray = Array.isArray;

var emitEvent = function (fragment, objId, keyPath, data) {
	if (!isArray(data.value)) {
		fragment.update(objId + '/' + keyPath, data);
		return;
	}
	data.value.forEach(function (data) {
		var key = data.key ? '*' + data.key : '';
		fragment.update(objId + '/' + keyPath + key, data);
	});
};

module.exports = function (driver, keyPaths) {
	var fragments = Object.create(null);

	ensureDriver(driver);
	keyPaths = aFrom(ensureSet(keyPaths));

	return function (objId) {
		var fragment = fragments[objId];
		if (fragment) return fragment;
		fragment = fragments[objId] = new Fragment();
		fragment.promise = driver.getValue(objId)(function (data) {
			if (data) fragment.update(objId, data);
			return deferred.map(keyPaths, function (keyPath) {
				return driver.getIndexedValue(objId, keyPath)(function (data) {
					if (data) emitEvent(fragment, objId, keyPath, data);
				});
			});
		});
		driver.on('object:' + objId, function (event) {
			if (keyPaths.has(event.keyPath)) {
				emitEvent(fragment, objId, event.keyPath, event.data, event.old);
			}
		});
		return fragment;
	};
};<commit_msg>-- cleanup<commit_after>// Returns fragment generator, where each returned fragment emits selected indexed data for
// specified object

'use strict';

var aFrom        = require('es5-ext/array/from')
  , deferred     = require('deferred')
  , ensureSet    = require('es6-set/valid-set')
  , ensureDriver = require('dbjs-persistence/ensure')
  , Fragment     = require('data-fragment')

  , isArray = Array.isArray;

var emitEvent = function (fragment, objId, keyPath, data) {
	if (!isArray(data.value)) {
		fragment.update(objId + '/' + keyPath, data);
		return;
	}
	data.value.forEach(function (data) {
		var key = data.key ? '*' + data.key : '';
		fragment.update(objId + '/' + keyPath + key, data);
	});
};

module.exports = function (driver, keyPaths) {
	var fragments = Object.create(null);

	ensureDriver(driver);
	keyPaths = aFrom(ensureSet(keyPaths));

	return function (objId) {
		var fragment = fragments[objId];
		if (fragment) return fragment;
		fragment = fragments[objId] = new Fragment();
		fragment.promise = driver.getValue(objId)(function (data) {
			if (data) fragment.update(objId, data);
			return deferred.map(keyPaths, function (keyPath) {
				return driver.getIndexedValue(objId, keyPath)(function (data) {
					if (data) emitEvent(fragment, objId, keyPath, data);
				});
			});
		});
		driver.on('object:' + objId, function (event) {
			if (keyPaths.has(event.keyPath)) {
				emitEvent(fragment, objId, event.keyPath, event.data);
			}
		});
		return fragment;
	};
};
{'fraction_non_alphanumeric': 0.1552298467688208, 'fraction_numerical': 0.0013324450366422385, 'mean_word_length': 3.988372093023256, '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': '2885653', 'n_tokens_mistral': 1050, 'n_tokens_neox': 967, 'n_words': 222}