mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
Co-authored-by: spalger <spalger@users.noreply.github.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> # Conflicts: # test/common/services/deployment.ts # test/common/services/index.ts # test/common/services/saved_object_info.ts # test/functional/apps/visualize/index.ts
83 lines
2 KiB
TypeScript
83 lines
2 KiB
TypeScript
/*
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
* 2.0 and the Server Side Public License, v 1; you may not use this file except
|
|
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
|
* Side Public License, v 1.
|
|
*/
|
|
|
|
import Chance from 'chance';
|
|
import { ToolingLog } from '@kbn/dev-utils';
|
|
|
|
import { FtrService } from '../ftr_provider_context';
|
|
|
|
let __CACHED_SEED__: number | undefined;
|
|
function getSeed(log: ToolingLog) {
|
|
if (__CACHED_SEED__ !== undefined) {
|
|
return __CACHED_SEED__;
|
|
}
|
|
|
|
__CACHED_SEED__ = Date.now();
|
|
log.debug('randomness seed: %j', __CACHED_SEED__);
|
|
return __CACHED_SEED__;
|
|
}
|
|
|
|
interface CharOptions {
|
|
pool?: string;
|
|
alpha?: boolean;
|
|
numeric?: boolean;
|
|
symbols?: boolean;
|
|
casing?: 'lower' | 'upper';
|
|
}
|
|
|
|
interface StringOptions extends CharOptions {
|
|
length?: number;
|
|
}
|
|
|
|
interface NumberOptions {
|
|
min?: number;
|
|
max?: number;
|
|
}
|
|
|
|
export class RandomnessService extends FtrService {
|
|
private readonly chance = new Chance(getSeed(this.ctx.getService('log')));
|
|
|
|
/**
|
|
* Generate a random natural number
|
|
*
|
|
* range: 0 to 9007199254740991
|
|
*
|
|
*/
|
|
naturalNumber(options?: NumberOptions) {
|
|
return this.chance.natural(options);
|
|
}
|
|
|
|
/**
|
|
* Generate a random integer
|
|
*/
|
|
integer(options?: NumberOptions) {
|
|
return this.chance.integer(options);
|
|
}
|
|
|
|
/**
|
|
* Generate a random number, defaults to at least 4 and no more than 8 syllables
|
|
*/
|
|
word(options: { syllables?: number } = {}) {
|
|
const { syllables = this.naturalNumber({ min: 4, max: 8 }) } = options;
|
|
|
|
return this.chance.word({
|
|
syllables,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Generate a random string, defaults to at least 8 and no more than 15 alpha-numeric characters
|
|
*/
|
|
string(options: StringOptions = {}) {
|
|
return this.chance.string({
|
|
length: this.naturalNumber({ min: 8, max: 15 }),
|
|
...(options.pool === 'undefined' ? { alpha: true, numeric: true, symbols: false } : {}),
|
|
...options,
|
|
});
|
|
}
|
|
}
|