Open eaton opened 1 year ago
Thank you @eaton! I'm pretty busy now. Promise to review it soon.
No worries — as you said, you're not working on this project as part of your job now, so no expectations. Just wanted to be sure I got this tidied up as an official PR rather than just playing in my own fork!
As briefly discussed in #116, this pull request adds the concept of pluggable serializers to fs-jetpack's read and write operations. Currently, JSON and JSON with dates are treated as special conveniences. This patch allows any object with
parse()
andstringily()
functions to be assigned as the serializer for a particular file extension.setSerializer(extension, serializer)
,deleteSerializer(extension)
, andlistSerializers()
functions have been added to the jetpack api.read(path, returnAs)
now supports an "auto" returnAs mode; this will check for file's extension in the serializers list, and attempt to parse the incoming data if a matching one is found. A custom parsing function can also be passed in, in lieu of the already-set serializers.write(path, data, options)
now supports a "serializer" option; it can contain a custom serializer object orfalse
; if set to false, no auto-serialization will occur. If the option is unset, the file's extension will be used to find a matching serializer in the current serializers map.serializeToJsonMaybe()
function has been renamed toserializeMaybe()
, and its safety checks to ensure the incoming data is serializable have been moved to the default JSON Serializer object. Any serializer that implements avalidate()
function can use the opportunity to check if the incoming data is serializable before proceeding. If it is not, serialization is skipped, just like the current JSON behavior.serializers.spec.ts
.Usage example:
While I'm relatively new to the fs-jetpack codebase, I've tried to follow its conventions as closely as possible and avoid any unnecessary side effects in how other functions work. A handful of updates to the read() and write() tests have been made to account for the new option properties, but other than that all other tests are unchanged and passing.
Potential areas for improvement:
read()
method's "auto" as a default mode might make sense given the fact thatreadFile()
allows users to bypass its conveniences. I'm pretty sure that would break existing behaviors, however.jsonIndent
.I've been using the code in this patch on my own projects for a bit to simplify reading/writing NDJSON, JSON5, and yaml configuration files. Adding other formats like TOML, INI, and even goofy stuff like MacOS .plist files is pretty easy. I'm curious if the general approach is appealing as a potential addition to fsJetpack, if a different way of solving the problem would be better, or if it feels altogether out of scope for the library. In any case, thanks for the great work you've done on fsJetpack! It's been a great time saver.