Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
@@ -1300,16 +1303,25 @@ Combine characters to get a string usingjoin('').
toKebabCase
Converts a string to kebab case.
-Use replace() to add spaces before capital letters, convert toLowerCase(), then replace() underscores and spaces with hyphens.
-Also check if a string starts with a hyphen and remove it if yes.
For more detailed explanation of this Regex Visit this Site
const toKebabCase = str => {
- str = str.replace(/([A-Z])/g," $1").toLowerCase().replace(/_/g,' ').replace(/-/g,' ').replace(/\s\s+/g, ' ').replace(/\s/g,'-');
- return str.startsWith('-') ? str.slice(1,str.length) : str;
+ let regex = rx = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
+ return str.match(regex).map(x =>{
+ return x.toLowerCase();
+ }).join('-');
}
// toKebabCase("camelCase") -> 'camel-case'
// toKebabCase("some text") -> 'some-text'
// toKebabCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some-mixed-string-with-spaces-underscores-and-hyphens'
// toKebabCase("AllThe-small Things") -> "all-the-small-things"
+// toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') -> "i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html"
toSnakeCase
Converts a string to snakecase.
@@ -1479,6 +1491,19 @@ UseNumber() to check if the coercion holds.
const validateNumber = n => !isNaN(parseFloat(n)) && isFinite(n) && Number(n) == n;
// validateNumber('10') -> true
+Uncategorized
+randomHexColorCode
Generates a random hexadecimal color code.
+Use Math.random to generate a random number and limit that number to fall in between 0 and 16 using Math.floor. Use the generated random number as index to access a character from 0 to F. Append it to color till the length is not 7.
const randomHexColorCode = () => {
+ let color = '#';
+ while(color.length < 7) color += '0123456789ABCDEF'[Math.floor(Math.random() * 16)];
+ return color;
+}
+// randomHexColorCode() -> "#e34155"
+// randomHexColorCode() -> "#fd73a6"
+// randomHexColorCode() -> "#4144c6"
+