Skip to content

Instantly share code, notes, and snippets.

@zwelden
zwelden / python_venv_command.sh
Created September 10, 2020 00:52
Python venv commands
$ python -m venv venv # create virtual environment
$ source venv/bin/active # active venv
$ deactive # deactive venv
@zwelden
zwelden / js_str_hex_dump.js
Created October 6, 2018 15:48
js string hex dump
function unicodeEscape(str) {
return str.replace(/[\s\S]/g, function(character) {
var escape = character.charCodeAt().toString(16),
longhand = escape.length > 2;
return '\\' + (longhand ? 'u' : 'x') + ('0000' + escape).slice(longhand ? -4 : -2);
});
}
@zwelden
zwelden / autoloader.php
Last active July 13, 2018 19:39
PSR-4 autoloader
<?php
// from: https://jeremycurny.com/2016/06/30/php-psr-4-autoloader/
spl_autoload_register(function ($class) {
$file = DIR_TO_CLASS_FILES . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require $file;
}
});
@zwelden
zwelden / find_string_in_file_by_file_type.sh
Last active April 29, 2019 16:46
bash: find string in file of a given type
$ find <file path> -name *.<file extention> -exec grep --color=always '<search term>' {} \; -printf '\033[32m%p\033[0m\n
# find /data/.../.../.../ -name *.php -exec grep --color=always 'something' {} \; -printf '\033[32m%p\033[0m\n
# super charged version
$ find /data/.../.../ \( -name '*.php' -o -name '*.js' \) -exec grep --color=always 'something' {} \; -printf '\033[32m%p\033[0m\n'
@zwelden
zwelden / email-validator.js
Created November 2, 2017 19:51
email validation with TLD
var isEmailWithTLD = function (email){
return /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*(\.\w{2,})+$/.test(email);
};
// Example
isEmailWithTLD('some@example'); // false
isEmailWithTLD('some@example.com'); // true
@zwelden
zwelden / universal-modual-loader-no-dep.js
Created September 25, 2017 02:46
universal js module loader w/o dependencies
;(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.YourModule = factory();
}
@zwelden
zwelden / universal-modual-loader-w-dep.js
Created September 25, 2017 02:45
universal js module loader -- with dependencies
;(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('jquery'));
} else {
root.YourModule = factory(root.jQuery);
}
@zwelden
zwelden / revealing-module-v3.js
Created September 22, 2017 14:57
Revealing Module v3
var myApplication = function(){
var name = 'Chris';
var age = '34';
var status = 'single';
function createMember(){
// [...]
}
function getMemberDetails(){
// [...]
}
@zwelden
zwelden / revealing-module-v2.js
Created September 22, 2017 14:55
Revealing Module v2
var App = (function() {
'use strict';
var settings;
return {
init: function(initialSettings) {
settings = initialSettings;
},
@zwelden
zwelden / revealing-module-v1.js
Created September 22, 2017 14:55
Revealing module
var module = (function (window, document, $) {
// private variables and functions
var _private = 'bar';
// constructor
var module = function () {
console.log('init module');
};
// prototype