Socialify

Folder ..

Viewing lorem.ts
169 lines (155 loc) • 4.8 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import type { Faker } from '.';
import type { Helpers } from './helpers';

export class Lorem {
  private readonly Helpers: Helpers;

  constructor(private readonly faker: Faker) {
    this.Helpers = faker.helpers;

    // Bind `this` so namespaced is working correctly
    for (const name of Object.getOwnPropertyNames(Lorem.prototype)) {
      if (name === 'constructor' || typeof this[name] !== 'function') {
        continue;
      }
      this[name] = this[name].bind(this);
    }
  }

  /**
   * Generates a word of a specified length.
   *
   * @method faker.lorem.word
   * @param length length of the word that should be returned. Defaults to a random length
   */
  word(length?: number): string {
    const hasRightLength = (word: string) => word.length === length;
    let properLengthWords: string[];
    if (typeof length === 'undefined') {
      properLengthWords = this.faker.definitions.lorem.words;
    } else {
      properLengthWords =
        this.faker.definitions.lorem.words.filter(hasRightLength);
    }
    return this.faker.random.arrayElement(properLengthWords);
  }

  /**
   * Generates a space separated list of words.
   *
   * @method faker.lorem.words
   * @param num number of words, defaults to 3
   */
  words(num?: number): string {
    if (typeof num == 'undefined') {
      num = 3;
    }
    const words: string[] = [];
    for (let i = 0; i < num; i++) {
      words.push(this.faker.lorem.word());
    }
    return words.join(' ');
  }

  /**
   * sentence
   *
   * @method faker.lorem.sentence
   * @param wordCount defaults to a random number between 3 and 10
   * @param range
   */
  // TODO @Shinigami92 2022-01-11: `range` is not in use
  sentence(wordCount?: number, range?: number): string {
    if (typeof wordCount == 'undefined') {
      wordCount = this.faker.datatype.number({ min: 3, max: 10 });
    }
    // if (typeof range == 'undefined') { range = 7; }

    // strange issue with the node_min_test failing for capitalize, please fix and add faker.lorem.back
    //return  faker.lorem.words(wordCount + Helpers.randomNumber(range)).join(' ').capitalize();

    const sentence = this.faker.lorem.words(wordCount);
    return sentence.charAt(0).toUpperCase() + sentence.slice(1) + '.';
  }

  /**
   * slug
   *
   * @method faker.lorem.slug
   * @param wordCount number of words, defaults to 3
   */
  slug(wordCount?: number): string {
    const words = this.faker.lorem.words(wordCount);
    return this.Helpers.slugify(words);
  }

  /**
   * sentences
   *
   * @method faker.lorem.sentences
   * @param sentenceCount defaults to a random number between 2 and 6
   * @param separator defaults to `' '`
   */
  sentences(sentenceCount?: number, separator?: string) {
    if (typeof sentenceCount === 'undefined') {
      sentenceCount = this.faker.datatype.number({ min: 2, max: 6 });
    }
    if (typeof separator == 'undefined') {
      separator = ' ';
    }
    const sentences: string[] = [];
    for (sentenceCount; sentenceCount > 0; sentenceCount--) {
      sentences.push(this.faker.lorem.sentence());
    }
    return sentences.join(separator);
  }

  /**
   * paragraph
   *
   * @method faker.lorem.paragraph
   * @param sentenceCount defaults to 3
   */
  paragraph(sentenceCount: number = 3): string {
    return this.faker.lorem.sentences(
      sentenceCount + this.faker.datatype.number(3)
    );
  }

  /**
   * paragraphs
   *
   * @method faker.lorem.paragraphs
   * @param paragraphCount defaults to 3
   * @param separator defaults to `'\n \r'`
   */
  paragraphs(paragraphCount: number = 3, separator: string = '\n \r'): string {
    const paragraphs: string[] = [];
    for (paragraphCount; paragraphCount > 0; paragraphCount--) {
      paragraphs.push(this.faker.lorem.paragraph());
    }
    return paragraphs.join(separator);
  }

  /**
   * Returns random text based on a random lorem method
   *
   * @method faker.lorem.text
   * @param times
   */
  // TODO @Shinigami92 2022-01-11: Is this a function-name alias?
  // Or can we just remove the `loremText`?
  // TODO @Shinigami92 2022-01-11: `times` is not in use
  text = function loremText(times?: number) {
    const loremMethods = [
      'lorem.word',
      'lorem.words',
      'lorem.sentence',
      'lorem.sentences',
      'lorem.paragraph',
      'lorem.paragraphs',
      'lorem.lines',
    ];
    const randomLoremMethod = this.faker.random.arrayElement(loremMethods);
    return this.faker.fake('{{' + randomLoremMethod + '}}');
  };

  /**
   * Returns lines of lorem separated by `'\n'`
   *
   * @method faker.lorem.lines
   * @param lineCount defaults to a random number between 1 and 5
   */
  lines(lineCount?: number): string {
    if (typeof lineCount === 'undefined') {
      lineCount = this.faker.datatype.number({ min: 1, max: 5 });
    }
    return this.faker.lorem.sentences(lineCount, '\n');
  }
}