From 796ce06006442450bb7ac207da30fa4e4b26b342 Mon Sep 17 00:00:00 2001 From: Mohammad Imran Date: Wed, 12 Jan 2022 20:59:47 +0530 Subject: [PATCH 1/4] feat: :sparkles: create word.ts --- src/word.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/word.ts diff --git a/src/word.ts b/src/word.ts new file mode 100644 index 00000000000..e69de29bb2d From ab54bd57cf5496db106ba07ce62959d103386be6 Mon Sep 17 00:00:00 2001 From: Mohammad Imran Date: Thu, 13 Jan 2022 12:52:15 +0530 Subject: [PATCH 2/4] feat: migrate word --- src/word.ts | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) diff --git a/src/word.ts b/src/word.ts index e69de29bb2d..d249e5d3442 100644 --- a/src/word.ts +++ b/src/word.ts @@ -0,0 +1,183 @@ +import type { Faker } from '.'; + +export class Word { + constructor(private readonly faker: Faker) { + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Word.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + } + + /** + * Returns an adjective of random or optionally specified length. + * If specified length is unresolvable, returns random adjective. + * + * @method faker.word.adjective + * @param {number} [length] - optional length of word to return + * @returns {string} a random adjective + */ + adjective(length?: number): string { + var wordList = this.faker.definitions.word.adjective; + if (length) { + wordList = this.faker.definitions.word.adjective.filter(function ( + word: string + ) { + return word.length == length; + }); + } + + // If result of filtered word list is undefined, return an element + // from the unfiltered list. + return ( + this.faker.random.arrayElement(wordList) || + this.faker.random.arrayElement(this.faker.definitions.word.adjective) + ); + } + + /** + * Returns an adverb of random or optionally specified length. + * If specified length is unresolvable, returns random adverb. + * + * @method faker.word.adverb + * @param {number} [length] - optional length of word to return + * @returns {string} random adverb + */ + adverb(length?: number): string { + var wordList = this.faker.definitions.word.adverb; + if (length) { + wordList = this.faker.definitions.word.adverb.filter(function (word) { + return word.length == length; + }); + } + // If result of filtered word list is undefined, return an element + // from the unfiltered list. + return ( + this.faker.random.arrayElement(wordList) || + this.faker.random.arrayElement(this.faker.definitions.word.adverb) + ); + } + + /** + * Returns a conjunction of random or optionally specified length. + * If specified length is unresolvable, returns random conjunction. + * + * @method faker.word.conjunction + * @param {number} [length] - optional length of word to return + * @returns {string} random conjunction + */ + conjunction(length?: number): string { + var wordList = this.faker.definitions.word.conjunction; + if (length) { + wordList = this.faker.definitions.word.conjunction.filter(function ( + word: string + ) { + return word.length == length; + }); + } + // If result of filtered word list is undefined, return an element + // from the unfiltered list. + return ( + this.faker.random.arrayElement(wordList) || + this.faker.random.arrayElement(this.faker.definitions.word.conjunction) + ); + } + /** + * Returns an interjection of random or optionally specified length. + * If specified length is unresolvable, returns random interjection. + * + * @method faker.word.interjection + * @param {number} [length] - optional length of word to return + * @returns {string} random interjection + */ + interjection(length?: number): string { + var wordList = this.faker.definitions.word.interjection; + if (length) { + wordList = this.faker.definitions.word.interjection.filter(function ( + word: string + ) { + return word.length == length; + }); + } + // If result of filtered word list is undefined, return an element + // from the unfiltered list. + return ( + this.faker.random.arrayElement(wordList) || + this.faker.random.arrayElement(this.faker.definitions.word.interjection) + ); + } + /** + * Returns a noun of random or optionally specified length. + * If specified length is unresolvable, returns random noun. + * + * @method faker.word.noun + * @param {number} [length] - optional length of word to return + * @returns {string} random noun + */ + noun(length?: number): string { + var wordList = this.faker.definitions.word.noun; + if (length) { + wordList = this.faker.definitions.word.noun.filter(function ( + word: string + ) { + return word.length == length; + }); + } + // If result of filtered word list is undefined, return an element + // from the unfiltered list. + return ( + this.faker.random.arrayElement(wordList) || + this.faker.random.arrayElement(this.faker.definitions.word.noun) + ); + } + /** + * Returns a preposition of random or optionally specified length. + * If specified length is unresolvable, returns random preposition. + * + * @method faker.word.preposition + * @param {number} [length] - optional length of word to return + * @returns {string} random preposition + */ + preposition(length?: number): string { + var wordList = this.faker.definitions.word.preposition; + if (length) { + wordList = this.faker.definitions.word.preposition.filter(function ( + word: string + ) { + return word.length == length; + }); + } + // If result of filtered word list is undefined, return an element + // from the unfiltered list. + return ( + this.faker.random.arrayElement(wordList) || + this.faker.random.arrayElement(this.faker.definitions.word.preposition) + ); + } + /** + * Returns a verb of random or optionally specified length. + * If specified length is unresolvable, returns random verb. + * + * @method faker.word.verb + * @param {number} [length] - optional length of word to return + * @returns {string} random verb + */ + verb(length?: number): string { + var wordList = this.faker.definitions.word.verb; + if (length) { + wordList = this.faker.definitions.word.verb.filter(function ( + word: string + ) { + return word.length == length; + }); + } + // If result of filtered word list is undefined, return an element + // from the unfiltered list. + return ( + this.faker.random.arrayElement(wordList) || + this.faker.random.arrayElement(this.faker.definitions.word.verb) + ); + } +} From ffe31867bf9d8c2672d4f62f2edec4d4d0ef4114 Mon Sep 17 00:00:00 2001 From: Mohammad Imran Date: Thu, 13 Jan 2022 13:09:15 +0530 Subject: [PATCH 3/4] feat: add word import in index.ts --- src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index f2f4520ef6f..70f74f4a079 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ import { Name } from './name'; import { Random } from './random'; import { System } from './system'; import { Time } from './time'; +import { Word } from './word'; export interface FakerOptions { locales?: string[]; @@ -192,7 +193,7 @@ export class Faker { readonly system: System = new System(this); readonly time: Time = new Time(); readonly vehicle = new (require('./vehicle'))(this); - readonly word = new (require('./word'))(this); + readonly word: Word = new Word(this); constructor(opts: FakerOptions = {}) { this.locales = this.locales || opts.locales || {}; From 3e509045ef3ffba2914e81008f4d60d019f9338f Mon Sep 17 00:00:00 2001 From: Mohammad Imran Date: Thu, 13 Jan 2022 23:05:39 +0530 Subject: [PATCH 4/4] refactor: word.ts with requested changes --- src/word.ts | 82 +++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/src/word.ts b/src/word.ts index d249e5d3442..a5f4c35e5e6 100644 --- a/src/word.ts +++ b/src/word.ts @@ -16,17 +16,15 @@ export class Word { * If specified length is unresolvable, returns random adjective. * * @method faker.word.adjective - * @param {number} [length] - optional length of word to return - * @returns {string} a random adjective + * @param optional length of word to return + * @returns a random adjective */ adjective(length?: number): string { var wordList = this.faker.definitions.word.adjective; if (length) { - wordList = this.faker.definitions.word.adjective.filter(function ( - word: string - ) { - return word.length == length; - }); + wordList = this.faker.definitions.word.adjective.filter( + (word: string) => word.length == length + ); } // If result of filtered word list is undefined, return an element @@ -42,15 +40,15 @@ export class Word { * If specified length is unresolvable, returns random adverb. * * @method faker.word.adverb - * @param {number} [length] - optional length of word to return - * @returns {string} random adverb + * @param optional length of word to return + * @returns random adverb */ adverb(length?: number): string { var wordList = this.faker.definitions.word.adverb; if (length) { - wordList = this.faker.definitions.word.adverb.filter(function (word) { - return word.length == length; - }); + wordList = this.faker.definitions.word.adverb.filter( + (word: string) => word.length == length + ); } // If result of filtered word list is undefined, return an element // from the unfiltered list. @@ -65,17 +63,15 @@ export class Word { * If specified length is unresolvable, returns random conjunction. * * @method faker.word.conjunction - * @param {number} [length] - optional length of word to return - * @returns {string} random conjunction + * @param optional length of word to return + * @returns random conjunction */ conjunction(length?: number): string { var wordList = this.faker.definitions.word.conjunction; if (length) { - wordList = this.faker.definitions.word.conjunction.filter(function ( - word: string - ) { - return word.length == length; - }); + wordList = this.faker.definitions.word.conjunction.filter( + (word: string) => word.length == length + ); } // If result of filtered word list is undefined, return an element // from the unfiltered list. @@ -89,17 +85,15 @@ export class Word { * If specified length is unresolvable, returns random interjection. * * @method faker.word.interjection - * @param {number} [length] - optional length of word to return - * @returns {string} random interjection + * @param optional length of word to return + * @returns random interjection */ interjection(length?: number): string { var wordList = this.faker.definitions.word.interjection; if (length) { - wordList = this.faker.definitions.word.interjection.filter(function ( - word: string - ) { - return word.length == length; - }); + wordList = this.faker.definitions.word.interjection.filter( + (word: string) => word.length == length + ); } // If result of filtered word list is undefined, return an element // from the unfiltered list. @@ -113,17 +107,15 @@ export class Word { * If specified length is unresolvable, returns random noun. * * @method faker.word.noun - * @param {number} [length] - optional length of word to return - * @returns {string} random noun + * @param optional length of word to return + * @returns random noun */ noun(length?: number): string { var wordList = this.faker.definitions.word.noun; if (length) { - wordList = this.faker.definitions.word.noun.filter(function ( - word: string - ) { - return word.length == length; - }); + wordList = this.faker.definitions.word.noun.filter( + (word: string) => word.length == length + ); } // If result of filtered word list is undefined, return an element // from the unfiltered list. @@ -137,17 +129,15 @@ export class Word { * If specified length is unresolvable, returns random preposition. * * @method faker.word.preposition - * @param {number} [length] - optional length of word to return - * @returns {string} random preposition + * @param optional length of word to return + * @returns random preposition */ preposition(length?: number): string { var wordList = this.faker.definitions.word.preposition; if (length) { - wordList = this.faker.definitions.word.preposition.filter(function ( - word: string - ) { - return word.length == length; - }); + wordList = this.faker.definitions.word.preposition.filter( + (word: string) => word.length == length + ); } // If result of filtered word list is undefined, return an element // from the unfiltered list. @@ -161,17 +151,15 @@ export class Word { * If specified length is unresolvable, returns random verb. * * @method faker.word.verb - * @param {number} [length] - optional length of word to return - * @returns {string} random verb + * @param optional length of word to return + * @returns random verb */ verb(length?: number): string { var wordList = this.faker.definitions.word.verb; if (length) { - wordList = this.faker.definitions.word.verb.filter(function ( - word: string - ) { - return word.length == length; - }); + wordList = this.faker.definitions.word.verb.filter( + (word: string) => word.length == length + ); } // If result of filtered word list is undefined, return an element // from the unfiltered list.