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KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;CAkBxC","file":"data.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 {ANY} from '@toreda/types';\nimport {Rules} from '../rules';\nimport {StrongTypeId} from './type/id';\nimport {Transforms} from '../transforms';\n\n/**\n * @category Core\n */\nexport class StrongData<ValueT> {\n\tpublic value: ValueT | null;\n\tpublic readonly initial: ValueT | null;\n\tpublic readonly fallbackDefault: ValueT;\n\tpublic readonly transforms: Transforms<ValueT>;\n\tpublic readonly rules: Rules<ValueT>;\n\tpublic readonly typeId: StrongTypeId;\n\tpublic readonly baseType: StrongTypeId;\n\n\tconstructor(fallbackDefault: ValueT, value: ValueT | null, rules: Rules<ValueT>, typeId: StrongTypeId) {\n\t\tthis.value = null;\n\t\tthis.fallbackDefault = fallbackDefault;\n\t\tthis.transforms = new Transforms<ValueT>(fallbackDefault);\n\t\tthis.rules = rules;\n\t\tthis.typeId = typeId;\n\t\tthis.baseType = 'StrongData';\n\t\tthis.initial = value;\n\t\tthis.set(value);\n\t}\n\n\t/**\n\t * Check if value passes this instance's rule validation.\n\t * @param value\n\t * @returns\n\t */\n\tpublic check(value?: ValueT | null): boolean {\n\t\tif (value === undefined) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn this.rules.run(value);\n\t}\n\n\tpublic get(fallback: ValueT): ValueT {\n\t\tif (this.value === null) {\n\t\t\tif (fallback === undefined || fallback === null) {\n\t\t\t\treturn this.fallbackDefault;\n\t\t\t}\n\n\t\t\treturn fallback;\n\t\t}\n\n\t\treturn this.value;\n\t}\n\n\tpublic set(value?: ValueT | null): boolean {\n\t\tif (value === undefined) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (value === null) {\n\t\t\tthis.value = null;\n\t\t\treturn true;\n\t\t}\n\n\t\tconst transformed = value;\n\n\t\tif (!this.check(value)) {\n\t\t\treturn false;\n\t\t}\n\n\t\tthis.value = transformed;\n\n\t\treturn true;\n\t}\n\n\tpublic getNull(): ValueT | null {\n\t\tif (this.value === undefined || this.value === null) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn this.value;\n\t}\n\n\tpublic reset(): void {\n\t\tthis.value = this.initial;\n\t}\n\n\t/**\n\t * Divide current `value` by `divisor`. Result is zero when\n\t * `divisor` or `value` are zero.\n\t * @param divisor\n\t * @returns\n\t */\n\tpublic div(divisor: number): number | null {\n\t\tconst curr = this.getNull();\n\n\t\tif (typeof divisor !== 'number' || typeof curr !== 'number') {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (divisor === 0 || curr === 0) {\n\t\t\tthis.set(0 as ANY);\n\t\t\treturn 0;\n\t\t}\n\n\t\tconst result = curr / divisor;\n\n\t\tif (isNaN(result)) {\n\t\t\treturn null;\n\t\t}\n\n\t\tthis.set(result as ANY);\n\t\treturn result;\n\t}\n\n\tpublic mul(value: number): number | null {\n\t\tconst curr = this.getNull();\n\t\tif (typeof value !== 'number' || typeof curr !== 'number') {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (value === 0 || curr === 0) {\n\t\t\tthis.set(0 as ANY);\n\t\t\treturn 0;\n\t\t}\n\n\t\tconst result = value * curr;\n\t\treturn this.set(result as ANY) ? result : null;\n\t}\n\n\t/**\n\t *\n\t * @param exponent\n\t * @returns\n\t */\n\tpublic pow(exponent: number): number | null {\n\t\tconst curr = this.getNull();\n\n\t\tif (typeof curr !== 'number' || curr === null) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst result = Math.pow(curr, exponent);\n\t\tif (isNaN(result)) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (result >= Number.MAX_SAFE_INTEGER) {\n\t\t\treturn null;\n\t\t}\n\n\t\tthis.set(result as ANY);\n\t\treturn result;\n\t}\n\n\t/**\n\t * Add value to Strong Type's current value, if it is a numeric type. Operation\n\t * ignored for non-numeric types.\n\t * @param value\n\t * @returns\n\t */\n\tpublic add(value: number): number | null {\n\t\tconst curr = this.getNull();\n\n\t\tif (typeof value !== 'number' || typeof curr !== 'number') {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst result = value + curr;\n\t\tif (isNaN(result)) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (result < Number.MIN_SAFE_INTEGER || result > Number.MAX_SAFE_INTEGER) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn this.set(result as ANY) ? result : null;\n\t}\n}\n"]}