aboutsummaryrefslogtreecommitdiff
path: root/lib/system.js
diff options
context:
space:
mode:
authorChristian <[email protected]>2021-03-18 15:34:36 +0100
committerChristian <[email protected]>2021-03-18 15:40:57 +0100
commite18d14dcaf17161a76fd59fc89b20eae9c17e98e (patch)
tree96e629b235850e8f29e2d0f7d2c261493de67001 /lib/system.js
parent81f27065297c3e29eb552911b9a5dc8755600efb (diff)
downloadfaker-e18d14dcaf17161a76fd59fc89b20eae9c17e98e.tar.xz
faker-e18d14dcaf17161a76fd59fc89b20eae9c17e98e.zip
perf(system): improve commonFileName, commonFileExt, fileType, directoryPath performance
fixes #822
Diffstat (limited to 'lib/system.js')
-rw-r--r--lib/system.js132
1 files changed, 70 insertions, 62 deletions
diff --git a/lib/system.js b/lib/system.js
index f3428271..564d46c0 100644
--- a/lib/system.js
+++ b/lib/system.js
@@ -1,27 +1,72 @@
// generates fake data for many computer systems properties
+var commonFileTypes = [
+ "video",
+ "audio",
+ "image",
+ "text",
+ "application"
+];
+
+var commonMimeTypes = [
+ "application/pdf",
+ "audio/mpeg",
+ "audio/wav",
+ "image/png",
+ "image/jpeg",
+ "image/gif",
+ "video/mp4",
+ "video/mpeg",
+ "text/html"
+];
+
+function setToArray(set) {
+ // shortcut if Array.from is available
+ if (Array.from) { return Array.from(set); }
+
+ var array = [];
+ set.forEach(function (item) {
+ array.push(item);
+ });
+ return array;
+}
+
/**
*
* @namespace faker.system
*/
-function System (faker) {
+function System(faker) {
+ var typeSet = new Set();
+ var extensionSet = new Set();
+ var mimeTypes = faker.definitions.system.mimeTypes;
+
+ Object.keys(mimeTypes).forEach(function (m) {
+ var type = m.split("/")[0];
+
+ typeSet.add(type);
+
+ if (mimeTypes[m].extensions instanceof Array) {
+ mimeTypes[m].extensions.forEach(function (ext) {
+ extensionSet.add(ext);
+ });
+ }
+ });
+
+ var types = setToArray(typeSet);
+ var extensions = setToArray(extensionSet);
+ var mimeTypeKeys = Object.keys(faker.definitions.system.mimeTypes);
+
/**
- * generates a file name with extension or optional type
+ * generates a file name
*
* @method faker.system.fileName
- * @param {string} ext
- * @param {string} type
*/
- this.fileName = function (ext, type) {
- var str = faker.fake("{{random.words}}.{{system.fileExt}}");
- str = str.replace(/ /g, '_');
- str = str.replace(/\,/g, '_');
- str = str.replace(/\-/g, '_');
- str = str.replace(/\\/g, '_');
- str = str.replace(/\//g, '_');
- str = str.toLowerCase();
- return str;
+ this.fileName = function () {
+ var str = faker.random.words() + "." + faker.system.fileExt();
+ return str
+ .toLowerCase()
+ .replace(/\W/g, "_");
};
/**
@@ -29,17 +74,12 @@ function System (faker) {
*
* @method faker.system.commonFileName
* @param {string} ext
- * @param {string} type
*/
- this.commonFileName = function (ext, type) {
+ this.commonFileName = function (ext) {
var str = faker.random.words() + "." + (ext || faker.system.commonFileExt());
- str = str.replace(/ /g, '_');
- str = str.replace(/\,/g, '_');
- str = str.replace(/\-/g, '_');
- str = str.replace(/\\/g, '_');
- str = str.replace(/\//g, '_');
- str = str.toLowerCase();
- return str;
+ return str
+ .toLowerCase()
+ .replace(/\W/g, "_");
};
/**
@@ -48,7 +88,7 @@ function System (faker) {
* @method faker.system.mimeType
*/
this.mimeType = function () {
- return faker.random.arrayElement(Object.keys(faker.definitions.system.mimeTypes));
+ return faker.random.arrayElement(mimeTypeKeys);
};
/**
@@ -57,29 +97,16 @@ function System (faker) {
* @method faker.system.commonFileType
*/
this.commonFileType = function () {
- var types = ['video', 'audio', 'image', 'text', 'application'];
- return faker.random.arrayElement(types)
+ return faker.random.arrayElement(commonFileTypes);
};
/**
- * returns a commonly used file extension based on optional type
+ * returns a commonly used file extension
*
* @method faker.system.commonFileExt
- * @param {string} type
*/
- this.commonFileExt = function (type) {
- var types = [
- 'application/pdf',
- 'audio/mpeg',
- 'audio/wav',
- 'image/png',
- 'image/jpeg',
- 'image/gif',
- 'video/mp4',
- 'video/mpeg',
- 'text/html'
- ];
- return faker.system.fileExt(faker.random.arrayElement(types));
+ this.commonFileExt = function () {
+ return faker.system.fileExt(faker.random.arrayElement(commonMimeTypes));
};
@@ -89,14 +116,6 @@ function System (faker) {
* @method faker.system.fileType
*/
this.fileType = function () {
- var types = [];
- var mimes = faker.definitions.system.mimeTypes;
- Object.keys(mimes).forEach(function(m){
- var parts = m.split('/');
- if (types.indexOf(parts[0]) === -1) {
- types.push(parts[0]);
- }
- });
return faker.random.arrayElement(types);
};
@@ -107,23 +126,12 @@ function System (faker) {
* @param {string} mimeType
*/
this.fileExt = function (mimeType) {
- var exts = [];
- var mimes = faker.definitions.system.mimeTypes;
-
- // get specific ext by mime-type
- if (typeof mimes[mimeType] === "object") {
+ if (mimeType) {
+ var mimes = faker.definitions.system.mimeTypes;
return faker.random.arrayElement(mimes[mimeType].extensions);
}
- // reduce mime-types to those with file-extensions
- Object.keys(mimes).forEach(function(m){
- if (mimes[m].extensions instanceof Array) {
- mimes[m].extensions.forEach(function(ext){
- exts.push(ext)
- });
- }
- });
- return faker.random.arrayElement(exts);
+ return faker.random.arrayElement(extensions);
};
/**