Skip to content

Instantly share code, notes, and snippets.

@blessdyb
blessdyb / Dockerfile1
Created October 21, 2021 06:53 — forked from xeoncross/Dockerfile1
Examples of using multi-stage builds with docker and Go to reduce the final image size / attack surface.
# Sample from @citizen428 https://dev.to/citizen428/comment/6cmh
FROM golang:alpine as build
RUN apk add --no-cache ca-certificates
WORKDIR /build
ADD . .
RUN CGO_ENABLED=0 GOOS=linux \
go build -ldflags '-extldflags "-static"' -o app
FROM scratch
COPY --from=build /etc/ssl/certs/ca-certificates.crt \
@blessdyb
blessdyb / the-rafpolyfill.js
Created February 16, 2021 05:49 — forked from getify/the-rafpolyfill.js
setTimeout vs. nested-RAF
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
@blessdyb
blessdyb / python_decimal_round_fix.py
Created October 15, 2018 22:32
Using decimal package to fix rounding discrepancy issue
from decimal import Decimal, ROUND_HALF_UP
# https://docs.python.org/3/library/functions.html#round
# https://support.office.com/en-us/f1/topic/csh?HelpId=xlmain11.chm60075&NS=MACEXCEL&Version=90&Lcid=1033&UiLcid=1033&EntryPoint=True
def round_fixed(value, digit):
value_str = str(value)
value_dec = Decimal(value_str)
exp_unit = Decimal('0.1') ** digit
mini_unit = Decimal('0.1') ** (digit + 1)
if value_dec % exp_unit == (5 * mini_unit):
value_dec += mini_unit
@blessdyb
blessdyb / JS_data_structure.js
Last active September 11, 2018 05:21
JS Data Structure
class Link {
constructor(data) {
this.next = null;
this.data = data;
}
}
class Stack {
constructor(data) {
this.top = new Link();
data.forEach(this.push.bind(this));
@blessdyb
blessdyb / combination_list.py
Created May 12, 2017 13:42
二维不定长数组全排列
import numpy as np
def search(A):
nums = len(A)
max = []
cur = []
for i in range(nums):
max.append(len(A[i]))
cur.append(0)
@blessdyb
blessdyb / RxJS.test.js
Created March 23, 2017 23:33
RxJS Test
const Rx = require('rxjs/Rx');
const request = require('request');
//Convert node method to Rx observable
const requestGet$ = function(url) {
let requestGetObservable = Rx.Observable.bindNodeCallback(request);
return requestGetObservable(url)
.do(x => console.log('requestGet$ is called'))
.map(x => x[1])
.map(x => JSON.parse(x))
@blessdyb
blessdyb / money_replace.js
Created February 8, 2017 22:27
Replace money from string
function replaceMoneyInString(disclaimer, new_price) {
const moneyRegexp = /\d+((,|'|\s)\d{3})*(\.\d*)?/;
let matches = disclaimer.match(moneyRegexp);
if (matches.length) {
let old_price_string = matches[0];
let splitter = old_price_string.replace(/\d/g, '').trim();
if (splitter.length === 1) {
let new_price_string = new_price.toString().replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, '$1' + splitter);
return disclaimer.replace(old_price_string, new_price_string);
@blessdyb
blessdyb / webpack.config.js
Last active February 12, 2017 08:08
Webpack2 React Integration
var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
bundle: './src/index.js',
vendor: [
'faker', 'lodash', 'redux',
'react-redux', 'react-dom', 'react-input-range',
@blessdyb
blessdyb / double_array_without_built_in_helpers.js
Created February 2, 2017 07:47
Double all elements of array without array helper methods
const numbers = [1, 2, 3];
function double(num) {
let [firstNumber, ...restNumbers] = num;
if (firstNumber) {
return [2 * firstNumber, ...(double(restNumbers))];
} else {
return [];
}
}
@blessdyb
blessdyb / php-unserialize_utf8_fix.php
Created September 13, 2016 06:41
PHP unserialize utf8 fix
<?php
/**
* Mulit-byte Unserialize (http://stackoverflow.com/questions/2853454/php-unserialize-fails-with-non-encoded-characters)
*
* UTF-8 will screw up a serialized string
*
* @access private
* @param string
* @return string
*/