Skip to content

Commit

Permalink
Add numbers dictionary (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasonny83 committed Jul 1, 2020
1 parent 2b1e84b commit 50e9917
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 4 deletions.
72 changes: 72 additions & 0 deletions README.md
Expand Up @@ -54,6 +54,7 @@ If you want to migrate, from an older version of the library to v4, please read
- [style](#style)
- [seed](#seed)
- [Dictionaries available](#dictionaries-available)
- [Numbers](#numbers)
- [Adjectives](#adjectives)
- [Animals](#animals)
- [Colors](#colors)
Expand All @@ -62,6 +63,13 @@ If you want to migrate, from an older version of the library to v4, please read
- [Star Wars](#star-wars)
- [Default dictionaries](#default-dictionaries)
- [Custom dictionaries](#custom-dictionaries)
- [Numbers Dictionary](#numbers-dictionary)
- [Numbers Dictionary API](#numbers-dictionary-api)
- [generate (options)](#generate-options)
- [options](#options-1)
- [min](#min)
- [max](#max)
- [length](#length-1)
- [Combining custom and provided dictionaries](#combining-custom-and-provided-dictionaries)
- [Migration guide](#migration-guide)
- [Migration guide from version 3 to version 4](#migration-guide-from-version-3-to-version-4)
Expand All @@ -72,6 +80,7 @@ If you want to migrate, from an older version of the library to v4, please read
- [Short](#short)
- [Contributing](#contributing)
- [License](#license)
- [Contributors ✨](#contributors-)

## Prerequisites

Expand Down Expand Up @@ -232,6 +241,10 @@ A seed is used when wanting to deterministically generate a name. As long as the

## Dictionaries available

#### Numbers

This is a dynamic dictionary. Read more in the [Numbers Dictionary](#numbers-dictionary) section

#### Adjectives

A list of more than 1,400 adjectives ready for you to use
Expand Down Expand Up @@ -365,6 +378,65 @@ const characterName: string = uniqueNamesGenerator({
}); // Green Luke Skywalker
```

### Numbers Dictionary

You can easily generate random numbers inside your unique name using the Numbers dictionary helper.

```typescript
import { uniqueNamesGenerator, NumberDictionary } from 'unique-names-generator';

const numberDictionary = NumberDictionary.generate({ min: 100, max: 999 });
const characterName: string = uniqueNamesGenerator({
dictionaries: [['Dangerous'], ['Snake'], numberDictionary],
length: 3,
separator: '',
style: 'capital'
}); // DangerousSnake123
```

## Numbers Dictionary API

### generate (options)

Returns a `string` with a random generated number between 1 and 999

### options

Type: `Config`

#### min

Type: `number`

required: `false`

default: `1`

The minimum value to be returned as a random number

#### max

Type: `number`

required: `false`

default: `999`

The maximum value to be returned as a random number

#### length

Type: `number`

required: `false`

The length of the random generated number to be returned.

Setting a length of 3 will always return a random number between `100` and `999`. This is the same as setting `100` and `999` as `min` and `max` option.

**Note** If set, this will ignore any `min` and `max` options provided.


### Combining custom and provided dictionaries

You can reuse the dictionaries provided by the library.
Expand Down
3 changes: 2 additions & 1 deletion src/dictionaries/index.ts
Expand Up @@ -4,5 +4,6 @@ import colors from './colors';
import countries from './countries';
import names from './names';
import starWars from './star-wars';
import { NumberDictionary } from './numbers';

export { adjectives, animals, colors, countries, names, starWars };
export { adjectives, animals, colors, countries, names, starWars, NumberDictionary };
26 changes: 26 additions & 0 deletions src/dictionaries/numbers.ts
@@ -0,0 +1,26 @@
interface Config {
min: number;
max: number;
length?: number;
}

const defaultConfig: Config = {
min: 1,
max: 999,
};

export class NumberDictionary {
public static generate(config: Partial<Config> = {}): string[] {
let min = config.min || defaultConfig.min;
let max = config.max || defaultConfig.max;

if (config.length) {
const length = Math.pow(10, config.length);
min = length / 10;
max = length - 1;
return [`${Math.floor(Math.random() * (max - min)) + min}`];
}

return [`${Math.floor(Math.random() * (max - min)) + min}`];
}
}
12 changes: 10 additions & 2 deletions src/index.ts
@@ -1,3 +1,11 @@
export * from './unique-names-generator';
export { uniqueNamesGenerator } from './unique-names-generator';
export { Config } from './unique-names-generator.constructor';
export * from './dictionaries/index';
export {
adjectives,
animals,
colors,
countries,
names,
starWars,
NumberDictionary,
} from './dictionaries/index';
24 changes: 24 additions & 0 deletions src/unique-names-generator.constructor.test.ts
@@ -1,4 +1,5 @@
import { UniqueNamesGenerator, Config } from './unique-names-generator.constructor';
import { NumberDictionary } from './dictionaries';

describe('randomNameGenerator', () => {
it('should exists', () => {
Expand Down Expand Up @@ -284,5 +285,28 @@ describe('randomNameGenerator', () => {
// Assert
expect(result).toEqual(expectedName);
});

it('should use a number dictionary', () => {
jest.spyOn(NumberDictionary, 'generate').mockReturnValue(['123']);

// Arrange
const numberDictionary = NumberDictionary.generate();
const config: Config = {
dictionaries: [['Dangerous'], ['Snake'], numberDictionary],
length: 3,
separator: '',
style: 'capital',
};

const expectedName = 'DangerousSnake123';

// Act
const uniqueNamesGenerator = new UniqueNamesGenerator(config);
const result = uniqueNamesGenerator.generate();

// Assert
expect(NumberDictionary.generate).toHaveBeenCalled();
expect(result).toEqual(expectedName);
});
});
});
72 changes: 71 additions & 1 deletion src/unique-names-generator.test.ts
@@ -1,4 +1,4 @@
import { uniqueNamesGenerator, Config, adjectives, colors, animals } from '.';
import { uniqueNamesGenerator, Config, adjectives, colors, animals, NumberDictionary } from '.';

describe('unique-names-generator', () => {
it('should generate a random name', () => {
Expand Down Expand Up @@ -218,4 +218,74 @@ describe('unique-names-generator', () => {
// Assert
expect(result).toEqual(expectedName);
});

it('should use a number dictionary', () => {
// Arrange
const numberDictionary = NumberDictionary.generate();
const config: Config = {
dictionaries: [['Dangerous'], ['Snake'], numberDictionary],
length: 3,
separator: '',
style: 'capital',
};

// Act
const result = uniqueNamesGenerator(config);

// Assert
expect(result).toMatch(/^DangerousSnake\d+$/);
});

it('should use a number dictionary with a minimum and a maximum value', () => {
// Arrange
const numberDictionary = NumberDictionary.generate({ min: 100, max: 999 });
const config: Config = {
dictionaries: [['Dangerous'], ['Snake'], numberDictionary],
length: 3,
separator: '',
style: 'capital',
};

// Act
const result = uniqueNamesGenerator(config);

// Assert
expect(result).toMatch(/^DangerousSnake\d{3}$/);
});

it('should use a number dictionary with a given length', () => {
// Arrange
const numberDictionary = NumberDictionary.generate({ length: 6 });
const config: Config = {
dictionaries: [['Dangerous'], ['Snake'], numberDictionary],
length: 3,
separator: '',
style: 'capital',
};

// Act
const result = uniqueNamesGenerator(config);

// Assert
expect(result).toMatch(/^DangerousSnake\d{6}$/);
});

it('should use a number dictionary with a given length', () => {
// Arrange
const firstRandomNumber = NumberDictionary.generate({ length: 2 });
const secondRandomNumber = NumberDictionary.generate({ min: 100, max: 999 });

const config: Config = {
dictionaries: [firstRandomNumber, ['Snake'], secondRandomNumber],
length: 3,
separator: '',
style: 'capital',
};

// Act
const result = uniqueNamesGenerator(config);

// Assert
expect(result).toMatch(/^\d{2}Snake\d{3}$/);
});
});

0 comments on commit 50e9917

Please sign in to comment.