<commit_before>/*
 * random-useragent
 * https://github.com/skratchdot/random-useragent
 *
 * Copyright (c) 2014 skratchdot
 * Licensed under the MIT license.
 */
'use strict';

var useragents = require('./useragent-data.json');
var rand = require('random-seed').create();

// cloning is slow, but it's only done when returning parsed user agent
// objects (so the data can't be changed by the end user).
// this can be a performance hit when in a loop, so use with caution.
var cloneData = function (data) {
	return JSON.parse(JSON.stringify(data));
};

var getData = function (filter) {
	return typeof filter === 'function' ? useragents.filter(filter) : useragents;
};

var getRandom = function (fn, filter) {
	var data = exports[fn](filter);
	return data.length ? data[rand.intBetween(0, data.length - 1)] : null;
};

exports.getRandom = function (filter) {
	return getRandom('getAll', filter);
};

exports.getRandomData = function (filter) {
	return cloneData(getRandom('getAllData', filter));
};

exports.getAll = function (filter) {
	return getData(filter).map(function (item) {
		return item.userAgent;
	});
};

exports.getAllData = function (filter) {
	return cloneData(getData(filter));
};<commit_msg>Better performance<commit_after>/*
 * random-useragent
 * https://github.com/skratchdot/random-useragent
 *
 * Copyright (c) 2014 skratchdot
 * Licensed under the MIT license.
 */
'use strict';

var useragents = require('./useragent-data.json');
var rand = require('random-seed').create();

// cloning is slow, but it's only done when returning parsed user agent
// objects (so the data can't be changed by the end user).
// this can be a performance hit when in a loop, so use with caution.
var cloneData = function (data) {
	return JSON.parse(JSON.stringify(data));
};

var getData = function (filter) {
	return typeof filter === 'function' ? useragents.filter(filter) : useragents;
};

exports.getRandom = function (filter) {
	var data = getData(filter);
	return data.length ? data[rand.intBetween(0, data.length - 1)].userAgent : null;
};

exports.getRandomData = function (filter) {
	var data = getData(filter);
	return data.length ? cloneData(data[rand.intBetween(0, data.length - 1)]) : null;
};

exports.getAll = function (filter) {
	return getData(filter).map(function (item) {
		return item.userAgent;
	});
};

exports.getAllData = function (filter) {
	return cloneData(getData(filter));
};
{'fraction_non_alphanumeric': 0.14273858921161825, 'fraction_numerical': 0.005809128630705394, 'mean_word_length': 4.568129330254042, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3965138', 'n_tokens_mistral': 767, 'n_tokens_neox': 738, 'n_words': 223}