Skip to content

Instantly share code, notes, and snippets.

Traveling and preparing my apps for macOS 12

Sindre Sorhus sindresorhus

Traveling and preparing my apps for macOS 12
View GitHub Profile

Pure ESM package

The package linked to from here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
sindresorhus /
Last active Oct 19, 2021
Ideas for machine learning usage in my apps

Machine learning for my apps

I'm trying to think of ways I could use machine learning to enhance my apps. Feedback wanted!

You can find my apps here.


  • Natural language processing to parse a new event title, like Fantastical.
sindresorhus /
Last active Oct 18, 2021
Compile Objective-C on the command-line with clang
# -fobjc-arc: enables ARC
# -fmodules: enables modules so you can import with `@import AppKit;`
# -mmacosx-version-min=10.6: support older OS X versions, this might increase the binary size
clang main.m -fobjc-arc -fmodules -mmacosx-version-min=10.6 -o main
sindresorhus / NativeButton.swift
Created Oct 11, 2019
Example of using NSButton in SwiftUI to access missing features like `keyEquivalent` (for example, to make the button the default and highlighted). Stack Overflow answer:
View NativeButton.swift
struct ContentView: View {
var body: some View {
NativeButton("Submit", keyEquivalent: .return) {
// Some action
sindresorhus /
Last active Oct 11, 2021
Gettings started writing a ESLint rule

Gettings started writing a ESLint rule

First, take a look at the ESLint rule documentation. Just skim it for now. It's very long and boring. You can come back to it later.

ESLint rules works on the AST (Abstract Syntax Tree) representation of the code. In short, this is a tree structure that describes the code in a very verbose form. ESLint walks this tree and rules can subscribe to be notified when it hits a specific node type, like a Literal type, which could be the "hello" part of const welcome = "hello";.

Go ahead and play around with some code in AST Explorer (Make sure the parser is espree). It's a great tool!

Here are some good articles on the subject (ignore the scaffolding parts):

sindresorhus /
Created Jan 17, 2017 — forked from XVilka/
True Colour (16 million colours) support in various terminal applications and terminals

Colours in terminal

It's a common confusion about terminal colours... Actually we have this:

  • plain ascii
  • ansi escape codes (16 colour codes with bold/italic and background)
  • 256 colour palette (216 colours + 16 ansi + 24 gray) (colors are 24bit)
  • 24bit true colour ("888" colours (aka 16 milion))
printf "\x1b[${bg};2;${red};${green};${blue}m\n"
sindresorhus / post-merge
Last active Sep 8, 2021
git hook to run a command after `git pull` if a specified file was changed.In this example it's used to run `npm install` if package.json changed and `bower install` if `bower.json` changed.Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
View post-merge
#!/usr/bin/env bash
# MIT © Sindre Sorhus -
# git hook to run a command after `git pull` if a specified file was changed
# Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
sindresorhus / countrycode-latlong-array.json
Created Nov 5, 2011
Country codes (ISO 3166) to latitude longitude - converted from
View countrycode-latlong-array.json
"ad": [
"ae": [
"af": [
View UIImage+asActivityItemSource.swift
extension UIImage {
private final class UIImageActivityItemSource: NSObject, UIActivityItemSource {
var image = UIImage()
var title: String?
var url: URL?
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
View strip-accents.js
function stripAccents(str) {
var reAccents = /[àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ]/g;
var replacements = 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY';
return str.replace(reAccents, function (match) {
return replacements[reAccents.source.indexOf(match)];