Skip to content

Commit

Permalink
fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
victory-sokolov committed Jan 22, 2025
1 parent ed3da74 commit c822704
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 236 deletions.
118 changes: 59 additions & 59 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
* @returns Camelcased string
*/
export const camelCase = (str: string): string => {
return (
str
// Convert the string to lowercase
.toLowerCase()
// Remove all non-alphanumeric characters and spaces
.replace(/[^a-z0-9]/g, ' ')
// Capitalize the first letter of each word (after spaces)
.replace(/\s(\w)/g, (match, letter) => letter.toUpperCase())
// Remove leading spaces
.replace(/\s+/g, '')
// Return the first letter in lowercase and the rest as-is
.replace(/^(\w)/, (match, letter) => letter.toLowerCase())
);
return (
str
// Convert the string to lowercase
.toLowerCase()
// Remove all non-alphanumeric characters and spaces
.replace(/[^a-z0-9]/g, ' ')
// Capitalize the first letter of each word (after spaces)
.replace(/\s(\w)/g, (match, letter) => letter.toUpperCase())
// Remove leading spaces
.replace(/\s+/g, '')
// Return the first letter in lowercase and the rest as-is
.replace(/^(\w)/, (match, letter) => letter.toLowerCase())
);
};

/**
Expand All @@ -25,10 +25,10 @@ export const camelCase = (str: string): string => {
* @returns Pascal case string
*/
export const pascalCase = (str: string, separator = ' '): string => {
return str
.split(separator)
.map((word) => word.replace(/^\w/, (c) => c.toUpperCase()))
.join('');
return str
.split(separator)
.map((word) => word.replace(/^\w/, (c) => c.toUpperCase()))
.join('');
};

/**
Expand All @@ -37,10 +37,10 @@ export const pascalCase = (str: string, separator = ' '): string => {
* @returns KebabCase string
*/
export const kebabCase = (str: string): string => {
return str
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/[\s_]/g, '-')
.toLowerCase();
return str
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/[\s_]/g, '-')
.toLowerCase();
};

/**
Expand All @@ -49,12 +49,12 @@ export const kebabCase = (str: string): string => {
* @returns escaped HTML string
*/
export const escapeHtml = (unsafe: string): string => {
return unsafe
.replaceAll('&', '&')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;')
.replaceAll("'", '&#039;');
return unsafe
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;')
.replaceAll('\'', '&#039;');
};

/**
Expand All @@ -63,7 +63,7 @@ export const escapeHtml = (unsafe: string): string => {
* @returns string with zerowidth characters removed
*/
export const removeZeroWidthSpace = (str: string): string =>
str.replace(/[\u200B-\u200D\uFEFF]/g, '');
str.replace(/[\u200B-\u200D\uFEFF]/g, '');

/**
* Check whether string ends with any item in the array
Expand All @@ -72,7 +72,7 @@ export const removeZeroWidthSpace = (str: string): string =>
* @returns boolean
*/
export const endsWithAny = (data: string, items: string[]): boolean => {
return items.some((element) => data.endsWith(element));
return items.some((element) => data.endsWith(element));
};

/**
Expand All @@ -82,7 +82,7 @@ export const endsWithAny = (data: string, items: string[]): boolean => {
* @returns boolean
*/
export const startsWithAny = (data: string, items: string[]): boolean => {
return items.some((element) => data.startsWith(element));
return items.some((element) => data.startsWith(element));
};

/**
Expand All @@ -91,18 +91,18 @@ export const startsWithAny = (data: string, items: string[]): boolean => {
* @returns True if UUID is valid
*/
export const isValidUUID = (id: string): boolean => {
const regexExp =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
return regexExp.test(id);
const regexExp
= /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
return regexExp.test(id);
};

/**
* Generate random HEX color
* @returns HEX code
*/
export const randomHexColorCode = (): string => {
const n = (Math.random() * 0xfffff * 1000000).toString(16);
return `#${n.slice(0, 6)}`;
const n = (Math.random() * 0xFFFFF * 1000000).toString(16);
return `#${n.slice(0, 6)}`;
};

/**
Expand All @@ -112,14 +112,14 @@ export const randomHexColorCode = (): string => {
* @returns Random string
*/
export const randomStr = (len = 32, prefix = '') => {
let s = prefix;
for (let i = 0; i < len; i++) {
const rand = Math.random();
const floor = Math.floor(rand * 36);
const c = floor.toString(36);
s += floor > 9 && rand > 0.3 && rand < 0.7 ? c.toUpperCase() : c;
}
return s.substring(0, len);
let s = prefix;
for (let i = 0; i < len; i++) {
const rand = Math.random();
const floor = Math.floor(rand * 36);
const c = floor.toString(36);
s += floor > 9 && rand > 0.3 && rand < 0.7 ? c.toUpperCase() : c;
}
return s.substring(0, len);
};

/**
Expand All @@ -128,14 +128,14 @@ export const randomStr = (len = 32, prefix = '') => {
* @returns slugified text
*/
export const slugify = (text: string) => {
return text
.toString()
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w-]+/g, '') // Remove all non-word chars
.replace(/-{2,}/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
return text
.toString()
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w-]+/g, '') // Remove all non-word chars
.replace(/-{2,}/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
};

/**
Expand All @@ -144,8 +144,8 @@ export const slugify = (text: string) => {
* @returns Capitalized string
*/
export const capitalize = (str: string): string => {
if (!str || typeof str !== 'string') return str;
return str.charAt(0).toUpperCase() + str.slice(1);
if (!str || typeof str !== 'string') return str;
return str.charAt(0).toUpperCase() + str.slice(1);
};

/**
Expand All @@ -155,8 +155,8 @@ export const capitalize = (str: string): string => {
* @returns Truncated string
*/
export const truncate = (str: string, length: number) => {
if (!str || str.length <= length) return str;
return `${str.slice(0, length)}...`;
if (!str || str.length <= length) return str;
return `${str.slice(0, length)}...`;
};

/**
Expand All @@ -165,9 +165,9 @@ export const truncate = (str: string, length: number) => {
* @returns Masked string
*/
export const maskString = (str: string) => {
const firstChars = str.slice(0, 4);
const lastChars = str.slice(str.length - 4);
return `${firstChars} **** ${lastChars}`;
const firstChars = str.slice(0, 4);
const lastChars = str.slice(str.length - 4);
return `${firstChars} **** ${lastChars}`;
};

/**
Expand All @@ -176,4 +176,4 @@ export const maskString = (str: string) => {
* @returns `true` if the string contains non-alphanumeric characters, otherwise `false`.
*/
export const isAlphaNumeric = (str: string): boolean =>
/^[a-z0-9]+$/i.test(str);
/^[a-z0-9]+$/i.test(str);
Loading

0 comments on commit c822704

Please sign in to comment.