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 / 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;
}
::-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 / 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);
}
@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 / 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 / infinite-swipe-react-swipeable.js
Created December 7, 2018 13:03
Infinite swipe using react-swipeable
onColorSwipe = (e, dx, dy, absX, absY, v) => {
const h = this.colorScroller.scrollHeight;
this.setState(state => {
const top = state.colorScrollerTop + state.colorScrollerDy;
if (-top > h * 4 / 5) { // almost at the end
return { colorScrollerTop: state.colorScrollerTop + h / 3, colorScrollerDy: -dy };
}
if (-top < h / 5) { // almost at the beginning
return { colorScrollerTop: state.colorScrollerTop - h / 3, colorScrollerDy: -dy };
}
@smddzcy
smddzcy / _schema.js
Last active May 14, 2019 20:35
Mongoose-like ORM for Cosmos DB SQL API
const _ = require('lodash');
const Redis = require('ioredis');
const sqlBuilder = require('mongo-sql'); // from https://github.com/smddzcy/mongo-sql#master
const uuid = require('uuid');
const throat = require('throat');
const DbError = require('../error/dbError');
const cast = require('../cast');
const client = new CosmosClient({ endpoint: '...', key: '...' });
const database = client.database('...');
@smddzcy
smddzcy / install-node.sh
Last active August 6, 2019 00:35
Install Node LTS 10 on Centos
sudo su
curl -sL https://rpm.nodesource.com/setup_10.x | bash -
sudo yum clean all && sudo yum makecache fast
sudo yum install -y gcc-c++ make
sudo yum install -y nodejs
@smddzcy
smddzcy / gist:b29b8ebb474fc9aad494b69e38740859
Created October 20, 2019 22:34
Set IBM COS (Cloud Object Storage) bucket CORS policy
Link: https://cloud.ibm.com/docs/services/cloud-object-storage/cli?topic=cloud-object-storage-curl#curl-list-buckets
Policy to add: <CORSConfiguration><CORSRule><AllowedMethod>PUT</AllowedMethod><AllowedMethod>POST</AllowedMethod><AllowedMethod>DELETE</AllowedMethod><AllowedMethod>GET</AllowedMethod><AllowedOrigin>*</AllowedOrigin><AllowedHeader>*</AllowedHeader></CORSRule></CORSConfiguration>
@smddzcy
smddzcy / gist:4ccd5051190a9505f09c02ae3025a5c8
Created October 20, 2019 22:54
Get .crt and .key from .pfx
openssl pkcs12 -in cert.pfx -clcerts -out cert.txt -nodes
# open txt and get certificate + key