Skip to content

Instantly share code, notes, and snippets.

@itashdv
itashdv / nestedCaseInsensitiveSortWithRamda.js
Created August 27, 2021 12:29
Case insensitive sorting with ramda sortWith
import { sortWith, path, split, ascend, descend } from "ramda";
const theList = [
{
name: "C",
purchase: {
period: {
start: "2020-01-26T21: 00: 00Z",
end: "3070-10-27T21: 00: 00Z",
},
@itashdv
itashdv / mainApplicationComponent.jsx
Created June 17, 2021 20:54
Material-UI v4 Menu (override styles of the dropdown component ul & li GLOBALLY)
'use strict';
import React from "react";
import { ThemeProvider } from '@material-ui/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import theme from 'theme.js';
export default class App extends React.Component {
render() {
@itashdv
itashdv / materialUISelectUl.jsx
Created June 17, 2021 20:22
Material-UI v4 Select (override styles of the dropdown component ul & li)
'use strict';
// https://stackoverflow.com/questions/50353676/how-to-change-the-style-of-the-dropdown-element-of-the-material-ui-select-compon
import React from "react";
import { makeStyles } from '@material-ui/core/styles';
import Select from "@material-ui/core/Select";
const useStyles = makeStyles({
select: {
@itashdv
itashdv / escapeSpecialCharacters.js
Created April 4, 2021 12:27
Escaping special characters in "new RegExp" constructor
function highlightText(text, search) {
RegExp.escape = function(string) {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
console.log(new RegExp(RegExp.escape(search), "gi"));
const html = text.replace(
new RegExp(RegExp.escape(search), "gi"),
(match) => `<span class=${styles.highlight}>${match}</span>`,
);
return html;
@itashdv
itashdv / emailExtractionWorking
Created August 27, 2020 09:59
emailExtractionWorking ouputs array straightaway
// http://rubular.com/r/twBPG8HQgP
regex = /\S+[a-z0-9]@[a-z0-9\.]+/img
"hello sean@example.com how are you? do you know bob@example.com?".match(regex)
// ["sean@example.com", "bob@example.com"]
@itashdv
itashdv / emailExtraction
Created August 27, 2020 06:49
Extract all Email addresses from a string
function findEmailAddresses(StrObj) {
var separateEmailsBy = ", ";
var email = "<none>"; // if no match, use this
var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9._-]+)/gi);
if (emailsArray) {
email = "";
for (var i = 0; i < emailsArray.length; i++) {
if (i != 0) email += separateEmailsBy;
email += emailsArray[i];
}
@itashdv
itashdv / mongoPromiseUse.js
Created April 16, 2020 18:41
Using promises in Mongoose
Schema.methods.saveAsync = function () {
return new Promise((resolve, reject) => {
this.save((err) => {
if (err) return reject(err)
resolve(this)
})
})
}
import {useState, useEffect} from "react";
import axios, {AxiosResponse} from "axios";
const useAxiosFetch = (url: string, timeout?: number) => {
const [data, setData] = useState<AxiosResponse | null>(null);
const [error, setError] = useState(false);
const [errorMessage, setErrorMessage] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
@itashdv
itashdv / axios-catch-error.js
Created December 28, 2019 06:45 — forked from fgilio/axios-catch-error.js
Catch request errors with Axios
/*
* Handling Errors using async/await
* Has to be used inside an async function
*/
try {
const response = await axios.get('https://your.site/api/v1/bla/ble/bli');
// Success 🎉
console.log(response);
} catch (error) {
// Error 😨