Skip to content

Commit cf57f2e

Browse files
chambo-ephilibea
andauthored
feat(random-name)!: split package in two to offer a raw and a react hook version (#500)
* feat(random-name)!: split package in two to offer a raw and a react hook version * fix: remove old code * fix: ensure packages built Co-authored-by: philibeaux <[email protected]>
1 parent 50be8b3 commit cf57f2e

File tree

14 files changed

+238
-60
lines changed

14 files changed

+238
-60
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
node-version: 17
1414
cache: 'yarn'
1515
- run: yarn
16+
- run: yarn run build
1617
- run: yarn run lint
1718
test:
1819
runs-on: ubuntu-20.04
@@ -27,25 +28,12 @@ jobs:
2728
node-version: ${{ matrix.node }}
2829
cache: 'yarn'
2930
- run: yarn
31+
- run: yarn run build
3032
- run: yarn run test:coverage
3133
- uses: codecov/[email protected]
32-
build:
33-
runs-on: ubuntu-20.04
34-
strategy:
35-
matrix:
36-
node: [ '14', '16', '17' ]
37-
needs: [lint, test]
38-
steps:
39-
- uses: actions/[email protected]
40-
- name: Use Node.js
41-
uses: actions/[email protected]
42-
with:
43-
node-version: ${{ matrix.node }}
44-
- run: yarn --pure-lockfile
45-
- run: yarn run build
4634
deploy:
4735
runs-on: ubuntu-20.04
48-
needs: [build]
36+
needs: [test]
4937
if: github.ref == 'refs/heads/master'
5038
steps:
5139
- uses: actions/[email protected]

packages/random-name/README.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,3 @@ randomName('superb') // => "superb-admiring-allen"
2424
randomName('superb', '_') // => "superb_admiring_allen"
2525
randomName('', '_') // => "admiring_allen"
2626
```
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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,5 @@
1717
"url": "https://github.com/scaleway/scaleway-lib",
1818
"directory": "packages/random-name"
1919
},
20-
"license": "MIT",
21-
"peerDependencies": {
22-
"react": ">=16.8"
23-
}
20+
"license": "MIT"
2421
}

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

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { renderHook } from '@testing-library/react-hooks'
2-
import randomName, { useRandomName } from '..'
1+
import randomName from '..'
32

43
describe('randomName', () => {
54
it('should return a random name separated by a dash', () => {
@@ -28,20 +27,4 @@ describe('randomName', () => {
2827
expect.arrayContaining([expect.stringMatching('boring-wozniak')]),
2928
)
3029
})
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-
4730
})

packages/random-name/src/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { useCallback } from 'react'
2-
31
const ADJECTIVES = [
42
'admiring',
53
'adoring',
@@ -839,8 +837,4 @@ const randomName = (prefix = '', separator = '-'): string => {
839837
return prefix.length > 0 ? `${prefix}${separator}${name}` : name
840838
}
841839

842-
const useRandomName = (prefix = '', separator = '-'): string =>
843-
useCallback(() => randomName(prefix, separator), [prefix, separator])()
844-
845-
export { useRandomName }
846840
export default randomName
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { join } = require('path');
2+
3+
module.exports = {
4+
rules: {
5+
'import/no-extraneous-dependencies': [
6+
'error',
7+
{ packageDir: [__dirname, join(__dirname, '../../')] }
8+
]
9+
}
10+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**/__tests__/**
2+
src
3+
.eslintrc.cjs
4+
!.npmignore
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5+
6+
### 2.1.1 (2021-10-21)
7+
8+
**Note:** Version bump only for package @scaleway/random-name
9+
10+
11+
12+
13+
14+
## [2.1.0](https://github.com/scaleway/scaleway-lib/compare/@scaleway/[email protected]...@scaleway/[email protected]) (2021-07-21)
15+
16+
17+
### Features
18+
19+
* **random-name:** base the generator on moby and expose react hook ([#281](https://github.com/scaleway/scaleway-lib/issues/281)) ([6e986be](https://github.com/scaleway/scaleway-lib/commit/6e986beb2609e533199a2b38b989c3b8b0c3c56b))
20+
21+
22+
23+
## 2.0.0 (2021-07-20)
24+
25+
26+
### ⚠ BREAKING CHANGES
27+
28+
* migrate the codebase to typescript
29+
30+
### Code Refactoring
31+
32+
* migrate to typescript ([#272](https://github.com/scaleway/scaleway-lib/issues/272)) ([3923c68](https://github.com/scaleway/scaleway-lib/commit/3923c68d6f7feadee7e2e30e32c9ef5d1f3003b9))
33+
34+
35+
36+
### 1.3.2 (2021-05-11)
37+
38+
**Note:** Version bump only for package @scaleway/random-name
39+
40+
41+
42+
43+
44+
### 1.3.1 (2021-05-10)
45+
46+
**Note:** Version bump only for package @scaleway/random-name
47+
48+
49+
50+
51+
52+
# 1.3.0 (2021-03-04)
53+
54+
55+
### Features
56+
57+
* updated eslint setting to force carriage return before return ([3514034](https://github.com/scaleway/scaleway-lib/commit/3514034804ae06083825adc3e57003ed8dba4933))
58+
59+
60+
61+
62+
63+
## 1.2.1 (2021-02-15)
64+
65+
**Note:** Version bump only for package @scaleway/random-name
66+
67+
68+
69+
70+
71+
# 1.2.0 (2021-01-28)
72+
73+
74+
### Bug Fixes
75+
76+
* correct browser entrypoints ([2c0724d](https://github.com/scaleway/scaleway-lib/commit/2c0724d00b45664757c380188618908fcae2c606))
77+
* **random-name:** add npmignore ([d7b7e70](https://github.com/scaleway/scaleway-lib/commit/d7b7e701db4ae090650408794a380ebe084e8407))
78+
79+
80+
### Features
81+
82+
* migrate randomName from shire and scw-ui ([5729af8](https://github.com/scaleway/scaleway-lib/commit/5729af8c38a436679e948025dfff89c45a9b8e5c))
83+
84+
85+
86+
87+
88+
## [1.1.3](https://github.com/scaleway/scaleway-lib/compare/@scaleway/[email protected]...@scaleway/[email protected]) (2020-11-08)
89+
90+
**Note:** Version bump only for package @scaleway/random-name
91+
92+
93+
94+
95+
96+
## [1.1.2](https://github.com/scaleway/scaleway-lib/compare/@scaleway/[email protected]...@scaleway/[email protected]) (2020-10-28)
97+
98+
99+
### Bug Fixes
100+
101+
* correct browser entrypoints ([8a7d0b5](https://github.com/scaleway/scaleway-lib/commit/8a7d0b503ee22eedb07d5021b3a3fd4e059e627a))
102+
103+
104+
105+
106+
107+
## [1.1.1](https://github.com/scaleway/scaleway-lib/compare/@scaleway/[email protected]...@scaleway/[email protected]) (2020-10-28)
108+
109+
110+
### Bug Fixes
111+
112+
* **random-name:** add npmignore ([2731b54](https://github.com/scaleway/scaleway-lib/commit/2731b5452bb33fb369ee2370156e565b5a328d42))
113+
114+
115+
116+
117+
118+
# 1.1.0 (2020-10-28)
119+
120+
121+
### Features
122+
123+
* migrate randomName from shire and scw-ui ([331a2f9](https://github.com/scaleway/scaleway-lib/commit/331a2f91a5aa777806bbc1dc1014fc5b6987db8c))

packages/use-random-name/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# `@scaleway/random-name`
2+
3+
A tiny utility to generate random names
4+
5+
Fully inspired by [Moby name generator](https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go)
6+
7+
---
8+
9+
## Install
10+
11+
```bash
12+
$ yarn add @scaleway/use-random-name
13+
```
14+
15+
## Usage
16+
17+
As a React hook
18+
```js
19+
import React from 'react'
20+
import useRandomName from '@scaleway/random-name'
21+
22+
const Component = () => {
23+
const name = useRandomName()
24+
25+
return (
26+
<span>{name}</span>
27+
)
28+
}
29+
```
30+
31+
You can also access the underlying @scaleway/random-name package with a named export
32+
```js
33+
import { randomName } from '@scaleway/use-random-name'
34+
35+
// randomName(prefix: string = '', separator: string = '-'): string
36+
37+
randomName() // => "admiring-allen"
38+
randomName('superb') // => "superb-admiring-allen"
39+
randomName('superb', '_') // => "superb_admiring_allen"
40+
randomName('', '_') // => "admiring_allen"
41+
```

0 commit comments

Comments
 (0)