A Neovim plugin enabling you to solve LeetCode problems.
refactor(cookie): trim cookie on read (#95)
1 file changed, +13 -2
+13-2lua/leetcode/cache/cookie.lua
| @@ -41,6 +41,17 @@ function Cookie.delete() | ||
| 41 | 41 | return pcall(path.rm, file) |
| 42 | 42 | end |
| 43 | 43 | |
| 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 | + | |
| 44 | 55 | ---@return lc.cache.Cookie | nil |
| 45 | 56 | function Cookie.get() |
| 46 | 57 | if not file:exists() then |
| @@ -55,8 +66,8 @@ function Cookie.get() | ||
| 55 | 66 | return hcookie |
| 56 | 67 | end |
| 57 | 68 | |
| 58 | - local contents = file:read() | |
| 59 | - if not contents or type(contents) ~= "string" then | |
| 69 | + local contents = Cookie.read() | |
| 70 | + if not contents then | |
| 60 | 71 | require("leetcode.command").delete_cookie() |
| 61 | 72 | return |
| 62 | 73 | end |