Skip to content

Instantly share code, notes, and snippets.

@jakub-g
jakub-g / .profile
Last active September 12, 2022 19:47
npm run Win-compatible bash completion (for custom scripts defined in `package.json`)
#!/bin/bash
alias npmrun='npm run'
alias _npmscripts_print="node -e \"console.log(Object.keys(require('./package.json').scripts, null, ' ').join(' '))\""
_npmscripts_completion()
{
local cur=${COMP_WORDS[COMP_CWORD]}
opts=$( _npmscripts_print )
COMPREPLY=( $(compgen -W "${opts}" -- $cur) )
}
complete -F _npmscripts_completion npmrun
@mluton
mluton / CachedDateFormatter.swift
Last active August 8, 2023 12:04
Swift class that instantiates and caches NSDateFormatter objects
class CachedDateFormatter {
static let sharedInstance = CachedDateFormatter()
var cachedDateFormatters = [String: NSDateFormatter]()
func formatterWith(#format: String, timeZone: NSTimeZone = NSTimeZone.localTimeZone(), locale: NSLocale = NSLocale(localeIdentifier: "en_US")) -> NSDateFormatter {
let key = "\(format.hashValue)\(timeZone.hashValue)\(locale.hashValue)"
if let cachedDateFormatter = cachedDateFormatters[key] {
return cachedDateFormatter
}