Skip to content

Commit afefe90

Browse files
authored
Added support for returning OrderedDict when using ntuple2dict. (#288)
* Added support for returning `OrderedDict` when using `ntuple2dict`. * Added `[type = Dict,] nt` in the docstring for `ntuple2dict`.
1 parent 72e683b commit afefe90

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DrWatson"
22
uuid = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
33
repo = "https://github.com/JuliaDynamics/DrWatson.jl.git"
4-
version = "2.4.1"
4+
version = "2.4.2"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/naming.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,11 @@ end
346346
# Credit of `ntuple` macro goes to Sebastian Pfitzner, @pfitzseb
347347

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

354355
"""
355356
dict2ntuple(dict) -> ntuple

test/stools_tests.jl

+6
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ rm(tmpdir, force = true, recursive = true)
313313
d11 = tosymboldict(OrderedDict,d10)
314314
@test isa(d11,OrderedDict)
315315

316+
#test ntuple2dict
317+
x = 3; y = 5.0;
318+
n = @ntuple x y
319+
@test isa(ntuple2dict(n),Dict)
320+
@test isa(ntuple2dict(OrderedDict,n),OrderedDict)
321+
316322
#test checktagtype!
317323
@test isa(DrWatson.checktagtype!(d3),Dict)
318324
@test isa(DrWatson.checktagtype!(d11),OrderedDict)

0 commit comments

Comments
 (0)