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 / simulateNativeLink.js
Created June 30, 2020 16:08
Simulate native link click programmatically in React.js
@smitroshin
smitroshin / debouncedOnChange.js
Created June 22, 2020 12:00
Using throttle and debounce in a React function component
/**
* Source: https://medium.com/@rajeshnaroth/using-throttle-and-debounce-in-a-react-function-component-5489fc3461b3
*/
const Search = () => {
const [userQuery, setUserQuery] = useState("");
const delayedQuery = useCallback(_.debounce(q => sendQuery(q), 500), []);
// alternative
// const delayedQuery = useRef(_.debounce(q => sendQuery(q), 500)).current;
const onChange = e => {
setUserQuery(e.target.value);
@smitroshin
smitroshin / isInViewport.js
Last active May 24, 2020 13:42
Check is element is visible in viewport
/**
* Source: https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/
* ! IS BUGGED: In some cases window.innerHeight is bigger than bounding.bottom
* even if element is fully visible.
* TODO: Needs to be improved.
*/
const isInViewport = (elem) => {
const bounding = elem.getBoundingClientRect();
return (
bounding.top >= 0 &&
@smitroshin
smitroshin / longPress.js
Created May 15, 2020 14:12
React - long press
class App extends Component {
constructor() {
super()
this.handleButtonPress = this.handleButtonPress.bind(this)
this.handleButtonRelease = this.handleButtonRelease.bind(this)
}
handleButtonPress () {
this.buttonPressTimer = setTimeout(() => alert('long press activated'), 1500);
}
@smitroshin
smitroshin / parseJwt.js
Created May 3, 2020 13:20
Decode JWT token
/**
* Decode JWT token
*
* @param {string} token
* @return {object}
*/
const parseJwt = (token) => {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(
@smitroshin
smitroshin / viewportSizes.js
Created April 26, 2020 22:07
Size of browser viewport
const vw = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
const vh = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
@smitroshin
smitroshin / countdownTimer.html
Created March 17, 2020 09:09
Countdown Timer
<!-- Display the countdown timer in an element -->
<!--Source: https://www.w3schools.com/howto/howto_js_countdown.asp -->
<p id="demo"></p>
<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
@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');
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 / 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;
};