Skip to content

Instantly share code, notes, and snippets.

View omsharp's full-sized avatar

Omar Rwemi omsharp

View GitHub Profile
@omsharp
omsharp / GZipper.cs
Last active August 29, 2015 14:19
Simple GZip Compressing Utility.
public static class GZipper
{
private const int BUFFER_SIZE = 64 * 1024;
public static byte[] Compress(byte[] data)
{
if (data == null)
throw new ArgumentNullException("data", "data can't be null!");
byte[] result;
@omsharp
omsharp / isHappy.rkt
Last active August 29, 2015 14:19
Displays "Happy :)" if n is a happy number, otherwise displays "Unhappy :("
#lang racket
; Is n a happy number.
(define (isHappy n)
; returns i squared.
(define (square i) (* i i))
; returns the last digit in n.
(define (getLastDigit n) (modulo n 10))
; returns n with the last digit omitted.
(define (omitLastDigit n) (truncate (/ n 10)))
; returns the square of the last digit in n.
@omsharp
omsharp / RestrictedInt.cpp
Last active April 23, 2016 15:37
A C++ template for restricted int.
template<int Min, int Max> class RestrictedInt {
private:
int value;
explicit RestrictedInt(int val) : value(val) {}
public:
int GetValue() const { return value; }
template<int Value> static RestrictedInt CreateObject() {
static_assert(Value >= Min && Value <= Max, "ResctrictedInt: value is out of range!");
@omsharp
omsharp / ImportMefComponent.cs
Created May 3, 2020 07:02 — forked from madskristensen/ImportMefComponent.cs
Import MEF components from non-MEF exported classes
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.TableManager;
public class ExtensionPackage : Package
{
[Import]
private ITableManagerProvider _tableManagerProvider;
protected override void Initialize()
@omsharp
omsharp / heroku_push
Last active October 11, 2020 22:44
Throwaway Commits for Heroku
#!/bin/bash
# Temporarily test current edits on Heroku
# https://rhodesmill.org/brandon/2012/quietly-pushing-to-heroku/
set -e
git add .
git commit -m 'Heroku temporary commit'
git push heroku master --force
@omsharp
omsharp / combine.ts
Created August 24, 2021 19:57
Combinations from n arrays picking one element from each array
export const combine = (arrays: string[][]) => {
// computing the total count of all combinations
let totalCount = arrays.reduce((count, array) => count * array.length, 1);
// initialize the result array
const result = new Array(totalCount);
for (let i = 0; i < totalCount; i++) {
result[i] = '';
}
@omsharp
omsharp / apollo-customFetch.ts
Created September 18, 2021 20:10
A custom fetch function for Apollo Client to handle authentication error and refresh JWT token.
const customFetch = async (
uri: RequestInfo,
options: RequestInit | undefined
) => {
const response = await fetch(uri, options);
const bodyJson = await response.json();
if (
bodyJson &&
bodyJson.errors &&
bodyJson.errors[0] &&
@omsharp
omsharp / delay.ts
Created September 19, 2021 17:47
A delay function
function delay(delayInms) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(2);
}, delayInms);
});
}
await delay(500);
@omsharp
omsharp / ValidationErrorText.tsx
Created October 4, 2021 21:34
An example on how to extend components with props (eg: Chakra UI)
import { Text, TextProps } from '@chakra-ui/react';
//! A good example on how to extend chakra ui components
export const ValidationErrorText: React.FC<TextProps> = ({
children,
...props
}) => {
return (
<Text fontSize="sm" color="red.500" {...props}>
{children}
@omsharp
omsharp / tinymce-react-nextjs.md
Created May 19, 2022 22:45 — forked from zhangshine/tinymce-react-nextjs.md
NextJs- React - Self hosted TinyMCE
  1. Install (TinyMCE 5.x)
npm install --save tinymce @tinymce/tinymce-react copy-webpack-plugin
  1. Copy static files(tinymce skins) to public folder. Edit file next.config.js
const path = require('path');
const webpack = require('webpack');
const CopyPlugin = require('copy-webpack-plugin');