App-Cheats
view release on metacpan or search on metacpan
}
# AJAX Javascript Example (simpler)
# 'onload' is newer and a replacement for 'onreadystatechange' with the state check
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onload = function() { // <== Difference
document.getElementById("demo").innerHTML = this.responseText;
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
# Submit a form using ajax. (prevents auto reload)
function save_details(endpoint) {
console.log("POST ", endpoint);
const form = document.querySelector("form[id=details]");
const xhttp = new XMLHttpRequest();
xhttp.open("POST", endpoint);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send( $("#details").serialize() );
}
#############################################################
## Javascript - Attributes
#############################################################
# Set/Get meta data for an HTML element
form = document.querySelector("form[id=details]");
form.setAttribute('_meta', "my_meta")
form.getAttribute('_meta')
#############################################################
## Javascript - Benchmark
#############################################################
# Time a function in javascript (benchmark)
# Not really accurate
const startTime = new Date().getTime();
const endTime = new Date().getTime();
console.log("sort function took " + (endTime-startTime) + "(ms)")
# Function for benchmarking different code snippets (timing,testing,profiling)
# Simple.
function benchmark(functions,iterations=1) {
for(const code of functions){ // foreach loop in javascript
const name = code.name; // Code refence to name
console.time(name); // Timing/benchmarking function
for(let i = 1; i <= iterations; i++ )
code();
console.timeEnd(name);
}
}
# Usage
benchmark([get_head_checkbox, get_head_checkbox2]);
benchmark([get_head_checkbox, get_head_checkbox2],10000);
# Function for benchmarking different code snippets (timing,testing,profiling)
# Includes percentages and sorted.
// function benchmark() {
// // Get inputs
// let code_refs = [...arguments];
// let iterations = 1;
// const last_index = code_refs.length - 1;
//
// // Check last input
// if(typeof(code_refs[last_index]) == "number")
// iterations = code_refs.pop();
//
// // Check for tests
// if(!code_refs.length)
// return;
//
// const stats = [];
//
// // Get statistics
// for(const code of code_refs){ // foreach loop in javascript
// const t0 = performance.now(); // High precision time
// for(let i = 1; i <= iterations; i++ ) // Run a specified amount of times
// code();
// const time = (performance.now()-t0).toFixed(2);
// stats.push({name: _get_code_name(code), time: time});
// }
//
// // Sort statistics: fastest (lowest time) first
// stats.sort((a,b) => (Number(a.time) >= Number(b.time)) ? 1 : -1);
//
// // Baseline to compare with
// const longest_time = stats[stats.length-1]["time"];
//
// // Add percentage to each test set
// for(const set of stats){
// const time = set.time;
// const percent = ((longest_time-time)/time*100).toFixed(0);
// set.percent = percent;
// }
//
// // Print statistics
// console.table(stats);
// }
//
# Javascript function to convert a block of code to a name
// function _get_code_name(code) {
// const name = code.name; // Code reference to name
// let toString = code.toString(); // Code reference to name
//
// if(name)
// return name;
//
// const is_anon_function1_start = /^\s*function\(\)\s*{\s*/; // function(){ code }
// const is_anon_function1_end = /\s*}$/;
// const is_anon_function2_start = /^\s*\(\s*\)\s*=>\s*{\s*/; // () => { code }
// const is_anon_function2_end = /\s*}$/;
// const is_anon_function3_start = /^\s*\(\s*\)\s*=>\s*(?!{)/; // () => code
// const is_anon_function3_end = /\s*$/;
//
// if(toString.match(is_anon_function1_start)){ // function(){ code }
// toString = toString
// .replace(is_anon_function1_start, "")
// .replace(is_anon_function1_end, "");
// }
// else if(toString.match(is_anon_function2_start)){ // () => { code }
// toString = toString
// .replace(is_anon_function2_start, "")
// .replace(is_anon_function2_end, "");
// }
// else if(toString.match(is_anon_function3_start)){ // () => code
// toString = toString
// .replace(is_anon_function3_start, "")
// .replace(is_anon_function3_end, "");
// }
//
// return toString;
// }
//
# Usage
benchmark(myfunc, myfunc2);
benchmark(myfunc, myfunc2,10000);
benchmark(myfunc, function(){ $('#table .row :checkbox') }, () => { $('#id') },() => $('#id') ,1000)
#############################################################
## Javascript - Callbacks
#############################################################
# Simple nameless function (coderef,annonym)
# No inputs.
#
# Perl
$greet = sub{ qq(Hello bob) }
print $greet->()
#
# JS
greet = () => `Hello bob`
greet = () => { return `Hello bob` }
" O opens a new tab instead
" gb for :GBrowse
" ]] and [[ to move between commits
" . to start command-line with :Git [CURSOR] SHA Ã la fugitive
" q or gq to close
"
" Fuzzy search for files mru (most recently used).
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim',
Plug 'pbogut/fzf-mru.vim'
"
" Vim Rooter - Change to project root on file open.
Plug 'airblade/vim-rooter'
"
call plug#end()
# Install plugin in vimrc file
:PlugInstall
# Vim plugin - Fuzzy search
:File
# Installing YouCompleteMe (autocomplete for vim).
#
# Original:
apt install build-essential cmake vim-nox python3-dev
apt install mono-complete golang nodejs default-jdk npm
cd ~/.vim/bundle/YouCompleteMe
python3 install.py --all
#
# Tried:
sudo apt install build-essential cmake python3-dev
sudo apt install golang nodejs
cd ~/.vim/bundle/YouCompleteMe
python3 install.py --system-libclang --clang-completer
:YcmdRestartServer
#
# YCM does not seem to work on android.
# Use COC instead.
#############################################################
## Vim Other
#############################################################
# Join lines (Vim)
J
# Repeat last text-changing command (Vim)
.
# Undo last change (Vim)
u
# Undo all changes to line (Vim)
U
# Redo last command (Vim)
<Control> + r
# Diplay file name, number of lines (and percent) (Vim)
<Control> + g
# Type a control character in Vim
Control-V <RET>
# Convert binary to hex (Vim)
:%!xxd
# Comment a block of lines (Vim)
<Control> + v
# Scroll down with arrow keys
<Shift> + i
# Change first line to have a comment
ESC
# all other lines will have same change (comments added)
# Scroll down half a page (Vim)
<Control> + D
# Scroll up half a page (Vim)
<Control> + U
# Find documentaion for a lookahead (Vim)
:h \@
# View Vim variables (Vim)
:set
# History is not working (Vim)
# Check permissions of ~/.viminfo
# View man page (Vim,help)
:h
# Search for spaces (Vim)
/\s\+
# \+ since all are taken literally unless escaped
# If accidentally pressed <Control> + s (Vim)
# that will freeze the terminal. This is due to "flow control"
# Do this to undo the effect:
<Control> + q
#
# To permanently disable this from occurring put this in .bashrc
stty -ixon
# Stop all output to terminal/xterm (Vim)
<Control> + s
# Restart output to terminal/xterm (Vim)
<Control> + q
# ERROR: the command is not available in this version (Vim)
sudo apt-get install vim
# Compare files (Vim)
vimdiff file1 file2 # vertical split
vimdiff -o file1 file2 # horizontal split
# Open multiple files in split screens (Vim(
( run in 0.760 second using v1.01-cache-2.11-cpan-7fcb06a456a )