ect-random-data

Trying to think about new user, contact or customer data can be tricky, so here's a simple library to make some of the job a little easier. This library offers random first and last names, and currently a random date of birth from 1/1/1970 onwards.

$0

6 downloads
exoduscloudtech

ECT Random Data

This library was generated with Angular CLI version 18.0.0.

Description

Trying to think about new user, contact or customer data can be tricky, so here's a simple library to make some of the job a little easier.

Usage

First add the EctRandomDataService service and the EctNgLinqService to your Modules' or AppComponent's Providers list.

As an example, if we want to create a UserRecordFetchService service class to create a random list of User records, this is what we do.

First, let's define a User interface under a models folder, calling the file users.models.ts:

export interface IUser {
  id: number;
  firstName: string;
  lastName: string;
  age: number;
  dob: Date;
  connected: Date,
  assets: number;
}

Then to get a create a service class to mock between 100 and 250 records, we use the following:

import { Injectable } from '@angular/core';

import { EctNgLinqService } from 'ect-ng-linq'; import { EctRandomDataService } from 'ect-random-data';

import { IUser, User } from './../models/users.models';

@Injectable() export class UserRecordFetchService {

private usersList = this.ngLinqService.createNgLinq<IUser>();

constructor(private randomDataService: EctRandomDataService, private ngLinqService: EctNgLinqService) { }

populateUserRecords(): void { const recordCount = this.randomDataService.getRandomNumberBetweenTwoValues(100, 250);

for(let ii = 1; ii &lt;= recordCount; ii++) {
  const userRecord = new User();
  userRecord.id = ii;
  userRecord.firstName = this.randomDataService.getAnyFirstName();
  userRecord.lastName = this.randomDataService.getLastName();
  userRecord.dob = this.randomDataService.getDateOfBirth();
  userRecord.age = this.randomDataService.getAgeFromDate(userRecord.dob);
  const hours = this.randomDataService.getRandomNumberBetweenTwoValues(8, 21);
  const minutes = this.randomDataService.getRandomNumberBetweenTwoValues(0, 59);
  const seconds = this.randomDataService.getRandomNumberBetweenTwoValues(0, 59);
  const day = this.randomDataService.getRandomNumberBetweenTwoValues(1, 28);
  const month = this.randomDataService.getRandomNumberBetweenTwoValues(1, 12);
  const year = this.randomDataService.getRandomNumberBetweenTwoValues(2010, 2022);
  userRecord.connected = new Date(year, month, day, hours, minutes, seconds);
  userRecord.assets = this.randomDataService.getRandomNumberBetweenTwoValues(100, 1000000) / this.randomDataService.getRandomNumberBetweenTwoValues(11, 29);

  this.usersList.add(userRecord);
}

}

getUserRecords(): NgLinqArray<IUser> { return this.usersList; } }

In the above class we have to import the NgLinqService service dependency and then the EctRandomData service, before we do anything else.

The populateUserRecords sets a recordCount variable to hold a random number between 100 and 250.

The loop then iterates between 1 and recordCount creating a new User record, setting it's properties to use random data, before adding the user record to the userList array.

Functions

The following functions are available from the IEctRandomDataService interface:

  • getRandomNumberBetweenTwoValues: Get a random number between 2 whole numbers.

  • getGirlFirstNames: Get a list of girl names.

  • getBoyFirstNames: Get a list of boy names.

  • getAllFirstNames: Get a list of first names, regardless of gender.

  • getLastNames: Get a list of last names.

  • getAnyFirstName: Get a random first name, regardless of gender.

  • getGirlFirstName: Get a random first name for a girl.

  • getBoyFirstName: Get a random first name for a boy.

  • getLastName: Get a random last name.

  • isBoyName: Determine whether a name is for a boy. If false, then it's a girl's name.

  • getDateOfBirth: Get a date of birth from 1970-01-01 onwards.

  • getAgeFromDate: Get the age from a date. Assumes a date from 1970-01-01 onwards.

Dependencies

This library does use, and therefore depends on the ECT NgLinq library. More details can be found here: (https://www.npmjs.com/package/ect-ng-linq)

Cost

If you find some benefit from using this package, please consider the time it took to put this together, and why not buy me a coffee? Goodness only knows that most of us would not function without coffee. All donations very much welcomed: (https://www.buymeacoffee.com/exoduscloudtech)

Further help

To get more help on the this ECT Random Data, please visit (https://angular-grid.net/free/ect-random-data)

Monetize your
open-source work

Supercharge your OSS projects by selling npm packages. Get started in just 5 minutes.