<commit_before>function extractLang(fields, lang) {
	return _.mapValues(fields, function(value, key) {
		return value[lang];
	});
}

var Field = require('./model');

var mw = {
	formatQuery: require('epiphany/middleware/format-query'),
	paginate: require('epiphany/middleware/paginate')
};

var cache = {};

module.exports = {
	create: function(req, res, next) {
		Field.create(req.body, function (err, field) {
			if (err) return next(err);

			res.locals.field = field;

			res.status(201);
			next();
		});
	},

	find: function(req, res, next) {
		var page = Math.max(0, req.query.page) || 0;
		var perPage = Math.max(0, req.query.limit) || res.locals.perPage;

		var query = Field.find(_.omit(req.query, 'limit', 'sort', 'page'),
			null,
			{ sort: req.query.sort || 'path', lean: true });

		if (perPage)
			query.limit(perPage).skip(perPage * page);

		query.exec(function(err, fields) {
			res.locals.fields = fields;
			next(err);
		});
	},

	findById: function(req, res, next) {
		if(req.params.id === 'new') return next();

		Field.findOne({ _id: req.params.id }).lean().exec(function (err, field) {
			if (err) return next(err);

			res.status(200);

			res.locals.field = field;

			next();
		});
	},

	findByPath: function(req, res, next) {
		var page = res.locals.page,
			path = page && page.path || req.path ;

		if(_.has(cache, path)) {
			res.locals.fields = cache[path];

			if(res.lang)
				res.locals.fields = extractLang(res.locals.fields, res.lang);
				
			return next();
		}

		var paths = page ? _.compact(_.pluck(page.pages, 'path')) : [];

		paths.push(path);<commit_msg>pluck > map (lodash change).<commit_after>function extractLang(fields, lang) {
	return _.mapValues(fields, function(value, key) {
		return value[lang];
	});
}

var Field = require('./model');

var mw = {
	formatQuery: require('epiphany/middleware/format-query'),
	paginate: require('epiphany/middleware/paginate')
};

var cache = {};

module.exports = {
	create: function(req, res, next) {
		Field.create(req.body, function (err, field) {
			if (err) return next(err);

			res.locals.field = field;

			res.status(201);
			next();
		});
	},

	find: function(req, res, next) {
		var page = Math.max(0, req.query.page) || 0;
		var perPage = Math.max(0, req.query.limit) || res.locals.perPage;

		var query = Field.find(_.omit(req.query, 'limit', 'sort', 'page'),
			null,
			{ sort: req.query.sort || 'path', lean: true });

		if (perPage)
			query.limit(perPage).skip(perPage * page);

		query.exec(function(err, fields) {
			res.locals.fields = fields;
			next(err);
		});
	},

	findById: function(req, res, next) {
		if(req.params.id === 'new') return next();

		Field.findOne({ _id: req.params.id }).lean().exec(function (err, field) {
			if (err) return next(err);

			res.status(200);

			res.locals.field = field;

			next();
		});
	},

	findByPath: function(req, res, next) {
		var page = res.locals.page,
			path = page && page.path || req.path ;

		if(_.has(cache, path)) {
			res.locals.fields = cache[path];

			if(res.lang)
				res.locals.fields = extractLang(res.locals.fields, res.lang);
				
			return next();
		}

		var paths = page ? _.compact(_.map(page.pages, 'path')) : [];

		paths.push(path);
{'fraction_non_alphanumeric': 0.18923124805477748, 'fraction_numerical': 0.0056022408963585435, 'mean_word_length': 3.488826815642458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '6499791', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1171, 'n_words': 244}