Skip to content

Commit 6e986be

Browse files
authored
feat(random-name): base the generator on moby and expose react hook (#281)
* feat(random-name): base the generator on moby and expose react hook * chore: remove old typings * chore: remove docker-names dependency
1 parent 0a5bea0 commit 6e986be

File tree

7 files changed

+888
-22
lines changed

7 files changed

+888
-22
lines changed

packages/random-name/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
A tiny utility to generate random names
44

5+
Fully inspired by [Moby name generator](https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go)
6+
57
---
68

79
## Install
810

911
```bash
10-
$ yarn add @scaleway/random-names
12+
$ yarn add @scaleway/random-name
1113
```
1214

1315
## Usage
@@ -22,3 +24,17 @@ randomName('superb') // => "superb-admiring-allen"
2224
randomName('superb', '_') // => "superb_admiring_allen"
2325
randomName('', '_') // => "admiring_allen"
2426
```
27+
28+
As a React hook
29+
```js
30+
import React from 'react'
31+
import { useRandomName } from '@scaleway/random-name'
32+
33+
const Component = () => {
34+
const name = useRandomName()
35+
36+
return (
37+
<span>{name}</span>
38+
)
39+
}
40+
```

packages/random-name/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
"directory": "packages/random-name"
1919
},
2020
"license": "MIT",
21-
"dependencies": {
22-
"docker-names": "^1.1.1"
21+
"devDependencies": {
22+
"@testing-library/react-hooks": "^7.0.1",
23+
"react": "^17.0.2"
24+
},
25+
"peerDependencies": {
26+
"react": ">=16.8"
2327
}
2428
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`randomName useRandomName useTranslation should not be defined without I18nProvider 1`] = `"gracious-hermann"`;

packages/random-name/src/__tests__/index.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import randomName from '..'
1+
import { renderHook } from '@testing-library/react-hooks'
2+
import randomName, { useRandomName } from '..'
23

3-
describe('randomNames', () => {
4+
describe('randomName', () => {
45
it('should return a random name separated by a dash', () => {
56
expect(randomName().split('-').length).toBe(2)
67
})
@@ -21,10 +22,26 @@ describe('randomNames', () => {
2122
expect(name.split('!').length).toBe(3)
2223
})
2324

24-
it('should never includes the word "cocks"', () => {
25+
it('should never have boring-wozniak', () => {
2526
const names = Array.from(Array(1000000), () => randomName())
2627
expect(names).not.toEqual(
27-
expect.arrayContaining([expect.stringMatching('cocks')]),
28+
expect.arrayContaining([expect.stringMatching('boring-wozniak')]),
2829
)
2930
})
31+
32+
describe('useRandomName', () => {
33+
beforeAll(() => {
34+
jest.spyOn(global.Math, 'random').mockReturnValue(0.4155913669444804)
35+
})
36+
37+
afterAll(() => {
38+
jest.spyOn(global.Math, 'random').mockRestore()
39+
})
40+
41+
it('useTranslation should not be defined without I18nProvider', () => {
42+
const { result } = renderHook(() => useRandomName())
43+
expect(result.current).toMatchSnapshot()
44+
})
45+
})
46+
3047
})

0 commit comments

Comments
 (0)