Skip to content

Instantly share code, notes, and snippets.

View negibouze's full-sized avatar

Yoshiaki Itakura negibouze

View GitHub Profile
@negibouze
negibouze / remove-null.ts
Last active November 23, 2020 07:59
Remove null value or convert to undefined.
type RemoveNull = {
(x: null): undefined
<T> (x: T): T
<T> (x: T[]): T[]
}
/**
* null を取り除く
* x が null の場合は undefined を返す
* x が Array の場合は null を undefined に変換した Array を返す
@negibouze
negibouze / create-timer.ts
Last active November 23, 2020 08:35
Create a timer object that executes a function regularly.
export type IntervalUnit = 'hour' | 'minute' | 'second' | 'millisecond'
export type Interval = {
time?: number
unit?: IntervalUnit
}
type StartOptions = {
immediately?: boolean
}
@negibouze
negibouze / gist:aa28c87c0271c2b8f9df
Last active December 16, 2015 02:47
NSDateFormatter extension
extension NSDateFormatter {
/**
カレンダー, ロケール, タイムゾーンを指定して初期化
@param calendarIdentifier カレンダーの識別子 (NSCalendarIdentifierGregorian...)
@param localeIdentifier ロケールの識別子 (ja_JP, en_US_POSIX...)
@param abbreviation タイムゾーンの略称 (GMT, JST...)
*/
convenience init(calendarIdentifier: String, localeIdentifier: String = "ja_JP", abbreviation: String = "GMT") {
self.init()
if let cal = NSCalendar(calendarIdentifier: calendarIdentifier) {
@negibouze
negibouze / gist:ea2a0d9da63b4c1f4790
Created January 20, 2015 07:49
UIColor with RGB
// Pattern A func
func rgb(red r: CGFloat, green g: CGFloat, blue b: CGFloat) -> UIColor {
return rgba(red: r, green: g, blue: b, alpha: 1.0)
}
func rgba(red r: CGFloat, green g: CGFloat, blue b: CGFloat, alpha a: CGFloat) -> UIColor {
return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
// Pattern B Extension