Skip to content

Instantly share code, notes, and snippets.

View syed-ahmad's full-sized avatar
Working

Syed Ahmad syed-ahmad

Working
View GitHub Profile

ZSH CheatSheet

This is a cheat sheet for how to perform various actions to ZSH, which can be tricky to find on the web as the syntax is not intuitive and it is generally not very well-documented.

Strings

Description Syntax
Get the length of a string ${#VARNAME}
Get a single character ${VARNAME[index]}
[alias]
s = status
co = checkout
cob = checkout -b
del = branch -D
br = branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate
save = !git add -A && git commit -m 'chore: savepoint'
undo = reset HEAD~1 --mixed
res = !git reset --hard
done = !git push origin HEAD
@syed-ahmad
syed-ahmad / generate-breadcrumbs-with-href.js
Created October 25, 2023 16:07
Generate breadcrumb href
function generateBreadcrumbs(paths) {
var breadcrumbs = [];
breadcrumbs.push({
name: 'Home',
href: '/',
});
paths.forEach((path, index) => {
let segments = [];
for(i = 0; i <= index; i++) {
@syed-ahmad
syed-ahmad / clean-json.js
Created October 25, 2023 12:24
Clean JSON
const https = require('https');
https.get('https://coderbyte.com/api/challenges/json/json-cleaning', (resp) => {
let raw = '';
const expected = {"name":{"first":"Daniel","last":"Smith"},"age":45, "items_removed": 1};
// parse json data here...
let index = 1;
resp.on('data' , (chunk) => {
raw += chunk;
@syed-ahmad
syed-ahmad / morse-code-dichotomic-search.js
Created October 6, 2023 08:23
Morse code dichotomic search.
// Determine the possible characters that could result from a certain morse code word with some potentially unknown signals (?).
// ? is either dit (.) or dah (-)
// This is incomplete, sorry.
const possibilities = signal => {
const signals = signal.split('');
console.log(`🚀`, signals);
const hasUnknownSignal = signals.includes('?');
@syed-ahmad
syed-ahmad / items.tests.js
Created March 8, 2022 17:22 — forked from tejendra/items.tests.js
How to clear RTK Query cache in tests between requests when using Mock Service Worker and Jest
import React from 'react';
import { rest } from 'msw';
import userEvent from '@testing-library/user-event';
import { render, screen } from '../testUtils';
import ItemsPage from '../pages/Items';
import server from '../mocks/server';
describe('given Items Page', () => {
test('fetches items from API on page load', async () => {
render(<ItemsPage />);
@syed-ahmad
syed-ahmad / RegExTest.cs
Created November 19, 2021 14:49 — forked from nikanos/RegExTest.cs
RegEx - Do not allow leading and trailing whitespace
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace RegExTest
{
static class MyExtensions
{
public static void PrintMatch(this Regex re, string input)
{
@syed-ahmad
syed-ahmad / getEventHandlers.js
Created November 4, 2021 15:49 — forked from jherax/getEventHandlers.js
jQuery: Gets all event handlers bound to a DOM Element
/**
* Gets all event-handlers from a DOM element.
* Events with namespace are allowed.
*
* @param {Element} node: DOM element
* @param {String} eventns: (optional) name of the event/namespace
* @return {Object}
*/
function getEventHandlers(element, eventns) {
const $ = window.jQuery;
@syed-ahmad
syed-ahmad / try-catch.ts
Last active May 6, 2021 16:19 — forked from karlhorky/try-catch.ts
Try-catch helper for promises and async/await
// Slightly improved version with destructuring working as expected
export default async function tryCatch<T>(
promise: Promise<T>
): Promise<{ error?: Error; data?: T }> {
try {
return { data: await promise };
} catch (error) {
return { error };
}
}
@syed-ahmad
syed-ahmad / jquery.centerit.js
Created March 10, 2017 17:21 — forked from tim-reynolds/jquery.centerit.js
Setting scroll to center on an element (jQuery) - Super simple stuff
/*
If you have a horizontal (or vertical) scroll container and want to set the scroll to center a specific
element in the container you can use the following super simple technique.
I'm going to show you how it was derived, because it's important to know why, not just how.
*/
/*
Setup:
[HTML]
<div class="outer">