Skip to content

Instantly share code, notes, and snippets.

@raaar
raaar / ChatGPT
Last active January 26, 2023 21:53
Ask a question from the terminal: sh ./chat.sh "what's the time?"
#!/bin/bash
# Set the API key
source ~/env.sh
endpoint="https://api.openai.com/v1/completions"
max_tokens=${2:-50}
prompt=$1
temperature=0.5
@raaar
raaar / request-polling.js
Last active July 19, 2022 14:23
Promise.race with AbortController
const BLOG_POSTS_URL = 'https://jsonplaceholder.typicode.com/posts';
const FETCH_TIMEOUT = 3000;
const FETCH_INTERVAL = 4000;
const TIMEOUT_ERROR_MESSAGE = 'Request timed out';
const controller = new AbortController()
const getPosts = (signal) => fetch(BLOG_POSTS_URL, {
signal
})
const str = (greet) => console.log(greet);
const id = "User1";
const obj = {};
const obj1 = {};
const logData = (...props) => {
console.log(...props);
props.forEach((item, index) => {
if(typeof item === 'function') {
@raaar
raaar / bst.js
Last active June 21, 2021 08:21
Binary search tree
class Node {
constructor(val) {
this.val = val;
this.left = undefined;
this.right = undefined;
this.parent = undefined;
}
}
class BST {
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 8888;
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname
, filename = path.join(process.cwd(), uri);
function add_custom_sizes() {
add_image_size( 'xlarge-square-thumb' , 500 , 500 , true );
add_image_size( 'large-square-thumb' , 340 , 340 , true );
add_image_size( 'medium-square-thumb' , 150 , 150 , true );
add_image_size( 'small-square-thumb' , 80 , 80 , true );
add_image_size( 'large-landscape-thumb' , 280, 210 , true);
add_image_size( 'medium-landscape-thumb' , 160, 120 , true);
add_image_size( 'small-landscape-thumb' , 100, 75 , true);
@raaar
raaar / WORDPRESS: change Posts label in admin area
Last active April 7, 2022 13:31
WORDPRESS: change Posts label in admin area
function change_post_label() {
global $menu;
global $submenu;
$menu[5][0] = 'News';
$submenu['edit.php'][5][0] = 'News';
$submenu['edit.php'][10][0] = 'Add News';
$submenu['edit.php'][16][0] = 'News Tags';
echo '';
}
function change_post_object() {
function add_custom_sizes() {
add_image_size( 'thumb-16-9', 480, 273, true );
add_image_size( 'thumb-3-2', 480, 320, true );
add_image_size( 'thumb-1-1', 480, 480, true );
}
add_action('after_setup_theme','add_custom_sizes');
/*
Custom Contnet & Excerpt Length
*/
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);