Skip to content

Instantly share code, notes, and snippets.

View smitroshin's full-sized avatar

Serghei Mitroșin smitroshin

  • Amdaris
  • Chisinau, Moldova
View GitHub Profile
@smitroshin
smitroshin / countries.json
Created July 18, 2018 15:21
Countries - name, dial_code, code
[
{
"name": "Afghanistan",
"dial_code": "+93",
"code": "AF"
},
{
"name": "Aland Islands",
"dial_code": "+358",
"code": "AX"
@smitroshin
smitroshin / removeCharFromString.js
Last active September 11, 2018 13:53
Removing character from string
function removeCharacter(str, charPos) {
part1 = str.substring(0, charPos);
part2 = str.substring(charPos + 1, str.length);
return `${part1}${part2}`;
}
export default removeCharacter;
// console.log(remove_character("Python",3));
@smitroshin
smitroshin / getNumFromString.js
Created July 24, 2018 13:34
Get array with numbers from string
function getNumFromString(string) {
const numbers = string.match(/\d+/g).map(Number);
return numbers;
}
export default getNumFromString;
// string = "border-radius:10px 20px 30px 40px"
// console.warn(getNumFromString(string));
@smitroshin
smitroshin / extractDomainFromUrl.js
Created September 11, 2018 13:50
Extract the domain name from url string
const extractHostname = (url) => {
let hostname = '';
// find & remove protocol (http, ftp, etc.) and get hostname
if (url.indexOf('//') > -1) hostname = url.split('/')[2];
else hostname = url.split('/')[0];
// find & remove port number
hostname = hostname.split(':')[0];
// find & remove "?"
@smitroshin
smitroshin / urlify.js
Created November 5, 2018 12:58
urlify
function urlify(text) {
var urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, function(url) {
return '<a href="' + url + '">' + url + '</a>';
})
}
var text = "Find me at http://www.example.com and also at http://stackoverflow.com";
var html = urlify(text);
@smitroshin
smitroshin / outsideClick.js
Last active April 8, 2019 11:43
Click outside in React.js
class Popover extends React.Component {
constructor() {
super();
this.handleClick = this.handleClick.bind(this);
this.handleOutsideClick = this.handleOutsideClick.bind(this);
this.state = {
popupVisible: false
};
const urlString = window.location.href;
const url = new URL(urlString);
const urlParamLang = url.searchParams.get('param');
@smitroshin
smitroshin / fillArrayWithConcat.js
Last active July 12, 2019 14:43
Fill array with one value 'n' times
// Fill array with value, len times
// Using concat
fillArray = (value, len) => {
if (len === 0) return [];
let a = [value];
while (a.length * 2 <= len) a = a.concat(a);
if (a.length < len) a = a.concat(a.slice(0, len - a.length));
return a;
};
const toBase64 = file => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
// toBase64(file).then(file => console.log(file));
@smitroshin
smitroshin / bootstrap.js
Created February 4, 2020 09:06
Sequence of code from Laravel
/**
* We'll load jQuery and the Bootstrap jQuery plugin which provides support
* for JavaScript based Bootstrap features such as modals and tabs. This
* code may be modified to fit the specific needs of your application.
*/
try {
window.Popper = require('popper.js').default;
window.$ = window.jQuery = require('jquery');
require('bootstrap');