目的
APIリクエスト・レスポンスをログに吐きだす際など用のメモ書き
例
javascript
const MASK_KEYS = ['email', 'password']
const maskJson = obj =>
MASK_KEYS.reduce(
(memo, key) =>
memo.replace(new RegExp(`"${key}":"[^,}]*"`), `"${key}":"XXXXXX"`),
JSON.stringify(obj)
)
console
maskJson({ email: 'sample@email.com', password: 'password' })
=> "{"email":"XXXXXX","password":"XXXXXX"}"
ruby
MASK_KEYS = %w[email password].freeze
def mask_json(hash)
MASK_KEYS.inject(JSON.generate(hash)) { |memo, key| memo.gsub(/("#{key}":"[^,}]*")/, "\"#{key}\":\"XXXXXX\"") }
end
console
mask_json({ email: 'sample@email.com', password: 'password' })
=> "{\"email\":\"XXXXXX\",\"password\":\"XXXXXX\"}"