Skip to content

Instantly share code, notes, and snippets.

View jancassio's full-sized avatar
:octocat:
Loading...

Jan Cássio jancassio

:octocat:
Loading...
View GitHub Profile
@jancassio
jancassio / getPreferredLanguages.md
Last active April 30, 2023 19:26
Parse and provides the best language option from a Accept-Language header value.

getPreferredLanguages

Parses the Accept-Language header's value in an array of language directives.

Usage

const acceptLanguageValue = 'pt-BR, en;q=0.9, fr;q=0.8, it;q=0.7, *;q=0.5'
@jancassio
jancassio / useMatchMedia.md
Created March 20, 2023 02:14
React hook to trigger updates when a media queries matches or not its query.

useMatchMedia hook

Allow to obtain match responses of given media query list items.

Details

This hook relies on window.matchMedia to provide precise updates when some media query matches or not its requirements. It will also handle and dispose event listeners properly and avoid uncessary re-renders.

API

@jancassio
jancassio / README.md
Created February 27, 2023 01:29
Maybe type + helpers for TypeScript

Maybe type for TypeScript

Maybe is a type to help to work with nullable or undefined types more pleasurable. Very inspired by Haskell and some bits of Rust's pattern matching.

API

Types

Maybe<T>

@jancassio
jancassio / BindingUnwrapperView.md
Last active October 16, 2022 01:40
BindingUnwrapperView

BindingUnwrapperView

⚠️ WARNING ⚠️

This concept still not working and I don't have a clue (so far) why it do not compile because the only error I get is:

failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project

DebugView Modifier

Extends View's protocol including some additional modifiers to help debugging SwiftUI

Example

import SwiftUI

struct ContentView: View {
@jancassio
jancassio / Cache.md
Last active November 23, 2021 00:41
Cache is a NSCache that allows to store value types in memory.

Cache

Allows to store values in memory, powered by system NSCache (but for value types too). The Cache class is full based in a John Sundell tutorial (check out at See Also section).

Also, there's a Environment modifier to allow share cache between views in same context. It would be helpful in situations where Cache is initialized just once in runtime.

And remember, Cache is only cache-memory, so use it wisely.

For file cache based, I trully recommend reading the article mentioned at See Also to understand and implement file-based cache.

@jancassio
jancassio / LadyBug.md
Last active November 9, 2021 14:26
LadyBug helps to track errors along the app execution.

LadyBug

LadyBug is cute and log messages along app execution. Behind the scenes LadyBug uses OSLog to perform log messages and store log in app archive according by level.

level persisted performance
debug faster
info ❓ (only during log collect) faster (out of log collect), lower (during log collect)
notice lower
error lower
@jancassio
jancassio / FontFamily.md
Last active November 15, 2021 03:15
FontFamily protocol helps to enumerate and access font face strongly typed.

FontFamily Enumerated (Swift)

Helps to quick enumarate font faces by strong typed enumerators.

Usage

enum Arial: String, FontFamily {
  case regular = "Arial-Regular"
 case bold = "Arial-Bold"
@jancassio
jancassio / README.md
Last active November 2, 2021 21:00
StatefulPreview

StatefulPreview.swift

What's it?

StatefulPreview is a simple struct wrapped in a PreviewProvider extension that enables stateful previews for SwiftUI views.

Why?

Fields wrapped in @Binding annotation, are not easy to preview as stateful value. It's possible to wrap a value using .constant utilty but it will not allow to modify the value.

@jancassio
jancassio / String+Localized.swift
Created April 15, 2021 23:53
Localizable String Extensions
//
// String+Localized.swift
//
// Created by Jan Cássio on 2021/03/27.
// Copyright © 2021 Jan Cassio. All rights reserved.
//
import Foundation
extension String {