Skip to content

Instantly share code, notes, and snippets.

View eranbetzalel's full-sized avatar

Eran Betzalel eranbetzalel

View GitHub Profile
@eranbetzalel
eranbetzalel / use-id-generated-items.ts
Created December 7, 2024 18:20
A lightweight React hook to generate unique keys for items without predefined IDs, avoiding the pitfalls of using array indexes as keys. Perfect for rendering lists where item identities are not explicitly defined.
import {useState} from 'react';
import {nanoid} from 'nanoid';
export type ItemWithId<T> = T & { id: string };
export const useIdGeneratedItems = <T>(items: T[]): [ItemWithId<T>[], (items: ItemWithId<T>[]) => void] => {
const [itemsWithId, setItemsWithId] = useState(() => items.map(item => ({...item, id: nanoid()})));
return [itemsWithId, setItemsWithId];
@eranbetzalel
eranbetzalel / batch-worker-host.ts
Last active January 21, 2025 07:41
This class provides an efficient and flexible way to handle batch processing of jobs in a BullMQ environment, leveraging NestJS. It ensures that jobs are processed in batches, which can be crucial for performance optimization in applications requiring bulk operations..
import {WorkerHost} from '@nestjs/bullmq';
import {Job} from 'bullmq';
export class BatchWorkerHost extends WorkerHost {
private jobBatchCreationTime: Date;
private jobBatch: Job[];
private jobBatchProcessPromise: Promise<void>;
private running: boolean = false;
private resolveJobBatchProcessPromise: () => void;
@eranbetzalel
eranbetzalel / 10bis-notify-open-rest.userscript.js
Last active August 11, 2021 10:31
10Bis - Receive alert when a restaurant is opened
// ==UserScript==
// @name 10bis Notfiy Restaurant has opened
// @description Receive alert when a restaurant is opened
// @version 0.1
// @include http*//www.10bis.co.il/next/Restaurants/menu/delivery/*
// @copyright Eran Betzalel
// ==/UserScript==
//======================================================================================================
@eranbetzalel
eranbetzalel / jira-sum-issue-rows.js
Created March 25, 2020 13:31
JIRA - Add Issues Sum Row (userscript)
// ==UserScript==
// @name JIRA - Add Issues Sum Row
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Calculate the sum row for JIRA issues view
// @author You
// @match https://*.atlassian.net/issues/*
// @grant none
// ==/UserScript==
@eranbetzalel
eranbetzalel / ngrams.js
Last active March 7, 2016 12:38
Implementation of the n-gram algorithm using JavaScript
function ngrams(array, length) {
var ngramsArray = [];
for (var i = 0; i < array.length - (length - 1); i++) {
var subNgramsArray = [];
for (var j = 0; j < length; j++) {
subNgramsArray.push(array[i + j])
}
@eranbetzalel
eranbetzalel / DecimalBytesConvertor.cs
Created April 14, 2013 20:07
Two methods for bytes-decimal conversion.
public decimal BytesToDecimal(byte[] buffer, int offset = 0)
{
var decimalBits = new int[4];
decimalBits[0] = buffer[offset + 0] | (buffer[offset + 1] << 8) | (buffer[offset + 2] << 16) | (buffer[offset + 3] << 24);
decimalBits[1] = buffer[offset + 4] | (buffer[offset + 5] << 8) | (buffer[offset + 6] << 16) | (buffer[offset + 7] << 24);
decimalBits[2] = buffer[offset + 8] | (buffer[offset + 9] << 8) | (buffer[offset + 10] << 16) | (buffer[offset + 11] << 24);
decimalBits[3] = buffer[offset + 12] | (buffer[offset + 13] << 8) | (buffer[offset + 14] << 16) | (buffer[offset + 15] << 24);
return new Decimal(decimalBits);
@eranbetzalel
eranbetzalel / DelimitedFileWriter.cs
Created April 12, 2013 12:58
Simple implementation of CSV (or any other character delimited file) writer.
class Car
{
public int CarId { get; set; }
public DateTime ManufactureDate { get; set; }
public decimal SomeDecimal { get; set; }
}
class DelimitedFileWriterExample
{
static void Main()