Skip to content

Instantly share code, notes, and snippets.

View smddzcy's full-sized avatar

Samed Düzçay smddzcy

View GitHub Profile
@smddzcy
smddzcy / retry-on-error.js
Created November 17, 2018 20:55
Retry a function a given number of times on error with a given interval.
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
const retry = async (fn, retryCount = 3, intervalMs = 250, originalError) => {
if (retryCount === 0) {
if (!originalError) throw new Error('Failed after 3 retries');
throw originalError;
}
try {
const res = fn();
if (res && typeof res.catch === 'function') {
@smddzcy
smddzcy / mongo-bulk-update.js
Created November 17, 2018 19:35
Mongo bulk insert createdAt fields from ObjectIDs
var collection = 'accounts';
var bulk = db[collection].initializeUnorderedBulkOp();
db[collection].find({}).forEach(row => {
const createdAt = dateFromObjectId(row._id);
bulk.find({ _id: ObjectID(row._id) }).updateOne({ $set: { createdAt } })
})
bulk.execute()
@smddzcy
smddzcy / express-error-handler.js
Created November 13, 2018 12:29
Default error handling for Express routes
['get', 'post', 'put', 'delete'].forEach((method) => {
const originalFn = app[method];
app[method] = function fn(...args) {
const routeFn = args[args.length - 1];
const firstArgs = args.slice(0, args.length - 1);
const newRouteFn = (req, res, next) => {
try {
if (routeFn.constructor.name === 'AsyncFunction') {
return routeFn(req, res, next).catch(next);
}
::-webkit-scrollbar {
width: 2px;
background-color: rgba(255,255,255,0.3);
}
::-webkit-scrollbar-thumb:vertical {
background: rgba(255,255,255,0.4);
}
@smddzcy
smddzcy / swal-remove-animations.scss
Created November 3, 2018 00:43
Remove pop animations from sweetalert modals
.swal-overlay--show-modal,
.swal-modal {
animation: none !important;
}
@smddzcy
smddzcy / react-loading.js
Created November 1, 2018 10:32
A basic loading indicator
import React from 'react';
import PropTypes from 'prop-types';
function Loading({ size, fullScreen, isGold }) {
const style = {
borderWidth: `${size / 10}px`,
width: `${size}px`,
height: `${size}px`,
};
if (fullScreen) {
import React from 'react';
import PropTypes from 'prop-types';
function Loading({ size, fullScreen, isGold }) {
const style = {
borderWidth: `${size / 10}px`,
width: `${size}px`,
height: `${size}px`,
};
if (fullScreen) {
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import Loading from '../Loading';
export default class Image extends PureComponent {
static loadedSrcs = new Set();
constructor(props, context) {
super(props, context);
this.state = { isLoaded: false, isLoading: false, error: null };
@smddzcy
smddzcy / elixirphoenix.bash
Last active February 1, 2017 09:36 — forked from likethesky/elixirphoenix.bash
Installing Elixir & the Phoenix framework with Homebrew on OS X
brew update && brew doctor # Repeat, until you've done *all* the Dr. has ordered!
brew install postgresql # You'll need postgres to do this... you may also need to 'initdb' as well. Google it.
brew install elixir
mix local.hex
createuser -d postgres # create the default 'postgres' user that Chris McCord seems to like -- I don't create mine w/a pw...
# Install the latest Phoenix. It will give us the command `mix phoenix.new`
mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez
@smddzcy
smddzcy / Solution.java
Created October 8, 2016 21:33
Very simple Graph implementation in Java
private static class Vertex {
private int uniqueLabel;
public Vertex(int uniqueLabel) {
super();
this.uniqueLabel = uniqueLabel;
}
@Override
public boolean equals(Object obj) {