Skip to content

Instantly share code, notes, and snippets.

@oliverbenns
oliverbenns / promo.ts
Last active December 3, 2022 22:00
Generate ecommerce promo code with Typescript
// Don't use characters that look similar in certain fonts, I/L/1, 0/O, B/8 etc.
const charSet = "ACDEFGHJKMNPQRSTUVWXYZ2345679";
const delimiter = '-'
interface GeneratePromoCodeParams {
len: number;
}
export const generatePromoCode = (params: GeneratePromoCodeParams) => {
let result = "";
@oliverbenns
oliverbenns / clean.sh
Last active February 7, 2020 11:24
Hard clean react native
#!/bin/bash
# This script does a hard clean of caches and dependencies. It can be
# particularly useful when switching between branches with different
# native module packages or react native versions. It may take awhile to run.
printf "Cleaning cache\n"
rm -rf $TMPDIR/react-native-packager-cache-*;
rm -rf $TMPDIR/metro-bundler-cache-*;
@oliverbenns
oliverbenns / birthday.go
Last active November 1, 2019 05:08
Birthday Problem in Go
package main
import "fmt"
import "math"
import "math/big"
func bigFactorial(n int64) *big.Int {
if (n > 0) {
x := bigFactorial(n - 1)
m := big.NewInt(n)
@oliverbenns
oliverbenns / reducer.ts
Last active September 25, 2018 12:31
Reducer issue
import { AnyAction, Reducer } from 'redux'
type UserState = Readonly<{
id: number
}>
export const initialState: UserState = {
id: -1
}
@oliverbenns
oliverbenns / visible.js
Created November 20, 2017 08:31
Hide/Show HOC
import React from 'react';
const visible = isVisible => Component => {
class VisibleComponent extends React.Component {
componentDidMount() {
this.unsubscribe = this.context.store.subscribe(this.handleChange.bind(this));
}
componentWillUnmount() {
this.unsubscribe();
@oliverbenns
oliverbenns / csv.js
Last active November 2, 2017 22:09
Js to Csv
// Usage:
// const records = [['James', 'james@james.com'], ['Steve', 'steve@steve.com']];
// const titles = ['name', 'email'];
// generateCsv(records, titles);
// RFC 4180
// Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.
const formatValue = value => {
const chars = [',', '"', '\n'];
const mustBeEnclosed = chars.some(c => value.includes(c));
@oliverbenns
oliverbenns / index.js
Created June 14, 2017 12:53
Level averaging
const attempts = [
[ 1.613, 4.2010000000000005, 13.652000000000001, 7.035, 5.371, 9.951, 8.934000000000001, 5.534, 4.251, 7.636, 15.402000000000001, 6.384 ],
[ 1.401, 5.051, 11.852, 5.534, 3.384, 4.95, 6.501, 7.1850000000000005, 5.702, 5.401, 10.685, 5.867 ],
[ 1.349, 3.984, 9.401, 2.983, 4.0840000000000005, 5.05, 6.939, 5.7170000000000005, 3.517, 3.384, 9.185, 6.417 ],
];
const levels = [];
for (let i = 0; i < attempts[0].length; i++) {
@oliverbenns
oliverbenns / index.js
Last active May 15, 2017 02:13
Google Map Area (Square) Calculator
const lngRadius = 0.25;
const point = {
lat: 51.507351,
lng: -0.127758,
};
const latmin = point.lat - lngRadius;
const latmax = point.lat + lngRadius;
const lngmin = point.lng - (lngRadius / 85 * 180); // Number of units different from lng so need to calculate
@oliverbenns
oliverbenns / index.js
Created May 12, 2017 06:51
Horizontal + vertical square counter
const countHoriz = 24;
// 16, 24
const width = 960;
const height = 600;
const squareSize = width / countHoriz;
const countVert = height / squareSize
@oliverbenns
oliverbenns / jsx-comment.sublime-snippet
Created May 9, 2017 04:45
JSX Comment Snipper for Sublime text
<snippet>
<content><![CDATA[
{/* ${1:JSX Comment} */}
]]></content>
<!-- Hit `jc` in any JS document to get a JSX comment -->
<tabTrigger>jc</tabTrigger>
<scope>source.js</scope>
</snippet>