aboutsummaryrefslogtreecommitdiff
path: root/src/modules/system
diff options
context:
space:
mode:
authorNick Hammond <[email protected]>2022-08-29 12:34:07 +0100
committerGitHub <[email protected]>2022-08-29 11:34:07 +0000
commit8fecd58b7cfd07826194e0de5d2c868c07c4d913 (patch)
tree8ce31e2baa185d3a6a53ce1e07b821ccc6b50116 /src/modules/system
parent7f8b8716ba69e24800e26d6c072c3076c01bfccf (diff)
downloadfaker-8fecd58b7cfd07826194e0de5d2c868c07c4d913.tar.xz
faker-8fecd58b7cfd07826194e0de5d2c868c07c4d913.zip
feat(system): add cron (#897)
Diffstat (limited to 'src/modules/system')
-rw-r--r--src/modules/system/index.ts73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts
index 46a12b19..6d2acca0 100644
--- a/src/modules/system/index.ts
+++ b/src/modules/system/index.ts
@@ -22,6 +22,16 @@ const commonInterfaceSchemas = {
pci: 'p',
} as const;
+const CRON_DAY_OF_WEEK = [
+ 'SUN',
+ 'MON',
+ 'TUE',
+ 'WED',
+ 'THU',
+ 'FRI',
+ 'SAT',
+] as const;
+
/**
* Generates fake data for many computer systems properties.
*/
@@ -264,4 +274,67 @@ export class System {
return `${prefix}${interfaceType}${commonInterfaceSchemas[interfaceSchema]}${suffix}`;
}
+
+ /**
+ * Returns a random cron expression.
+ *
+ * @param options The optional options to use.
+ * @param options.includeYear Whether to include a year in the generated expression. Defaults to `false`.
+ * @param options.includeNonStandard Whether to include a @yearly, @monthly, @daily, etc text labels in the generated expression. Defaults to `false`.
+ *
+ * @example
+ * faker.system.cron() // '45 23 * * 6'
+ * faker.system.cron({ includeYear: true }) // '45 23 * * 6 2067'
+ * faker.system.cron({ includeYear: false }) // '45 23 * * 6'
+ * faker.system.cron({ includeNonStandard: false }) // '45 23 * * 6'
+ * faker.system.cron({ includeNonStandard: true }) // '@yearly'
+ */
+ cron(
+ options: {
+ includeYear?: boolean;
+ includeNonStandard?: boolean;
+ } = {}
+ ): string {
+ const { includeYear = false, includeNonStandard = false } = options;
+
+ // create the arrays to hold the available values for each component of the expression
+ const minutes = [this.faker.datatype.number({ min: 0, max: 59 }), '*'];
+ const hours = [this.faker.datatype.number({ min: 0, max: 23 }), '*'];
+ const days = [this.faker.datatype.number({ min: 1, max: 31 }), '*', '?'];
+ const months = [this.faker.datatype.number({ min: 1, max: 12 }), '*'];
+ const daysOfWeek = [
+ this.faker.datatype.number({ min: 0, max: 6 }),
+ this.faker.helpers.arrayElement(CRON_DAY_OF_WEEK),
+ '*',
+ '?',
+ ];
+ const years = [this.faker.datatype.number({ min: 1970, max: 2099 }), '*'];
+
+ const minute = this.faker.helpers.arrayElement(minutes);
+ const hour = this.faker.helpers.arrayElement(hours);
+ const day = this.faker.helpers.arrayElement(days);
+ const month = this.faker.helpers.arrayElement(months);
+ const dayOfWeek = this.faker.helpers.arrayElement(daysOfWeek);
+ const year = this.faker.helpers.arrayElement(years);
+
+ // create and return the cron expression string
+ let standardExpression = `${minute} ${hour} ${day} ${month} ${dayOfWeek}`;
+ if (includeYear) {
+ standardExpression += ` ${year}`;
+ }
+
+ const nonStandardExpressions = [
+ '@annually',
+ '@daily',
+ '@hourly',
+ '@monthly',
+ '@reboot',
+ '@weekly',
+ '@yearly',
+ ];
+
+ return !includeNonStandard || this.faker.datatype.boolean()
+ ? standardExpression
+ : this.faker.helpers.arrayElement(nonStandardExpressions);
+ }
}