Skip to content

Instantly share code, notes, and snippets.

@alexpilugin
alexpilugin / gist:ef36f94f61038472d8295e3541d7f63e
Created February 13, 2024 09:05
(Vue 3 Composables) JavaScript version of EventBus from a developit/mitt TypeScript library
// JavaScript Version of: https://raw.githubusercontent.com/developit/mitt/main/src/index.ts
// Define the event bus outside of the exported function to ensure a singleton
const all = new Map();
const on = (type, handler) => {
const handlers = all.get(type) || [];
all.set(type, [...handlers, handler]);
};
const off = (type, handler) => {
@alexpilugin
alexpilugin / git.migrate
Created December 3, 2020 13:51
Move your existing git repository # to a new remote repository
#!/bin/bash
# Sometimes you need to move your existing git repository
# to a new remote repository (/new remote origin).
# Here are a simple and quick steps that does exactly this.
#
### OPTION 1 ###########################################################################################
# Let's assume we call "old repo" the repository you wish
# to move, and "new repo" the one you wish to move to.
#
@alexpilugin
alexpilugin / gist:52234fcdbf4f5a7dba2b06666cb1a675
Created September 25, 2019 00:43
function that recursively invokes itself each 5 seconds
//Make a function that recursively invokes itself at the end on a timer
//This immediately invokes my_func, and then recursively invokes it at the end of the function after a 5 second delay.
(function my_func() {
// your code
setTimeout( my_func, 5000 );
})();
(function my_func() {
// your code
String.prototype.shuffle = function () {
var a = this.split(""),
n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
function getNumberId(prefix) {
var p = prefix || "i";
function shuffle(str) {
var a = str.split("");
var n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
<template>
<div class="dhtml-gantt-chart">
{{word}}
<div id="gantt_here" ref="dhtml" style="width:100%; height:400px;"></div>
</div>
</template>
<script>
//Component's location: src/components/charts/DHTMLganttChart.vue
//Library's location: src/assets/dhtmlgantt-v4-pro/
//@returns String
function getFormatedDate(formating, date) {
const format = formating || 'DD MMM YYYY';
const days = ["Sun", "Mon", "Tue", "We", "Thu", "Fri", "Sat"];
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
function parse(str) {
var time = new Date(date);
return isNaN(time.getTime()) ? null : time;
}
var arr = [
{key:"11", value:"1100"},
{key:"22", value:"2200"}
];
var obj = arr.reduce(function(obj,item) {
obj[item.key] = item.value;
return obj;
},{});
console.log(JSON.stringify(obj));
var myArray = [{ 'id': '73', 'foo': 'one' }, { 'id': '45', 'foo': 'two' }];
function searchByPropValue(prop, value) {
return function (element) {
if (element.hasOwnProperty(prop)) {
if (value) {
if (element[prop] == value) return element;
} else {
return element;
}
@alexpilugin
alexpilugin / generateUUID v4.js
Created July 23, 2018 21:15
Generate Unique ID v4
function generateUUID() {
let d = new Date().getTime()
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
let r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)
})
return uuid
}