Skip to content

Instantly share code, notes, and snippets.

@waquwex
waquwex / templated_function.cpp
Created July 14, 2024 18:31
String literal and string as argument for templated function
// It needs to be converted into string to match this: const T& input
template <typename T>
typename enable_if<
is_same<typename decay<T>::type, string>::value || // type can decay into string
is_same<typename decay<T>::type, string_view>::value || // type can decay into string_view
is_convertible<T, const char*>::value>::type // const char* can be converted into the upper "same" types
printString(const T& input)
{
cout << "Printing string: " << input << endl;
@waquwex
waquwex / randomBool.cpp
Created June 6, 2024 23:45
Random Bool
bool randomBool() {
std::random_device rd;
mt19937 generator;
if (rd.entropy() == 0) { // Fallback to using time for randomness
using namespace std::chrono;
generator = mt19937(static_cast<unsigned int>(chrono::system_clock::now().time_since_epoch().count()));
} else {
generator = mt19937(rd());
}
@waquwex
waquwex / auth.http
Created June 4, 2024 22:15
.http Get Bearer token from response body and use it in another authenticated request
## Tested with VS CODE Rest Client extension
## First trigger getToken request, it will use data from getToken response body in specified requests.
### Get Token for "bob"
# @name getToken
POST http://localhost:5001/connect/token
Content-Type: application/x-www-form-urlencoded
client_id=rest_tester
@waquwex
waquwex / composeCanvasAndLayoutModifier.kt
Created April 26, 2024 23:15
Jetpack Compose Canvas and LayoutModifier
@Composable
fun MyCustomShape(color: Color) {
Canvas(modifier = Modifier
.size(100.dp)
.background(Color.Black)
) {
drawCircle(color = color, radius = 50.0f, center = center)
}
}
@waquwex
waquwex / customError.ts
Created April 23, 2024 18:47
Custom Error in TypeScript Promise
class RejectedError extends Error {
gibberishCode = 999;
constructor() {
super("REJECTED!!!");
}
}
const example = () => {
return new Promise((resolve: (value: string) => void, reject: (reason: RejectedError) => void) => {
@waquwex
waquwex / App.tsx
Created April 5, 2024 14:39
React Native Navigation
import { NavigationContainer } from "@react-navigation/native";
import GibberishScreen from "./screens/GibberishScreen";
import HomeScreen from "./screens/HomeScreen";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
export type AppStackParamList = {
Home: undefined;
Gibberish: { example: string };
}
@waquwex
waquwex / reasonableRegexPatterns.js
Created March 5, 2024 20:38
Reasonable Regex patterns, Email, Username, Password
const EMAIL_REGEX_PATTERN = /^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$/;
const USERNAME_REGEX_PATTERN = /^([A-Za-z\d._-]{5,})$/; // English letters + Numbers + . _ -
const PASSWORD_REGEX_PATTERN = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[._*-])[A-Za-z\d._*-]{8,}$/;
// Should contain at least one uppercase english letter, atl east one lowercase english letter,
// at least one number, at least one of . _ * -
@waquwex
waquwex / copyCurrentUrlToClipboard.js
Created March 1, 2024 15:55
Copy current url to clipboard
navigator.clipboard.writeText(window.location.href);
@waquwex
waquwex / wordCount.sql
Created February 26, 2024 21:14
T-SQL Get Word Count of Text Column (STRING_SPLIT)
SELECT
id,
name,
COUNT(value) AS WordCount
FROM
Peoples
CROSS APPLY
STRING_SPLIT(name, ' ')
GROUP BY
id, name
@waquwex
waquwex / SQLServerOnDocker.md
Last active February 26, 2024 15:42
SQLServer on Docker

Deploy Image to Docker

sh docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD={PW}' -p 1401:1433 -d mcr.microsoft.com/mssql/server

Copy .bak File Docker Instance

sh docker cp Footballers.bak {INSTANCE_ID}:/var/opt/mssql/data/{BACKUP_NAME}.bak