aboutsummaryrefslogtreecommitdiff
path: root/lib/system.js
diff options
context:
space:
mode:
authorMarak <[email protected]>2016-02-06 08:12:15 +0530
committerMarak <[email protected]>2016-02-06 08:16:23 +0530
commitc8bc1d518781e884f3079bb67101173dd3df16ad (patch)
tree087ef2f87bbcfc15fa2efa9cd16eda33824464a1 /lib/system.js
parent322a2ad47757514bd381819f44e8d3bc857b55e1 (diff)
downloadfaker-c8bc1d518781e884f3079bb67101173dd3df16ad.tar.xz
faker-c8bc1d518781e884f3079bb67101173dd3df16ad.zip
[api] Added concept of "System" module
* Allows for creation of fake systems data * So far has file-system related methods * Mime-types * File extensions * File names
Diffstat (limited to 'lib/system.js')
-rw-r--r--lib/system.js97
1 files changed, 97 insertions, 0 deletions
diff --git a/lib/system.js b/lib/system.js
new file mode 100644
index 00000000..6d7fcf11
--- /dev/null
+++ b/lib/system.js
@@ -0,0 +1,97 @@
+// generates fake data for many computer systems properties
+
+function System (faker) {
+
+ // generates a file name with extension or optional 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.toLowerCase();
+ return str;
+ };
+
+ this.commonFileName = function (ext, type) {
+ var str = faker.fake("{{random.words}}.{{system.commonFileExt}}");
+ str = str.replace(/ /g, '_');
+ str = str.replace(/\,/g, '_');
+ str = str.replace(/\-/g, '_');
+ str = str.replace(/\\/g, '_');
+ str = str.toLowerCase();
+ return str;
+ };
+
+ this.mimeType = function () {
+ return faker.random.arrayElement(Object.keys(faker.definitions.system.mimeTypes));
+ };
+
+ // returns a commonly used file type
+ this.commonFileType = function () {
+ var types = ['video', 'audio', 'image', 'text', 'application'];
+ return faker.random.arrayElement(types)
+ };
+
+ // returns a commonly used file extension based on optional 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));
+ };
+
+
+ // returns any file type available as mime-type
+ 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);
+ };
+
+ this.fileExt = function (mimeType) {
+ var exts = [];
+ var mimes = faker.definitions.system.mimeTypes;
+
+ // get specific ext by mime-type
+ if (typeof mimes[mimeType] === "object") {
+ 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);
+ };
+
+ this.directoryPath = function () {
+ // TODO
+ };
+
+ this.filePath = function () {
+ // TODO
+ };
+
+
+}
+
+module['exports'] = System; \ No newline at end of file