Skip to content

Instantly share code, notes, and snippets.

View sagirk's full-sized avatar

Sagir Khan sagirk

View GitHub Profile
@sagirk
sagirk / pollWithExponentialBackoff.ts
Last active May 6, 2021 06:43
Poll without hammering the server – use the exponential backoff algorithm to space out retries
/**
* A configuration object for `pollWithExponentialBackoff`
*/
interface IPollOptions {
/**
* A callback that will be run on every poll attempt
*/
executor: () => Promise<any>
/**
* A callback that will be run after each poll attempt to check if further
@sagirk
sagirk / querySelectorAllRegex.ts
Last active March 10, 2022 00:01
`querySelectorAll` with regex support
/**
* querySelectorAll with regex support.
* Adapted from: https://stackoverflow.com/a/62144522.
*
* Note: In the `attributeToSearch` parameter's absence, all attributes on all
* DOM nodes will be searched. This can get quite expensive for large DOM trees.
*
* Usage example:
* `querySelectorAllRegex(/someregex/, 'target-specific-attribute-if-needed');`
*
@sagirk
sagirk / debounceAsync.ts
Last active April 16, 2021 04:43
Debounce promise-returning & async functions
/**
* A function that emits a side effect and returns a promise.
*/
type Procedure = (...args: any[]) => Promise<void>;
/**
* `debounceAsync` debounces promise-returning & async functions.
* Adapted from Sindre's p-debounce.
* Ref: https://github.com/sindresorhus/p-debounce
*
@sagirk
sagirk / learn-react.md
Created November 18, 2018 18:23
How to become a React expert
  1. Carefully do the official [tutorial][].
  2. Read the official guide to [main concepts][].
  3. Read the official [advanced guides][].
  4. Watch [building React from scratch][] to get an idea of how React actually works (this covers the "stack" reconciler which was used in React 15 and earlier).
  5. Go through the [React "fiber" architecture][] which is the default reconciler since React 16.
  6. Go crazy, build your own projects, and stop doing React tutorials!
@sagirk
sagirk / parseTime.js
Last active August 24, 2018 03:11
Utility to manually parse a time string (in the ISO 8601 format) back to a Date object
const utilities = {
/**
* Manually parse a time string back to a Date object.
* Read the following to understand why Date.parse() cannot be relied upon:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
* https://stackoverflow.com/a/20463521
*
* @param {string} time - The time to be parsed in the ISO 8601 format.
* @returns {Date}
*/
@sagirk
sagirk / add.js
Last active July 2, 2018 04:52
An `add(...)` function that keeps going forever
/**
* Second Implementation
*
* 1. The function `bind(a, b)` will return the calling function with the first
* argument as context for, and the rest of the arguments as arguments to the
* returned function.
*
* 2. In addition to the bind call, the `valueOf` property on every function
* type is the function that is called whenever the JS runtime needs to typecast
* that function to a primitive type. The type of variable actually being
{"contents":{"editor":{"insertSpaces":false},"files":{"trimTrailingWhitespace":true,"exclude":{".git":true,".build":true,"**/.DS_Store":true,"build/**/*.js":{"when":"$(basename).ts"}},"associations":{"OSSREADME.json":"jsonc"}},"search":{"exclude":{"**/node_modules":true,"**/bower_components":true,".build/**":true,"out/**":true,"out-build/**":true,"out-vscode/**":true,"i18n/**":true,"extensions/**/out/**":true,"test/smoke/out/**":true}},"tslint":{"enable":true},"lcov":{"path":["./.build/coverage/lcov.info","./.build/coverage-single/lcov.info"],"watch":[{"pattern":"**/*.test.js","command":"${workspaceFolder}/scripts/test.sh --coverage --run ${file}","windows":{"command":"${workspaceFolder}\\scripts\\test.bat --coverage --run ${file}"}}]},"typescript":{"tsdk":"node_modules/typescript/lib","preferences":{"importModuleSpecifier":"non-relative","quoteStyle":"single"}},"npm":{"exclude":"**/extensions/**"},"emmet":{"excludeLanguages":[]},"launch":{"version":"0.1.0","configurations":[{"type":"node","request":"launch",
@sagirk
sagirk / listInstalledExtensions.js
Created June 15, 2018 14:24
Generate a list of installed extensions in Visual Studio Code and copy it to clipboard
// Credits: Kent C. Dodds https://github.com/kentcdodds
// Ref: https://github.com/kentcdodds/ama/issues/406#issuecomment-391764106
// Tip: load up the below script in a Quokka buffer to have the list just waiting in your clipboard
const {execSync, spawn} = require('child_process')
const result = execSync('code --list-extensions')
const list = String(result)
.split('\n')
@sagirk
sagirk / searchForFunctions.js
Last active May 22, 2018 11:35
Regular expression to search for functions (regular and fat arrow) in JavaScript code
/**
* Match for patterns: `function (...) {...}` and `(...) => {...}`
* (`...` could be zero or more words, digits, spaces, underscores, commas)
*/
/function\s*\(([\w\d\s_,]*)\)\s*\{([^}]*)\}|\(([\w\d\s_,]*)\)\s*=>\s*\{([^}]*)\}/gi
/**
* Match for patterns: `function (...)` and `(...) =>`
* (`...` could be zero or more words, digits, spaces, underscores, commas)
*/
@sagirk
sagirk / librarySystem.js
Last active February 4, 2019 12:55
librarySystem with dependencies
// librarySystem with dependencies
(function () {
var libraryStorage = {};
function librarySystem(libraryName, dependencies, callback) {
// If librarySystem is called in 'create' mode, store the library.
if (arguments.length === 3) {
// If the library has dependencies, fetch the dependencies first and then store the library.