Skip to content

Instantly share code, notes, and snippets.

View Rowadz's full-sized avatar
🤙
🦖🦖

Mohammad Alrawad Rowadz

🤙
🦖🦖
View GitHub Profile
@Rowadz
Rowadz / unnest-with-ordinality.sql
Last active October 15, 2023 17:40
How to use unnest and "with ordinality" to order rows or arrays based on their occurrence in a list
create table emojies(
name text
);
INSERT INTO emojies VALUES('👻'), ('👺'), ('👹'), ('🤡'), ('🏚️'), ('🛸')
select * from emojies
JOIN UNNEST(ARRAY['🛸', '👻', '👺', '🏚️', '👹', '🤡']) WITH ORDINALITY my_order_table("name", "ordinality_order") USING("name")
ORDER BY "ordinality_order"
@Rowadz
Rowadz / race-condition-of-promises-with-a-stopping-condition.ts
Created August 31, 2023 09:47
race condition of promises with a stopping condition
import axios, { AxiosResponse } from 'axios'
import { v4 as uuidv4 } from 'uuid'
export type PromiseStatus<T> = {
promise: Promise<T>
isFulfilled: boolean
isRejected: boolean
}
@Rowadz
Rowadz / short-circuit-promises.ts
Created August 26, 2023 11:42
short circuit promises in Nodejs
import axios, { AxiosResponse } from 'axios'
type TODO = {
userId: number
id: number
title: string
completed: boolean
}
const DEFAULT_TIMEOUT = 200
import PromiseQueue from 'p-queue'
import got, { Request } from 'got'
import colors from '@jshor/colors'
console.clear()
let counter = 0
let errorCounter = 0
const { bold, green, red } = colors
@Rowadz
Rowadz / magicObject.js
Created June 21, 2022 10:17
An infinite proxy object that will never return undefined
const magicObject = new Proxy(
{},
{
get(_, prop) {
return new Proxy({ [prop]: 'hmmm?' }, this)
},
}
)
@Rowadz
Rowadz / map-that-keeps-the-object-references.ts
Last active September 26, 2022 12:26
A map object that keeps the object references event if you try to override them.
const refKeeperMapFactory = (() => {
type RefMap = Map<string, Record<string, any>>
let refKeeper: RefMap
return () => {
if (refKeeper) {
return refKeeper
}
const mapProxyHandler = {
get(target: RefMap, name: string) {
const functionOrValue = Reflect.get(target, name)
@Rowadz
Rowadz / soft-delete-with-undo-postgresql.sql
Created May 22, 2022 22:52
Soft delete with unique index and undo delete postgresql
CREATE TABLE IF NOT EXISTS artices (
id serial PRIMARY KEY,
title VARCHAR (50) NOT NULL,
slug VARCHAR (255) NOT NULL,
deleted_at TIMESTAMP NULL,
created_on TIMESTAMP not NULL DEFAULT NOW()
);
CREATE UNIQUE INDEX IF NOT EXISTS unique_slug
@Rowadz
Rowadz / pipe_async_pipe.js
Created December 18, 2021 09:51
pip and asynchronous pipe functions in JS
Function.prototype.pipe = function (start, ...funcs) {
return funcs.reduce((prev, func) => func(prev), start)
}
const func01 = (prev) => prev + 1
const func02 = (prev) => prev + 1
const func03 = (prev) => prev + 1
const func04 = (prev) => prev + 1
const func05 = (prev) => prev + 1
import React, { useState } from 'react'
import ReactDOM from 'react-dom'
import { Button } from 'rsuite'
import 'rsuite/dist/styles/rsuite-dark.css'
import { generate } from 'shortid'
import User from './User'
import Viewer from './Viewer'
const App = () => {
const [users, setUsers] = useState([
@Rowadz
Rowadz / combineEntityAdapterStates.js
Last active March 18, 2022 00:14
combining multiple createEntityAdapter generated state into the same slice from redux toolkit
import {
createSlice,
createEntityAdapter,
createAsyncThunk,
} from '@reduxjs/toolkit'
export const fetchData = createAsyncThunk(
'users/fetchData',
async (_, { dispatch }) => {
const data = await fetch('http://localhost:3001/users').then((res) =>