{"_id":"@ashnazg/literalizer","_rev":"1-8bc732074b10624a941f071b907a1985","name":"@ashnazg/literalizer","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@ashnazg/literalizer","version":"0.0.1","description":"creates literals for code-gen of JS use cases","main":"literalizer.js","repository":{"type":"git","url":"q:/git/me/projects.git"},"repository_path":"ashnazg-npm/literalizer","author":{"name":"Alexander Mathews xander@ashnazg.com"},"license":"MIT","dependencies":{"@ashnazg/async-walk":"^1.0.0","@ashnazg/utils":"^0.1.0"},"_id":"@ashnazg/literalizer@0.0.1","dist":{"shasum":"5963fc0910dcc6334d8d640a80a986b9b3d8ac77","integrity":"sha512-FFvG8oyUop7UpvQQEAqSVZLvwWDOBZqGRPobcHm6ao1wDc46rFXkEz5fBgrQObBymqVKUaiMTHxZlImy4hQQGg==","tarball":"https://registry.npmjs.org/@ashnazg/literalizer/-/literalizer-0.0.1.tgz","fileCount":5,"unpackedSize":13257,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfuaZ/CRA9TVsSAnZWagAA5U0QAIQMpf+vK9ZjylKXNL0V\nUuUGZMOcsm+FGeoY1tNsZLDwWwE/Q/n5jA+rORX0WJbF2AUC3T0o2u4dCqFO\n7aKFDfmFoeIIsqAy1MHLY2YnuICymlYcFT+QUwkKCa1WavzCyTGrIZSsubjE\ns7b0FOPOvtgqS2zFTS0ImCpnsiDBJOO6NVGhFndxl18EiVCZrvS3aN7Mn0GC\n4+I6+Tuu0lpO2+Mpt1q9hREiuHVhKVJ3/iuF/ZR+xOjse9uV6/4EZx4JfJlE\nJtmA/xhiwZPrZTLSRKjXc7z/nRQ1TLUhfSaET8pHYxuC1bBKha3e1YmszP+L\n1Fur+FeuMbe3py4Aw6gXPdca02LXGszRTSFPlPMZkddGY3eSMxFOkmodPfVC\nj5qKZzAHL2SZuOuAlRG9ELP65hTN4XPBF33TVN+eEFn1+a4naC2LSa+AHAIi\ni0uQvwHRFdF+oGGj79cguJS3Wkqb3P56RF3sC57S+e8jnjm49o2k6RzYxmKh\nOWaSmKK23G0MwXC4uoh4IlAOqJOWpnfjKkp9jv8F/rZnxTmQ/Lla95pHouEF\nr7fQ6C6lenvG/uw+x7QBmiQT122Nd5xDa620MvEtnB++SzseYDDCEshhNndZ\nNGd6oJbJ+mh28qwefhek5WZIkeo/9MYzlPh2R1d2H60V9Y5JWZh0N+ZTiR6F\nKf8C\r\n=XTiq\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICV/Iz3s1lB8wJAVwzWgJ+bRahlLc4ezhhC5ekJNuW4BAiBsGWRGQcxXndGbyIt4SKDmptdmyyRGNmcp8sLQS/+tOA=="}]},"_npmUser":{"name":"xander","email":"xander@ashnazg.com"},"directories":{},"maintainers":[{"name":"xander","email":"xander@ashnazg.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/literalizer_0.0.1_1606002303023_0.8489372722850803"},"_hasShrinkwrap":false}},"time":{"created":"2020-11-21T23:45:02.852Z","0.0.1":"2020-11-21T23:45:03.186Z","modified":"2022-04-04T15:45:52.389Z"},"maintainers":[{"name":"xander","email":"xander@ashnazg.com"}],"description":"creates literals for code-gen of JS use cases","repository":{"type":"git","url":"q:/git/me/projects.git"},"author":{"name":"Alexander Mathews xander@ashnazg.com"},"license":"MIT","readme":"---\ntitle: \"@ashnazg/literalizer\"\nsidebar_label: \"literalizer\"\n---\n<!--\nnote to self: it lives at ~/projects/ashnazg-npm/literalizer/literalizer.js\n-->\n\nMuch like the carpal-tunnel silly oneliners in [utils](utils), this liblet was born because I like the literal notation in javascript more than the simpler rules of JSON.\n\nThere's plenty of relaxed JSON _parsers_, but no serializers to those variants that I could find, and I want this for code-generation and config-writing templating tools.\n\n(In other words, I want to dynamically create plain-old-javascript things and then export them as a config file like \".eslintrc.js\" in the same style that would result if I did it by hand.)\n\n## Usage\nAt it's core, it's just an equivalent to `JSON.stringify($, null, '\\t')` that avoids quoting keys:\n\n```\nvar literalizer = require('@ashnazg/literalizer'); // ~/projects/ashnazg-npm/literalizer/literalizer.js\n\nvar map = {foo: 'bar'};\nconsole.log(literalizer(map)); // => {foo: \"bar\"}\n```\n\nIt doesn't (yet) take a replacer, but it does take an optional 2nd param that's a config:\n```\nvar example_predefined_js_variable = ...;\nvar map = {foo: 'bar'};\nliteralizer(map, {\n\tname: 'variable_name_in_output',\n\texports: !!caller_wants_a_node_module_file,\n\tfold_width: 80, // simple non-scalars who's final line is shorter than this are compacted\n\tlocal_scope: {example_predefined_js_variable} // the final output will NOT express the values of any variable mentioned here.\n});\n```\n\n### Compacting some fields based on final width\nThe compactor will pretty-print a map or list in one-line form if and only if:\n1. fold_width is set\n2. The final line is shorter than fold_width (including unpacked tabs at 4-space, key names, and the comma at the end.)\n3. no child element is a non-empty map or list\n\n```\nvar literalizer = require('./literalizer.js');\nvar this_folds = {\n\tsmartcode: false,\n\t'much foldy': 'so much fold.',\n};\nvar this_does_not = {\n\tsmartcode: false,\n\t'much foldy': 'too much to fold.',\n\tribbit: 'ribbit ribbit ribbit.', // because this line pushes it above the width limit given below\n};\n\nvar these_are_all_fine = ['but this is fine', 42, [], {}, null, undefined];\n\nvar but_this_array_has_a_nontrivial_child = [{f:1}];\n\nconsole.log(literalizer({\n\tthis_folds,\n\tthis_does_not,\n\tthese_are_all_fine,\n\tbut_this_array_has_a_nontrivial_child\n}, {\n\tfold_width: 80\n}));\n```\n\n**results**\n```\n{\n\tthis_folds: {smartcode: false, \"much foldy\": \"so much fold.\"},\n\tthis_does_not: {\n\t\tsmartcode: false,\n\t\t\"much foldy\": \"too much to fold.\",\n\t\tribbit: \"ribbit ribbit ribbit.\"\n\t},\n\tthese_are_all_fine: [\"but this is fine\", 42, [], {}, null, undefined],\n\tbut_this_array_has_a_nontrivial_child: [\n\t\t{f: 1}\n\t]\n}\n```\n\n### instant module pattern\nThere's some trivial syntactic sugar to wrap the final output in one of a few common file patterns:\n```\nvar literalizer = require('./literalizer.js');\nfunction genConf() {\n\treturn {\n\t\tprop1: 'val1',\n\t\tsubsettings: {\n\t\t\tthing:'1'\n\t\t}\n\t};\n}\nconsole.log(literalizer(genConf(), {exports: true}));\n```\n**results**\n```\nmodule.exports = {\n\tprop1: \"val1\",\n\tsubsettings: {\n\t\tthing: \"1\"\n\t}\n};\n```\n\n### named module level vars\n```\nconsole.log(literalizer(genConf(), {name: 'new_conf'}));\n```\n**results**\n```\nvar new_conf = {\n\tprop1: \"val1\",\n\tsubsettings: {\n\t\tthing: \"1\"\n\t}\n};\n```\n\n### named exports\n```\nconsole.log(literalizer(genConf(), {name: 'subconfig', exports: true}));\n```\n**results**\n```\nexports.subconfig = {\n\tprop1: \"val1\",\n\tsubsettings: {\n\t\tthing: \"1\"\n\t}\n};\n```\n\n### depend on existing local scope vars\nIf you're already writing js vars in the file's preamble before the part generated using this library, you can have the output re-use those instead of blindly serializing the value redundantly:\n\n```\nvar existing_map = {foo:'bar'};\nfunction existingFunc() {};\n\nconsole.log(`\nvar existing_map = {foo:'bar'};\nfunction existingFunc() {};\n`);\n\nconsole.log(literalizer({\n\ttop: 'level prop',\n\tworks_in_maps: {\n\t\tmixed: 'data',\n\t\texistingFunc,\n\t\texisting_map\n\t},\n\tworks_in_lists: [\n\t\t\"because we look up these by value, we can tell when to just use the var name in list mode\",\n\t\texistingFunc,\n\t\texisting_map\n\t]\n}, {\n\tname: 'external',\n\texports: true,\n\tlocal_scope: { existing_map, existingFunc }\n}));\n```\n**results**\n```\nvar existing_map = {foo:'bar'};\nfunction existingFunc() {};\n\nexports.external = {\n\ttop: \"level prop\",\n\tworks_in_maps: {\n\t\tmixed: \"data\",\n\t\texistingFunc,\n\t\texisting_map\n\t},\n\tworks_in_lists: [\n\t\t\"because we look up these by value, we can tell when to just use the var name in list mode\",\n\t\texistingFunc,\n\t\texisting_map\n\t]\n};\n```\n\n## TODO\n1. support renaming in local_scope.\n\t```\n\tthis_doesnt_work: {\n\t\tnewname: existingFunc\n\t},\n\t```\n2. regexs\n3. generators\n4. inline fat arrows\n\n## Release 0.0.1\nHappy with it for the plain old json-like work I'm on right now; annoyed at how much work it takes to reflect details about a JS-function like \"are you a fat arrow?\" that you'd need to isomorphically print the source code in all contexts.\n\n(Since I don't need function literals in my current projects, I'm stopping here, as playing with functions has consumed 90% of the time put in so far for imperfect results.)\n","readmeFilename":"README.md"}