jg-bignumber 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"processing.js","sourceRoot":"","sources":["../../src/processing/processing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEzE,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAQ7C,OAAO,EACL,WAAW,EACX,iBAAiB,EACjB,0BAA0B,EAC1B,sBAAsB,EACtB,cAAc,GACf,MAAM,mBAAmB,CAAC;AAE3B;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAsB,EAAc,EAAE;IACjE,IAAI,SAAqB,CAAC;IAC1B,IAAI,CAAC;QACH,SAAS,GAAG,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IACtE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,0BAA0B,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACjD,CAAC;IACD,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,EAAE,CAAC;QAC1B,MAAM,0BAA0B,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,KAAsB,EACtB,aAAiB,EACL,EAAE;IACd,MAAM,MAAM,GAAG,WAAW,CAAC,aAAa,CAAC,CAAC;IAC1C,MAAM,SAAS,GAAG,cAAc,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,aAAa,EA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C;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,KAAc,EAAW,EAAE;IACjD,IAAI,CAAC;QACH,OAAO,CAAC,YAAY,CAAC,KAAwB,CAAC,CAAC,SAAS,EAAE,CAAC;IAC7D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC","sourcesContent":["import { Exception, extractMessage, isEncodedError } from 'jg-exception';\n\nimport { BigNumber } from '../shared/constants.js';\nimport { ERRORS } from '../shared/errors.js';\nimport type {\n IBigNumber,\n IBigNumberFormat,\n IBigNumberValue,\n IConfig,\n IOutput,\n} from '../shared/types.js';\nimport {\n buildConfig,\n buildFormatConfig,\n buildInvalidValueException,\n convertBigNumberToType,\n roundBigNumber,\n} from '../utils/utils.js';\n\n/**\n * Creates a BigNumber from a number, a numeric string, or a BigNumber, which is returned as is.\n * Only finite values are accepted: `NaN` and `±Infinity` are rejected.\n * @param value The value.\n * @returns The BigNumber.\n * @example\n * getBigNumber('0.1').plus('0.2').toString(); // '0.3'\n * getBigNumber(Infinity); // throws INVALID_VALUE\n * @throws\n * - INVALID_VALUE: if the value isn't a finite number or BigNumber throws an error\n */\nexport const getBigNumber = (value: IBigNumberValue): IBigNumber => {\n let bigNumber: IBigNumber;\n try {\n bigNumber = BigNumber.isBigNumber(value) ? value : BigNumber(value);\n } catch (error) {\n throw buildInvalidValueException(value, error);\n }\n if (!bigNumber.isFinite()) {\n throw buildInvalidValueException(value);\n }\n return bigNumber;\n};\n\n/**\n * Rounds a value and returns it as the type the configuration asks for: a number by default, or a\n * string or a BigNumber.\n * @param value The value.\n * @param configuration The decimal places, rounding mode, and output type.\n * @returns The processed value.\n * @example\n * processValue(512.155); // 512.16\n * processValue(512.155, { decimalPlaces: 1, roundingMode: 'ROUND_FLOOR' }); // 512.1\n * processValue('0.000001', { decimalPlaces: 6, type: 'string' }); // '0.000001'\n * @throws\n * - INVALID_VALUE: if the value isn't a finite number or BigNumber throws an error\n * - INVALID_DECIMAL_PLACES: if the number of decimal places is invalid for any reason\n * - INVALID_ROUNDING_MODE: if the rounding mode name is not supported\n * - INVALID_TYPE: if the processing type is not supported\n */\nexport const processValue = <T extends Partial<IConfig>>(\n value: IBigNumberValue,\n configuration?: T,\n): IOutput<T> => {\n const config = buildConfig(configuration);\n const bigNumber = roundBigNumber(getBigNumber(value), config.decimalPlaces, config.roundingMode);\n return convertBigNumberToType(bigNumber, config.type) as IOutput<T>;\n};\n\n/**\n * Processes a value and formats it for people, with group separators, a prefix, and a suffix.\n * @param value The value.\n * @param config How the value is processed and formatted.\n * @returns The formatted value.\n * @example\n * prettifyValue(1234567.891); // '1,234,567.89'\n * prettifyValue(1234.5, { processing: { decimalPlaces: 2 }, format: { prefix: '$' } }); // '$1,234.5'\n * @throws\n * - INVALID_VALUE: if the value isn't a finite number or BigNumber throws an error\n * - INVALID_DECIMAL_PLACES: if the number of decimal places is invalid for any reason\n * - INVALID_ROUNDING_MODE: if the rounding mode name is not supported\n * - INVALID_TYPE: if the processing type is not supported\n * - INVALID_BIGNUMBER_FORMAT: if any of the format properties are invalid\n */\nexport const prettifyValue = (\n value: IBigNumberValue,\n config: { processing?: Partial<IConfig>; format?: Partial<IBigNumberFormat> } = {},\n): string => {\n try {\n return processValue(value, {\n ...config.processing,\n type: 'bignumber',\n }).toFormat(buildFormatConfig(config.format));\n } catch (error) {\n if (isEncodedError(error)) {\n throw error;\n }\n throw new Exception(\n `The value could not be formatted: ${extractMessage(error)}`,\n ERRORS.INVALID_BIGNUMBER_FORMAT,\n );\n }\n};\n\n/**\n * Checks whether a value is a BigNumber instance, from this package or any other copy of\n * bignumber.js.\n * @param value The value.\n * @returns Whether it's a BigNumber.\n * @example\n * isBigNumber(getBigNumber(1)); // true\n * isBigNumber(1); // false\n */\nexport const isBigNumber = (value: unknown): value is IBigNumber => BigNumber.isBigNumber(value);\n\n/**\n * Checks whether a value is a finite number, numeric string, or BigNumber.\n * @param value The value.\n * @returns Whether it's a finite number.\n * @example\n * isNumber('12.5'); // true\n * isNumber(Infinity); // false\n * isNumber('abc'); // false\n */\nexport const isNumber = (value: unknown): boolean => {\n try {\n getBigNumber(value as IBigNumberValue);\n return true;\n } catch {\n return false;\n }\n};\n\n/**\n * Checks whether a value is a finite integer, in any of the supported types.\n * @param value The value.\n * @returns Whether it's an integer.\n * @example\n * isInteger('12'); // true\n * isInteger(12.5); // false\n */\nexport const isInteger = (value: unknown): boolean => {\n try {\n return getBigNumber(value as IBigNumberValue).isInteger();\n } catch {\n return false;\n }\n};\n\n/**\n * Checks whether a value is a finite number with a fractional part, in any of the supported types.\n * @param value The value.\n * @returns Whether it's a float.\n * @example\n * isFloat(12.5); // true\n * isFloat('12'); // false\n */\nexport const isFloat = (value: unknown): boolean => {\n try {\n return !getBigNumber(value as IBigNumberValue).isInteger();\n } catch {\n return false;\n }\n};\n"]}
1
+ {"version":3,"file":"processing.js","sourceRoot":"","sources":["../../src/processing/processing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEtD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAQ7C,OAAO,EACL,WAAW,EACX,iBAAiB,EACjB,0BAA0B,EAC1B,sBAAsB,EACtB,cAAc,GACf,MAAM,mBAAmB,CAAC;AAE3B;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAsB,EAAc,EAAE;IACjE,IAAI,SAAqB,CAAC;IAC1B,IAAI,CAAC;QACH,SAAS,GAAG,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IACtE,CAAC;IAAC,OAAO,KAAK,EA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AAC,KAAc,EAAuB,EAAE,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;AAEjG;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAW,EAAE;IAClD,IAAI,CAAC;QACH,YAAY,CAAC,KAAwB,CAAC,CAAC;QACvC,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAc,EAAW,EAAE;IACnD,IAAI,CAAC;QACH,OAAO,YAAY,CAAC,KAAwB,CAAC,CAAC,SAAS,EAAE,CAAC;IAC5D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,KAAc,EAAW,EAAE;IACjD,IAAI,CAAC;QACH,OAAO,CAAC,YAAY,CAAC,KAAwB,CAAC,CAAC,SAAS,EAAE,CAAC;IAC7D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC","sourcesContent":["import { Exception, isException } from 'jg-exception';\n\nimport { BigNumber } from '../shared/constants.js';\nimport { ERRORS } from '../shared/errors.js';\nimport type {\n IBigNumber,\n IBigNumberFormat,\n IBigNumberValue,\n IConfig,\n IOutput,\n} from '../shared/types.js';\nimport {\n buildConfig,\n buildFormatConfig,\n buildInvalidValueException,\n convertBigNumberToType,\n roundBigNumber,\n} from '../utils/utils.js';\n\n/**\n * Creates a BigNumber from a number, a numeric string, or a BigNumber, which is returned as is.\n * Only finite values are accepted: `NaN` and `±Infinity` are rejected.\n * @param value The value.\n * @returns The BigNumber.\n * @example\n * import { getBigNumber } from 'jg-bignumber';\n *\n * getBigNumber('0.1').plus('0.2').toString(); // => '0.3'\n * @throws\n * - INVALID_VALUE: if the value isn't a finite number or BigNumber throws an error\n */\nexport const getBigNumber = (value: IBigNumberValue): IBigNumber => {\n let bigNumber: IBigNumber;\n try {\n bigNumber = BigNumber.isBigNumber(value) ? value : BigNumber(value);\n } catch (error) {\n throw buildInvalidValueException(value, error);\n }\n if (!bigNumber.isFinite()) {\n throw buildInvalidValueException(value);\n }\n return bigNumber;\n};\n\n/**\n * Rounds a value and returns it as the type the configuration asks for: a number by default, or a\n * string or a BigNumber.\n * @param value The value.\n * @param configuration The decimal places, rounding mode, and output type.\n * @returns The processed value.\n * @example\n * import { processValue } from 'jg-bignumber';\n *\n * processValue(512.155); // => 512.16\n * processValue(512.155, { decimalPlaces: 1, roundingMode: 'ROUND_FLOOR' }); // => 512.1\n * processValue('0.000001', { decimalPlaces: 6, type: 'string' }); // => '0.000001'\n * @throws\n * - INVALID_VALUE: if the value isn't a finite number or BigNumber throws an error\n * - INVALID_DECIMAL_PLACES: if the number of decimal places is invalid for any reason\n * - INVALID_ROUNDING_MODE: if the rounding mode name is not supported\n * - INVALID_TYPE: if the processing type is not supported\n */\nexport const processValue = <T extends Partial<IConfig>>(\n value: IBigNumberValue,\n configuration?: T,\n): IOutput<T> => {\n const config = buildConfig(configuration);\n const bigNumber = roundBigNumber(getBigNumber(value), config.decimalPlaces, config.roundingMode);\n return convertBigNumberToType(bigNumber, config.type) as IOutput<T>;\n};\n\n/**\n * Processes a value and formats it for people, with group separators, a prefix, and a suffix.\n * @param value The value.\n * @param config How the value is processed and formatted.\n * @returns The formatted value.\n * @example\n * import { prettifyValue } from 'jg-bignumber';\n *\n * prettifyValue(1234567.891); // => '1,234,567.89'\n * prettifyValue(1234.5, { processing: { decimalPlaces: 2 }, format: { prefix: '$' } }); // => '$1,234.5'\n * @throws\n * - INVALID_VALUE: if the value isn't a finite number or BigNumber throws an error\n * - INVALID_DECIMAL_PLACES: if the number of decimal places is invalid for any reason\n * - INVALID_ROUNDING_MODE: if the rounding mode name is not supported\n * - INVALID_TYPE: if the processing type is not supported\n * - INVALID_BIGNUMBER_FORMAT: if any of the format properties are invalid\n */\nexport const prettifyValue = (\n value: IBigNumberValue,\n config: { processing?: Partial<IConfig>; format?: Partial<IBigNumberFormat> } = {},\n): string => {\n try {\n return processValue(value, {\n ...config.processing,\n type: 'bignumber',\n }).toFormat(buildFormatConfig(config.format));\n } catch (error) {\n if (isException(error)) {\n throw error;\n }\n throw new Exception('The value could not be formatted.', {\n code: ERRORS.INVALID_BIGNUMBER_FORMAT,\n cause: error,\n });\n }\n};\n\n/**\n * Checks whether a value is a BigNumber instance, from this package or any other copy of\n * bignumber.js.\n * @param value The value.\n * @returns Whether it's a BigNumber.\n * @example\n * import { getBigNumber, isBigNumber } from 'jg-bignumber';\n *\n * isBigNumber(getBigNumber(1)); // => true\n * isBigNumber(1); // => false\n */\nexport const isBigNumber = (value: unknown): value is IBigNumber => BigNumber.isBigNumber(value);\n\n/**\n * Checks whether a value is a finite number, numeric string, or BigNumber.\n * @param value The value.\n * @returns Whether it's a finite number.\n * @example\n * import { isNumber } from 'jg-bignumber';\n *\n * isNumber('12.5'); // => true\n * isNumber(Infinity); // => false\n * isNumber('abc'); // => false\n */\nexport const isNumber = (value: unknown): boolean => {\n try {\n getBigNumber(value as IBigNumberValue);\n return true;\n } catch {\n return false;\n }\n};\n\n/**\n * Checks whether a value is a finite integer, in any of the supported types.\n * @param value The value.\n * @returns Whether it's an integer.\n * @example\n * import { isInteger } from 'jg-bignumber';\n *\n * isInteger('12'); // => true\n * isInteger(12.5); // => false\n */\nexport const isInteger = (value: unknown): boolean => {\n try {\n return getBigNumber(value as IBigNumberValue).isInteger();\n } catch {\n return false;\n }\n};\n\n/**\n * Checks whether a value is a finite number with a fractional part, in any of the supported types.\n * @param value The value.\n * @returns Whether it's a float.\n * @example\n * import { isFloat } from 'jg-bignumber';\n *\n * isFloat(12.5); // => true\n * isFloat('12'); // => false\n */\nexport const isFloat = (value: unknown): boolean => {\n try {\n return !getBigNumber(value as IBigNumberValue).isInteger();\n } catch {\n return false;\n }\n};\n"]}
@@ -1,4 +1,4 @@
1
- import { Exception, extractMessage } from 'jg-exception';
1
+ import { Exception } from 'jg-exception';
2
2
  import { ERRORS } from '../shared/errors.js';
3
3
  import { validateDecimalPlaces } from '../validations/validations.js';
4
4
  // the numeric rounding mode bignumber.js uses for each mode name
@@ -33,9 +33,10 @@ const describeValue = (value) => {
33
33
  * @param error The error bignumber.js threw, if any.
34
34
  * @returns The INVALID_VALUE exception.
35
35
  */
36
- export const buildInvalidValueException = (value, error) => new Exception(error === undefined
37
- ? `BigNumber could not be instantiated with: ${describeValue(value)}.`
38
- : `BigNumber could not be instantiated with: ${describeValue(value)} | ${extractMessage(error)}`, ERRORS.INVALID_VALUE);
36
+ export const buildInvalidValueException = (value, error) => new Exception(`BigNumber could not be instantiated with: ${describeValue(value)}.`, {
37
+ code: ERRORS.INVALID_VALUE,
38
+ cause: error,
39
+ });
39
40
  /**
40
41
  * Builds the configuration used to process a value, applying the defaults: 2 decimal places,
41
42
  * ROUND_HALF_UP, and a number output. The decimal places apply to every rounding mode.
@@ -63,7 +64,9 @@ export const buildConfig = (config) => {
63
64
  const getRoundingMode = (name) => {
64
65
  const roundingMode = Object.hasOwn(ROUNDING_MODES, name) ? ROUNDING_MODES[name] : undefined;
65
66
  if (roundingMode === undefined) {
66
- throw new Exception(`The rounding mode '${describeValue(name)}' is invalid.`, ERRORS.INVALID_ROUNDING_MODE);
67
+ throw new Exception(`The rounding mode '${describeValue(name)}' is invalid.`, {
68
+ code: ERRORS.INVALID_ROUNDING_MODE,
69
+ });
67
70
  }
68
71
  return roundingMode;
69
72
  };
@@ -94,7 +97,7 @@ export const convertBigNumberToType = (value, type) => {
94
97
  case 'bignumber':
95
98
  return value;
96
99
  default:
97
- throw new Exception(`The buildType '${describeValue(type)}' is invalid for '${value.toString()}'.`, ERRORS.INVALID_TYPE);
100
+ throw new Exception(`The buildType '${describeValue(type)}' is invalid for '${value.toString()}'.`, { code: ERRORS.INVALID_TYPE });
98
101
  }
99
102
  };
100
103
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEzD,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAU7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAEtE,iEAAiE;AACjE,MAAM,cAAc,GAA+D;IACjF,QAAQ,EAAE,CAAC;IACX,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,CAAC;IACb,WAAW,EAAE,CAAC;IACd,aAAa,EAAE,CAAC;IAChB,eAAe,EAAE,CAAC;IAClB,eAAe,EAAE,CAAC;IAClB,eAAe,EAAE,CAAC;IAClB,gBAAgB,EAAE,CAAC;CACpB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,aAAa,GAAG,CAAC,KAAc,EAAU,EAAE;IAC/C,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,KAAc,EAAE,KAAe,EAAa,EAAE,CACvF,IAAI,SAAS,CACX,KAAK,KAAK,SAAS;IACjB,CAAC,CAAC,6CAA6C,aAAa,CAAC,KAAK,CAAC,GAAG;IACtE,CAAC,CAAC,6CAA6C,aAAa,CAAC,KAAK,CAAC,MAAM,cAAc,CAAC,KAAK,CAAC,EAAE,EAClG,MAAM,CAAC,aAAa,CACrB,CAAC;AAEJ;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,MAAyB,EAAW,EAAE;IAChE,MAAM,aAAa,GAAG,MAAM,EAAE,aAAa,IAAI,CAAC,CAAC;I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,CAAC;AACH,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,MAAkC,EAAoB,EAAE,CAAC,CAAC;IAC1F,MAAM,EAAE,MAAM,EAAE,MAAM,IAAI,EAAE;IAC5B,YAAY,EAAE,MAAM,EAAE,YAAY,IAAI,GAAG;IACzC,YAAY,EAAE,MAAM,EAAE,YAAY,IAAI,EAAE;IACxC,gBAAgB,EAAE,MAAM,EAAE,gBAAgB,IAAI,GAAG;IACjD,cAAc,EAAE,MAAM,EAAE,cAAc,IAAI,GAAG;IAC7C,SAAS,EAAE,MAAM,EAAE,SAAS,IAAI,CAAC;IACjC,kBAAkB,EAAE,MAAM,EAAE,kBAAkB,IAAI,CAAC;IACnD,sBAAsB,EAAE,MAAM,EAAE,sBAAsB,IAAI,GAAG;IAC7D,iBAAiB,EAAE,MAAM,EAAE,iBAAiB,IAAI,CAAC;IACjD,MAAM,EAAE,MAAM,EAAE,MAAM,IAAI,EAAE;CAC7B,CAAC,CAAC;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GACzB,CAAC,SAAyB,EAAE,EAAE,CAC9B,CAAC,CAAa,EAAE,CAAa,EAAU,EAAE;IACvC,IAAI,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;QACpB,OAAO,SAAS,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACtC,CAAC;IACD,IAAI,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC;QACvB,OAAO,SAAS,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACtC,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC,CAAC","sourcesContent":["import { Exception, extractMessage } from 'jg-exception';\n\nimport { ERRORS } from '../shared/errors.js';\nimport type {\n IBigNumber,\n IBigNumberFormat,\n IBigNumberRoundingMode,\n IBigNumberRoundingModeName,\n IBigNumberToType,\n IConfig,\n IType,\n} from '../shared/types.js';\nimport { validateDecimalPlaces } from '../validations/validations.js';\n\n// the numeric rounding mode bignumber.js uses for each mode name\nconst ROUNDING_MODES: Record<IBigNumberRoundingModeName, IBigNumberRoundingMode> = {\n ROUND_UP: 0,\n ROUND_DOWN: 1,\n ROUND_CEIL: 2,\n ROUND_FLOOR: 3,\n ROUND_HALF_UP: 4,\n ROUND_HALF_DOWN: 5,\n ROUND_HALF_EVEN: 6,\n ROUND_HALF_CEIL: 7,\n ROUND_HALF_FLOOR: 8,\n};\n\n/**\n * Describes a value for an error message, without failing on values that can't be stringified,\n * such as symbols' descriptions or objects without a prototype.\n * @param value The value.\n * @returns The description, or `UNKNOWN`.\n */\nconst describeValue = (value: unknown): string => {\n try {\n return String(value);\n } catch {\n return 'UNKNOWN';\n }\n};\n\n/**\n * Builds the exception thrown when a value can't become a finite BigNumber.\n * @param value The value that failed.\n * @param error The error bignumber.js threw, if any.\n * @returns The INVALID_VALUE exception.\n */\nexport const buildInvalidValueException = (value: unknown, error?: unknown): Exception =>\n new Exception(\n error === undefined\n ? `BigNumber could not be instantiated with: ${describeValue(value)}.`\n : `BigNumber could not be instantiated with: ${describeValue(value)} | ${extractMessage(error)}`,\n ERRORS.INVALID_VALUE,\n );\n\n/**\n * Builds the configuration used to process a value, applying the defaults: 2 decimal places,\n * ROUND_HALF_UP, and a number output. The decimal places apply to every rounding mode.\n * @param config The configuration given by the caller.\n * @returns The complete configuration.\n * @throws\n * - INVALID_DECIMAL_PLACES: if decimalPlaces is not a number or it is outside of the 0 - 100 range\n */\nexport const buildConfig = (config?: Partial<IConfig>): IConfig => {\n const decimalPlaces = config?.decimalPlaces ?? 2;\n validateDecimalPlaces(decimalPlaces);\n return {\n decimalPlaces,\n roundingMode: config?.roundingMode ?? 'ROUND_HALF_UP',\n type: config?.type ?? 'number',\n };\n};\n\n/**\n * Looks up the numeric rounding mode bignumber.js uses for a mode name.\n * @param name The rounding mode name.\n * @returns The numeric rounding mode.\n * @throws\n * - INVALID_ROUNDING_MODE: if the rounding mode name is not supported\n */\nconst getRoundingMode = (name: IBigNumberRoundingModeName): IBigNumberRoundingMode => {\n const roundingMode = Object.hasOwn(ROUNDING_MODES, name) ? ROUNDING_MODES[name] : undefined;\n if (roundingMode === undefined) {\n throw new Exception(\n `The rounding mode '${describeValue(name)}' is invalid.`,\n ERRORS.INVALID_ROUNDING_MODE,\n );\n }\n return roundingMode;\n};\n\n/**\n * Rounds a BigNumber to a number of decimal places with a rounding mode.\n * @param value The value to round.\n * @param decimalPlaces The maximum number of decimal places.\n * @param roundingMode The rounding mode name.\n * @returns The rounded BigNumber.\n * @throws\n * - INVALID_ROUNDING_MODE: if the rounding mode name is not supported\n */\nexport const roundBigNumber = (\n value: IBigNumber,\n decimalPlaces: number,\n roundingMode: IBigNumberRoundingModeName,\n): IBigNumber => value.decimalPlaces(decimalPlaces, getRoundingMode(roundingMode));\n\n/**\n * Converts a BigNumber into the requested type.\n * @param value The BigNumber.\n * @param type The output type.\n * @returns The converted value.\n * @throws\n * - INVALID_TYPE: if the processing type is not supported\n */\nexport const convertBigNumberToType = <T extends IType>(\n value: IBigNumber,\n type: T,\n): IBigNumberToType<T> => {\n switch (type) {\n case 'string':\n return value.toString() as IBigNumberToType<T>;\n case 'number':\n return value.toNumber() as IBigNumberToType<T>;\n case 'bignumber':\n return value as IBigNumberToType<T>;\n default:\n throw new Exception(\n `The buildType '${describeValue(type)}' is invalid for '${value.toString()}'.`,\n ERRORS.INVALID_TYPE,\n );\n }\n};\n\n/**\n * Builds the format prettifyValue applies, filling in bignumber.js's defaults.\n * @param config The format settings given by the caller.\n * @returns The complete format.\n */\nexport const buildFormatConfig = (config?: Partial<IBigNumberFormat>): IBigNumberFormat => ({\n prefix: config?.prefix ?? '',\n negativeSign: config?.negativeSign ?? '-',\n positiveSign: config?.positiveSign ?? '',\n decimalSeparator: config?.decimalSeparator ?? '.',\n groupSeparator: config?.groupSeparator ?? ',',\n groupSize: config?.groupSize ?? 3,\n secondaryGroupSize: config?.secondaryGroupSize ?? 0,\n fractionGroupSeparator: config?.fractionGroupSeparator ?? ' ',\n fractionGroupSize: config?.fractionGroupSize ?? 0,\n suffix: config?.suffix ?? '',\n});\n\n/**\n * Builds a comparator that orders BigNumbers in a direction.\n * @param direction The sort direction.\n * @returns The comparator.\n */\nexport const sortBigNumbers =\n (direction: 'asc' | 'desc') =>\n (a: IBigNumber, b: IBigNumber): number => {\n if (a.isLessThan(b)) {\n return direction === 'asc' ? -1 : 1;\n }\n if (a.isGreaterThan(b)) {\n return direction === 'asc' ? 1 : -1;\n }\n return 0;\n };\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAU7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAEtE,iEAAiE;AACjE,MAAM,cAAc,GAA+D;IACjF,QAAQ,EAAE,CAAC;IACX,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,CAAC;IACb,WAAW,EAAE,CAAC;IACd,aAAa,EAAE,CAAC;IAChB,eAAe,EAAE,CAAC;IAClB,eAAe,EAAE,CAAC;IAClB,eAAe,EAAE,CAAC;IAClB,gBAAgB,EAAE,CAAC;CACpB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,aAAa,GAAG,CAAC,KAAc,EAAU,EAAE;IAC/C,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,KAAc,EAAE,KAAe,EAAa,EAAE,CACvF,IAAI,SAAS,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,CAAC;IACD,IAAI,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC;QACvB,OAAO,SAAS,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACtC,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC,CAAC","sourcesContent":["import { Exception } from 'jg-exception';\n\nimport { ERRORS } from '../shared/errors.js';\nimport type {\n IBigNumber,\n IBigNumberFormat,\n IBigNumberRoundingMode,\n IBigNumberRoundingModeName,\n IBigNumberToType,\n IConfig,\n IType,\n} from '../shared/types.js';\nimport { validateDecimalPlaces } from '../validations/validations.js';\n\n// the numeric rounding mode bignumber.js uses for each mode name\nconst ROUNDING_MODES: Record<IBigNumberRoundingModeName, IBigNumberRoundingMode> = {\n ROUND_UP: 0,\n ROUND_DOWN: 1,\n ROUND_CEIL: 2,\n ROUND_FLOOR: 3,\n ROUND_HALF_UP: 4,\n ROUND_HALF_DOWN: 5,\n ROUND_HALF_EVEN: 6,\n ROUND_HALF_CEIL: 7,\n ROUND_HALF_FLOOR: 8,\n};\n\n/**\n * Describes a value for an error message, without failing on values that can't be stringified,\n * such as symbols' descriptions or objects without a prototype.\n * @param value The value.\n * @returns The description, or `UNKNOWN`.\n */\nconst describeValue = (value: unknown): string => {\n try {\n return String(value);\n } catch {\n return 'UNKNOWN';\n }\n};\n\n/**\n * Builds the exception thrown when a value can't become a finite BigNumber.\n * @param value The value that failed.\n * @param error The error bignumber.js threw, if any.\n * @returns The INVALID_VALUE exception.\n */\nexport const buildInvalidValueException = (value: unknown, error?: unknown): Exception =>\n new Exception(`BigNumber could not be instantiated with: ${describeValue(value)}.`, {\n code: ERRORS.INVALID_VALUE,\n cause: error,\n });\n\n/**\n * Builds the configuration used to process a value, applying the defaults: 2 decimal places,\n * ROUND_HALF_UP, and a number output. The decimal places apply to every rounding mode.\n * @param config The configuration given by the caller.\n * @returns The complete configuration.\n * @throws\n * - INVALID_DECIMAL_PLACES: if decimalPlaces is not a number or it is outside of the 0 - 100 range\n */\nexport const buildConfig = (config?: Partial<IConfig>): IConfig => {\n const decimalPlaces = config?.decimalPlaces ?? 2;\n validateDecimalPlaces(decimalPlaces);\n return {\n decimalPlaces,\n roundingMode: config?.roundingMode ?? 'ROUND_HALF_UP',\n type: config?.type ?? 'number',\n };\n};\n\n/**\n * Looks up the numeric rounding mode bignumber.js uses for a mode name.\n * @param name The rounding mode name.\n * @returns The numeric rounding mode.\n * @throws\n * - INVALID_ROUNDING_MODE: if the rounding mode name is not supported\n */\nconst getRoundingMode = (name: IBigNumberRoundingModeName): IBigNumberRoundingMode => {\n const roundingMode = Object.hasOwn(ROUNDING_MODES, name) ? ROUNDING_MODES[name] : undefined;\n if (roundingMode === undefined) {\n throw new Exception(`The rounding mode '${describeValue(name)}' is invalid.`, {\n code: ERRORS.INVALID_ROUNDING_MODE,\n });\n }\n return roundingMode;\n};\n\n/**\n * Rounds a BigNumber to a number of decimal places with a rounding mode.\n * @param value The value to round.\n * @param decimalPlaces The maximum number of decimal places.\n * @param roundingMode The rounding mode name.\n * @returns The rounded BigNumber.\n * @throws\n * - INVALID_ROUNDING_MODE: if the rounding mode name is not supported\n */\nexport const roundBigNumber = (\n value: IBigNumber,\n decimalPlaces: number,\n roundingMode: IBigNumberRoundingModeName,\n): IBigNumber => value.decimalPlaces(decimalPlaces, getRoundingMode(roundingMode));\n\n/**\n * Converts a BigNumber into the requested type.\n * @param value The BigNumber.\n * @param type The output type.\n * @returns The converted value.\n * @throws\n * - INVALID_TYPE: if the processing type is not supported\n */\nexport const convertBigNumberToType = <T extends IType>(\n value: IBigNumber,\n type: T,\n): IBigNumberToType<T> => {\n switch (type) {\n case 'string':\n return value.toString() as IBigNumberToType<T>;\n case 'number':\n return value.toNumber() as IBigNumberToType<T>;\n case 'bignumber':\n return value as IBigNumberToType<T>;\n default:\n throw new Exception(\n `The buildType '${describeValue(type)}' is invalid for '${value.toString()}'.`,\n { code: ERRORS.INVALID_TYPE },\n );\n }\n};\n\n/**\n * Builds the format prettifyValue applies, filling in bignumber.js's defaults.\n * @param config The format settings given by the caller.\n * @returns The complete format.\n */\nexport const buildFormatConfig = (config?: Partial<IBigNumberFormat>): IBigNumberFormat => ({\n prefix: config?.prefix ?? '',\n negativeSign: config?.negativeSign ?? '-',\n positiveSign: config?.positiveSign ?? '',\n decimalSeparator: config?.decimalSeparator ?? '.',\n groupSeparator: config?.groupSeparator ?? ',',\n groupSize: config?.groupSize ?? 3,\n secondaryGroupSize: config?.secondaryGroupSize ?? 0,\n fractionGroupSeparator: config?.fractionGroupSeparator ?? ' ',\n fractionGroupSize: config?.fractionGroupSize ?? 0,\n suffix: config?.suffix ?? '',\n});\n\n/**\n * Builds a comparator that orders BigNumbers in a direction.\n * @param direction The sort direction.\n * @returns The comparator.\n */\nexport const sortBigNumbers =\n (direction: 'asc' | 'desc') =>\n (a: IBigNumber, b: IBigNumber): number => {\n if (a.isLessThan(b)) {\n return direction === 'asc' ? -1 : 1;\n }\n if (a.isGreaterThan(b)) {\n return direction === 'asc' ? 1 : -1;\n }\n return 0;\n };\n"]}
@@ -8,7 +8,7 @@ import { ERRORS } from '../shared/errors.js';
8
8
  */
9
9
  export const validateDecimalPlaces = (decimalPlaces) => {
10
10
  if (typeof decimalPlaces !== 'number' || decimalPlaces < 0 || decimalPlaces > 100) {
11
- throw new Exception(`The decimalPlaces '${String(decimalPlaces)}' must be a number ranging 0 - 100.`, ERRORS.INVALID_DECIMAL_PLACES);
11
+ throw new Exception(`The decimalPlaces '${String(decimalPlaces)}' must be a number ranging 0 - 100.`, { code: ERRORS.INVALID_DECIMAL_PLACES });
12
12
  }
13
13
  };
14
14
  /**
@@ -20,7 +20,7 @@ export const validateDecimalPlaces = (decimalPlaces) => {
20
20
  */
21
21
  export const validateValuesArray = (values, calculationName) => {
22
22
  if (!Array.isArray(values)) {
23
- throw new Exception(`Cannot run ${calculationName} on an invalid sequence of BigNumber Values. Received: ${String(values)}`, ERRORS.INVALID_VALUES_ARRAY);
23
+ throw new Exception(`Cannot run ${calculationName} on an invalid sequence of BigNumber Values. Received: ${String(values)}`, { code: ERRORS.INVALID_VALUES_ARRAY });
24
24
  }
25
25
  };
26
26
  /**
@@ -32,10 +32,14 @@ export const validateValuesArray = (values, calculationName) => {
32
32
  */
33
33
  export const validatePositiveValue = (value, allowZero) => {
34
34
  if (allowZero && value.isLessThan(0)) {
35
- throw new Exception(`The value '${value.toString()}' must be greater than or equal to 0.`, ERRORS.NEGATIVE_VALUE_NOT_ALLOWED);
35
+ throw new Exception(`The value '${value.toString()}' must be greater than or equal to 0.`, {
36
+ code: ERRORS.NEGATIVE_VALUE_NOT_ALLOWED,
37
+ });
36
38
  }
37
39
  else if (!allowZero && value.isLessThanOrEqualTo(0)) {
38
- throw new Exception(`The value '${value.toString()}' must be greater than 0.`, ERRORS.NEGATIVE_VALUE_NOT_ALLOWED);
40
+ throw new Exception(`The value '${value.toString()}' must be greater than 0.`, {
41
+ code: ERRORS.NEGATIVE_VALUE_NOT_ALLOWED,
42
+ });
39
43
  }
40
44
  };
41
45
  //# sourceMappingURL=validations.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"validations.js","sourceRoot":"","sources":["../../src/validations/validations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAG7C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,aAAqB,EAAQ,EAAE;IACnE,IAAI,OAAO,aAAa,KAAK,QAAQ,IAAI,aAAa,GAAG,CAAC,IAAI,aAAa,GAAG,GAAG,EAAE,CAAC;QAClF,MAAM,IAAI,SAAS,CACjB,sBAAsB,MAAM,CAAC,aAAa,CAAC,qCAAqC,EAChF,MAAM,CAAC,sBAAsB,CAC9B,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,MAAe,EAAE,eAAuB,EAAQ,EAAE;IACpF,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC3B,MAAM,IAAI,SAAS,CACjB,cAAc,eAAe,0DAA0D,MAAM,CAAC,MAAM,CAAC,EAAE,EACvG,MAAM,CAAC,oBAAoB,CAC5B,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,KAAiB,EAAE,SAAmB,EAAQ,EAAE;IACpF,IAAI,SAAS,IAAI,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,SAAS,CACjB,cAAc,KAAK,CAAC,QAAQ,EAAE,uCAAuC,EACrE,MAAM,CAAC,0BAA0B,CAClC,CAAC;IACJ,CAAC;SAAM,IAAI,CAAC,SAAS,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC,CAAC,EAAE,CAAC;QACtD,MAAM,IAAI,SAAS,CACjB,cAAc,KAAK,CAAC,QAAQ,EAAE,2BAA2B,EACzD,MAAM,CAAC,0BAA0B,CAClC,CAAC;IACJ,CAAC;AACH,CAAC,CAAC","sourcesContent":["import { Exception } from 'jg-exception';\n\nimport { ERRORS } from '../shared/errors.js';\nimport type { IBigNumber } from '../shared/types.js';\n\n/**\n * Ensures the number of decimal places is a number from 0 to 100.\n * @param decimalPlaces The number of decimal places.\n * @throws\n * - INVALID_DECIMAL_PLACES: if decimalPlaces is not a number or it is outside of the 0 - 100 range\n */\nexport const validateDecimalPlaces = (decimalPlaces: number): void => {\n if (typeof decimalPlaces !== 'number' || decimalPlaces < 0 || decimalPlaces > 100) {\n throw new Exception(\n `The decimalPlaces '${String(decimalPlaces)}' must be a number ranging 0 - 100.`,\n ERRORS.INVALID_DECIMAL_PLACES,\n );\n }\n};\n\n/**\n * Ensures a sequence of values is an array.\n * @param values The values.\n * @param calculationName The calculation the values are for, used in the message.\n * @throws\n * - INVALID_VALUES_ARRAY: if values is not a valid array\n */\nexport const validateValuesArray = (values: unknown, calculationName: string): void => {\n if (!Array.isArray(values)) {\n throw new Exception(\n `Cannot run ${calculationName} on an invalid sequence of BigNumber Values. Received: ${String(values)}`,\n ERRORS.INVALID_VALUES_ARRAY,\n );\n }\n};\n\n/**\n * Ensures a value is positive, or zero when allowed.\n * @param value The value.\n * @param allowZero Whether zero is accepted.\n * @throws\n * - NEGATIVE_VALUE_NOT_ALLOWED: if the value is not positive\n */\nexport const validatePositiveValue = (value: IBigNumber, allowZero?: boolean): void => {\n if (allowZero && value.isLessThan(0)) {\n throw new Exception(\n `The value '${value.toString()}' must be greater than or equal to 0.`,\n ERRORS.NEGATIVE_VALUE_NOT_ALLOWED,\n );\n } else if (!allowZero && value.isLessThanOrEqualTo(0)) {\n throw new Exception(\n `The value '${value.toString()}' must be greater than 0.`,\n ERRORS.NEGATIVE_VALUE_NOT_ALLOWED,\n );\n }\n};\n"]}
1
+ {"version":3,"file":"validations.js","sourceRoot":"","sources":["../../src/validations/validations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAG7C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,aAAqB,EAAQ,EAAE;IACnE,IAAI,OAAO,aAAa,KAAK,QAAQ,IAAI,aAAa,GAAG,CAAC,IAAI,aAAa,GAAG,GAAG,EAAE,CAAC;QAClF,MAAM,IAAI,SAAS,CACjB,sBAAsB,MAAM,CAAC,aAAa,CAAC,qCAAqC,EAChF,EAAE,IAAI,EAAE,MAAM,CAAC,sBAAsB,EAAE,CACxC,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,MAAe,EAAE,eAAuB,EAAQ,EAAE;IACpF,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC3B,MAAM,IAAI,SAAS,CACjB,cAAc,eAAe,0DAA0D,MAAM,CAAC,MAAM,CAAC,EAAE,EACvG,EAAE,IAAI,EAAE,MAAM,CAAC,oBAAoB,EAAE,CACtC,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,KAAiB,EAAE,SAAmB,EAAQ,EAAE;IACpF,IAAI,SAAS,IAAI,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,SAAS,CAAC,cAAc,KAAK,CAAC,QAAQ,EAAE,uCAAuC,EAAE;YACzF,IAAI,EAAE,MAAM,CAAC,0BAA0B;SACxC,CAAC,CAAC;IACL,CAAC;SAAM,IAAI,CAAC,SAAS,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC,CAAC,EAAE,CAAC;QACtD,MAAM,IAAI,SAAS,CAAC,cAAc,KAAK,CAAC,QAAQ,EAAE,2BAA2B,EAAE;YAC7E,IAAI,EAAE,MAAM,CAAC,0BAA0B;SACxC,CAAC,CAAC;IACL,CAAC;AACH,CAAC,CAAC","sourcesContent":["import { Exception } from 'jg-exception';\n\nimport { ERRORS } from '../shared/errors.js';\nimport type { IBigNumber } from '../shared/types.js';\n\n/**\n * Ensures the number of decimal places is a number from 0 to 100.\n * @param decimalPlaces The number of decimal places.\n * @throws\n * - INVALID_DECIMAL_PLACES: if decimalPlaces is not a number or it is outside of the 0 - 100 range\n */\nexport const validateDecimalPlaces = (decimalPlaces: number): void => {\n if (typeof decimalPlaces !== 'number' || decimalPlaces < 0 || decimalPlaces > 100) {\n throw new Exception(\n `The decimalPlaces '${String(decimalPlaces)}' must be a number ranging 0 - 100.`,\n { code: ERRORS.INVALID_DECIMAL_PLACES },\n );\n }\n};\n\n/**\n * Ensures a sequence of values is an array.\n * @param values The values.\n * @param calculationName The calculation the values are for, used in the message.\n * @throws\n * - INVALID_VALUES_ARRAY: if values is not a valid array\n */\nexport const validateValuesArray = (values: unknown, calculationName: string): void => {\n if (!Array.isArray(values)) {\n throw new Exception(\n `Cannot run ${calculationName} on an invalid sequence of BigNumber Values. Received: ${String(values)}`,\n { code: ERRORS.INVALID_VALUES_ARRAY },\n );\n }\n};\n\n/**\n * Ensures a value is positive, or zero when allowed.\n * @param value The value.\n * @param allowZero Whether zero is accepted.\n * @throws\n * - NEGATIVE_VALUE_NOT_ALLOWED: if the value is not positive\n */\nexport const validatePositiveValue = (value: IBigNumber, allowZero?: boolean): void => {\n if (allowZero && value.isLessThan(0)) {\n throw new Exception(`The value '${value.toString()}' must be greater than or equal to 0.`, {\n code: ERRORS.NEGATIVE_VALUE_NOT_ALLOWED,\n });\n } else if (!allowZero && value.isLessThanOrEqualTo(0)) {\n throw new Exception(`The value '${value.toString()}' must be greater than 0.`, {\n code: ERRORS.NEGATIVE_VALUE_NOT_ALLOWED,\n });\n }\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jg-bignumber",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Exact decimal math with bignumber.js: rounding, formatting, and sum, mean, percentage, and exchange calculations.",
5
5
  "author": "Jesus Graterol",
6
6
  "license": "MIT",
@@ -15,7 +15,8 @@
15
15
  "types": "./dist/index.d.ts",
16
16
  "files": [
17
17
  "dist",
18
- "CHANGELOG.md"
18
+ "CHANGELOG.md",
19
+ "API.md"
19
20
  ],
20
21
  "sideEffects": false,
21
22
  "engines": {
@@ -43,22 +44,25 @@
43
44
  ],
44
45
  "dependencies": {
45
46
  "bignumber.js": "^11.1.5",
46
- "jg-exception": "^1.0.0"
47
+ "jg-exception": "^1.1.0"
47
48
  },
48
49
  "devDependencies": {
49
50
  "@jg-packages/eslint-config": "0.0.0",
50
51
  "@jg-packages/tsconfig": "0.0.0",
51
52
  "@jg-packages/vitest-config": "0.0.0",
52
53
  "@types/node": "24.19.2",
54
+ "@vitest/coverage-v8": "5.0.5",
53
55
  "eslint": "10.12.0",
56
+ "fast-check": "4.10.2",
54
57
  "typescript": "6.0.3",
55
58
  "vitest": "5.0.5"
56
59
  },
57
60
  "scripts": {
58
- "build": "node ../../internal/build-tools/src/clean-dist.ts && tsc -p tsconfig.build.json",
61
+ "build": "node ../../internal/build-tools/src/clean-dist.ts && tsc -p tsconfig.build.json && node ../../internal/api-reference/src/write-api-markdown.ts",
59
62
  "lint": "eslint .",
60
63
  "test": "pnpm test:unit",
61
64
  "test:bench": "vitest bench --run",
65
+ "test:coverage": "vitest run --coverage --project unit",
62
66
  "test:unit": "vitest run --project unit",
63
67
  "typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.browser.json"
64
68
  }