Skip to content

Instantly share code, notes, and snippets.

View frydlewicz's full-sized avatar

Kamil Frydlewicz frydlewicz

View GitHub Profile
@frydlewicz
frydlewicz / objectHelper.ts
Last active July 26, 2020 08:14
TypeScript methods cloning and comparing objects using WeakMap and WeakSet
export interface IObject {
[key: string]: any;
}
export function cloneObject(source: IObject, maxDeep: number = Infinity): IObject {
if (typeof source !== 'object') {
throw new Error('Argument must be an object');
}
const refs = new WeakMap<IObject, IObject>();
const dest: IObject = {};
@frydlewicz
frydlewicz / stack.ts
Last active July 27, 2020 16:16
Stack TypeScript class with build-in iterator
export default class Stack<T> {
private readonly maxSize: number;
private readonly loop: boolean;
private readonly data: T[];
private pos!: number;
constructor(maxSize: number, loop: boolean = false) {
if (!Number.isInteger(maxSize) || maxSize < 1) {
throw new Error('Invalid parameter');
@frydlewicz
frydlewicz / queue.ts
Last active July 27, 2020 16:16
Queue TypeScript class with build-in iterator
export default class Queue<T> {
private readonly maxSize: number;
private readonly loop: boolean;
private readonly data: T[];
private size!: number;
private toProduce!: number;
private toConsume!: number;
constructor(maxSize: number, loop: boolean = false) {
@frydlewicz
frydlewicz / callWhenLeaves.js
Last active March 8, 2020 18:04
Call function when user wants to leave the website
(function(g,e,c){if(typeof g!="function"){return}var d=false;var f=0;var b=function(){var h=(new Date).getTime();if(d&&(f==0||e&&h-f>c)){document.removeEventListener("mouseleave",b);f=h;g()}};var a=function(j){var h=innerHeight/10;var i=j.movementY;var k=j.clientY;if(i<0&&k<h){d=true;document.addEventListener("mouseleave",b)}else{document.removeEventListener("mouseleave",b);d=false}};document.addEventListener("mousemove",a)})(
function () { // callback function
alert("Do not leave!")
},
true, // repeat calling
5000 // interval in milliseconds
);
@frydlewicz
frydlewicz / lazyLoading.html
Last active March 8, 2020 18:05
Lazy loading script for images and backgrounds
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
(function(g,f,j,i,h){$(window).on("load",function(){i=0;$("["+g+"]").each(function(){function a(b){h=b.attr(f);if(b.attr(g)=="style"){b.css("background-image","url("+h+")")}else{b[0].src=h}}setTimeout(a,++i*j,$(this))})})})(
"data-lazyLoading", // attribute for type
"data-src", // attribute for src
100 // delay in milliseconds
);
</script>
<img data-lazyLoading data-src="https://frydlewicz.pl/photo/Warszawa/DSC07655.jpg" alt>
<div data-lazyLoading="style" data-src="https://frydlewicz.pl/photo/Warszawa/DSC07644.jpg"></div>