Skip to content

Commit dfccc28

Browse files
committed
[IMPL] types routes + [FIX] various
1 parent 8d04573 commit dfccc28

149 files changed

Lines changed: 710 additions & 122 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<br>
99
</h1>
1010

11-
<h3 align="center">A collection of Hooks, Components and Utilities for React
11+
<h3 align="center">A collection of Hooks, Components, Utilities and Types for React
1212
</h3>
1313

1414
<div align="center">
@@ -196,6 +196,10 @@ Go to [Demo](https://ndriadev.github.io/react-tools) to see and try all implemen
196196
- [_removeDuplicatedFromArrayObjects_](#removeDuplicatedFromArrayObjects)
197197
- [_removePropertiesFromArrayObjects_](#removePropertiesFromArrayObjects)
198198

199+
- [__TYPES__](#types)
200+
201+
- [__UTILITY TYPES__](#utility-types)
202+
199203
## HOOKS
200204

201205
## STATE
@@ -1141,6 +1145,111 @@ Function that, given an array of objects and a property or an array of propertie
11411145
removePropertiesFromArrayObjects<T, E extends string | number | symbol = keyof T>(array: T[], property: E | E[]): Omit<T, E>[]
11421146
```
11431147
1148+
## TYPES
1149+
1150+
## Utility Types
1151+
1152+
Typescript utility types for specified use cases.
1153+
1154+
### NestedKeyOf
1155+
1156+
Utility type that constructs a type by picking all properties and nested proprerties from __`T`__ in form _`property.nestedProprerty`_.
1157+
```tsx
1158+
type NestedKeyOf<T extends Record<string, unknown>>
1159+
```
1160+
1161+
### ErrorModel
1162+
1163+
Utility type that constructs an object from __`T`__ and whose property values are _`boolean`_.
1164+
```tsx
1165+
type ErrorModel<T extends object>
1166+
```
1167+
1168+
### SelectivePartial
1169+
1170+
Utility type that works like __Partial__ but allows to specify which properties set to optional.
1171+
```tsx
1172+
type SelectivePartial<T extends object, E extends keyof T>
1173+
```
1174+
1175+
### RecursivePartial
1176+
1177+
Utility type that works like __Partial__ but set nested properties to optional also.
1178+
```tsx
1179+
type RecursivePartial<T extends object>
1180+
```
1181+
1182+
### Optional
1183+
1184+
Utility type that constructs a type that is __`T`__ or __`E`__, if specified otherwise __`null`__.
1185+
```tsx
1186+
type Optional<T = unknown, E = null>
1187+
```
1188+
1189+
### DependencyListTyped
1190+
1191+
Utility type that works like __DependencyList__ react type but it can be specified dependencies list element types.
1192+
```tsx
1193+
type DependencyListTyped<T = unknown>
1194+
```
1195+
1196+
### Union
1197+
1198+
Utility type that given an array constructs union type from array elements type.
1199+
```tsx
1200+
type Union<T extends unknown[]>
1201+
```
1202+
1203+
### ExtractTail
1204+
1205+
Utility type that given an array extracts a new array with all elements from array expect first.
1206+
```tsx
1207+
type ExtractTail<T extends unknown[]>
1208+
```
1209+
1210+
### ExtractMiddle
1211+
1212+
Utility type that given an array extracts a new array with all elements from array expect first and last.
1213+
```tsx
1214+
type ExtractMiddle<T extends unknown[]>
1215+
```
1216+
1217+
### ExtractHead
1218+
1219+
Utility type that given an array extracts a new array with all elements from array expect last.
1220+
```tsx
1221+
type ExtractHead<T extends unknown[]>
1222+
```
1223+
1224+
### PartialRecord
1225+
1226+
Utility type that constructs a record with all properties set to optional.
1227+
```tsx
1228+
type PartialRecord<K extends keyof any, T>
1229+
```
1230+
1231+
### ArrayMinLength1
1232+
1233+
Utility type that constructs an array of __`T`__ with one element at least.
1234+
```tsx
1235+
type ArrayMinLength1<T>
1236+
```
1237+
1238+
### LanguageBCP47Tags
1239+
1240+
Utility type for Language BCP-47 tags.
1241+
```tsx
1242+
type LanguageBCP47Tags
1243+
```
1244+
1245+
### TypedArray
1246+
1247+
Utility type for __`Typed Arrays`__.
1248+
```tsx
1249+
type TypedArray
1250+
```
1251+
1252+
11441253
## ESLint configuration
11451254
To validate dependencies of custom hooks like `useMemoCompare`, configure `exhaustive-deps` with the `additionalHooks` option:
11461255
```js

apps/react-tools-demo/scripts/generateMainLayout.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ async function createLinkRouter(router) {
156156
createLinkHooksRoutes(router, HOOKS_STATE_FILES, HOOKS_LIFECYCLE_FILES, HOOKS_PERFORMANCE_FILES, HOOKS_EVENTS_FILES, HOOKS_APIDOM_FILES);
157157
createLinkRoutes(router, COMPONENTS_FILES, "components")
158158
createLinkRoutes(router, UTILS_FILES, "utils")
159+
createLinkRoutes(router, ["utilityTypes"], "types")
159160
}
160161

161162
async function generateMainLayout() {

apps/react-tools-demo/scripts/generateMarkdown.js

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const __dirname = new URL('.', import.meta.url).pathname;
88

99
const PATH_DEMO_SRC = path.join(__dirname, '..', 'src');
1010
const PATH_LIB_SRC = path.join(__dirname, "..", "..", "..", "packages", "react-tools-lib", "src");
11+
const PATH_UTILITY_MODEL_TYPES_FILE = path.join(__dirname, "..", "..", "..", "packages", "react-tools-lib", "src", "models", "utilityTypes.model.ts");
1112
const DEMO_COMPONENT_DIR_NAME = "pages";
1213
const MARKDOWWN_DIR_NAME = "markdown";
1314
const HOOKS_DIR_NAME = "hooks";
@@ -213,13 +214,15 @@ ${jsDoc.usage ? `\n## Usage\n\n${jsDoc.usage}\n`:""}
213214
${jsDoc.type}
214215
\`\`\`
215216
216-
> ### Params
217+
${jsDoc.params && jsDoc.params.length > 0 ?
218+
`> ### Params
217219
>
218220
${jsDoc.params && jsDoc.params.length > 0
219-
? jsDoc.params.map(el => `> - __${el.name}__: _${el.type}_${el.description ? ' \n'+el.description : ""}`).join("\n")
221+
? jsDoc.params.map(el => `> - __${el.name}__: _${el.type}_${el.description ? ' \n' + el.description : ""}`).join("\n")
220222
: ">"
221223
}
222224
>
225+
`:""}
223226
224227
> ### Returns
225228
>
@@ -452,11 +455,72 @@ async function generateComponentsMarkDown() {
452455
}
453456
}
454457

458+
async function generateModelMarkDown() {
459+
const utilityTypesFile = await fs.readFile(path.join(PATH_UTILITY_MODEL_TYPES_FILE), { encoding: "utf8" });
460+
const stringBuffer = {
461+
value: "",
462+
/**
463+
*
464+
* @param {string} s
465+
* @returns {this}
466+
*/
467+
add(s) {
468+
this.value += s + "\n";
469+
return this;
470+
},
471+
/**
472+
*
473+
* @param {string[]} s
474+
* @returns {this}
475+
*/
476+
set(s) {
477+
s.forEach(slice => { this.value += slice + "\n" });
478+
return this;
479+
}
480+
}
481+
const utilityTypesFileSplitted = utilityTypesFile.split("\n").slice(2).filter(el => !["/**", " */", ""].includes(el));
482+
stringBuffer.set([
483+
"# Utility Types",
484+
"",
485+
"Typescript utility types for specified use cases.",
486+
""
487+
]);
488+
for (let i = 0, size = utilityTypesFileSplitted.length; i < size;) {
489+
const description = utilityTypesFileSplitted[i].substring(3);
490+
const type = utilityTypesFileSplitted[i + 1];
491+
let title, generics;
492+
if (type.indexOf("> =") !== -1) {
493+
title = type.substring(12, type.indexOf("<"));
494+
generics = type.substring(type.indexOf("<"), type.indexOf("> =") + 1);
495+
} else {
496+
title = type.substring(12, type.indexOf(" ="));
497+
}
498+
stringBuffer.set([
499+
"## " + title,
500+
"",
501+
description,
502+
"```tsx",
503+
"type " + title + (generics ? generics : ""),
504+
"```",
505+
""
506+
]);
507+
i++;
508+
while (!utilityTypesFileSplitted[i].startsWith(" * ")) {
509+
i++;
510+
if (i === size) {
511+
break;
512+
}
513+
}
514+
}
515+
await fs.writeFile(path.join(PATH_DEMO_SRC, MARKDOWWN_DIR_NAME, "utilityTypes.md"), stringBuffer.value, { encoding: "utf8" });
516+
}
517+
455518
async function generateMarkDown() {
456519
try {
457520
await deleteAllFiles(path.join(PATH_DEMO_SRC, MARKDOWWN_DIR_NAME));
458521
await generateUtilsMarkDown();
459522
await generateHooksMarkDown();
523+
await generateModelMarkDown();
460524
await generateComponentsMarkDown();
461525
process.exit(0);
462526
} catch (error) {

apps/react-tools-demo/scripts/generateRouter.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,24 @@ async function createRouter(router) {
274274
router.add(` children: [`);
275275
createRoutes(router, UTILS_FILES, "utils")
276276
router.add(` ]`);
277+
router.add(' },');
278+
router.add(' {');
279+
router.add(' path: "types",');
280+
router.add(' element: <Suspense fallback={<Spinner/>}>');
281+
router.add(' <Outlet />');
282+
router.add(' </Suspense>,');
283+
router.add(` children: [`);
284+
router.add(` {`);
285+
router.add(` index: true,`);
286+
router.add(` element: <Navigate to={"/types/utilityTypes"} replace/>,`);
287+
router.add(` },`);
288+
router.add(` {`);
289+
router.add(` path: "utilityTypes",`);
290+
router.add(` element: <Suspense fallback={<Spinner/>}>`);
291+
router.add(` <ComponentLayout markdown={utilityTypesMD} />`);
292+
router.add(` </Suspense>`);
293+
router.add(` }`);
294+
router.add(` ]`);
277295
router.add(' }');
278296
router.add(' ]');
279297
router.add(' }');

apps/react-tools-demo/src/layout/MainLayout.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,18 @@ export default function MainLayout() {
15061506
>
15071507
removePropertiesFromArrayObjects
15081508
</Link>
1509+
<p className="sub-type">Types</p>
1510+
<Link
1511+
className={pathname === "/types/utilityTypes" ? 'active' : ''}
1512+
ref={node => linksRef.current["utilityTypes"] = node}
1513+
to="/types/utilityTypes"
1514+
onClick={() => {
1515+
containerRef.current?.scrollTo(0, 0);
1516+
window.innerWidth < 1190 && closeNav();
1517+
}}
1518+
>
1519+
utilityTypes
1520+
</Link>
15091521
</nav>
15101522
</>
15111523
}

apps/react-tools-demo/src/markdown/ErrorBoundary.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ it is rendered when an error occurred. It can be an element, or a Component or a
3535
element to render.
3636
>
3737
38+
3839
> ### Returns
3940
>
4041
> __result__: element or fallback.

apps/react-tools-demo/src/markdown/For.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ callback executed to map _of_ elements.
6262
callback executed to sort _of_ elements.
6363
>
6464

65+
6566
> ### Returns
6667
>
6768
> __result__: elements list, rendered from _of_ prop or _fallback_.

apps/react-tools-demo/src/markdown/LazyComponent.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function that will be executed before loading component .
4242
function that will be executed after loading component .
4343
>
4444

45+
4546
> ### Returns
4647
>
4748
> __element__

apps/react-tools-demo/src/markdown/Show.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ optional element to render when _when_ prop is false.
3636
optional element to render when _when_ prop is true.
3737
>
3838

39+
3940
> ### Returns
4041
>
4142
> __element__: the element rendered or null.

apps/react-tools-demo/src/markdown/SwitchCase.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ optional element to render when _when_ prop is false.
4848
__Case__ components.
4949
>
5050
51+
5152
> ### Returns
5253
>
5354
> __element__: _JSX.Element|null_

0 commit comments

Comments
 (0)