UNPKG

@toreda/strong-types

Version:

Better TypeScript code in fewer lines.

1 lines 4.6 kB
{"version":3,"sources":["../src/dbl/make.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAGH,OAAO,EAAC,MAAM,EAAC,MAAM,YAAY,CAAC;AAClC,OAAO,EAAC,GAAG,EAAC,MAAM,QAAQ,CAAC;AAS3B;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,GAAG,CAoG7E","file":"make.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 Big from 'big.js';\nimport {BigArg} from '../big/arg';\nimport {Dbl} from '../dbl';\nimport {Rules} from '../rules';\nimport {createType} from '../create/type';\nimport {toDblBig} from '../to/dbl/big';\nimport {toFloat} from '../to/float';\n\nconst BIG_ZERO = Big(0);\nconst BIG_ONE = Big(1);\n\n/**\n * Make instance of arbitrary precision decimal type.\n * @param fallback\n * @param initial\n * @returns\n *\n * @category Maths\n */\nexport function dblMake(fallback: BigArg | null, initial?: BigArg | null): Dbl {\n\tconst rules = new Rules<Big>();\n\n\trules.add().must.match.type.big();\n\n\tconst bigFallback = toDblBig(fallback);\n\tconst bigInitial = toDblBig(initial);\n\n\tconst strong = createType<Big>(bigFallback ?? BIG_ZERO, bigInitial, rules, 'Dbl');\n\n\treturn Object.assign(strong, {\n\t\tincrement: (): Big | null => {\n\t\t\tconst value = strong._data.getNull();\n\n\t\t\tif (value === null) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst result = value.add(BIG_ONE);\n\n\t\t\treturn strong._data.set(result) ? result : null;\n\t\t},\n\t\tdecrement: (): Big | null => {\n\t\t\tconst value = strong._data.getNull();\n\t\t\tif (value === null) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst result = value.minus(Big(1));\n\t\t\treturn strong._data.set(result) ? result : null;\n\t\t},\n\t\tmul: (input: number | string | Big): Big | null => {\n\t\t\tconst curr: Big = strong.get(BIG_ZERO);\n\n\t\t\tconst value = toDblBig(input);\n\t\t\tif (value === null) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst result = curr.mul(value);\n\n\t\t\treturn strong._data.set(result) ? result : null;\n\t\t},\n\t\tpow: (exponent: number | string | Big): Big | null => {\n\t\t\tconst curr = strong._data.getNull();\n\t\t\tconst value = toFloat(exponent);\n\n\t\t\tif (curr === null || value === null) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst result = curr.pow(value);\n\n\t\t\treturn strong._data.set(result) ? result : null;\n\t\t},\n\t\tdiv: (input: number | string | Big): Big | null => {\n\t\t\tconst curr = strong.get(BIG_ZERO);\n\t\t\tconst value = toDblBig(input);\n\n\t\t\tif (curr === null || value === null) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tif (value === BIG_ZERO || curr === BIG_ZERO) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst result = curr.div(value);\n\n\t\t\treturn strong._data.set(result) ? result : null;\n\t\t},\n\t\tadd: (input: number | string | Big): Big | null => {\n\t\t\tconst value = toDblBig(input);\n\t\t\tconst curr = strong.getNull();\n\n\t\t\tif (value === null) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tif (curr === null) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst result = curr.add(value);\n\n\t\t\treturn strong._data.set(result) ? result : null;\n\t\t},\n\t\tsub: (input: number | string | Big): Big | null => {\n\t\t\tconst value = toDblBig(input);\n\t\t\tconst curr = strong.getNull();\n\n\t\t\tif (value === null || curr === null) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst result = curr.minus(value);\n\n\t\t\treturn strong._data.set(result) ? result : null;\n\t\t}\n\t});\n}\n"]}