This library is a set of typescript utilities to save time
run
npm install clever-ts-utilities
. And then, use functions like this: let clone = copy(data);
returns an items paginated array from an array, index page and size page
paginate(params: {data: Array<any>, indexPage: number, pageSize: number}): Array<any>
Remove begening common chars of two strings
strRemoveBeginingCommonChars(params: {[key: string]: string}): {[key: string]: string}
Get values from a dictionary
getValuesFromDictionnary(dic: {[key: string]: any}): any[]
Checks that all string values in an array are equal
isAllEqual(chars: any[]): boolean
Return a sublist from a list that has column that contain a certain value.
enumarableFromListEqual(list: any[], column: string, value: any): any[]
Delete items from an array.
deleteFromArray(array: any[], item: any): any[] | boolean
Sort array by property.
objectSort(arr: Array<any>, prop: string, isAscendent: boolean = true): Array<any>
By default javacript when assigning one variable to another, refers to the same memory address. This method allows you to copy the passed element whatever its type.
copy(obj: any): any
deternine if data is undefined or null.
isUndefinedOrNull(data: string): boolean
Used to literally determine the type of the passed object (including the array type)
jsType(obj: any): string
Get an object in a list by value of one field, like id.
getObjectByFieldValue(obj: any[], fieldname: string, fieldvalue: any): any
Match two objects.
objectsEquals(obj1: any, obj2: any, ignoreProps?: string[]): boolean
Match an object list.
allObjectsEquals(objs: any[], ignoreProps?: string[]): boolean
Check if an array contain object
arrayObjectContain(array: any[], obj: any, ignoreProps?: string[]): boolean
Execute a Set Javascript in scope of object
setObjectsEquals(objs: any[], ignoreProps?: string[]): any[]
Allows you to perform arithmetic operations on dates
dateOperation(params: {date: Date, amount: number, operation: DateOperationEnum}): Date
Allow you to quickly compare dates
dateCompare(first: Date, operator: DateCompareOperator, second: Date): boolean
Takes date and return first and last date of the current month
dateMonthPeriod(date: Date): Array<Date>
Takes date and return first and last date of the current year
dateYearPeriod(date: Date): Array<Date>
Erase spaces in a character chain
cleanSpace(str: string): string
Adds thousands separators in a string
thousandSeparator(nombre: string | number, args: ThousandSeparatorArg = {decimal: true, pres: 2, arround: false}): string
deternine if a string is undefined or null or empty.
isStringUndefinedOrNullOrEmpty(data: string): boolean
Reduce string dictionnary to chars at specified position
getCharsAt(paramList: {[key: string]: string}, ofs: number): {[key: string]: string}
Insert string into another by refering an anchor around it string can be added.
insertString(params: {
str: string;
toAdd: string;
anchor: string;
position: "before" | "after";
}): string
Manage iterations on chars in string
const charCollection = new CharsCollection("alphabet");
const charIteration = charCollection.createIterator();
while (charIteration.hasMore()) {
console.log(charIteration.getNext());
}
console.log(charIteration.toList());
Apply trim on all properties of an object
trimObject(obj: any): any;
------------------------------------
// Example:
const obj: any = {
alpha: ' dfg dfg ',
beta: ' ddgdg ',
qs: [
{
al: ' dfgdf ',
df: 2,
azq: false
},
{
al: ' dfgdf ',
df: 2,
azq: false
},
],
az: {
alpha2: ' dgfgd dfgdfg ',
beta2: {
alpha3: ' dfgdfg dfg',
sd: 12,
xd: true,
}
}
}
console.log(trimObject(obj));
// Output:
// {
// "alpha": "dfg dfg",
// "beta": "ddgdg",
// "qs": [
// {
// "al": "dfgdf",
// "df": 2,
// "azq": false
// },
// {
// "al": "dfgdf",
// "df": 2,
// "azq": false
// }
// ],
// "az": {
// "alpha2": "dgfgd dfgdfg",
// "beta2": {
// "alpha3": "dfgdfg dfg",
// "sd": 12,
// "xd": true
// }
// }
// }
Convert file to base 64
fileToBase64(file: File): Promise<string | any>
Convert file to blob
fileToBlob(file: File): Promise<Blob | any>
Convert file to string
fileToString(file: File): Promise<string | ArrayBuffer>
Convert File objet to FileModel
serializeFile(file: File): Promise<FileModel>
Convert FileModel objet to Blob
fileModelToBlob(fileModel: FileModel): Blob
Return the download url of a file
buildDownloadFileUrl(file: FileModel): string
Class that manage http request pendings
Get instance of the class
Increment the url collection
Decrement the url collection
Check if request is pending. It must be used in Observer class.
Register observer
Unregister observer
This is a set of utilities for clean architecture project.
This class maybe extended by models and define an update method to model
export class User extends BaseModelWithoutProps<User> {
login: string;
email: string;
password: string;
phone: string;
avatar: string;
}
// Initialize a new instance of the User class:
const user: User = new User(); // option 1
const user: User = new User({ // option 2
login: 'admin',
email: '[email protected]',
});
// Update the user instance:
user.update({
login: 'admin 2',
});
This class work like BaseModelWithoutProps
. But add id
, createdAt
and updatedAt
properties to model
This is a command handler interface for CQRS approach
interface ICommandHandler<C>
This is a query handler interface for CQRS approach
interface IQueryHandler<Q, R>
This is a usecase interface for hexagonal and approach
interface IUsecase<C, T>
This is a mapper interface for mapping between domain and data layer
interface IMapper<I, O>
AutoMapper allow to map automaticaly Objects like C# or Java AutoMappers.
It work well with EsNext target configuration
const source = {
name: "John",
surname: "Doe",
age: 30
}
interface IDestination {
firstName: string;
lastName: string;
age: number;
}
const destination = new AutoMapper()
.forSource(Object)
.forDestination(IDestination)
.mapFrom("name", "firstName") // Optional - if not specified, it will be mapped by default
.mapFrom("surname", "lastName") // Optional - if not specified, it will be mapped by default
.execute(source);
OmitMethods
type omit methods from type.
type OmitMethods<T>
Get responsive dialog width with 90% width for mobile devices
export function responsiveDialogWidth(width: string): string