UNPKG

@toreda/strong-types

Version:

Better TypeScript code in fewer lines.

1 lines 3.39 kB
{"version":3,"sources":["../src/map/jsonifier.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAGH,OAAO,EAAC,SAAS,EAAC,MAAM,QAAQ,CAAC;AAEjC;;GAEG;AACH,qBAAa,YAAY;IACjB,OAAO,CAAC,GAAG,EAAE,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAQhD,UAAU,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAgCjD,UAAU,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO;CAiBxC","file":"jsonifier.d.ts","sourcesContent":["/**\n *\tMIT License\n *\n *\tCopyright (c) 2019 - 2021 Toreda, Inc.\n *\n *\tPermission is hereby granted, free of charge, to any person obtaining a copy\n *\tof this software and associated documentation files (the \"Software\"), to deal\n *\tin the Software without restriction, including without limitation the rights\n *\tto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n *\tcopies of the Software, and to permit persons to whom the Software is\n *\tfurnished to do so, subject to the following conditions:\n\n * \tThe above copyright notice and this permission notice shall be included in all\n * \tcopies or substantial portions of the Software.\n *\n * \tTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n *\tIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n *\tFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * \tAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n *\tLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n *\tOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * \tSOFTWARE.\n *\n */\n\nimport type {Strong} from '../strong';\nimport {StrongMap} from '../map';\n\n/**\n * @category Strong Map\n */\nexport class MapJsonifier {\n\tpublic jsonify(map: StrongMap): Record<string, unknown> {\n\t\tif (!map) {\n\t\t\tthrow Error(`Bad MapJsonifier.jsonify attempt - map arg missing.`);\n\t\t}\n\n\t\treturn this.jsonifyMap(map);\n\t}\n\n\tpublic jsonifyMap(map: unknown): Record<string, unknown> {\n\t\tconst result: Record<string, unknown> = {};\n\n\t\tconst keys = Object.keys(map as Record<string, unknown>);\n\n\t\tfor (const keyName of keys) {\n\t\t\tconst child: unknown = (map as Record<string, unknown>)[keyName];\n\n\t\t\tif (child === undefined) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (child === null) {\n\t\t\t\tresult[keyName] = null;\n\t\t\t} else if (child instanceof StrongMap) {\n\t\t\t\tresult[keyName] = this.jsonifyMap(child);\n\t\t\t} else if ((child as Strong).baseType === 'StrongType') {\n\t\t\t\tresult[keyName] = this.jsonifyKey(child);\n\t\t\t} else if (typeof child !== 'object') {\n\t\t\t\tresult[keyName] = this.jsonifyKey(child);\n\t\t\t} else if (Array.isArray(child)) {\n\t\t\t\tresult[keyName] = this.jsonifyKey(child);\n\t\t\t} else if ((child as HTMLElement)?.nodeType != null) {\n\t\t\t\tresult[keyName] = this.jsonifyKey(child);\n\t\t\t} else {\n\t\t\t\tresult[keyName] = this.jsonifyMap(child);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tpublic jsonifyKey(key: unknown): unknown {\n\t\tif (key === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif (key === null) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst assumeKeyIsStrongType = key as Strong;\n\n\t\tif (assumeKeyIsStrongType?.baseType === 'StrongType') {\n\t\t\treturn assumeKeyIsStrongType();\n\t\t}\n\n\t\treturn key;\n\t}\n}\n\ntype HTMLElement = {\n\tnodeType: number;\n};\n"]}