Skip to content

Instantly share code, notes, and snippets.

@AvocadoVenom
Created January 27, 2019 09:49
Show Gist options
  • Save AvocadoVenom/01a8464304575d26ef36c00c287bff40 to your computer and use it in GitHub Desktop.
Save AvocadoVenom/01a8464304575d26ef36c00c287bff40 to your computer and use it in GitHub Desktop.
Angular service to provide random data.
import { Injectable } from '@angular/core';
export interface Item {
name: string;
value: number;
abs: number;
}
@Injectable({
providedIn: 'root'
})
export class DataService {
private readonly NAMES = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
private readonly MIN_ITEM = 10;
private readonly MAX_ITEM = 20;
private readonly MAX_VALUE = 100;
constructor() { }
private generateRandomValue(start: number, end: number) {
return Math.ceil(Math.random() * (end - start) + start);
}
getData(): Item[] {
const nbItems = this.generateRandomValue(this.MIN_ITEM, this.MAX_ITEM);
const samples = [];
for (let i = 0; i < nbItems; i++) {
const val = this.generateRandomValue(1, this.MAX_VALUE);
samples.push({
name: this.NAMES[i],
value: val,
abs: Math.abs(val)
});
}
return samples;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment