@toreda/strong-types
Version:
Better TypeScript code in fewer lines.
1 lines • 5.37 kB
Source Map (JSON)
{"version":3,"sources":["../src/map/parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,eAAe,CAAC;AAExC,OAAO,EAAC,SAAS,EAAC,MAAM,QAAQ,CAAC;AACjC,OAAO,EAAC,MAAM,EAAC,MAAM,WAAW,CAAC;AACjC,OAAO,EAAC,gBAAgB,IAAI,OAAO,EAAC,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAC,cAAc,IAAI,KAAK,EAAC,MAAM,gBAAgB,CAAC;AAEvD;;;;GAIG;AACH,qBAAa,SAAS;IACd,KAAK,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,OAAO,GAAG,IAAI,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO;IAc/E,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,GAAG,IAAI;IAmBrE,QAAQ,CAAC,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,GAAG,OAAO;IAkC7F;;;;;;;;OAQG;IACI,QAAQ,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI,EAAE,UAAU,EAAE,KAAK,GAAG,OAAO;CAkCjF","file":"parser.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 */\nimport type {Data} from '@toreda/types';\n\nimport {StrongMap} from '../map';\nimport {Strong} from '../strong';\nimport {MapParserOptions as Options} from './parser/options';\nimport {MapParserState as State} from './parser/state';\n\n/**\n * Recursively parse provided object properties.\n *\n * @category Strong Map\n */\nexport class MapParser {\n\tpublic parse(map: StrongMap, data?: Data | unknown | null, options?: Options): boolean {\n\t\tif (!map) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (!data) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst parseState = new State(options);\n\n\t\treturn this.parseMap(map, data, parseState);\n\t}\n\n\tpublic parseStrongKey(key: Strong, value: unknown, _parseState: State): void {\n\t\tif (!key || value === undefined) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (key.baseType !== 'StrongType') {\n\t\t\treturn;\n\t\t}\n\n\t\tconst strongValue = value as Strong;\n\t\t// When value is also a StrongType invoke it to get its value. Otherwise set\n\t\t// the strong key with value.\n\t\tif (strongValue != null && strongValue.baseType === 'StrongType') {\n\t\t\tkey(strongValue());\n\t\t} else {\n\t\t\tkey(value);\n\t\t}\n\t}\n\n\tpublic parseKey(map: StrongMap, keyName: string, value: unknown, _parseState: State): boolean {\n\t\tif (!map) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (value === undefined) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (typeof keyName !== 'string' || !keyName) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (value === null) {\n\t\t\tmap[keyName] = null;\n\t\t\treturn true;\n\t\t}\n\n\t\tlet result: Strong | unknown;\n\t\tconst strongValue = value as Strong;\n\t\tif (strongValue.hasOwnProperty('typeId') && strongValue.baseType === 'StrongType') {\n\t\t\tresult = strongValue();\n\t\t} else {\n\t\t\tresult = value;\n\t\t}\n\n\t\tif (typeof map[keyName] !== typeof result) {\n\t\t\treturn false;\n\t\t}\n\n\t\tmap[keyName] = result;\n\t\treturn true;\n\t}\n\n\t/**\n\t * Recursively parse map and children.\n\t * @param map\t\t\tMap to match properties against and store parsed values.\n\t * @param json\t\t\tObject to parse into map.\n\t * @param parseState\tInternal state for current parse.\n\t * @returns\t\t\t\tBoolean indicating success or failure.\n\t *\t\t\t\t\t\ttrue\t- \tMap parse successful.\n\t *\t\t\t\t\t\tfalse\t-\tMap parse not successful.\n\t */\n\tpublic parseMap(map: StrongMap, data: unknown | null, parseState: State): boolean {\n\t\tif (!map) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (typeof data === 'undefined' || data === {}) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst keys: string[] = Object.keys(map);\n\n\t\tfor (const keyName of keys) {\n\t\t\tconst child = map[keyName];\n\t\t\tconst keyValue = (data as Data)[keyName];\n\n\t\t\t// Skip built-in properties.\n\t\t\tif (!map.hasOwnProperty(keyName)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Child is also a StrongMap. Parse it recursively.\n\t\t\tif (child instanceof StrongMap) {\n\t\t\t\tthis.parseMap(child, keyValue as Data, parseState);\n\t\t\t} else if ((child as Strong)?.baseType === 'StrongType') {\n\t\t\t\t// Child is a StrongType.\n\t\t\t\tthis.parseStrongKey(child as Strong, keyValue, parseState);\n\t\t\t} else if (typeof child !== 'object') {\n\t\t\t\t// Child is not a StrongType and not an object.\n\t\t\t\tthis.parseKey(map, keyName, keyValue, parseState);\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n}\n"]}