Skip to content

Instantly share code, notes, and snippets.

View devhero's full-sized avatar
😁

Andrea Parisi devhero

😁
  • devhero
  • Milan
View GitHub Profile
@devhero
devhero / regex_tips
Last active October 2, 2017 10:29
regex tips
// search 2 words multiline
(?=.*\bWORD1\b).*\n*(?=.*\bWORD2\b).*
// exclude word
\b((?!word)\w)+\b
// iterate over matches and do something with group's result
var myString = "something format_abc";
var myRegexp = /(?:^|\s)format_(.*?)(?:\s|$)/g;
match = myRegexp.exec(myString);
@devhero
devhero / destructuring.js
Created September 22, 2017 09:27 — forked from mikaelbr/destructuring.js
Several demos and usages for ES6 destructuring. Runnable demos and slides about the same topic: http://git.mikaelb.net/presentations/bartjs/destructuring
// === Arrays
var [a, b] = [1, 2];
console.log(a, b);
//=> 1 2
// Use from functions, only select from pattern
var foo = () => [1, 2, 3];

Source

A simplified approach to calling APIs with redux ⏬

Redux has taken the web development by storm, and after the success of react and flux, it has made the flux philosophy more accessible through its simplified approach. Although, something has always bothered me about redux :

Why is it so complicated to call APIs?!

@devhero
devhero / webpack_proxy.js
Created September 15, 2017 13:18
webpack proxy
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'https://other-server.example.com',
pathRewrite: {'^/api' : ''}
}
}
}
}
@devhero
devhero / python_json_str.py
Created September 11, 2017 09:14
python json - the JSON object must be str, not 'bytes'
data = json.loads(request.get_data(as_text=True))
@devhero
devhero / array_index_of_object.js
Created August 30, 2017 09:53
find the index of an object in an array
var elementPos = array.map(function(x) {return x.id; }).indexOf(idYourAreLookingFor);
var objectFound = array[elementPos];
@devhero
devhero / delayed_exec.py
Created August 22, 2017 12:00
python delayed execution (like setTimeout() in javascript)
from threading import Timer
def exec_func():
print('go!')
Timer(1, exec_func).start()
print('ready?')
# prints:
@devhero
devhero / mac_os_printer_drivers.txt
Created July 10, 2017 15:41
mac os printer drivers
Apple official downloads (big files):
https://support.apple.com/downloads/macos
Gutenprint generics:
http://gimp-print.sourceforge.net/p_Supported_Printers.php
@devhero
devhero / envcontext.py
Last active February 12, 2019 20:01
A Python context manager for setting/unsetting environment variables
"""
Context manager for environment variables
Usage:
os.environ['MYVAR'] = 'oldvalue'
with EnvironmentContex(MYVAR='myvalue', MYVAR2='myvalue2'):
print os.getenv('MYVAR') # Should print myvalue.
print os.getenv('MYVAR2') # Should print myvalue2.
$('*[media="screen"],*[media="print"]').attr('media', '')
// If page doesn't have jQuery, inject it:
var el = document.createElement('script'); el.type = "text/javascript"; el.src = "http://code.jquery.com/jquery-latest.pack.js"; document.body.appendChild(el);