diff --git a/changelog.md b/changelog.md index 8bdbc7416980..9078895c9edf 100644 --- a/changelog.md +++ b/changelog.md @@ -84,9 +84,6 @@ - Added `httpcore.is1xx` and missing HTTP codes. - Added `jsconsole.jsAssert` for JavaScript target. -- Added `mimetypes.mimesExtMaxLen` thats equal to the length of the longest "ext" from `mimes`. -- Added `mimetypes.mimesMaxLen` thats equal to the length of the longest "mime" from `mimes`. - - Added `posix_utils.osReleaseFile` to get system identification from `os-release` file on Linux and the BSDs. https://www.freedesktop.org/software/systemd/man/os-release.html diff --git a/lib/pure/mimetypes.nim b/lib/pure/mimetypes.nim index 880ccdff1117..89054fc38ae4 100644 --- a/lib/pure/mimetypes.nim +++ b/lib/pure/mimetypes.nim @@ -8,7 +8,25 @@ # ## This module implements a mimetypes database -import strtabs, std/private/since + +runnableExamples: + var m = newMimetypes() + doAssert m.getMimetype("mp4") == "video/mp4" + doAssert m.getExt("text/html") == "html" + ## Values can be uppercase too. + doAssert m.getMimetype("MP4") == "video/mp4" + doAssert m.getExt("TEXT/HTML") == "html" + ## If values are invalid then ``default`` is returned. + doAssert m.getMimetype("INVALID") == "text/plain" + doAssert m.getExt("INVALID/NONEXISTENT") == "txt" + doAssert m.getMimetype("") == "text/plain" + doAssert m.getExt("") == "txt" + ## Register new Mimetypes. + m.register(ext = "fakext", mimetype = "text/fakelang") + doAssert m.getMimetype("fakext") == "text/fakelang" + doAssert m.getMimetype("FaKeXT") == "text/fakelang" + +import strtabs from strutils import startsWith, toLowerAscii, strip type @@ -1916,49 +1934,3 @@ func register*(mimedb: var MimeDB, ext: string, mimetype: string) = assert mimetype.strip.len > 0, "mimetype argument can not be empty string" {.noSideEffect.}: mimedb.mimes[ext.toLowerAscii()] = mimetype.toLowerAscii() - - -since (1, 5): - func mimesLongest(): array[2, int] {.compiletime.} = - runnableExamples: - static: - doAssert mimesLongest() >= (ext: 24, mime: 73) - var currentKeyLength, currentValLength: int - for item in mimes: - currentKeyLength = item[0].len - currentValLength = item[1].len - if currentKeyLength > result[0]: result[0] = currentKeyLength - if currentValLength > result[1]: result[1] = currentValLength - - const - ctValue = mimesLongest() # Use 2 const instead of func, save tuple unpack. - mimesExtMaxLen*: int = ctValue[0] ## \ - ## The length of the longest "ext" from `mimes`, - ## this is useful for optimizations with `newStringOfCap` and `newString`. - mimesMaxLen*: int = ctValue[1] ## \ - ## The length of the longest "mime" from `mimes`, - ## this is useful for optimizations with `newStringOfCap` and `newString`. - ## - ## See also: - ## * `newStringOfCap `_ - ## * `newString `_ - - -runnableExamples: - static: - block: - var m = newMimetypes() - doAssert m.getMimetype("mp4") == "video/mp4" - doAssert m.getExt("text/html") == "html" - ## Values can be uppercase too. - doAssert m.getMimetype("MP4") == "video/mp4" - doAssert m.getExt("TEXT/HTML") == "html" - ## If values are invalid then ``default`` is returned. - doAssert m.getMimetype("INVALID") == "text/plain" - doAssert m.getExt("INVALID/NONEXISTENT") == "txt" - doAssert m.getMimetype("") == "text/plain" - doAssert m.getExt("") == "txt" - ## Register new Mimetypes. - m.register(ext = "fakext", mimetype = "text/fakelang") - doAssert m.getMimetype("fakext") == "text/fakelang" - doAssert m.getMimetype("FaKeXT") == "text/fakelang" diff --git a/tests/stdlib/tmimetypes.nim b/tests/stdlib/tmimetypes.nim new file mode 100644 index 000000000000..cd41e614bcc7 --- /dev/null +++ b/tests/stdlib/tmimetypes.nim @@ -0,0 +1,13 @@ +discard """ + targets: "c js" +""" + +import std/mimetypes +template main() = + var m = newMimetypes() + doAssert m.getMimetype("mp4") == "video/mp4" + # see also `runnableExamples`. + # xxx we should have a way to avoid duplicating code between runnableExamples and tests + +static: main() +main()