From 0205183ed821fa1bc04bbb290e7ab713db6e5a91 Mon Sep 17 00:00:00 2001 From: Mohd Imran <42844688+MohdImran001@users.noreply.github.com> Date: Fri, 14 Jan 2022 23:15:42 +0530 Subject: [PATCH] feat: migrate company (#132) Co-authored-by: Shinigami92 --- src/company.ts | 141 +++++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 3 +- 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/company.ts diff --git a/src/company.ts b/src/company.ts new file mode 100644 index 00000000000..6ca733a32a3 --- /dev/null +++ b/src/company.ts @@ -0,0 +1,141 @@ +import type { Faker } from '.'; +import type { Fake } from './fake'; + +let f: Fake['fake']; + +export class Company { + constructor(private readonly faker: Faker) { + f = this.faker.fake; + + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Company.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + } + + /** + * suffixes + * + * @method faker.company.suffixes + */ + suffixes(): string[] { + // Don't want the source array exposed to modification, so return a copy + return this.faker.definitions.company.suffix.slice(0); + } + + /** + * companyName + * + * @method faker.company.companyName + * @param format + */ + companyName(format?: number): string { + const formats = [ + '{{name.lastName}} {{company.companySuffix}}', + '{{name.lastName}} - {{name.lastName}}', + '{{name.lastName}}, {{name.lastName}} and {{name.lastName}}', + ]; + + if (typeof format !== 'number') { + format = this.faker.datatype.number(formats.length - 1); + } + + return f(formats[format]); + } + + /** + * companySuffix + * + * @method faker.company.companySuffix + */ + companySuffix(): string { + return this.faker.random.arrayElement(this.faker.company.suffixes()); + } + + /** + * catchPhrase + * + * @method faker.company.catchPhrase + */ + catchPhrase(): string { + return f( + '{{company.catchPhraseAdjective}} {{company.catchPhraseDescriptor}} {{company.catchPhraseNoun}}' + ); + } + + /** + * bs + * + * @method faker.company.bs + */ + bs(): string { + return f('{{company.bsBuzz}} {{company.bsAdjective}} {{company.bsNoun}}'); + } + + /** + * catchPhraseAdjective + * + * @method faker.company.catchPhraseAdjective + */ + catchPhraseAdjective(): string { + return this.faker.random.arrayElement( + this.faker.definitions.company.adjective + ); + } + + /** + * catchPhraseDescriptor + * + * @method faker.company.catchPhraseDescriptor + */ + catchPhraseDescriptor(): string { + return this.faker.random.arrayElement( + this.faker.definitions.company.descriptor + ); + } + + /** + * catchPhraseNoun + * + * @method faker.company.catchPhraseNoun + */ + catchPhraseNoun(): string { + return this.faker.random.arrayElement(this.faker.definitions.company.noun); + } + + /** + * bsAdjective + * + * @method faker.company.bsAdjective + */ + bsAdjective(): string { + return this.faker.random.arrayElement( + this.faker.definitions.company.bs_adjective + ); + } + + /** + * bsBuzz + * + * @method faker.company.bsBuzz + */ + bsBuzz(): string { + return this.faker.random.arrayElement( + this.faker.definitions.company.bs_verb + ); + } + + /** + * bsNoun + * + * @method faker.company.bsNoun + */ + bsNoun(): string { + return this.faker.random.arrayElement( + this.faker.definitions.company.bs_noun + ); + } +} diff --git a/src/index.ts b/src/index.ts index 16a3b3475b5..c7298cde4b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { Address } from './address'; import { Animal } from './animal'; import { Commerce } from './commerce'; +import { Company } from './company'; import { Database } from './database'; import { Datatype } from './datatype'; import { _Date } from './date'; @@ -184,7 +185,7 @@ export class Faker { readonly address: Address = new Address(this); readonly animal: Animal = new Animal(this); readonly commerce: Commerce = new Commerce(this); - readonly company = new (require('./company'))(this); + readonly company: Company = new Company(this); readonly database: Database = new Database(this); readonly date: _Date = new _Date(this); readonly finance = new Finance(this);