# cookie いろいろやってみた📝

cookie いろいろいじってみたので、メモを残しておきます。

# Rails

# 保存

[1] pry(#<TopController>)> cookies[:test] = 'aiueo'
=> "aiueo"

# 有効期限の設定

[2] pry(#<TopController>)> cookies[:test2] = { value: 'sample', expires: 1.day.after }
=> {:value=>"sample",
 :expires=>Sun, 30 Jun 2019 01:13:59 JST +09:00,
 :path=>"/"}

[3] pry(#<TopController>)> cookies[:test3] = { value: true, expires: 3.days.after }
=> {:value=>true, :expires=>Tue, 02 Jul 2019 01:14:30 JST +09:00, :path=>"/"}

# 参照

[4] pry(#<TopController>)> cookies[:test]
=> "aiueo"

# 削除

[5] pry(#<TopController>)> cookies.delete :test
=> "aiueo"
[6] pry(#<TopController>)> cookies[:test]
=> nil

# JavaScript

> document.cookie
< "test=aiueo; test2=sample; test3=true;"
> document.cookie.match(/test2=sample/);
< ["test2=sample"] (1)

# 配列内に特定の要素が存在するか

> document.cookie.split("; ").indexOf("test2=sample") >= 0;
< true
> document.cookie.split(";").filter(item => item.includes("test2=sample"));
< [" test2=sample"] (1)
> document.cookie.split(";").filter(item => item.includes("test2=sample")).length;
< 1

# 参考🔗