irongit

A Neovim plugin enabling you to solve LeetCode problems.

refactor(cookie): trim cookie on read (#95)

kawreauthored
parent a92e764commit 458da8ea7e73e8ed221165aa56a595e33c4ec644Browse files

1 file changed, +13 -2

+13-2lua/leetcode/cache/cookie.lua
@@ -41,6 +41,17 @@ function Cookie.delete()
4141 return pcall(path.rm, file)
4242 end
4343
44+---@return string|nil
45+function Cookie.read()
46+ local contents = file:read()
47+
48+ if not contents or type(contents) ~= "string" then
49+ return
50+ end
51+
52+ return select(1, contents:gsub("^%s*(.-)%s*$", "%1"))
53+end
54+
4455 ---@return lc.cache.Cookie | nil
4556 function Cookie.get()
4657 if not file:exists() then
@@ -55,8 +66,8 @@ function Cookie.get()
5566 return hcookie
5667 end
5768
58- local contents = file:read()
59- if not contents or type(contents) ~= "string" then
69+ local contents = Cookie.read()
70+ if not contents then
6071 require("leetcode.command").delete_cookie()
6172 return
6273 end