Skip to content

Instantly share code, notes, and snippets.

View frentsel's full-sized avatar

Frentsel Alexandr frentsel

  • frentsel
  • Ukraine
View GitHub Profile
@frentsel
frentsel / super easy router.js
Last active November 29, 2015 13:54
router.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Router js</title>
</head>
<body>
<h1 id="title"><?=!empty($_GET['page']) ? strip_tags($_GET['page']) : 'Home';?></h1>
<ul>
<li><a onclick="Router.go('/'); return false;" href="/">Home</a></li>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
@frentsel
frentsel / FileReader.js
Last active October 16, 2015 09:16
FileReader.js
<form enctype="multipart/form-data">
<input type="file" name="files" id="files" multiple onchange="File.set(this)">
</form>
var File = (function(){
var files = [],
set = function(obj){
for(var i in obj.files)
{
@frentsel
frentsel / jquery.fData.js
Last active July 27, 2018 12:04
Tiny jQuery plugin fData for works with form as a json object
$.fn.fData = function(_data) {
var $form = this;
if (!_data) {
return $form.serializeArray().reduce(function(obj, field) {
obj[field.name] = field.value;
return obj;
}, {});
}
const CustomPromise = function(callback) {
const res = (fn, ...args) => fn && callOnce(fn, args);
const rej = (fn, ...args) => fn && callOnce(fn, args);
let callOnce = (fn, args) => {
fn.apply(null, args);
callOnce = () => { };
};
@frentsel
frentsel / delay.js
Last active November 22, 2018 15:31
// Promise
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// or pseudo Promise
const delay = (time) => ({
then: (cb) => setTimeout(cb, time)
});
// usage
delay(5000).then(…);
@frentsel
frentsel / Stack.js
Last active September 7, 2018 15:21
Data structure - Stack (javascript)
const Stack = new function() {
let data = [];
this.pop = () => data.pop();
this.push = (el) => data.push(el);
this.peek = () => data[data.length - 1];
};
Stack.push(1);
@frentsel
frentsel / Queue.js
Last active September 7, 2018 15:25
Data structure - Queue (javascript)
function Queue() {
var collection = [];
this.dequeue = () => collection.shift();
this.enqueue = (el) => collection.push(el);
this.peek = () => collection[0];
this.size = () => collection.length;
this.get = () => collection.slice(0);
}
@frentsel
frentsel / BinaryTree.js
Last active November 23, 2018 12:08
Binary Tree example (Javascript)
var a = [...Array(5000000).keys()];
var chunk = (data, center) => {
const chunks = [];
while (data.length)
chunks.push(data.splice(0, center));
return chunks;
}
var treeBuild = (array, tree = []) => {
if (!array.length) return tree;
@frentsel
frentsel / recursive.function.js
Created September 11, 2018 05:39
Recursive function javascript
const data = [1, [2, [3], 4], 5];
const sum = (data) => {
return data.reduce((res, el) => {
if (el.length) {
return res + sum(el);
}
return res + el;
}, 0);
}