Skip to content

Instantly share code, notes, and snippets.

@bobisme
bobisme / getDomPath.js
Created November 13, 2024 14:31
Get a selector for any given DOM element.
const getDomPath = (element) => {
const path = [];
let currentElement = element;
while (currentElement) {
let selector = currentElement.tagName.toLowerCase();
// Add id if it exists
if (currentElement.id) {
selector += `#${currentElement.id}`;
@bobisme
bobisme / race.ts
Last active August 28, 2024 15:05
Promise.race with error
(async () => {
const promiseA = new Promise((res) => setTimeout(() => res("yes"), 500));
const promiseB = new Promise((_, rej) => setTimeout(() => rej("fail"), 3000));
return await Promise.race([promiseA, promiseB]);
})()
.then((x) => console.log("val =", x))
.catch((err) => {
console.error("error here", err);
});
@bobisme
bobisme / ts-node-repl.ts
Created July 15, 2024 14:42
Set up a REPL at the end of a script with history and preview.
import * as path from "node:path";
import * as repl from "node:repl";
(async () => {
// MAIN EXECUTION/SETUP GOES HERE
const replServer = repl.start({
prompt: ">> ",
useColors: true,
@bobisme
bobisme / tracing_subscriber.rs
Created December 15, 2023 01:37
common options for tracing subscriber logging to console
fn subscriber() -> impl tracing::subscriber::Subscriber {
use tracing_subscriber::{fmt::format::FmtSpan, prelude::*};
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(
tracing_subscriber::fmt::layer()
.compact()
.with_target(false)
.without_time()
.with_span_events(FmtSpan::CLOSE),
package main
import (
"fmt"
"io"
"time"
"github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log"
"github.com/rs/zerolog/log"
require 'base64'
require 'openssl'
require 'securerandom'
require 'uri'
BASE_URL = ENV['OAUTH2_BASE_URL']
REDIRECT_URI = ENV['REDIRECT_URI']
CLIENT_ID = ENV['OAUTH2_CLIENT_ID']
code_verifier = Base64.urlsafe_encode64(SecureRandom.bytes(128), padding: false)
@bobisme
bobisme / log.rb
Created July 26, 2018 16:46
Simplifying the semantic logger formatter
#!/usr/bin/env ruby
require 'logger'
require 'semantic_logger'
class KubeFmt < SemanticLogger::Formatters::Json
# redefine the defaults for log_host and log_application to be false
def initialize(
time_format: :iso_8601, log_host: false, log_application: false,
time_key: :timestamp
request = require('superagent')
request.get('http://httpbin.org/status/200')
.then(req => console.log(`got status: ${req.status}`))
.catch(err => console.error(err))
// got status: 200
request.get('http://httpbin.org/status/500')
.then(req => console.log(`got status: ${req.status}`))
@bobisme
bobisme / 20180226225426_create_cats.rb
Created May 3, 2018 00:12
rails relationship model
class CreateCats < ActiveRecord::Migration[5.0]
def change
create_table :cats do |t|
t.string :name
t.timestamps
end
end
end
require 'rails_helper'
# class Cat < ApplicationRecord
# has_many :cat_toy
# has_many :toys, through: :cat_toy, dependent: :destroy
# end
#
# class Toy < ApplicationRecord
# end
#