UNPKG

@toreda/strong-types

Version:

Better TypeScript code in fewer lines.

1 lines 3.27 kB
{"version":3,"sources":["../src/range.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,SAAS,CAAC;AACnC,OAAO,EAAC,SAAS,EAAC,MAAM,OAAO,CAAC;AAChC,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAG9C;;;;GAIG;AACH,qBAAa,KAAM,SAAQ,SAAS;IACnC,SAAgB,GAAG,EAAE,KAAK,CAAC;IAC3B,SAAgB,GAAG,EAAE,KAAK,CAAC;IAC3B,SAAgB,MAAM,EAAE,YAAY,CAAC;gBAEzB,UAAU,EAAE,MAAM,GAAG,IAAI,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI;IAQhE;;;;OAIG;IACI,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO;IAgBtD;;OAEG;IACI,KAAK,IAAI,IAAI;CAIpB","file":"range.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 {Float} from './float';\nimport {StrongMap} from './map';\nimport {StrongTypeId} from './strong/type/id';\nimport {floatMake} from './float/make';\n\n/**\n * General numeric range with min and max value.\n *\n * @category Maths\n */\nexport class Range extends StrongMap {\n\tpublic readonly min: Float;\n\tpublic readonly max: Float;\n\tpublic readonly typeId: StrongTypeId;\n\n\tconstructor(defaultMin: number | null, defaultMax: number | null) {\n\t\tsuper();\n\n\t\tthis.min = floatMake(typeof defaultMin === 'number' ? defaultMin : 0);\n\t\tthis.max = floatMake(typeof defaultMax === 'number' ? defaultMax : 0);\n\t\tthis.typeId = 'Range';\n\t}\n\n\t/**\n\t * Check if provided value exists between min and max range values (inclusive).\n\t * @param value\n\t * @returns\n\t */\n\tpublic in(value: number, exclusive?: boolean): boolean {\n\t\tif (typeof value !== 'number') {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Value must be strict greater than range min and strictly less than the\n\t\t// range max in exclusive mode.\n\t\tif (exclusive === true) {\n\t\t\treturn value > this.min() && value < this.max();\n\t\t} else {\n\t\t\t// Inclusive comparison requires value` be greater than or equal to range\n\t\t\t// min and less than or equal to range max.\n\t\t\treturn value >= this.min() && value <= this.max();\n\t\t}\n\t}\n\n\t/**\n\t * Reset min and max to initial values.\n\t */\n\tpublic reset(): void {\n\t\tthis.max.reset();\n\t\tthis.min.reset();\n\t}\n}\n"]}