Skip to content

Instantly share code, notes, and snippets.

@juice49
juice49 / upload-image.ts
Last active July 21, 2023 09:46
Upload Sanity images with tags
import { type SanityDocument, type Reference } from 'sanity'
import { type UploadBody, type SanityImageAssetDocument } from '@sanity/client'
import { getCliClient } from 'sanity/cli'
const client = getCliClient()
interface Tag extends SanityDocument {
_type: 'media.tag'
name: {
_type: 'slug'
@juice49
juice49 / create-permission-resources-for-update-only-role.ts
Last active March 17, 2022 15:39
Sanity Update-Only Role Creation Scripts
import getIt from 'get-it'
import base from 'get-it/lib/middleware/base'
import jsonRequest from 'get-it/lib/middleware/jsonRequest'
import jsonResponse from 'get-it/lib/middleware/jsonResponse'
import promise from 'get-it/lib/middleware/promise'
import headers from 'get-it/lib/middleware/headers'
import httpErrors from 'get-it/lib/middleware/httpErrors'
const API_VERSION = 'v2021-10-04'
const PROJECT_ID = 'xxxxxxxx'
@juice49
juice49 / stitches-create-scale-variant.tsx
Created October 19, 2021 23:08
Stitches Create Scale Variant
import { CSS } from '@stitches/react'
import { theme } from './stitches.config'
function createScaleVariant<Scale extends string & keyof typeof theme>(
property: string,
scale: Scale,
): Record<keyof typeof theme[Scale], CSS> {
return Object.keys(theme.space).reduce(
(reduced, value) => ({
...reduced,
@juice49
juice49 / next-plain-image.tsx
Last active September 26, 2021 22:31
Next Plain Image
import React from 'react'
import {
ImageConfig,
imageConfigDefault,
LoaderValue,
VALID_LOADERS,
} from 'next/dist/server/image-config'
if (typeof window === 'undefined') {
@juice49
juice49 / readme.md
Created November 30, 2012 16:53
Using the Laravel Validator, make a field required if another field is set to a given attribute.

Setup

  • Add this file to application/libraries (or, if you are already extending the Validator class, add the contents there).
  • Remove Validator alias from config/application.php in order to avoid conflicts.
  • Add "required_if_attribute" => "The :attribute field is required." to application/language/[language]/validation.php.

Usage

Define the rule using:

required_if_attribute:[field],[operator],[value]

@juice49
juice49 / UniqueFilename
Created July 2, 2014 10:14
Laravel unique filename
/**
* Creates a unique filename by appending a number
*
* i.e. if image.jpg already exists, returns
* image2.jpg
*/
function uniqueFilename($path, $name, $ext) {
$output = $name;
$basename = basename($name, '.' . $ext);
@juice49
juice49 / use-lazy-module.js
Created August 28, 2019 15:48
useLazyModule
// Sadly a full dynamic import is not compatible
// with webpack's `import()`.
import { useState } from 'react'
const useLazyModule = path => {
const [ module, setModule ] = useState()
;(async () => {
const module = await import(path)
@juice49
juice49 / sql-fns.js
Last active June 7, 2019 16:42
sql-fns
/* sql-tag
mysql2
minigrate */
// from redux
function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
@juice49
juice49 / index.js
Created June 6, 2019 18:52
Fluent calculator
class Calculator {
constructor (value = 0) {
this.value = value
}
add (value = 0) {
this.value += value
return this
}
@juice49
juice49 / index.js
Created May 10, 2019 22:03
Fluid values mixin for CSS-in-JS
const fluid = (properties, [ minVw, maxVw ], [ minVal, maxVal ]) => properties
.map(property => `
${property}: ${minVal};
@media (min-width: ${minVw}) {
${property}: calc(${minVal} + ${parseFloat(maxVal) - parseFloat(minVal)} * (100vw - ${minVw}) / ${parseFloat(maxVw) - parseFloat(minVw)});
}
@media (min-width: ${maxVw}) {
${property}: ${maxVal};