Skip to content

Instantly share code, notes, and snippets.

View Yago's full-sized avatar
👨‍💻

Yann Gouffon Yago

👨‍💻
View GitHub Profile
@Yago
Yago / useChatGtp.ts
Created February 21, 2024 15:06
Custom and simple ChatGPT react hook (not super secure tho)
import { useState } from 'react';
import OpenAI from 'openai';
import { Messages } from '@/types/Chat';
const openai = new OpenAI({
apiKey: process.env.NEXT_PUBLIC_OPENAI_API_KEY,
dangerouslyAllowBrowser: true,
});

Spatie's Ray globally in a JS/TS project

Follow these steps to add Ray globally available in your JavaScript/TypeScript project:

  1. Create the required files
$ mkdir ./ray
$ touch ./ray/index.js
$ touch ./ray/ray.d.ts
  1. Import in your app's entry import 'path/to/ray';
{
"people": [
{"id":1,"firstname":"Amitie","lastname":"Dorking"},
{"id":2,"firstname":"Lurlene","lastname":"Benyan"},
{"id":3,"firstname":"Carrissa","lastname":"Hawkin"},
{"id":4,"firstname":"Dukey","lastname":"Nolleau"},
{"id":5,"firstname":"Benjy","lastname":"Durston"},
{"id":6,"firstname":"Merrill","lastname":"MacAlpyne"},
{"id":7,"firstname":"Harlin","lastname":"Endrici"},
{"id":8,"firstname":"Reid","lastname":"Greve"},
@Yago
Yago / generate-images.mjs
Last active January 19, 2022 14:59
Generate image resolutions/formats for the web using ImageMagick and zx
#!/usr/bin/env zx
/* Require:
* - ImageMagick (https://imagemagick.org/)
* - rename (for macOS https://formulae.brew.sh/formula/rename)
* - zx (https://github.com/google/zx)
*
* Execute this script in your images source folder
* f.ex. $ cd my-original-images/ && npx zx generate-images.mjs
*
@Yago
Yago / example.jsx
Last active April 27, 2021 09:04
Next/Image Wordpress Loader
// Dans Wordpress
// [16, 32, 48, 64, 96, 128, 256, 384, 640, 750, 828, 1080, 1200, 1920, 2048, 2880, 3840].forEach(width => {
// ratios.forEach(ratio => {
// add_image_size(`${ratio}-cool`, width, ratio.h * width / ratio.w );
// });
// });
import useDimensions from "react-cool-dimensions";
const loader = ({ src, width }) => {
const delay = (time) => new Promise(resolve => setTimeout(resolve, time));
const thanks = async () => {
console.log('👊');
await delay(500);
console.log('🖐️');
await delay(500);
console.log('🎤');
};
@Yago
Yago / func-class.js
Last active November 28, 2018 13:44
class Calculator {
constructor(origin) {
this.origin = origin;
}
sum(...args) {
return args.reduce((acc, n) => typeof n === 'number' ? acc + n : acc, this.origin);
}
}
const calculator = {
sum(...args) {
return args.reduce((acc, n) => typeof n === 'number' ? acc + n : acc, 0);
},
sumOldSchool: function () {
return [...arguments].reduce(function (acc, n) {
if (typeof n === 'number') return acc + n;
return acc;
}, 0);
const sum = {
current: 0,
get result() {
return `The current result is ${this.current}`;
},
set add(n) {
if (typeof n === 'number') this.current = this.current + n;
}
};
@Yago
Yago / func-arrow.js
Last active November 28, 2018 13:27
const sum = (...args) => {
return args.reduce((acc, n) => typeof n === 'number' ? acc + n : acc, 0);
}
sum(31,7,4); // return 42