Skip to content

Instantly share code, notes, and snippets.

@wiyoe
wiyoe / array-to-oject.js
Created February 26, 2018 13:20
Javascript Array to Object
const peopleArray = [
{ id: 123, name: "dave", age: 23 },
{ id: 456, name: "chris", age: 23 },
{ id: 789, name: "bob", age: 23 },
{ id: 101, name: "tom", age: 23 },
{ id: 102, name: "tim", age: 23 }
];
const arrayToObject = (array) =>
array.reduce((obj, item) => {
@wiyoe
wiyoe / tr-slug.php
Created February 23, 2018 07:25
Turkish Slug Characters
'tr' => [ /* Turkish */
'ş' => 's',
'Ş' => 'S',
'ı' => 'i',
'İ' => 'I',
'ç' => 'c',
'Ç' => 'C',
'ü' => 'u',
'Ü' => 'U',
'ö' => 'o',
@wiyoe
wiyoe / customEvent.js
Created February 20, 2018 11:36
Javascript Custom Event
document.body.addEventListener("myEventName", doSomething, false);
function doSomething(e) {
alert("Event is called: " + e.type);
}
var myEvent = new CustomEvent("myEventName");
document.body.dispatchEvent(myEvent);
@wiyoe
wiyoe / getParameters.js
Created February 20, 2018 10:38
Get Query String Parameters with JavaScript
// Assuming "?post=1234&action=edit"
var urlParams = new URLSearchParams(window.location.search);
console.log(urlParams.has('post')); // true
console.log(urlParams.get('action')); // "edit"
console.log(urlParams.getAll('action')); // ["edit"]
console.log(urlParams.toString()); // "?post=1234&action=edit"
console.log(urlParams.append('active', '1')); // "?post=1234&action=edit&active=1"
@wiyoe
wiyoe / videoType.js
Created February 20, 2018 09:03
Detect Supported Video Formats with JavaScript
function supportsVideoType(type) {
let video;
// Allow user to create shortcuts, i.e. just "webm"
let formats = {
ogg: 'video/ogg; codecs="theora"',
h264: 'video/mp4; codecs="avc1.42E01E"',
webm: 'video/webm; codecs="vp8, vorbis"',
vp9: 'video/webm; codecs="vp9"',
hls: 'application/x-mpegURL; codecs="avc1.42E01E"'
@wiyoe
wiyoe / debounce.js
Created February 20, 2018 08:59
Javascript debounce code.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
@wiyoe
wiyoe / debounce.js
Last active February 20, 2018 08:20
Javascript debounce code.
function debounce(fn, delay) {
var timer = null;
return function() {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function() {
fn.apply(context, args);
}, delay);
};
}
@wiyoe
wiyoe / parse-youtube-id.js
Created February 20, 2018 07:05
Javascript Parse Youtube ID from Url
// Parse YouTube ID from url
function _parseYouTubeId(url) {
var regex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
return url.match(regex) ? RegExp.$2 : url;
}
@wiyoe
wiyoe / checktype.js
Created February 20, 2018 06:59
Javascript Check Variable Types
// Check variable types
var _is = {
object: function(input) {
return input !== null && typeof input === 'object';
},
array: function(input) {
return input !== null && (typeof input === 'object' && input.constructor === Array);
},
number: function(input) {
return input !== null && ((typeof input === 'number' && !isNaN(input - 0)) || (typeof input === 'object' && input.constructor === Number));
@wiyoe
wiyoe / php-timezone.php
Last active February 19, 2018 11:07
PHP Timezone Examples
<?php
$items = [];
array_push($items, ["key" => "Current Date T", "value" => date('Y-m-d H:i:s T')]);
array_push($items, ["key" => "Current Date", "value" => $date = date('Y-m-d H:i:s')]);
array_push($items, ["key" => "LOCALE TIMESTAMP: ", "value" => strtotime($date)]); // true
array_push($items, ["key" => "UTC TIMESTAMP: ", "value" => strtotime($date." UTC")]); // wrong
array_push($items, ["key" => "TIME", "value" => time()]);
array_push($items, ["key" => "BD from LOCALE", "value" => strtotime("1992-01-28 02:00:00")]); // 696556800
array_push($items, ["key" => "BD from UTC 00:00:00", "value" => strtotime("1992-01-28 00:00:00 UTC")]); // 696556800