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 / CodePiece.swift
Created October 4, 2016 09:40
やっぱり、他の数値型と違って、Boolをas NSNumberしたときだけは、同値は同一のclassインスタンスになるようだ #CodePiece
let num = 1 as NSNumber
num === num
//true
(num as Int as NSNumber) === (num as Int as NSNumber)
//false
(num as UInt as NSNumber) === (num as UInt as NSNumber)
//false
@takasek
takasek / CodePiece.swift
Created January 20, 2018 07:16
structのmutating funcでも、クロージャ内ではselfを書き替えることはできないよという話 #love_swift #CodePiece
struct A {
var hoge = "a" {
didSet { print("fixed", hoge) }
}
// たとえmutatingなfuncの中でも、
mutating func fix() {
// クロージャの中ではselfの書き換えはできない
DispatchQueue.main.async {
self.hoge = "b" // Error: Closure cannot implicitly capture a mutating self parameter
@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 / 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 / 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 / wf_text_counter.js
Last active November 4, 2019 15:02
Text counter for WorkFlowy
// もはや動いていなかったので削除
// 修正していただい版をご利用ください
// https://gist.github.com/jnory/b39cd65508fbaa2fa39ebed932ca47a7
@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()