Skip to content

Commit

Permalink
v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alessioVelluso committed Sep 14, 2024
1 parent 03563b9 commit d8d5e78
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 445 deletions.
114 changes: 26 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@



`v2.1.2`
`v3.0.0`

This is a package i made for myself but can surely be helpful to others, feel free to contribute if you like it.

Expand All @@ -11,10 +11,11 @@ This is a package i made for myself but can surely be helpful to others, feel fr
> If you need excel js install[fast-js-excel](https://github.com/alessioVelluso/FastExcel) or take a look at [word-file-utils](https://github.com/alessioVelluso/WordFileUtils) if you need some file utilities without the use of exceljs library.
> **DO NOT INSTALL ALL THREE LIBS CAUSE ONE IS THE "PARENT" OF THE OTHER:**
> 1. `utils-stuff`
> 2. `word-file-utils` (including utils-stuff)
> 3. `fast-js-excel` (including exceljs, word-file-utils (including utils-stuff))
> 2. `utils-logger` (including utils-stuff)
> 3. `word-file-utils` (including utils-logger)
> 4. `fast-js-excel` (including exceljs, word-file-utils (including utils-stuff))
>
>So if you install word-file utils you can use the classes of utils-stuff and so on, choose the one for your pourpose.
>So if you install word-file utils you don't need to install utils-logger and so on, choose the one for your pourpose.


Expand All @@ -36,34 +37,14 @@ You can import two different classes, `GenericUtils` as default & `ClientFilters
At the moment, the interface of the class is as it follows:

```ts
interface IGenericUtils {
parseDate: (date?:string) => string
catchRes: <T>(isOk:false, response:T | null, error?:string | null) => CatchedResponse<T>
catchResError:(err:any) => CatchedResponse<any>
isAxiosOk: (res:{ status:number, [Key:string]: GenericType} /* pass an AxiosResponse */) => boolean;
isStringValid: (str?:string) => boolean
arrayDiff: <T = string | number>(originalArray:T[], currentArray:T[]) => { removed:T[], added:T[] };
isNumeric: (str:string) => boolean;

log: (message:any, color:LogColors) => void;
logColor: (coloredMessage:any, ...messages:any[]) => void;
logDetail: (...messages:any[]) => void;
logError: (...errs:any[]) => void;

logFile: (message:string, type?:"log" | "error", isClosing?:boolean) => void;
}



interface IClientFilters<T extends ClientFilter> {
values:T
currentParams:string;
currentHref:string;
currentWholeUrl:string;

buildParams:() => string | null;
buildWholeUrl:() => string | null;
// setHref:() => string | null; --- private
export interface IGenericUtils {
parseDate: (date?:string) => string
resOk: <T>(response:T) => CatchedResponse<T>
resError:(err:any) => CatchedResponse<any>
isAxiosOk: (res:{ status:number, [Key:string]: GenericType} /* pass an AxiosResponse */) => boolean;
isStringValid: (str?:string) => boolean;
arrayDiff: <T = string | number>(originalArray:T[], currentArray:T[]) => ArrayDifference<T>;
isNumeric: (str:string) => boolean;
}
```

Expand All @@ -73,14 +54,19 @@ interface IClientFilters<T extends ClientFilter> {
## Initialize the class

```ts
import { GenericUtils , ClientFilters} from "word-file-utils"
import { GenericUtils } from "word-file-utils"
```
**GenericUtils:** I suggest you to create a generic utils class extending mine if you want a solid way to store all your utils functions or whatever.
You can find an example in the `test3/utils.ts` file in this repo.
The constructor of GenericUtils follows this interface:
```ts
constructor(data?:LoggerConstructor)
interface LoggerConstructor { logFilePath?:string, debug?:boolean }
protected readonly dateLocale:DateLocales = "en-US";
protected readonly isNumericRegex:RegExp = /^-?\d+(\.\d+)?$/
constructor(constructor?:GenericUtilsConstructor)
export interface GenericUtilsConstructor {
locale?:DateLocales,
numericValidation?:RegExp
}
```

While the logFilePath is required only if you have to write log files somewhere in prod, the debug flag is by default set to true and will avoid any logging in console if set to false *(for the log methods of this class)*.
Expand All @@ -94,70 +80,22 @@ class MyGenericUtils extends GenericUtils {
return `Hello ${name}`
}
}
const mgu = new MyGenericUtils(/*{ debug: env.DEBUG }*/)
const mgu = new MyGenericUtils(/*{ locale: "it-IT" }*/)


// Or simply export mine directly
const gu = new GenericUtils(/*{ debug: env.DEBUG }*/)
const gu = new GenericUtils(/*{ locale: "it-IT" }*/)
```

Export it however you want but i raccomand you to init a single object and use it through all the project.
```ts
export default new GenericUtils(/*{ debug: env.DEBUG }*/)
export default new GenericUtils(/*{ locale: "it-IT" }*/)

// Or destruct the single functions
const gu = new GenericUtils(/*{ debug: env.DEBUG }*/)
const { log, logDetails, logError } = gu;
export { log, logDetails, logError }
const { resOk, resError, isStringValid } = gu;
export { resOk, resError, isStringValid }
```

The related methods are really simple and can be easily read in the realted `/package/src/GenericUtils.ts` file in this repo.
The only method not-so-easy to read is the `isNumeric` RegExp wich will return true if the passed string is any int, float, double or negative number.


**ClientFilters**: For the ClientFilters class i reccomand you to initialize a new object every file that requires it.

I reccomand you to create a `/types/filters.ts` file to store all different filters interface you'll use through app as those can be helpful for queries or other logics.
```ts
interface ExampleFilter extends ClientFilter { startDate:Date, endDate:Date, type:number, active?:boolean }
interface AnotherFilter extends ClientFilter { startDate:Date, endDate:Date, name?:string }
```

To create a ClientFilters object consider this code:
```ts
const filter = new ClientFilters<ExampleFilter>({
startDate: new Date(),
endDate: new Date(),
type: 2,
});
```
In this case we will have a `filter` object having:
1. `.values`: the current values of the filters (the ones specified in the constructors)
2. `.currentParams`: a string rapresenting the current values parsed as a param string (`?value1=1&value2=true`)
3. `.currentHref`: if the object is initialized in a client, this will be set as the related window.location.href value, else null.
4. `.currentWholeUrl`: if the currentHref is not null, this prop will hold the combination of currentHref and currentParams, else it will be null.




## Types

```ts
// --- Generic Utils
export interface GenericObject { [Key:string]: string | number | boolean | Date | GenericObject }
export type GenericType = string | number | boolean | Date | GenericObject
export interface CatchedResponse<T> { isOk:boolean, response: T | null, error?:string | null }
export interface PaginatedParams<T = null>{ currentPage:number, quantity:number, filter?:T }
export interface PaginatedResponse<T>{ totalPages:number, data:T }
export interface SelectOptions { id:string, text:string }


// --- Logger
export type LogColors = "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "gray" | null
export interface LoggerConstructor { logFilePath?:string, debug?:boolean }


// --- Custom Navigation
export type ClientFilterTypes = string | number | Date | boolean | Array<string | number> | undefined
export interface ClientFilter { [Key:string]: ClientFilterTypes }
```
114 changes: 26 additions & 88 deletions package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@



`v2.1.2`
`v3.0.0`

This is a package i made for myself but can surely be helpful to others, feel free to contribute if you like it.

Expand All @@ -11,10 +11,11 @@ This is a package i made for myself but can surely be helpful to others, feel fr
> If you need excel js install[fast-js-excel](https://github.com/alessioVelluso/FastExcel) or take a look at [word-file-utils](https://github.com/alessioVelluso/WordFileUtils) if you need some file utilities without the use of exceljs library.
> **DO NOT INSTALL ALL THREE LIBS CAUSE ONE IS THE "PARENT" OF THE OTHER:**
> 1. `utils-stuff`
> 2. `word-file-utils` (including utils-stuff)
> 3. `fast-js-excel` (including exceljs, word-file-utils (including utils-stuff))
> 2. `utils-logger` (including utils-stuff)
> 3. `word-file-utils` (including utils-logger)
> 4. `fast-js-excel` (including exceljs, word-file-utils (including utils-stuff))
>
>So if you install word-file utils you can use the classes of utils-stuff and so on, choose the one for your pourpose.
>So if you install word-file utils you don't need to install utils-logger and so on, choose the one for your pourpose.


Expand All @@ -36,34 +37,14 @@ You can import two different classes, `GenericUtils` as default & `ClientFilters
At the moment, the interface of the class is as it follows:

```ts
interface IGenericUtils {
parseDate: (date?:string) => string
catchRes: <T>(isOk:false, response:T | null, error?:string | null) => CatchedResponse<T>
catchResError:(err:any) => CatchedResponse<any>
isAxiosOk: (res:{ status:number, [Key:string]: GenericType} /* pass an AxiosResponse */) => boolean;
isStringValid: (str?:string) => boolean
arrayDiff: <T = string | number>(originalArray:T[], currentArray:T[]) => { removed:T[], added:T[] };
isNumeric: (str:string) => boolean;

log: (message:any, color:LogColors) => void;
logColor: (coloredMessage:any, ...messages:any[]) => void;
logDetail: (...messages:any[]) => void;
logError: (...errs:any[]) => void;

logFile: (message:string, type?:"log" | "error", isClosing?:boolean) => void;
}



interface IClientFilters<T extends ClientFilter> {
values:T
currentParams:string;
currentHref:string;
currentWholeUrl:string;

buildParams:() => string | null;
buildWholeUrl:() => string | null;
// setHref:() => string | null; --- private
export interface IGenericUtils {
parseDate: (date?:string) => string
resOk: <T>(response:T) => CatchedResponse<T>
resError:(err:any) => CatchedResponse<any>
isAxiosOk: (res:{ status:number, [Key:string]: GenericType} /* pass an AxiosResponse */) => boolean;
isStringValid: (str?:string) => boolean;
arrayDiff: <T = string | number>(originalArray:T[], currentArray:T[]) => ArrayDifference<T>;
isNumeric: (str:string) => boolean;
}
```

Expand All @@ -73,14 +54,19 @@ interface IClientFilters<T extends ClientFilter> {
## Initialize the class

```ts
import { GenericUtils , ClientFilters} from "word-file-utils"
import { GenericUtils } from "word-file-utils"
```
**GenericUtils:** I suggest you to create a generic utils class extending mine if you want a solid way to store all your utils functions or whatever.
You can find an example in the `test3/utils.ts` file in this repo.
The constructor of GenericUtils follows this interface:
```ts
constructor(data?:LoggerConstructor)
interface LoggerConstructor { logFilePath?:string, debug?:boolean }
protected readonly dateLocale:DateLocales = "en-US";
protected readonly isNumericRegex:RegExp = /^-?\d+(\.\d+)?$/
constructor(constructor?:GenericUtilsConstructor)
export interface GenericUtilsConstructor {
locale?:DateLocales,
numericValidation?:RegExp
}
```

While the logFilePath is required only if you have to write log files somewhere in prod, the debug flag is by default set to true and will avoid any logging in console if set to false *(for the log methods of this class)*.
Expand All @@ -94,70 +80,22 @@ class MyGenericUtils extends GenericUtils {
return `Hello ${name}`
}
}
const mgu = new MyGenericUtils(/*{ debug: env.DEBUG }*/)
const mgu = new MyGenericUtils(/*{ locale: "it-IT" }*/)


// Or simply export mine directly
const gu = new GenericUtils(/*{ debug: env.DEBUG }*/)
const gu = new GenericUtils(/*{ locale: "it-IT" }*/)
```

Export it however you want but i raccomand you to init a single object and use it through all the project.
```ts
export default new GenericUtils(/*{ debug: env.DEBUG }*/)
export default new GenericUtils(/*{ locale: "it-IT" }*/)

// Or destruct the single functions
const gu = new GenericUtils(/*{ debug: env.DEBUG }*/)
const { log, logDetails, logError } = gu;
export { log, logDetails, logError }
const { resOk, resError, isStringValid } = gu;
export { resOk, resError, isStringValid }
```

The related methods are really simple and can be easily read in the realted `/package/src/GenericUtils.ts` file in this repo.
The only method not-so-easy to read is the `isNumeric` RegExp wich will return true if the passed string is any int, float, double or negative number.


**ClientFilters**: For the ClientFilters class i reccomand you to initialize a new object every file that requires it.

I reccomand you to create a `/types/filters.ts` file to store all different filters interface you'll use through app as those can be helpful for queries or other logics.
```ts
interface ExampleFilter extends ClientFilter { startDate:Date, endDate:Date, type:number, active?:boolean }
interface AnotherFilter extends ClientFilter { startDate:Date, endDate:Date, name?:string }
```

To create a ClientFilters object consider this code:
```ts
const filter = new ClientFilters<ExampleFilter>({
startDate: new Date(),
endDate: new Date(),
type: 2,
});
```
In this case we will have a `filter` object having:
1. `.values`: the current values of the filters (the ones specified in the constructors)
2. `.currentParams`: a string rapresenting the current values parsed as a param string (`?value1=1&value2=true`)
3. `.currentHref`: if the object is initialized in a client, this will be set as the related window.location.href value, else null.
4. `.currentWholeUrl`: if the currentHref is not null, this prop will hold the combination of currentHref and currentParams, else it will be null.




## Types

```ts
// --- Generic Utils
export interface GenericObject { [Key:string]: string | number | boolean | Date | GenericObject }
export type GenericType = string | number | boolean | Date | GenericObject
export interface CatchedResponse<T> { isOk:boolean, response: T | null, error?:string | null }
export interface PaginatedParams<T = null>{ currentPage:number, quantity:number, filter?:T }
export interface PaginatedResponse<T>{ totalPages:number, data:T }
export interface SelectOptions { id:string, text:string }


// --- Logger
export type LogColors = "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "gray" | null
export interface LoggerConstructor { logFilePath?:string, debug?:boolean }


// --- Custom Navigation
export type ClientFilterTypes = string | number | Date | boolean | Array<string | number> | undefined
export interface ClientFilter { [Key:string]: ClientFilterTypes }
```
7 changes: 3 additions & 4 deletions package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "utils-stuff",
"version": "2.1.2",
"version": "3.0.0",
"author": "Alessio Velluso",
"license": "MIT",
"description": "Generic utils, both for server or client uses",
Expand All @@ -26,10 +26,9 @@
"build": "tsup"
},
"keywords": [
"log",
"filters",
"frontend",
"backend",
"client",
"color",
"utils",
"utilities"
]
Expand Down
Loading

0 comments on commit d8d5e78

Please sign in to comment.