Skip to content

Instantly share code, notes, and snippets.

package com.example;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import SwiftUI
let identifiers = NSLocale.availableLocaleIdentifiers
let locale = NSLocale(localeIdentifier: "en_US")
let currencyFormatter = NumberFormatter()
currencyFormatter.numberStyle = .currency
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.maximumFractionDigits = 2
@ondrej-kvasnovsky
ondrej-kvasnovsky / HttpError.ts
Last active October 23, 2020 16:56
NodeJS proxy/gateway using routing-controllers & axios, checking role permissions using Secured annotation
import { HttpError as HE } from 'routing-controllers';
export class HttpError extends HE {
constructor(httpCode: number, message?: string, readonly data?: any) {
super(httpCode, message);
Object.setPrototypeOf(this, HttpError.prototype);
}
toJSON() {
@ondrej-kvasnovsky
ondrej-kvasnovsky / cosineSimilarity.ts
Created September 14, 2020 21:40
Cosine Similarity
function cosineSimilarity(a: number[], b: number[]) {
let dotProduct = 0;
let magnitudeA = 0;
let magnitudeB = 0;
for (let i = 0; i < a.length; i++) {
dotProduct += (a[i] * b[i]);
magnitudeA += (a[i] * a[i]);
magnitudeB += (b[i] * b[i]);
}
magnitudeA = Math.sqrt(magnitudeA);
@ondrej-kvasnovsky
ondrej-kvasnovsky / groups.js
Created July 7, 2020 20:14
JavaScript get Regex groups
const input = 'hi:ha:ho';
const matches = input.match(/(.*):(.*):(.*)/);
const firstGroup = matches[1];
const secondGroup = matches[2];
const thirdGroup = matches[3];
console.log(firstGroup, secondGroup, thirdGroup);
// prints out: hi ha ho
@ondrej-kvasnovsky
ondrej-kvasnovsky / encrypt.py
Created February 18, 2020 00:03
Encrypt string using NaCl in Python
import base64
payload = 'VKlNGb4mCR/5BBunW4d7jpwp/KI1pRcsW/DeuMzXThh+nz8HRMI54bmWLKjC3haJuBGZQmX/SfNIACU='
payloadEncoded = payload.encode("UTF-8")
payloadArray = base64.b64decode(payload)
nonceArray = payloadArray[0:24]
msgArray = payloadArray[24:len(payloadArray)]
from nacl.secret import SecretBox
@ondrej-kvasnovsky
ondrej-kvasnovsky / gist:10213c32a50c2bcf8d6a3612adc0524e
Created November 26, 2019 02:47
How to test successful promise and rejected when a promis fails (async await)
async function doSomething() {
return 'some result'
}
async function doSomethingAndFail() {
throw new Error()
}
describe('promise testing', () => {
@ondrej-kvasnovsky
ondrej-kvasnovsky / structjson.js
Created January 24, 2019 12:11
Struct to JSON in JavaScript
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@ondrej-kvasnovsky
ondrej-kvasnovsky / update.sh
Created December 28, 2018 17:23
Pull latest changes from all git repositories in a directory and call npm install for each project
#!/bin/bash
CUR_DIR=$(pwd)
echo "\n\033[1mPulling in latest changes for all repositories in $CUR_DIR directory\033[0m\n"
for i in $(find . -name ".git" | cut -c 3-); do
echo "";
echo "\033[33m"+$i+"\033[0m";
cd "$i";
@ondrej-kvasnovsky
ondrej-kvasnovsky / search.sh
Created November 21, 2018 16:52
Search all files in give directory
#! /bin/sh -
# that also works in any sh, so you don't even need to have or use bash
file_pattern="/Users/ondrej/Downloads/emails/*.dms"
# all uppercase variables should be reserved for environment variables
IFS='' # disable splitting
for f in $file_pattern # here we're not quoting the variable so
# we're invoking the split+glob operator.