Skip to content

Instantly share code, notes, and snippets.

Avatar

Loris Leiva lorisleiva

View GitHub Profile
@lorisleiva
lorisleiva / js-next-react.md
Last active Jun 15, 2022
[Metaplex] Integrate the new JS SDK with React
View js-next-react.md
@lorisleiva
lorisleiva / TweetCard.vue
Last active Feb 10, 2022
TweetCard and TweetFormUpdate for Episode 12 of the Solana series.
View TweetCard.vue
<script setup>
import { ref, toRefs, computed } from 'vue'
import { useWorkspace } from '@/composables'
import TweetFormUpdate from './TweetFormUpdate'
const props = defineProps({
tweet: Object,
})
const { tweet } = toRefs(props)
@lorisleiva
lorisleiva / solana-1.8.x-apple-m1.md
Last active Jan 15, 2022
Install and use Solana CLI in an Apple M1 before v1.9
View solana-1.8.x-apple-m1.md

Install Solana on Apple M1

Unfortunately, at the time of writing, the Solana installer script above will not work on Apple M1 computers.

The installation will be successful and you’ll be able to call solana commands but other Solana binaries such as solana-test-validator will throw various errors since it’s expecting dependencies to be located elsewhere. If you’ve already installed it that way, no worries, simply run rm -rf ~/.local/share/solana to uninstall Solana.

I’ll be sure to update this article if/when this will work out-of-the-box for M1 computers.

In the meantime, we’ve got a little workaround to go through. We need to clone the Solana repository and compile the binary from the source code. Don’t worry, the installation process is still super simple, it just takes a bit longer due to the compilation time. Here are the steps.

@lorisleiva
lorisleiva / pipeline.js
Last active Jun 21, 2022
Laravel-like pipelines in JavaScript and TypeScript
View pipeline.js
export function pipeline(initialValue, pipes, then) {
then = then ?? ((t) => t);
const pipelineCallback = pipes
.slice()
.reverse()
.reduce((next, pipe) => (passable) => pipe(passable, next), then);
return pipelineCallback(initialValue);
}
@lorisleiva
lorisleiva / useAutoresizeTextarea.js
Created Nov 15, 2021
Auto-resize a textarea based on its content (Vue 3 composition API)
View useAutoresizeTextarea.js
import { watchEffect } from "vue"
export const useAutoresizeTextarea = (element) => {
const resizeTextarea = () => {
element.value.style.height = 'auto'
element.value.style.height = element.value.scrollHeight + 'px'
}
watchEffect(onInvalidate => {
if (! element.value) return
@lorisleiva
lorisleiva / AppWithLocalStorage.vue
Last active Apr 30, 2022
Abstraction of the local storage using Vue3's composition API
View AppWithLocalStorage.vue
<script setup>
import useLocalStorage from './useLocalStorage'
const publicKey = useLocalStorage('solana-wallet-public-key')
</script>
<template>
<div>
<input type="text" v-model="publicKey">
<div v-text="publicKey"></div>
</div>
View nodes-visitors-2.php
<?php
// Nodes.
abstract class Node implements Visitable {}
class Number extends Node {
public function __construct(public float $value){}
public function accept(Visitor $visitor) {
return $visitor->visitNumber($this);
}
View nodes-visitors-1.php
<?php
abstract class Node {}
class Calculator extends Node {
public function __construct(public array $statements){}
}
class Add extends Node {
public function __construct(public Node $left, public Node $right){}
@lorisleiva
lorisleiva / toSqlWithBindings.php
Last active May 20, 2022
A little macro to get the SQL from a query builder without the annoying "?".
View toSqlWithBindings.php
<?php
use Illuminate\Database\Eloquent\Builder;
Builder::macro('toSqlWithBindings', function () {
$bindings = array_map(
fn ($value) => is_numeric($value) ? $value : "'{$value}'",
$this->getBindings()
);
View Model.js
// resources/js/models/Model.js
export default class Model {
constructor (attributes = {}) {
this.fill(attributes)
}
static make (attributes = {}) {
return Array.isArray(attributes)
? attributes.map(nested => new this(nested))