Skip to content

Instantly share code, notes, and snippets.

function StreamedText() {
const [text, setText] = React.useState("");
React.useEffect(() => { getStream() }, []);
function getStream() {
// Fetch the stream
const stream = await fetch('/api/answer', { method: 'POST', body: JSON.stringify({ query } }) })
const data = stream.body;
if (!data) return;
@yazinsai
yazinsai / stream.ts
Last active May 16, 2023 07:40
Streams an OpenAI response, compatible with Next.js
/**
* Usage:
*
* export const config = {
* runtime: "edge",
* };
*
* const stream = await streamFromResponse(await openai.complete({
* model: 'gpt-3.5-turbo',
* prompt: 'this is a test',
const flattenObjectToMongoDbFormat = (obj, ancestors = []) => {
/**
* When you want to update a MongoDB document, without overriding
* nested fields that are not specified, you need to specify the
* nested fields in the format:
* `$set: { "parent.nested.key": "value" }`
*
* This method takes a nested object (usually from an incoming API
* request) and converts it to the nested format.
*/
export const flattenObjectToMongoDbFormat = (obj: any, parent: String = ''): any => {
/**
* When you want to update a MongoDB document, without overriding
* nested fields that are not specified, you need to specify the
* nested fields in the format:
* `$set: { "parent.nested.key": "value" }`
*
* This method takes a nested object (usually from an incoming API
* request) and converts it to the nested format.
*/
@yazinsai
yazinsai / bash-cheatsheet.sh
Created April 6, 2021 07:35 — forked from kannaiah/bash-cheatsheet.sh
Bash CheatSheet for UNIX Systems --> UPDATED VERSION --> https://github.com/LeCoupa/awesome-cheatsheets
#!/bin/bash
#####################################################
# Name: Bash CheatSheet for Mac OSX
#
# A little overlook of the Bash basics
#
# Usage:
#
# Author: J. Le Coupanec
# Date: 2014/11/04
@yazinsai
yazinsai / convertkit.js
Created November 11, 2020 10:50
A simple script to register a user to ConvertKit using only Javascript
/* Submit an email address to ConvertKit using only Javascript */
function submitForm(email) {
fetch('https://app.convertkit.com/forms/XXXXXX/subscriptions', {
// replace XXXXXX above with your form ID (e.g. 1231234)
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `email_address=${email}`
}).then((response) => {
const elemsIn = (tag, parent) => Array.from(parent.getElementsByTagName(tag));
const speedUp = (v) => v.playbackRate = 2;
const videos = elemsIn('video', document);
const iframes = elemsIn('iframe', document);
videos.forEach(speedUp);
iframes.forEach((f) => elemsIn('video', f.contentWindow.document).forEach(speedUp));
@yazinsai
yazinsai / parse-ayat.rb
Created July 5, 2020 09:45
A simple Ruby script to parse text from the Quran into text without diacritics
#!/usr/bin/env ruby
# 1. Copy the raw text from https://www.corequran.com/
# 2. Run "ruby parse.rb"
# 3. Paste the copied text
# 4. Press "Ctrl + D" to see the output
def parse(str)
str
.split("\n")
@yazinsai
yazinsai / util.js
Created May 1, 2020 11:50 — forked from tprynn/util.js
Frida utility functions
module.exports = {
hexStringToIntArray: function(str) {
var res = []
for(var i = 0; i < str.length; i += 2) {
res.push(parseInt(str.substring(i, i+2), 16))
}
return res
},
byteArrayToHexString: function(array) {
=begin
Converts:
# people.csv
Name, Age, Height
Ahmed, 12, 103
Mohamed, 19, 176
.. into:
# people.json