Skip to content

Instantly share code, notes, and snippets.

View alordiel's full-sized avatar
🛶
Požuri polako

a.vasilev alordiel

🛶
Požuri polako
View GitHub Profile
@alordiel
alordiel / default-function-parameters.js
Last active May 24, 2017 07:11
Passing default values to JS function
function x(a,b){
a = a || 10;
b = b || 12;
console.log(a);
console.log(b);
}
x();
// 10
@alordiel
alordiel / isJson.js
Created May 25, 2017 06:01
Short JS function for checking if a string is JSON ready
function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
@alordiel
alordiel / scrollToSelector.js
Created May 25, 2017 06:45
JS - scroll to top function that accepts selector element for offset from top
function scrollToPlace(selector,scrollTime,additionalToOffset) {
jQuery('html, body').animate({
scrollTop: jQuery(selector).offset().top - additionalToOffset
}, scrollTime);
}
@alordiel
alordiel / add_login_logout_link_to_menu.php
Created May 25, 2017 07:37
WP PHP - function to append login/logut element to the end of navigation menu
@alordiel
alordiel / array-used-in-replace.js
Created June 1, 2017 08:32
JS: using array of elements to seach and array of element for replace in standard replace function
var search = ['Á','Â','Á','Ã','É','Ê','Í','Ó','Ô','Õ','Ú','Ç','á','â','ã','é','ê','í','ó','ô','õ','ú','ç','À','à']
var replace= ['a','a','a','a','e','e','i','o','o','o','u','c','a','a','a','e','e','i','o','o','o','u','c','a','a'];
var string = 'Ele eé um homem';
for (var i = 0; search.length > i; i++) {
if (string.indexOf(search[i]) != -1) {
string = string.replace(search[i],replace[i]);
}
}
@alordiel
alordiel / get_list_of_posts.php
Last active June 6, 2017 12:02
WP PHP: build an array of post id => post title pairs by CPT
/**
* Function to query a given post type and to build array of the avialable titles. Used for select lists
* Used by:
*
* @param string $post_type Post type
* @return array $pairs Associative array with key[post id] and value[post title]
*/
function get_post_type_list_of_posts($post_type){
$pairs = array();
@alordiel
alordiel / variable-types.js
Created July 20, 2017 08:30
JS varaible types and definitions
var a;
var b = "";
var c = 0;
var d = null;
var e = [];
var f = {};
var g = false;
console.log("a: " + typeof a); // "undefined"
@alordiel
alordiel / comparing-types.js
Created July 20, 2017 08:44
JS types comparing
console.log(null == undefined); // TRUE
console.log(null === undefined); // FALSE
console.log(null == ""); // FALSE
console.log(null == 0); // FALSE
console.log(undefined == ""); // FALSE
console.log(undefined == 0); // FALSE
console.log("" == 0); // TRUE
console.log("" === 0); // FALSE
console.log(false == 0); // TRUE
console.log(false == ""); // TRUE
@alordiel
alordiel / types-conversions.js
Created July 20, 2017 09:05
JS Convering one type of variable to another type
var str = "string";
var num = 123;
var boo = false;
var obj = {h:3};
console.log(String(str)); // "string"
console.log(Number(str)); // NaN
console.log(Boolean(str)); // true
console.log(Object(str)); // "string"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
.verb-wrapper{
width:400px;
border: 1px solid green;