feat: initial commit
This commit is contained in:
28
src/functions/capitalizeFirstChar.ts
Normal file
28
src/functions/capitalizeFirstChar.ts
Normal 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)}`;
|
||||
};
|
||||
27
src/functions/classNames.ts
Normal file
27
src/functions/classNames.ts
Normal 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(" ");
|
||||
28
src/functions/conditionalJoin.ts
Normal file
28
src/functions/conditionalJoin.ts
Normal 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);
|
||||
31
src/functions/isDeepWeakEqual.ts
Normal file
31
src/functions/isDeepWeakEqual.ts
Normal 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);
|
||||
};
|
||||
33
src/functions/randomInRange.ts
Normal file
33
src/functions/randomInRange.ts
Normal 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;
|
||||
};
|
||||
39
src/functions/removeFromArrayByKeyValue.ts
Normal file
39
src/functions/removeFromArrayByKeyValue.ts
Normal 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]);
|
||||
});
|
||||
};
|
||||
30
src/functions/removeObjectProperty.ts
Normal file
30
src/functions/removeObjectProperty.ts
Normal 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;
|
||||
};
|
||||
45
src/functions/replaceFromArrayByKeyValue.ts
Normal file
45
src/functions/replaceFromArrayByKeyValue.ts
Normal 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;
|
||||
};
|
||||
39
src/functions/splitIntoChuks.ts
Normal file
39
src/functions/splitIntoChuks.ts
Normal 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
28
src/index.ts
Normal 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";
|
||||
Reference in New Issue
Block a user