Skip to content

Instantly share code, notes, and snippets.

@yuval-a
yuval-a / areObjectsEqual
Last active February 13, 2023 16:56
Simple objects equality checker function
// Check if objects are equal (have the same keys with the same values), all values must be primitives (no functions, objects or arrays)
function areObjectsEqual(obj1, obj2) {
let obj1KeysCount = 0;
for (const key in obj1) {
if (obj1[key] !== obj2[key]) return false;
obj1KeysCount++;
}
return Object.keys(obj2).length === obj1KeysCount;
}
@yuval-a
yuval-a / gist:43bac660b3b46063ff58263d7ee2cb24
Created October 9, 2022 12:06
Fibonacci positive Nth item formula
const G = (1 + Math.sqrt(5)) / 2; // Golden Ratio
Math.round(G ** n / Math.sqrt(5));
@yuval-a
yuval-a / gist:73fe8ac54a313bacea5c3d8a41c5ca72
Last active October 18, 2021 12:46
Usefull Node command line options
* `--trace_gc`: Logs garbage collection operations.
* `--inspect`: Runs node in debugging mode. open Chrome with `chrome://inspect` to see the debug session.
@yuval-a
yuval-a / gist:ad8dd131506c56da2a634693db72e2c9
Last active April 19, 2021 08:15
str_length that ignores Hebrew "nikud" characters
function str_length(str) {
let length = 0, nikud_chars = /[ְֱֲֳִֵֶַָֹֻּׁׂ]/;
for (let i=0,len=str.length;i<len;i++)
length += nikud_chars.test(str[i]) ? 0 : 1;
return length;
}
@yuval-a
yuval-a / gist:01dfc6b38c39a80ef66cbbc613e041bc
Last active March 15, 2021 08:49
useEvents High Order Hook/Function for separation of template and logic in React
/* events should be an object containing DOM event names as keys (click, keyup, hover...), with the values as objects having
* element "names" as keys, and event handler functions as values. E.g.:
* {
* click: {
* button1() { // handle button1 click },
* button2() { // handle button2 click },
* keyup {
* input1() { // handle input1 keyup }
* }
* }
@yuval-a
yuval-a / react-component-empty-class
Created January 31, 2019 17:56
React Component class "abstract" prototype
import React, { Component } from 'react'
export default class ComponentName extends Component {
constructor(props) {
console.log ("constructor");
super(props);
this.state = {
};
}
// A quick Javascript class for creating a countdown timer
// leadingZero is a boolean, onTimeCallback will be called on each second update with the timer string
function Countdown(hours,minutes,seconds, leadingZero, onTimeCallback) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.countdownStr = hours+":"+minutes+":"+seconds;
this.onTime = onTimeCallback;
this.leadingZero = leadingZero;
@yuval-a
yuval-a / XMLHTTP_GET_Request.js
Last active August 31, 2015 09:15
XMLHTTP GET Request
var url = '/.../';
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200)
console.log(req.responseText);
else
console.log("Error loading page");
};
req.send();
@yuval-a
yuval-a / formvalidation.js
Created July 26, 2015 23:02
Form validation - one function
// put 'req' on the elements of any required input fields. Don't use the 'required' attribute, otherwise the default action
// will trigger.
function onFormSubmit(form) {
var isValid = true;
Array.prototype.forEach.call(form.elements, function (e) {
if ((e.value.length == 0 || ! /\S/g.test(e.value)) && e.getAttribute('req') !== null && e.getAttribute('req') !== undefined) {
isValid = false;
e.value = "";
if (e.getAttribute('req') == "") {
@yuval-a
yuval-a / css transitions
Created May 25, 2015 19:05
transitions with vendor prefix
-webkit-transition: prop 0.2s ease;
-moz-transition: prop 0.2s ease;
-o-transition: prop 0.2s ease;
transition: prop 0.2s ease;