Skip to content

Commit

Permalink
Added support for returning OrderedDict when using ntuple2dict. (#…
Browse files Browse the repository at this point in the history
…288)

* Added support for returning `OrderedDict` when using `ntuple2dict`.

* Added `[type = Dict,] nt` in the docstring for `ntuple2dict`.
  • Loading branch information
rs7q5 authored Sep 11, 2021
1 parent 72e683b commit afefe90
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DrWatson"
uuid = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
repo = "https://github.com/JuliaDynamics/DrWatson.jl.git"
version = "2.4.1"
version = "2.4.2"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
5 changes: 3 additions & 2 deletions src/naming.jl
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,11 @@ end
# Credit of `ntuple` macro goes to Sebastian Pfitzner, @pfitzseb

"""
ntuple2dict(nt) -> dict
ntuple2dict([type = Dict,] nt) -> dict
Convert a `NamedTuple` to a dictionary.
"""
ntuple2dict(nt::NamedTuple) = Dict(k => nt[k] for k in keys(nt))
ntuple2dict(::Type{DT},nt::NamedTuple) where {DT<:AbstractDict} = DT(k => nt[k] for k in keys(nt))
ntuple2dict(nt::NamedTuple) = ntuple2dict(Dict,nt)

"""
dict2ntuple(dict) -> ntuple
Expand Down
6 changes: 6 additions & 0 deletions test/stools_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ rm(tmpdir, force = true, recursive = true)
d11 = tosymboldict(OrderedDict,d10)
@test isa(d11,OrderedDict)

#test ntuple2dict
x = 3; y = 5.0;
n = @ntuple x y
@test isa(ntuple2dict(n),Dict)
@test isa(ntuple2dict(OrderedDict,n),OrderedDict)

#test checktagtype!
@test isa(DrWatson.checktagtype!(d3),Dict)
@test isa(DrWatson.checktagtype!(d11),OrderedDict)
Expand Down

0 comments on commit afefe90

Please sign in to comment.