Skip to content

Instantly share code, notes, and snippets.

View farskid's full-sized avatar
🎯
Focusing

Farzad Yousefzadeh farskid

🎯
Focusing
View GitHub Profile
@farskid
farskid / gitzip.sh
Created June 1, 2021 13:54 — forked from LeonardoCardoso/gitzip.sh
Zip folder ignoring files listed on .gitignore
#...
function gitzip() {
git archive -o $@.zip HEAD
}
#... gitzip ZIPPED_FILE_NAME
@farskid
farskid / index.js
Last active April 15, 2021 10:20
Conventional subscription API for DOM events. Subscriber returns an unsubscribe callback that once called, will detach the event handler.
// ...args: [event, handler, capture]
function onEvent(element, ...args) {
element.addEventListener(...args);
return () => {
element.removeEventListener(...args);
}
}
const form = $('#form');
const detachSubmit = onEvent(form, 'submit', submitForm});
@farskid
farskid / log.js
Created February 19, 2021 16:57
Programmatically assign log levels to chalk colorful methods
const chalk = require("chalk");
const logLevelToChalkColors = {
error: chalk.red,
log: chalk.white,
info: chalk.blueBright,
};
// Programmatically generate log methods based on the level to color config above ^
const logger = Object.entries(logLevelToChalkColors).reduce(
@farskid
farskid / what-forces-layout.md
Created December 14, 2020 12:30 — forked from paulirish/what-forces-layout.md
What forces layout/reflow. The comprehensive list.

What forces layout / reflow

All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or layout thrashing, and is common performance bottleneck.

Generally, all APIs that synchronously provide layout metrics will trigger forced reflow / layout. Read on for additional cases and details.

Element APIs

Getting box metrics
  • elem.offsetLeft, elem.offsetTop, elem.offsetWidth, elem.offsetHeight, elem.offsetParent
@farskid
farskid / README.md
Last active December 4, 2020 20:43 — forked from kentcdodds/README.md
user-package-stats

user-package-stats

I was poking around trying to figure out all the packages I have access to publish and got curious. So I write this little script to determine the download stats for all the packages I have publish access to.

Feel free to try it yourself. Just change the username passed to getUserDownloadStats.

By default, the stats are sorted by their average daily downloads (descending). That should give you an idea of the most "popular" package of a given user relative to how long that package has been around.

You can use it with npx like so:

@farskid
farskid / sum.js
Created November 17, 2020 11:58
overloading Javascript functions, unlimited sum
function unlimitedAdd(...nums) {
const func = unlimitedAdd;
func.sum = nums.reduce((total, current) => total + current, 0) + (func.sum || 0);
func.done = function() {
return func.sum;
}
return func;
}
@farskid
farskid / get.js
Created November 15, 2020 14:39
Lodash get in pure Javascript
function get(obj, path) {
const steps = path.split('.');
return steps.reduce((result, step) => {
const isArrayItem = /\[\d+\]/.test(step);
if (isArrayItem) {
const [_, prop, index] = step.match(/^(.+)\[(\d+)\]$/)
return get(result, `${prop}.${index}`);
@farskid
farskid / machine.js
Created April 25, 2020 20:18
Generated by XState Viz: https://xstate.js.org/viz
function applyInsertModeStyles() {
console.log("NOT SERIALIZED, WILL GET CALLED");
showmode.textContent = "-- INSERT --";
cursor.style.width = "2.5px";
}
const modeMachine = Machine({
id: "input-mode",
initial: "normal",
context: {
@farskid
farskid / quick-argument-parser.js
Created March 16, 2020 11:13 — forked from binoculars/quick-argument-parser.js
Node.js quick CLI arguments parser for key-value pair arguments
/**
* Takes arguments in the form of --arg-name value and puts them into an object (argMap).
* Argument keys begin with double hyphens and additional hyphens are converted to camelCase.
*
* E.g. `node quick-argument-parser.js --test-arg1 'value1' --test-arg2 'value2'` will create argMap with the value:
* ```
* {
* testArg1: 'value1',
* testArg2: 'value2'
* }
@farskid
farskid / index.txt
Last active February 4, 2020 13:16
Useful git tips, aliases and configs
# Set upstream automatically on `git push`
`git config --global push.default current`
## The reference to the last branch is saved into `-` in git.
# Checkout to the previous branch
`git checkout -`
# Merge into the last branch
`git merge -`