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
@syed-ahmad
syed-ahmad / index.html
Created March 10, 2017 15:52 — forked from d3noob/.block
Show / hide elements on mouse click with d3.js
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body { font: 12px Arial;}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
@syed-ahmad
syed-ahmad / chart.js
Last active March 10, 2017 17:19 — forked from chrtze/chart.js
Line Chart Example
var Chart = (function(window,d3) {
var svg, data, x, y, xAxis, yAxis, dim, chartWrapper, line, path, margin = {}, width, height, locator;
var breakPoint = 768;
d3.csv('data.csv', init); //load data, then initialize chart
//called once the data is loaded
function init(csv) {
@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">
@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 / 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 / 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 / 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 / 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 / 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 / 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++) {