feat: initial commit

This commit is contained in:
2024-03-12 18:41:52 +02:00
commit 2a359099ba
19 changed files with 1592 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
/*
Copyright 2024 Resultium LLC
This file is part of @resultium/utils.
@resultium/utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
@resultium/utils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Capitalizes first character of the given string
*
* @param string - string to modify
* @returns new string
*/
export const capitalizeFirstChar = (string: string) => {
return `${string[0].toUpperCase()}${string.slice(1)}`;
};

View File

@@ -0,0 +1,27 @@
/*
Copyright 2024 Resultium LLC
This file is part of @resultium/utils.
@resultium/utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
@resultium/utils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Conditionally joins given class strings
*
* @param classes - class strings to join
* @returns new string
*/
export const classNames = (...classes: Array<string>) =>
classes.filter(Boolean).join(" ");

View File

@@ -0,0 +1,28 @@
/*
Copyright 2024 Resultium LLC
This file is part of @resultium/utils.
@resultium/utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
@resultium/utils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Conditionally joins given strings
*
* @param array - strings to join
* @param joinChar - character used to join strings
* @returns new string
*/
export const conditionalJoin = (array: Array<string>, joinChar: string) =>
array.filter(Boolean).join(joinChar);

View File

@@ -0,0 +1,31 @@
/*
Copyright 2024 Resultium LLC
This file is part of @resultium/utils.
@resultium/utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
@resultium/utils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Weakly compares two objects
*
* @remarks uses JSON.stringify()
*
* @param obj1 - first object to compare with second
* @param obj2 - second object to compare with first
* @returns true if equal
*/
export const isDeepWeakEqual = (obj1: object, obj2: object) => {
return JSON.stringify(obj1) === JSON.stringify(obj2);
};

View File

@@ -0,0 +1,33 @@
/*
Copyright 2024 Resultium LLC
This file is part of @resultium/utils.
@resultium/utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
@resultium/utils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Returns a pseudo-random number in given range
*
* @remarks
* Uses Math.random(), which is not cryptographically secure
* Returns only integers
*
* @param min - minimum acceptable value
* @param max - maximum acceptable value
* @returns number [min; max]
*/
export const randomInRange = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min)) + min;
};

View File

@@ -0,0 +1,39 @@
/*
Copyright 2024 Resultium LLC
This file is part of @resultium/utils.
@resultium/utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
@resultium/utils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Removes an object from an array by key value object.
*
* @remarks
* This method doesn't support deep comparements.
*
* @param array - array which shall be manipulated with
* @param keyValue - key-value object which defines which object shall be removed from the array
* @returns modified array
*/
export const removeFromArrayByKeyValue = <T>(
array: Array<T>,
keyValue: Partial<{ [K in keyof T]: T[K] }>,
) => {
const keys = Object.keys(keyValue) as Array<keyof T>;
return array.filter((item) => {
return !keys.every((key) => item[key] === keyValue[key]);
});
};

View File

@@ -0,0 +1,30 @@
/*
Copyright 2024 Resultium LLC
This file is part of @resultium/utils.
@resultium/utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
@resultium/utils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Removes an object property by provided key
*
* @param object - object which shall be manipulated
* @param key - key which should be used to remove a property from object
* @returns new object
*/
export const removeObjectProperty = <T>(object: T, key: keyof T) => {
delete object[key];
return object;
};

View File

@@ -0,0 +1,45 @@
/*
Copyright 2024 Resultium LLC
This file is part of @resultium/utils.
@resultium/utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
@resultium/utils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Replaces an object from an array with key value object search
*
* @remarks
* This method doesn't support deep comparements.
*
* @param array - array which shall be manipulated with
* @param keyValue - key-value object which defines which object shall be removed from the array
* @param newObject - object or values to replace within the searched object
* @returns modified array
*/
export const replaceFromArrayByKeyValue = <T>(
array: Array<T>,
keyValue: Partial<{ [K in keyof T]: T[K] }>,
newObject: Partial<{ [K in keyof T]: T[K] }>,
) => {
const keys = Object.keys(keyValue) as Array<keyof T>;
const itemIndex = array.findIndex((item) => {
return !keys.every((key) => item[key] === keyValue[key]);
});
array[itemIndex] = { ...array[itemIndex], ...newObject };
return array;
};

View File

@@ -0,0 +1,39 @@
/*
Copyright 2024 Resultium LLC
This file is part of @resultium/utils.
@resultium/utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
@resultium/utils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Splits an array into chunks
*
* @param array - array which shall be split into chunks
* @param chunkSize - size of a single chunk
* @returns array of chunks
*/
export const SplitIntoChunks = <T>(array: T[], chunkSize: number): T[][] => {
return array.reduce<T[][]>((splitArray, item, index) => {
const chunkIndex = Math.floor(index / chunkSize);
if (!splitArray[chunkIndex]) {
splitArray[chunkIndex] = [];
}
splitArray[chunkIndex]?.push(item);
return splitArray;
}, []);
};

28
src/index.ts Normal file
View File

@@ -0,0 +1,28 @@
/*
Copyright 2024 Resultium LLC
This file is part of @resultium/utils.
@resultium/utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
@resultium/utils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
export * from "./functions/capitalizeFirstChar";
export * from "./functions/classNames";
export * from "./functions/conditionalJoin";
export * from "./functions/isDeepWeakEqual";
export * from "./functions/randomInRange";
export * from "./functions/removeFromArrayByKeyValue";
export * from "./functions/removeObjectProperty";
export * from "./functions/replaceFromArrayByKeyValue";
export * from "./functions/splitIntoChuks";