@codeleap/hooks 4.3.9 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +10 -10
- package/package.json.bak +3 -3
- package/src/index.ts +1 -0
- package/src/useComponentTestId.ts +44 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/hooks",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.1",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -9,22 +9,22 @@
|
|
|
9
9
|
"directory": "packages/hooks"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@codeleap/config": "
|
|
13
|
-
"@codeleap/types": "
|
|
14
|
-
"@codeleap/utils": "
|
|
15
|
-
"@codeleap/logger": "
|
|
12
|
+
"@codeleap/config": "5.0.1",
|
|
13
|
+
"@codeleap/types": "5.0.1",
|
|
14
|
+
"@codeleap/utils": "5.0.1",
|
|
15
|
+
"@codeleap/logger": "5.0.1",
|
|
16
16
|
"ts-node-dev": "1.1.8"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "echo 'No build needed'"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@codeleap/types": "
|
|
23
|
-
"@codeleap/utils": "
|
|
24
|
-
"@codeleap/logger": "
|
|
22
|
+
"@codeleap/types": "5.0.1",
|
|
23
|
+
"@codeleap/utils": "5.0.1",
|
|
24
|
+
"@codeleap/logger": "5.0.1",
|
|
25
25
|
"axios": "^1.7.9",
|
|
26
|
-
"typescript": "5.
|
|
27
|
-
"react": "18.
|
|
26
|
+
"typescript": "5.5.2",
|
|
27
|
+
"react": "18.2.0",
|
|
28
28
|
"@tanstack/react-query": "5.60.6"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/package.json.bak
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/hooks",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.1",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"@codeleap/utils": "workspace:*",
|
|
24
24
|
"@codeleap/logger": "workspace:*",
|
|
25
25
|
"axios": "^1.7.9",
|
|
26
|
-
"typescript": "5.
|
|
27
|
-
"react": "18.
|
|
26
|
+
"typescript": "5.5.2",
|
|
27
|
+
"react": "18.2.0",
|
|
28
28
|
"@tanstack/react-query": "5.60.6"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { AnyRecord } from '@codeleap/types'
|
|
2
|
+
import { useRef, Children } from 'react'
|
|
3
|
+
|
|
4
|
+
const simpleHash = (str: string): string => {
|
|
5
|
+
let hash = 0
|
|
6
|
+
for (let i = 0; i < str.length; i++) {
|
|
7
|
+
hash = (hash << 5) - hash + str.charCodeAt(i)
|
|
8
|
+
hash |= 0
|
|
9
|
+
}
|
|
10
|
+
return Math.abs(hash).toString()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const normalizeProps = <P extends AnyRecord>(props: P, keys: Array<keyof P>): Partial<P> => {
|
|
14
|
+
return keys.reduce((acc, key) => {
|
|
15
|
+
if (key === 'children') {
|
|
16
|
+
acc[key] = Children.map(props[key], (child) =>
|
|
17
|
+
typeof child === 'object' ? '[Component]' : child
|
|
18
|
+
)
|
|
19
|
+
} else {
|
|
20
|
+
acc[key] = props[key]
|
|
21
|
+
}
|
|
22
|
+
return acc
|
|
23
|
+
}, {} as Partial<P>)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const normalizeDebugName = (debugName: string) => {
|
|
27
|
+
return debugName.trim().replace(/\s+/g, '-').toLowerCase()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const generateComponentTestId = <P extends AnyRecord>(componentName: string, props: P, keys: Array<keyof P>) => {
|
|
31
|
+
const hasDebugName = typeof props?.['debugName'] === 'string'
|
|
32
|
+
if (hasDebugName) return `${componentName}:${normalizeDebugName(props?.['debugName'])}`
|
|
33
|
+
const extractedProps = normalizeProps(props, keys)
|
|
34
|
+
return `${componentName}:${simpleHash(JSON.stringify(extractedProps))}`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const useComponentTestId = <P extends AnyRecord>(
|
|
38
|
+
Component: any,
|
|
39
|
+
props: P,
|
|
40
|
+
keys: Array<keyof P>
|
|
41
|
+
) => {
|
|
42
|
+
const testIdRef = useRef(generateComponentTestId(Component.styleRegistryName, props, keys))
|
|
43
|
+
return testIdRef.current
|
|
44
|
+
}
|