Repository: styleguidist/react-styleguidist
Pull request #1595 (2020-05-17 12:35:31)

Fix Sections with numbers in the name don't work
Resolve:

- Sections with numbers in the name don't work #1594

The main reason is that `parseInt` parses the first number string as a number.
Therefore, when `1.Doc` given, it was computed as `1`.



codecov[bot]: # [Codecov](https://codecov.io/gh/styleguidist/react-styleguidist/pull/1595?src=pr&el=h1) Report
> Merging [#1595](https://codecov.io/gh/styleguidist/react-styleguidist/pull/1595?src=pr&el=desc) into [master](https://codecov.io/gh/styleguidist/react-styleguidist/commit/b4c28451fe1cdce0757da851b9f2e1db458794e2&el=desc) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

| [Impacted Files](https://codecov.io/gh/styleguidist/react-styleguidist/pull/1595?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/client/utils/getInfoFromHash.ts](https://codecov.io/gh/styleguidist/react-styleguidist/pull/1595/diff?src=pr&el=tree#diff-c3JjL2NsaWVudC91dGlscy9nZXRJbmZvRnJvbUhhc2gudHM=) | `100.00% <100.00%> (ø)` | |

mitsuruog: I've updated the PR. please review me again.

Patch:
diff --git a/src/client/utils/getInfoFromHash.ts b/src/client/utils/getInfoFromHash.ts
index 754669b6..f85793f2 100644
--- a/src/client/utils/getInfoFromHash.ts
+++ b/src/client/utils/getInfoFromHash.ts
@@ -1,8 +1,7 @@
-import isNaN from 'lodash/isNaN';
 import { hasInHash, getHashAsArray } from './handleHash';
 
-function filterNumbers(item: string): boolean {
-	return isNaN(parseInt(item, 10)) && item !== '';
+function hasDigitsOnly(item: string): boolean {
+	return item.match(/^\d+$/) !== null;
 }
 
 /**
@@ -25,12 +24,12 @@ export default function getInfoFromHash(
 	const shouldIsolate = hasInHash(hash, '#!/');
 	if (shouldIsolate || hasInHash(hash, '#/')) {
 		const hashArray = getHashAsArray(hash, shouldIsolate ? '#!/' : '#/');
-		const index = parseInt(hashArray[hashArray.length - 1], 10);
+		const targetHash = hashArray[hashArray.length - 1];
 		return {
 			isolate: shouldIsolate,
-			hashArray: hashArray.filter(filterNumbers),
+			hashArray: hashArray.filter(item => !hasDigitsOnly(item)),
 			targetName: hashArray[0],
-			targetIndex: isNaN(index) ? undefined : index,
+			targetIndex: hasDigitsOnly(targetHash) ? parseInt(targetHash, 10) : undefined,
 		};
 	}
 	return {};


Test patch:
diff --git a/src/client/utils/__tests__/getInfoFromHash.spec.ts b/src/client/utils/__tests__/getInfoFromHash.spec.ts
index e92f7cde..0dbdb6e6 100644
--- a/src/client/utils/__tests__/getInfoFromHash.spec.ts
+++ b/src/client/utils/__tests__/getInfoFromHash.spec.ts
@@ -35,4 +35,24 @@ describe('getInfoFromHash', () => {
 			targetIndex: undefined,
 		});
 	});
+
+	it('should extract target index when the URL ends with a number', () => {
+		const result = getInfoFromHash('#/Documentation/Files/Buttons/5');
+		expect(result).toEqual({
+			isolate: false,
+			hashArray: ['Documentation', 'Files', 'Buttons'],
+			targetName: 'Documentation',
+			targetIndex: 5,
+		});
+	});
+
+	it('should return a proper parsed result even though the hash starts with a number', () => {
+		const result = getInfoFromHash('#/1.Documentation');
+		expect(result).toEqual({
+			isolate: false,
+			hashArray: ['1.Documentation'],
+			targetName: '1.Documentation',
+			targetIndex: undefined,
+		});
+	});
 });