Skip to content

Instantly share code, notes, and snippets.

View basarat's full-sized avatar
🌹
youtube.com/basaratali

Basarat Ali Syed basarat

🌹
youtube.com/basaratali
View GitHub Profile
@basarat
basarat / md5.ts
Created November 2, 2017 00:43
Create md5 using TypeScript / JavaScript / NodeJS
import * as crypto from 'crypto';
export const md5 = (contents: string) => crypto.createHash('md5').update(contents).digest("hex");
@basarat
basarat / mixin.ts
Last active August 18, 2023 20:29
export type Class = new (...args: any[]) => any;
export function DisposableMixin<Base extends Class>(base: Base) {
return class extends base {
isDisposed: boolean = false;
dispose() {
this.isDisposed = true;
}
};
}
@basarat
basarat / script.md
Created March 13, 2023 06:02
Promise Fulfilled vs Resolved

Notice that we’ve been using the term, fulfilled instead of resolved, and the reason is that a promise can be resolved to another promise and which point its fate becomes dependent on the other promise. If the other promise gets fulfilled our promise gets fulfilled, if the other promise is rejected, our promise gets rejected.

Both alpha and beta are resolved, but they settle to different fates. Alpha gets fulfilled, Beta gets rejected.

const delayFulfill =
  () => new Promise(
    res => setTimeout(() => res('fulfilled'), 1000)
 );
@basarat
basarat / cardinaldirections.js
Last active November 14, 2022 17:44
Snapping an angle to the nearest cardinal direction in javascript
/**
* Given "0-360" returns the nearest cardinal direction "N/NE/E/SE/S/SW/W/NW"
*/
export function getCardinal(angle) {
/**
* Customize by changing the number of directions you have
* We have 8
*/
const degreePerDirection = 360 / 8;
@basarat
basarat / _app.tsx
Last active April 6, 2022 19:27
nextjs-typestyle 🌹
import App from 'next/app'
import { setStylesTarget } from 'typestyle'
export default class MyApp extends App {
componentDidMount() {
/**
* Hydrate typestyle
*/
setStylesTarget(document.getElementById('styles-target')!)
}
@basarat
basarat / demo.ts
Last active February 21, 2022 22:02
import { add } from 'date-fns';
/** Add one day to a given input */
function tomorrow(date: Date) {
return add(date, { days: 1 });
}
const now = new Date('2022-04-03 00:00:00');
const next = tomorrow(now);
console.log(now.toLocaleString(), '--', next.toLocaleString());
type DeepReadonly<T> = {
readonly [P in keyof T]
: DeepReadonly<T[P]>;
}
// create react app
npx create-react-app my-app --template typescript
cd my-react-app
// install
npm i cypress cypress-react-unit-test
// tsconfig.json
"types": [
@basarat
basarat / form.tsx
Created October 6, 2016 05:20
React forms that don't reload page
import * as React from 'react';
interface FormProps {
/**
* Automatically prevents the default browser behavior so you don't have to
*/
onSubmit: () => any;
children?: JSX.Element;
}
export const Form = ({ onSubmit, children}: FormProps) =>
@basarat
basarat / LICENCE
Created December 24, 2020 02:24
This license applies to all public gists https://gist.github.com/basarat
MIT License
Copyright (c) 2020 Basarat Ali Syed
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: