Omar Albeik
Menu
Blog
Snippets
Projects
Gallery
About
Code Snippets
Swift, Async/await, Firebase
Observe changes for key in Firebase database (async/await)
Observe changes for key in Firebase database using Swift's async/await
Swift, Async/await, Firebase
Set Encodable value for key in Firebase database (async/await)
Set an Encodable value for a key in Firebase database using Swift's async/await
Swift, Firebase, Async/await
Delete Firebase user (async/await)
Delete Firebase user using Swift's async/await
Swift, Firebase, Async/await
Observe Firebase auth state (async/await)
Observe Firebase auth state changes using Swift's async/await
iOS, Swift, UIKit
Reusable protocol
Easy UITableView/UICollectionView cell registering/dequeuing with the power of protocols and extensions
Swift
Subscript with default arguments
Swift 5.2 added subscript with default arguments, it gets quite powerful for custom subscripts, but also default ones like in this example
Typescript
Get a random element from array
Get a random element from array - O(1)
Swift, iOS, UIKit
Screenshot from UIView
Generate a screenshot from a UIView
Python
Print to the same line
Print to console using the same line (useful for progress logging)
Python
Read and write JSON
Read and write JSON
Git
Sync a fork from original repository
Sync a fork of a repository to keep it up-to-date with the upstream repository.
Sass
Global Sass variables in styled-components
Define and use global Sass variables in styled-components
Django, Python
Autocomplete fields in Django Admin
Show autocomplete search dropdown for ManyToManyField fields in Django admin.
Django, Python
Pre-populate fields in Django admin
Pre-populate fields' text based on the content of another field in Django admin
Docker
Use Docker networks to share the same localhost
Use Docker networks to share the same localhost between containers.
NextJS
Create dynamic pages in NextJS
Create dynamic pages that fetch their content from an API in NextJS
Nginx
Serve static robots.txt
Serve a static robots.txt in Nginx
Nginx
Disable Nginx version in headers
Disable Nginx version in response headers for security reasons
Python
Multiple requirements files
Use different requirements files for development and production environments
Python
Join URL and path
Use urllib to join a URL and path the proper way without worrying about leading or trailing slashes
NextJS, Javascript
Redirect index to page
Redirect index to another page in NextJS applications.
Git
Clear out the history of a git repository
Steps to clean out the commits history of a git repository
Docker
Stop all Docker containers
One-liner to stop all Docker containers
Python, Django
Django TrimCharField
A CharField that ignores leading and trailing spaces in data
Swift
Create a Result from optional value-error pair
If you use ObjC APIs that have optionals for both value and error in their callback handlers, this extension to map them into a Result type might be useful.
Swift
Use first(where:)
Use first(where:) to get the first element of the sequence that satisfies the given predicate.
Swift
Multiple levels sorting with tuples
Use tuples to sort Comparable objects based on multiple properties. Example: sorting contacts by surname; then by name, to see all names from the same family in a row.
Swift
Use property observers
Use Swift's property observers to observe and respond to changes in a property’s value. Both `willSet` and `didSet` takes a default parameter to get new and old values
Swift
Generics in typealias
Use a generic type in type aliases to increase code reusability. Notice how the same type alias is used for all Post, [Post], and [Book] 😎
Swift
The ~= operator
Use the ~= operator in Swift to check for patterns. Say you have an array and you want to get an object at index in a safe way
Git
Updating a feature branch
Bring your feature branch up to date with master.
Git
Pull submodules after cloning repo
Pull submodules in a git repo after cloning.
Javascript
Dark mode change detection
Respond to appearance changes in system preference live in the browser in macOS 10.14+ and iOS 13+.
Nginx
Remove trailing slash from URL
Avoid duplicate content in your website analytics by removing the trailing slash from URL using Nginx
Python, Django
Make trailing slash optional in Django
Make trailing space optional in Django by overriding `SimpleRouter` in your `urls.py`
Shell
Remove all node_modules directories
Remove all node_modules directories before taking a backup of your files to make your life easier :)
Git
Git Commits Per Author
Get number of commits in a git repository per author.
Git
LOC Per Author
Get number of lines contributed per each author in a git repository
Javascript
Encode string and replace /%20/ with +
Encode string and replace /%20/ with + to be used in a URL
Javascript
List of countries
List of all countries - suitable for a contact page.
Swift
Validate email address format
A simple extension to check if an email is in valid format using Regex
Swift, UIKit
Get iOS application icon
Get the icon for an iOS application
iOS, Swift
Initialize UIColor from HEX value
A simple Swift extension to initialize UIColor from a hex string taking into consideration edge cases and hex representation variations
Swift
Decode Bool from Int or String
Because, you know, sometimes the backend guys send us a 1 or TRUE and expect us to map it to native Bool object, well, they are kinda right, it shouldn't matter that much
Swift
Save and retrieve Codable objects to UserDefaults
Because why not!
Javascript
Create an object from a given array
Create an object from a given array
Javascript
Copy String To Clipboard
Copy a string to clipboard
Javascript
Check if object is Empty
Check if an object is Empty
Swift
Extensions Wrapper
Use the power of protocols and generic types to avoid extension conflicts
Swift
Safe Collection Subscript
Safe subscript protects the collection from out of bounds error by using optionals.
Copyright © 2024
Omar Albeik
. All rights reserved.