Skip to content

Instantly share code, notes, and snippets.

View AkatQuas's full-sized avatar
🔭
from book to code

Akat AkatQuas

🔭
from book to code
View GitHub Profile
@AkatQuas
AkatQuas / event-loop.md
Last active June 28, 2022 13:32
Depth in Event Loop, both browser and Node.js.

Depth in Event Loop

For those JavaScript programmers, event loop is an important concept, inevitably.

Literally, event loop is what JavaScritp uses to implement non-blocking execution. Understanding how the event loops works internally would benefit you a lot when programming in JavaScript.

There are two major environments JavaScript runs in: browser and Node.js.

Browser

@AkatQuas
AkatQuas / scope.js
Last active December 10, 2019 08:39
what is the value of `a` ?
debugger;
var a;
if (true) {
a = {a: 2};
function a() {}
a.ot = '9';
a = {a: 4};
console.log('1', a);
@AkatQuas
AkatQuas / promise-interception.js
Last active November 30, 2019 11:59
Tricky code to intercept a promise. Awesome!
const NoopPromise = {
then: () => NoopPromise,
catch: () => NoopPromise,
};
Promise.resolve().then(() => {
const number = Math.random();
if (number > 0.5) {
return number;
}
@AkatQuas
AkatQuas / insert-space.js
Created November 1, 2019 06:07
Adding zero-space width character to the text (string)
/**
The zero-width space character is encoded in Unicode as U+200B ZERO WIDTH SPACE (HTML ​)
*/
function insertSpaces(string){
return Array.from(string).join('\u200b');
}
// see more at https://en.wikipedia.org/wiki/Zero-width_space
@AkatQuas
AkatQuas / io-with-terminal.js
Last active October 28, 2019 01:56
simple lines for read from/print to terminal
process.stdin.resume();
process.stdin.setEncoding('ascii'); // 'utf-8' also supported
let _input = "";
process.stdin.on('data', function (data) {
_input += data;
});
process.stdin.on('end', function () {
@AkatQuas
AkatQuas / walk.js
Created September 24, 2019 11:10
walk through a directory recursively, return all the files, filter supported
const fse = require('fs-extra');
const path = require('path');
const isNotDot = (filepath) => {
const REG = /^\..+/i;
return !REG.test(filepath);
};
/**
* calculate the relative path from `start` to `end`
@AkatQuas
AkatQuas / mute-body-scroll.js
Created August 17, 2019 03:02
mute the body scrolling when modal is poped
let nowTop = 0;
const bodyEl = document.body;
export default mute => {
if (mute) {
nowTop = window.scrollY;
bodyEl.style.position = 'fixed';
bodyEl.style.top = -nowTop + 'px';
} else {
@AkatQuas
AkatQuas / reading-collection.md
Last active April 10, 2020 06:22
The collection of useful readings, including articles, videos, tutorials, libraries, etc.
@AkatQuas
AkatQuas / App-createContext-example.jsx
Last active August 15, 2019 08:34
A brief example for using `useContext`, `useReducer` to create global state without libraries.
import React, { createContext, useReducer, useContext, Fragment } from 'react';
import './App.css';
const authReducer = (state, action) => {
switch (action.type) {
case 'increment':
return {
...state,
age: state.age + 1
};