Skip to content

Commit 5eea5d2

Browse files
authored
fix: parser treats "Nan" as a number instead of a string (#22)
* fix: parser treats "Nan" as a number instead of a string
1 parent 5b3cbe2 commit 5eea5d2

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
- Fixed typo in `ObsidianPasteImg`'s command description
2626
- Fixed the case when `opts.attachments` is `nil`.
2727
- Fixed bug where `ObsidianNewFromTemplate` did not respect `note_id_func`
28+
- Fixed bug where parser treats "Nan" as a number instead of a string
2829

2930
## [v3.9.0](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.9.0) - 2024-07-11
3031

lua/obsidian/util.lua

+8
Original file line numberDiff line numberDiff line change
@@ -1341,4 +1341,12 @@ util.buffer_is_empty = function(bufnr)
13411341
end
13421342
end
13431343

1344+
---Check if a string is NaN
1345+
---
1346+
---@param v any
1347+
---@return boolean
1348+
util.isNan = function(v)
1349+
return tostring(v) == tostring(0 / 0)
1350+
end
1351+
13441352
return util

lua/obsidian/yaml/parser.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ end
555555
---@diagnostic disable-next-line: unused-local
556556
Parser._parse_number = function(self, i, text)
557557
local out = tonumber(text)
558-
if out == nil then
558+
if out == nil or util.isNan(out) then
559559
return false, nil, nil
560560
else
561561
return true, nil, out

test/obsidian/yaml/parser_spec.lua

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ describe("Parser class", function()
3030
return parser:parse_number(str)
3131
end, "foo")
3232
assert.is_false(ok)
33+
ok, _ = pcall(function(str)
34+
return parser:parse_number(str)
35+
end, "Nan")
36+
assert.is_false(ok)
3337
end)
3438

3539
it("should parse booleans while trimming whitespace", function()

0 commit comments

Comments
 (0)