Skip to content

Instantly share code, notes, and snippets.

View doronguttman's full-sized avatar
👨‍💻
codecodecodecodecode

Doron Guttman doronguttman

👨‍💻
codecodecodecodecode
View GitHub Profile
@doronguttman
doronguttman / promise.polyfill.ts
Created December 1, 2022 20:16
Promise.raceTo (race with predicate)
interface PromiseConstructor {
/**
* Creates a Promise that is resolved with the value of the first resolved Promise that matches the predicate or rejected if any of the Promises are rejected.
* @param promises An array of Promises.
* @param [predicate] Optional. A function that is called with the result of each Promise. If the function returns true, the Promise is considered to be resolved. If no predicate is provided, the first truthy resolved Promise is returned.
* @returns A new Promise which is resolved with the value of the first resolved Promise that matches the predicate.
*/
raceTo<T extends readonly unknown[] | []>(promises: T, predicate?: (result: Awaited<T[number]>) => boolean): Promise<Awaited<T[number]> | undefined>;
}
javascript:(function(){Array.from(document.querySelectorAll(`details[data-resolved=true].review-thread-component`)).forEach(({style}) => style.display = style.display === "none" ? "" : "none"); Array.from(document.querySelectorAll(`div.js-timeline-item .TimelineItem.js-comment-container`)).forEach(({style}) => style.display = style.display === "none" ? "" : "none")})()
@doronguttman
doronguttman / method-wrapper.ts
Created September 19, 2022 19:11
Wrap method with try-catch
const WithTryCatch: MethodDecorator = function <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void {
const org = descriptor.value as (...args: any[]) => any;
if (typeof org !== "function") { return; }
descriptor.value = function () {
try {
console.debug(`withTryCatch: ${String(propertyKey)} trying...`);
let result = org.apply(target, Array.from(arguments));
if (result instanceof Promise) {
return result
.then(r => {
@doronguttman
doronguttman / observable-map.ts
Last active September 8, 2022 13:33
Observable map/set (TypeScript)
import { Observer } from "./vue-helper";
class MapObserver extends Observer {
constructor(value: unknown, shallow?: boolean, mock?: boolean) {
super(value, shallow, mock);
if (!shallow && value instanceof Map) {
this.observeArray(value);
}
}
@doronguttman
doronguttman / stategy.test.ts
Last active July 13, 2022 16:31
Nest.js Strategy Pattern
import { StrategyModule } from "./strategy.module";
(async () => {
try {
const moduleRef = await Test.createTestingModule({
imports: [StrategyModule]
}).compile();
const factory = moduleRef.get(StrategyFactoryService);
factory.getStrategy("strategy-1").execute();
@doronguttman
doronguttman / override-history-state.ts
Created April 26, 2022 19:34
Find what code touches url history state
(() => {
const org = history;
const keys = ["pushState", "replaceState"];
keys.forEach(k => {
const desc = Object.getOwnPropertyDescriptor(history, k);
const value = desc.value;
delete desc.value;
delete desc.writable;
desc.get = function() {
debugger;
@doronguttman
doronguttman / Startup.cs
Last active November 29, 2021 16:29
Get First Kestrel Endpoint Address
// Type: Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature
// Assembly: Microsoft.AspNetCore.Hosting.Server.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
// using Microsoft.AspNetCore.Hosting.Server.Features;
public void Configure(IApplicationBuilder app)
{
var address = app.ServerFeatures
.Select(i => i.Value)
.OfType<IServerAddressesFeature>()
.SelectMany(f => f.Addresses)
@doronguttman
doronguttman / gist:52768cdc190d5f20c427b7a7d5cb983f
Created July 25, 2019 16:31
vue.js scaffold TypeScript class vscode snippet
{
"Vue component scaffold": {
"prefix": "vue component scaffold",
"body": [
"import { Component, Vue } from \"vue-property-decorator\";",
"",
"@Component",
"export default class ${1:${TM_FILENAME_BASE/([-]?)([a-z])([a-z]*)/${2:/upcase}${3}/g}} extends Vue {",
"\t${0}",
"}",
@doronguttman
doronguttman / vue.json
Last active July 25, 2019 15:34
multi-file vue.js scaffold vscode snippet
{
"scaffold multi-file": {
"prefix": "multi file scaffold (TS/SCSS)",
"body": [
"<template>",
"\t${0}",
"</template>",
"",
"<script lang=\"ts\" src=\"./${1:$TM_FILENAME_BASE}.ts\"></script>",
"<style lang=\"scss\" src=\"./${1:$TM_FILENAME_BASE}.scss\"></style>",