UNPKG

@toreda/strong-types

Version:

Better TypeScript code in fewer lines.

1 lines 3.51 kB
{"version":3,"sources":["../src/create/type.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAC,MAAM,EAAC,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAC;AAE/C;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAChC,eAAe,EAAE,MAAM,EACvB,OAAO,EAAE,MAAM,GAAG,IAAI,EACtB,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,EACpB,MAAM,EAAE,YAAY,GAClB,MAAM,CAAC,MAAM,CAAC,CAqDhB","file":"type.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 {Rules} from '../rules';\nimport {Strong} from '../strong';\nimport {StrongData} from '../strong/data';\nimport {StrongTypeId} from '../strong/type/id';\n\n/**\n * Create a StrongType.\n * @param fallbackArg\n * @param initial\n * @param rules\n * @returns\n *\n * @category Core\n */\nexport function createType<ValueT>(\n\tfallbackDefault: ValueT,\n\tinitial: ValueT | null,\n\trules: Rules<ValueT>,\n\ttypeId: StrongTypeId\n): Strong<ValueT> {\n\tconst instance = new StrongData<ValueT>(fallbackDefault, initial, rules, typeId);\n\n\tconst baseType: StrongTypeId = 'StrongType';\n\n\treturn Object.assign(\n\t\t(value?: ValueT | null): ValueT => {\n\t\t\tif (typeof value !== 'undefined') {\n\t\t\t\tinstance.set(value);\n\t\t\t}\n\n\t\t\treturn instance.get(instance.fallbackDefault);\n\t\t},\n\t\t{\n\t\t\t/**\n\t\t\t * Get current value and return provided fallback if\n\t\t\t * @param fallback\n\t\t\t * @returns\n\t\t\t */\n\t\t\tget: (fallback: ValueT): ValueT => {\n\t\t\t\treturn instance.get(fallback);\n\t\t\t},\n\t\t\t/**\n\t\t\t * Get current value, or null if there isn't one.\n\t\t\t * @returns\t\tCurrent value when set, otherwise null.\n\t\t\t */\n\t\t\tgetNull: (): ValueT | null => {\n\t\t\t\treturn instance.getNull();\n\t\t\t},\n\t\t\t/**\n\t\t\t * Reset instance properties to their starting values.\n\t\t\t */\n\t\t\treset: (): void => {\n\t\t\t\tinstance.reset();\n\n\t\t\t\tif (initial !== undefined) {\n\t\t\t\t\tinstance.set(initial);\n\t\t\t\t}\n\t\t\t},\n\t\t\t/**\n\t\t\t * Read-only check to determine if provided value passes\n\t\t\t * rule validation for this instance.\n\t\t\t * @param value\n\t\t\t * @returns\n\t\t\t */\n\t\t\tcheck: (target?: ValueT | null): boolean => {\n\t\t\t\treturn instance.check(target);\n\t\t\t},\n\t\t\ttypeId: typeId,\n\t\t\tbaseType: baseType,\n\t\t\t_data: instance\n\t\t}\n\t);\n}\n"]}