All files / src/generators TextGenerator.ts

85.18% Statements 23/27
84.61% Branches 11/13
71.42% Functions 5/7
85.18% Lines 23/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                    1x     32x   3x   1x   2x   1x   25x                 32x   32x       32x                       4x       2x             18x     5x 5x     4x 4x     3x 3x     3x 3x     3x 3x                  
import { Faker, base, de, en, es, fr, it } from '@faker-js/faker'
 
import dateFormat from 'date-format'
import PersonGenerator, { PersonGeneratorOptions } from './PersonGenerator'
 
export type TextGeneratorOptions = {
    seed?: number
    locale?: string
}
 
const DEFAULT_REF_DATE = '2020-01-01T00:00:00.000Z'
 
function resolveLocales(locale?: string) {
    switch (locale) {
    case 'de':
        return [de, en, base]
    case 'es':
        return [es, en, base]
    case 'fr':
        return [fr, en, base]
    case 'it':
        return [it, en, base]
    default:
        return [en, base]
    }
}
 
export default class TextGenerator {
    public generator: Faker
    public seed: number
 
    constructor(opts?: TextGeneratorOptions) {
        this.seed = opts?.seed ?? Date.now() ^ (Math.random() * 0x100000000)
 
        this.generator = new Faker({
            locale: resolveLocales(opts?.locale),
            seed: this.seed,
        })
        this.generator.setDefaultRefDate(DEFAULT_REF_DATE)
    }
 
    public person(opts: PersonGeneratorOptions ): PersonGenerator {
        return new PersonGenerator(this.generator, opts)
    }
 
    public slug(min: number, max: number): string {
        return this.generator.lorem.slug({ min, max })
    }
 
    public sentence(min: number, max: number): string {
        return this.generator.lorem.sentence({ min, max })
    }
 
    public paragraph(min: number, max: number): string {
        return this.generator.lorem.paragraph({ min, max })
    }
 
    public date(
        when: 'recent'|'past'|'soon'|'future'|'anytime',
        format?: string
    ): string {
        switch (when) {
        case 'anytime':
        {
            const date = new Date(this.generator.date.anytime().toISOString())
            return dateFormat(format, date)
        }
        case 'recent': {
            const date = new Date(this.generator.date.recent().toISOString())
            return dateFormat(format, date)
        }
        case 'past': {
            const date = new Date(this.generator.date.past().toISOString())
            return dateFormat(format, date)
        }
        case 'soon': {
            const date = new Date(this.generator.date.soon().toISOString())
            return dateFormat(format, date)
        }
        case 'future': {
            const date = new Date(this.generator.date.future().toISOString())
            return dateFormat(format, date)
        }
        default: {
            const date = new Date(this.generator.date.anytime().toISOString())
            return dateFormat(format, date)
        }
        }
    }
}