Instantly share code, notes, and snippets.
Full-Time Open-Sourcerer. Focused on Swift & JavaScript. Makes macOS apps, CLI tools, npm packages.
sindresorhus /chalk-source-index-with-malware.js
Last activeSeptember 25, 2025 17:16
DO NOT EXECUTE.https://github.com/chalk/chalk/issues/656 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| import ansiStyles from '#ansi-styles'; | |
| import supportsColor from '#supports-color'; | |
| import { // eslint-disable-line import/order | |
| stringReplaceAll, | |
| stringEncaseCRLFWithFirstIndex, | |
| } from './utilities.js'; | |
sindresorhus /actions.md
CreatedNovember 26, 2024 21:10
Shortcuts actions provided by the Actions app. Can be useful to feed into an AI.https://sindresorhus.com/actionssindresorhus /random-case-iterable.swift
Last activeFebruary 3, 2024 19:31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| extensionCaseIterable{ | |
| publicstaticfunc randomCaseIterableElement(using generator:inoutsomeRandomNumberGenerator)->Self{ | |
| allCases.randomElement(using:&generator)! | |
| } | |
| publicstaticfunc randomCaseIterableElement()->Self{ | |
| vargenerator=SystemRandomNumberGenerator() | |
| returnrandomCaseIterableElement(using:&generator) | |
| } | |
| } |
sindresorhus /NSSharingService-sharingServices-deprecation-warning-fix.swift
CreatedOctober 19, 2022 07:56
macOS 13 deprecated `NSSharingService.sharingServices()`. Here's how to silence the deprecation until you can move to the new API. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| privateprotocolSilenceDeprecationNSSharingService{ | |
| func sharingServices(forItems items:[Any])->[NSSharingService] | |
| } | |
| privatefinalclassSilenceDeprecationNSSharingServiceImplementation:SilenceDeprecationNSSharingService{ | |
| @available(macOS, deprecated:13) | |
| func sharingServices(forItems items:[Any])->[NSSharingService]{ | |
| NSSharingService.sharingServices(forItems: items) | |
| } | |
| } |
sindresorhus /UIScreen-screens-deprecation-warning-fix.swift
Last activeOctober 19, 2022 21:46
iOS 16 deprecated `UIScreen#screens`. Here's how to silence the deprecation until you can move to the new API. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| privateprotocolSilenceDeprecationForUIScreenWindows{ | |
| varscreens:[UIScreen]{get} | |
| } | |
| privatefinalclassSilenceDeprecationForUIScreenWindowsImplementation:SilenceDeprecationForUIScreenWindows{ | |
| @available(iOS, deprecated:16) | |
| varscreens:[UIScreen]{UIScreen.screens} | |
| } | |
| extensionUIScreen{ |
sindresorhus /SwiftUI Placeholder idea.swift
Last activeAugust 22, 2022 02:59
For a macOS app, if you want an action in both the main menu and the UI, it's quite boilerplaty. You need to synchronize some value with@focusedvalue. What if we had something like this? This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| struct CameraCommands:Commands{ | |
| var body:someCommands{ | |
| CommandGroup(replacing:.newItem){ | |
| // This menu item will be enabled when the key view has a button with `.command(.takePhoto)`. The action logic is implemented in the button. | |
| Placeholder("Take Photo", id:.takePhoto) | |
| // Alternatively, it could be: | |
| // Button("Take Photo") {} | |
| // .placeholder(.takePhoto) | |
| } |
sindresorhus /Collection+safe.swift
CreatedJanuary 19, 2022 13:18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| extensionCollection{ | |
| /** | |
| Returns the element at the given index if any,otherwise `nil`. | |
| ``` | |
| guardlet element=array[position]else{ | |
| return | |
| } | |
| print(element) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| extensionNSView{ | |
| /** | |
| Shake the view. | |
| - Note: It will do nothing if the user has enabled the “Reduce motion” accessibility preference. | |
| */ | |
| func shake( | |
| duration:TimeInterval=0.3, | |
| direction:NSUserInterfaceLayoutOrientation, | |
| moveAmount:Double=5 |
sindresorhus /Sequence+splittingEvenly.swift
CreatedNovember 9, 2021 09:57
https://twitter.com/sindresorhus/status/1455456708759748615 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| extensionCollectionwhere Element:Comparable{ | |
| /** | |
| Returns the index of the first occurrence of the lowest value in the collection. | |
| ``` | |
| [4, 2, 3, 2, 5].firstIndexOfMinElement() | |
| //=> 1 | |
| ``` | |
| */ | |
| func firstIndexOfMinElement()->Index?{ |
sindresorhus /UnexpectedNilError.swift
CreatedNovember 3, 2021 05:56
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| // Using `LocalizedError` here as the `.localizedDescription` for `Error` is often missing in places like an alert and Crashlytics/Sentry. | |
| struct UnexpectedNilError:LocalizedError{ | |
| letmessage:String? | |
| letfile:String | |
| letline:Int | |
| init( | |
| _ message:String?, | |
| file:String= #fileID, | |
| line:Int= #line |
NewerOlder