Skip to content

Instantly share code, notes, and snippets.

@takasek
takasek / code.gs
Created May 27, 2022 13:32
Notion API で、databaseの "Done" がついてるかどうかを見て "Name" の頭に "■" をつけたり消したりする
function doGet() {
const notion_token = 'secret_...';
const database_id = '...';
const headers = {
'Content-Type' : 'application/json; charset=UTF-8',
'Authorization': `Bearer ${notion_token}`,
'Notion-Version': '2021-05-13',
};
const fetched = UrlFetchApp.fetch(`https://api.notion.com/v1/databases/${database_id}/query`, {
"method" : "post",
@takasek
takasek / AtCoderTemplate.swift
Last active October 30, 2020 01:09
SwiftでAtCoderに挑戦するためのPlayground用テンプレート
import Foundation
struct Example {
let input: String
let expectation: String
}
// テストケース列挙
let examples: [(String, Example)] = [
("1", Example(
input: """
@takasek
takasek / PolymorphicDecodable.swift
Last active July 18, 2020 09:39
「派生型プロパティを Decodable で扱う」 https://hitorigoto.zumuya.com/200716_decodableProtocolProperty にインスパイヤされたコード。いじってたらPropertyWrapperがなくなってしまった…
// 汎用コード
struct CustomCodingKey: CodingKey, ExpressibleByStringLiteral {
let stringValue: String
let intValue: Int? = nil
init?(stringValue: String) { self.stringValue = stringValue }
init?(intValue: Int) { return nil }
init(stringLiteral value: String) { stringValue = value }
init(_ value: String) { stringValue = value }
}
@takasek
takasek / CodePiece.rb
Created July 11, 2020 13:04
さっきの https://anoqode.com/question?id=9dd1d9f9-bba9-4cde-b331-1ab78b155b01 ですけど、RawRepresentableなstructを噛ませておくと、失敗した文字列も失わずに済むので好きです #swiftzoomin #CodePiece
let jsonData = """
[
"https://www.apple.com",
"https://swift.org/blog/",
"https://github.com/apple/swift/releases/tag/swift 5.2.4-RELEASE"
]
"""
.data(using: .utf8)!
struct SafeURL: RawRepresentable, Decodable {
let rawValue: String
@takasek
takasek / EntityID.swift
Last active October 15, 2019 02:52
「EntityはIDを持ってるよね」をprotocolで表現し、Entity.IDを定義不要にする
protocol Entity {
associatedtype RawIDValue: Hashable
typealias ID = EntityID<Self>
}
struct EntityID<E: Entity>: RawRepresentable, Hashable {
let rawValue: E.RawIDValue
}
extension EntityID {
init(_ value: E.RawIDValue) {
@takasek
takasek / CodePiece.swift
Created June 2, 2019 07:45
静的型付け言語であるSwiftもダックタイピングはできるよ(オブジェクト指向(メッセージ指向)言語であるObj-Cとcompatibleだから)というコード #CodePiece
class Cat: NSObject {
@objc func bark() { print("meow") }
func hoge() {}
}
class Dog: NSObject {
@objc func bark() { print("bow-wow") }
func hoge() {}
}
let cat = Cat()
@takasek
takasek / CodePiece.swift
Created March 21, 2019 08:34
`KeyPath` for immutable property works differently between Swift4.2 / 5 #CodePiece #tryswiftconf
func replace<T>(_ root: inout T, keyPath: KeyPath<T, String>) {
if let k = keyPath as? WritableKeyPath<T, String> {
print("writable:", keyPath)
root[keyPath: k] += " 🖍"
} else {
print("not writable:", keyPath)
}
}
struct S {
let a = "let"
@takasek
takasek / iOS_architecture_03.swift
Created February 11, 2019 10:42
iOSアプリ設計パターン入門 第3章サンプルコード
protocol MessageSenderDelegate {
func stateの変化を伝える()
}
protocol Message {
}
protocol MessageInput {
associatedtype Payload
func validate() throws -> Payload
}
@takasek
takasek / CodingKeyToSnake.swift
Last active August 5, 2020 06:08
CodingKeyをJSONのkeyに寄せて書くことができた!
// https://github.com/apple/swift/blob/b0f5815d2b003df628b1bcfe94681fec489c9492/stdlib/public/Darwin/Foundation/JSONEncoder.swift#L153
func _convertToSnakeCase(_ stringKey: String) -> String {
guard !stringKey.isEmpty else { return stringKey }
var words : [Range<String.Index>] = []
// The general idea of this algorithm is to split words on transition from lower to upper case, then on transition of >1 upper case characters to lowercase
//
// myProperty -> my_property
// myURLProperty -> my_url_property
//
@takasek
takasek / LoginUseCaseTests.swift
Created September 28, 2018 04:58
RxTest, TestSchedulerによるテスト例
import RxSwift
import RxCocoa
import RxTest
struct LoginData: Equatable {
let id: Int
}
protocol LoginRepository {
func post(data: LoginData) -> Single<Account>
}