nhb-toolbox 4.0.30 → 4.0.32
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.
- package/dist/cjs/index.js +10 -14
- package/dist/cjs/number/basics.js +10 -65
- package/dist/cjs/number/guards.js +10 -0
- package/dist/cjs/number/percent.js +81 -0
- package/dist/dts/index.d.ts +3 -2
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/number/basics.d.ts +1 -27
- package/dist/dts/number/basics.d.ts.map +1 -1
- package/dist/dts/number/guards.d.ts +7 -0
- package/dist/dts/number/guards.d.ts.map +1 -1
- package/dist/dts/number/percent.d.ts +17 -0
- package/dist/dts/number/percent.d.ts.map +1 -0
- package/dist/dts/number/types.d.ts +65 -0
- package/dist/dts/number/types.d.ts.map +1 -1
- package/dist/esm/index.js +3 -2
- package/dist/esm/number/basics.js +10 -62
- package/dist/esm/number/guards.js +9 -0
- package/dist/esm/number/percent.js +78 -0
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.convertHslToRgb = exports.convertHslToHex = exports.convertHslaToRgba = exports.convertHslaToHex8 = exports.convertHexToRgb = exports.convertHexToHsl = exports.convertHex8ToRgba = exports.convertHex8ToHsla = exports.convertColorCode = exports.generateRandomHSLColor = exports.generateRandomColorInHexRGB = exports.getColorForInitial = exports.getNumbersInRange = exports.roundToNearestInterval = exports.roundToNearest = exports.roundNumberToNearestInterval = exports.roundNumber = exports.numberToOrdinal = exports.getRandomFloat = exports.getRandomDecimal = exports.getOrdinalNumber = exports.getOrdinal = exports.formatCurrency = exports.convertToOrdinal = exports.convertNumberToOrdinal = exports.convertNumberToCurrency = exports.clampNumber = exports.cardinalToOrdinal = exports.isPrimeNumber = exports.isPrime = exports.getPrimeNumbers = exports.findPrimeNumbers = exports.numberToWords = exports.convertToRomanNumerals = exports.convertNumberToWords = exports.isPerfectSquare = exports.isParOfFibonacciSeries = exports.isParOfFibonacci = exports.isOddNumber = exports.isOdd = exports.isMultiple = exports.
|
|
5
|
-
exports.splitArray = exports.rotateArray = exports.removeDuplicatesFromArray = exports.moveArrayElement = exports.createOptionsArray = exports.sortAnArray = exports.shuffleArray = exports.isValidEmptyArray = exports.isInvalidOrEmptyArray = exports.getLastArrayElement = exports.flattenArray = exports.filterArrayOfObjects = exports.minutesToUTCOffset = exports.getTotalMinutesFromUTC = exports.getTotalMinutesFromTime = exports.getTotalMinutes = exports.getTimeStringFromUTC = exports.getMinutesFromUTC = exports.getCurrentTime = exports.getCurrentDateTime = exports.formatUTCOffset = exports.extractTotalMinutesFromTime = exports.extractTimeStringFromUTC = exports.extractTimeFromUTC = exports.extractMinutesFromUTC = exports.extractHourMinute = exports.convertMinutesToUTCOffset = exports.chronusts = exports.chronusjs = exports.chronus = exports.chronosts = exports.chronosjs = exports.chronos = exports.Chronus = exports.Chronos = exports.isValidUTCOffSet = exports.isValidUTC = exports.isValidTimeString = exports.isValidTime = exports.isLeapYear = exports.greet = exports.getGreeting = exports.generateGreeting = exports.Colour = exports.Color =
|
|
6
|
-
exports.removeFromLocalStorage = exports.getFromSessionStorage = exports.getFromLocalStorage = exports.toggleFullScreen = exports.smoothScrollTo = exports.copyToClipboard = exports.updateQueryParam = exports.getQueryParams = exports.generateQueryParams = exports.formatQueryParams = exports.createQueryParams = exports.remapObjectFields = exports.remapFields = exports.pickObjectFieldsByCondition = exports.pickObjectFields = exports.pickFieldsByCondition = exports.pickFields = exports.convertObjectValues = exports.sanitizeData = exports.parseStringifiedValues = exports.parseStringifiedPrimitives = exports.parseStringifiedObjectValues = exports.parsePrimitives = exports.parsePrimitiveData = exports.parseObjectValues = exports.mergeObjects = exports.mergeAndFlattenObjects = exports.flattenObjectKeyValue = exports.flattenObjectDotNotation = exports.extractUpdatedFields = exports.extractUpdatedAndNewFields = exports.extractNewFields = exports.countObjectFields = exports.cloneObject = exports.isValidFormData = exports.isOriginFileObj = exports.isFileUpload = exports.isFileList = exports.isFileArray = exports.isCustomFileArray = exports.isCustomFile = exports.serializeForm = exports.parseFormData = exports.createFormData = exports.createControlledFormData =
|
|
7
|
-
exports.isSet = exports.isReturningPromise = exports.isRegularExpression = exports.isRegExp = exports.isPromise = exports.isObjectWithKeys = exports.isObjectEmpty = exports.isObject = exports.isNotEmptyObject = exports.isMethodDescriptor = exports.isMethod = exports.isMap = exports.isJSONObject = exports.isJSON = exports.isFunction = exports.isError = exports.isEmptyObjectGuard = exports.isEmptyObject = exports.isDate = exports.isBigInt = exports.isArrayWithLength = exports.isArrayOfType = exports.isArray = exports.doesReturnPromise = exports.isUndefined = exports.isTruthy = exports.isSymbol = exports.isString = exports.isPrimitive = exports.isPositiveInteger = exports.isNumber = exports.isNull = exports.isNonEmptyString = exports.isInteger = exports.isFalsy = exports.isBoolean = exports.throttleAction = exports.isDeepEqual = exports.getStaticMethodsCount = exports.getStaticMethodNames = exports.getInstanceMethodsCount = exports.getInstanceMethodNames = exports.getClassDetails = exports.debounceAction = exports.countStaticMethods =
|
|
8
|
-
exports.isValidURL = exports.isValidEmail = exports.isUUID = exports.isURL = exports.isPhoneNumber = exports.isNumericString = exports.isNodeEnvironment = exports.isNodeENV = exports.isNode = exports.isIPAddress = exports.isExpectedNodeENV = exports.isEnvironment = exports.isEmailArray = exports.isEmail = exports.isDateString = exports.isBrowser = exports.isBase64 =
|
|
3
|
+
exports.areInvalidNumbers = exports.getNthFibonacci = exports.getMemoizedFibonacciSeries = exports.getMemoizedFibonacci = exports.getFibonacciSeriesMemo = exports.getFibonacciSeries = exports.getFibonacciNumbers = exports.getFibonacci = exports.generateFibonacci = exports.fibonacciGenerator = exports.calculatePercentage = exports.sumOfNumbers = exports.sumNumbers = exports.sumDigits = exports.reverseNumber = exports.getSumOfNumbers = exports.getRandomNumber = exports.getAverageOfNumbers = exports.getAverage = exports.convertToDecimal = exports.calculateLCM = exports.calculateLCD = exports.calculateHCF = exports.calculateGCD = exports.calculateAverage = exports.levenshteinDistance = exports.getLevenshteinDistance = exports.extractNumbersFromString = exports.slugifyString = exports.reverseString = exports.replaceAllInString = exports.normalizeString = exports.maskString = exports.formatWithPlural = exports.formatUnitWithPlural = exports.formatNumberWithPluralUnit = exports.extractURLs = exports.extractEmails = exports.convertStringCase = exports.isSnakeCase = exports.isPascalCase = exports.isPalindrome = exports.isKebabCase = exports.isEmojiOnly = exports.isCamelCase = exports.generateAnagrams = exports.truncateString = exports.trimString = exports.generateRandomID = exports.capitalizeString = void 0;
|
|
4
|
+
exports.convertRgbToRgba = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertRgbaToHsla = exports.convertRgbaToHex8 = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHslaToRgba = exports.convertHslaToHex8 = exports.convertHexToRgb = exports.convertHexToHsl = exports.convertHex8ToRgba = exports.convertHex8ToHsla = exports.convertColorCode = exports.generateRandomHSLColor = exports.generateRandomColorInHexRGB = exports.getColorForInitial = exports.getNumbersInRange = exports.roundToNearestInterval = exports.roundToNearest = exports.roundNumberToNearestInterval = exports.roundNumber = exports.numberToOrdinal = exports.getRandomFloat = exports.getRandomDecimal = exports.getOrdinalNumber = exports.getOrdinal = exports.formatCurrency = exports.convertToOrdinal = exports.convertNumberToOrdinal = exports.convertNumberToCurrency = exports.clampNumber = exports.cardinalToOrdinal = exports.isPrimeNumber = exports.isPrime = exports.getPrimeNumbers = exports.findPrimeNumbers = exports.numberToWords = exports.convertToRomanNumerals = exports.convertNumberToWords = exports.isPerfectSquare = exports.isParOfFibonacciSeries = exports.isParOfFibonacci = exports.isOddNumber = exports.isOdd = exports.isMultiple = exports.isInvalidNumbers = exports.isFibonacci = exports.isEvenNumber = exports.isEven = void 0;
|
|
5
|
+
exports.convertIntoFormData = exports.naturalSortForString = exports.naturalSort = exports.compareSorter = exports.compareNaturally = exports.splitArray = exports.rotateArray = exports.removeDuplicatesFromArray = exports.moveArrayElement = exports.createOptionsArray = exports.sortAnArray = exports.shuffleArray = exports.isValidEmptyArray = exports.isInvalidOrEmptyArray = exports.getLastArrayElement = exports.flattenArray = exports.filterArrayOfObjects = exports.minutesToUTCOffset = exports.getTotalMinutesFromUTC = exports.getTotalMinutesFromTime = exports.getTotalMinutes = exports.getTimeStringFromUTC = exports.getMinutesFromUTC = exports.getCurrentTime = exports.getCurrentDateTime = exports.formatUTCOffset = exports.extractTotalMinutesFromTime = exports.extractTimeStringFromUTC = exports.extractTimeFromUTC = exports.extractMinutesFromUTC = exports.extractHourMinute = exports.convertMinutesToUTCOffset = exports.chronusts = exports.chronusjs = exports.chronus = exports.chronosts = exports.chronosjs = exports.chronos = exports.Chronus = exports.Chronos = exports.isValidUTCOffSet = exports.isValidUTC = exports.isValidTimeString = exports.isValidTime = exports.isLeapYear = exports.greet = exports.getGreeting = exports.generateGreeting = exports.Colour = exports.Color = void 0;
|
|
6
|
+
exports.countInstanceMethods = exports.convertArrayToString = exports.saveToSessionStorage = exports.saveToLocalStorage = exports.removeFromSessionStorage = exports.removeFromLocalStorage = exports.getFromSessionStorage = exports.getFromLocalStorage = exports.toggleFullScreen = exports.smoothScrollTo = exports.copyToClipboard = exports.updateQueryParam = exports.getQueryParams = exports.generateQueryParams = exports.formatQueryParams = exports.createQueryParams = exports.remapObjectFields = exports.remapFields = exports.pickObjectFieldsByCondition = exports.pickObjectFields = exports.pickFieldsByCondition = exports.pickFields = exports.convertObjectValues = exports.sanitizeData = exports.parseStringifiedValues = exports.parseStringifiedPrimitives = exports.parseStringifiedObjectValues = exports.parsePrimitives = exports.parsePrimitiveData = exports.parseObjectValues = exports.mergeObjects = exports.mergeAndFlattenObjects = exports.flattenObjectKeyValue = exports.flattenObjectDotNotation = exports.extractUpdatedFields = exports.extractUpdatedAndNewFields = exports.extractNewFields = exports.countObjectFields = exports.cloneObject = exports.isValidFormData = exports.isOriginFileObj = exports.isFileUpload = exports.isFileList = exports.isFileArray = exports.isCustomFileArray = exports.isCustomFile = exports.serializeForm = exports.parseFormData = exports.createFormData = exports.createControlledFormData = void 0;
|
|
7
|
+
exports.isValidSet = exports.isValidObject = exports.isValidMap = exports.isValidJSON = exports.isValidArray = exports.isSet = exports.isReturningPromise = exports.isRegularExpression = exports.isRegExp = exports.isPromise = exports.isObjectWithKeys = exports.isObjectEmpty = exports.isObject = exports.isNotEmptyObject = exports.isMethodDescriptor = exports.isMethod = exports.isMap = exports.isJSONObject = exports.isJSON = exports.isFunction = exports.isError = exports.isEmptyObjectGuard = exports.isEmptyObject = exports.isDate = exports.isBigInt = exports.isArrayWithLength = exports.isArrayOfType = exports.isArray = exports.doesReturnPromise = exports.isUndefined = exports.isTruthy = exports.isSymbol = exports.isString = exports.isPrimitive = exports.isPositiveInteger = exports.isNumber = exports.isNull = exports.isNonEmptyString = exports.isInteger = exports.isFalsy = exports.isBoolean = exports.throttleAction = exports.isDeepEqual = exports.getStaticMethodsCount = exports.getStaticMethodNames = exports.getInstanceMethodsCount = exports.getInstanceMethodNames = exports.getClassDetails = exports.debounceAction = exports.countStaticMethods = void 0;
|
|
8
|
+
exports.isValidURL = exports.isValidEmail = exports.isUUID = exports.isURL = exports.isPhoneNumber = exports.isNumericString = exports.isNodeEnvironment = exports.isNodeENV = exports.isNode = exports.isIPAddress = exports.isExpectedNodeENV = exports.isEnvironment = exports.isEmailArray = exports.isEmail = exports.isDateString = exports.isBrowser = exports.isBase64 = void 0;
|
|
9
9
|
// ! String Utilities
|
|
10
10
|
var basics_1 = require("./string/basics");
|
|
11
11
|
Object.defineProperty(exports, "capitalizeString", { enumerable: true, get: function () { return basics_1.capitalizeString; } });
|
|
@@ -44,23 +44,17 @@ Object.defineProperty(exports, "calculateGCD", { enumerable: true, get: function
|
|
|
44
44
|
Object.defineProperty(exports, "calculateHCF", { enumerable: true, get: function () { return basics_2.calculateHCF; } });
|
|
45
45
|
Object.defineProperty(exports, "calculateLCD", { enumerable: true, get: function () { return basics_2.calculateLCM; } });
|
|
46
46
|
Object.defineProperty(exports, "calculateLCM", { enumerable: true, get: function () { return basics_2.calculateLCM; } });
|
|
47
|
-
Object.defineProperty(exports, "calculateOriginalFromPercentage", { enumerable: true, get: function () { return basics_2.getOriginalFromPercentage; } });
|
|
48
|
-
Object.defineProperty(exports, "calculatePercentage", { enumerable: true, get: function () { return basics_2.getPercentage; } });
|
|
49
47
|
Object.defineProperty(exports, "convertToDecimal", { enumerable: true, get: function () { return basics_2.convertToDecimal; } });
|
|
50
48
|
Object.defineProperty(exports, "getAverage", { enumerable: true, get: function () { return basics_2.getAverage; } });
|
|
51
49
|
Object.defineProperty(exports, "getAverageOfNumbers", { enumerable: true, get: function () { return basics_2.getAverage; } });
|
|
52
|
-
Object.defineProperty(exports, "getOriginalNumberFromPercentage", { enumerable: true, get: function () { return basics_2.getOriginalFromPercentage; } });
|
|
53
|
-
Object.defineProperty(exports, "getPercentage", { enumerable: true, get: function () { return basics_2.getPercentage; } });
|
|
54
|
-
Object.defineProperty(exports, "getPercentageOfWhat", { enumerable: true, get: function () { return basics_2.getOriginalFromPercentage; } });
|
|
55
|
-
Object.defineProperty(exports, "getPercentageResult", { enumerable: true, get: function () { return basics_2.getValueFromPercentage; } });
|
|
56
50
|
Object.defineProperty(exports, "getRandomNumber", { enumerable: true, get: function () { return basics_2.getRandomNumber; } });
|
|
57
51
|
Object.defineProperty(exports, "getSumOfNumbers", { enumerable: true, get: function () { return basics_2.sumNumbers; } });
|
|
58
|
-
Object.defineProperty(exports, "getValueFromPercentage", { enumerable: true, get: function () { return basics_2.getValueFromPercentage; } });
|
|
59
|
-
Object.defineProperty(exports, "percentageOfWhat", { enumerable: true, get: function () { return basics_2.getOriginalFromPercentage; } });
|
|
60
52
|
Object.defineProperty(exports, "reverseNumber", { enumerable: true, get: function () { return basics_2.reverseNumber; } });
|
|
61
53
|
Object.defineProperty(exports, "sumDigits", { enumerable: true, get: function () { return basics_2.sumDigits; } });
|
|
62
54
|
Object.defineProperty(exports, "sumNumbers", { enumerable: true, get: function () { return basics_2.sumNumbers; } });
|
|
63
55
|
Object.defineProperty(exports, "sumOfNumbers", { enumerable: true, get: function () { return basics_2.sumNumbers; } });
|
|
56
|
+
var percent_1 = require("./number/percent");
|
|
57
|
+
Object.defineProperty(exports, "calculatePercentage", { enumerable: true, get: function () { return percent_1.calculatePercentage; } });
|
|
64
58
|
var fibonacci_1 = require("./number/fibonacci");
|
|
65
59
|
Object.defineProperty(exports, "fibonacciGenerator", { enumerable: true, get: function () { return fibonacci_1.fibonacciGenerator; } });
|
|
66
60
|
Object.defineProperty(exports, "generateFibonacci", { enumerable: true, get: function () { return fibonacci_1.fibonacciGenerator; } });
|
|
@@ -72,9 +66,11 @@ Object.defineProperty(exports, "getMemoizedFibonacci", { enumerable: true, get:
|
|
|
72
66
|
Object.defineProperty(exports, "getMemoizedFibonacciSeries", { enumerable: true, get: function () { return fibonacci_1.getFibonacciSeriesMemo; } });
|
|
73
67
|
Object.defineProperty(exports, "getNthFibonacci", { enumerable: true, get: function () { return fibonacci_1.getNthFibonacci; } });
|
|
74
68
|
var guards_2 = require("./number/guards");
|
|
69
|
+
Object.defineProperty(exports, "areInvalidNumbers", { enumerable: true, get: function () { return guards_2.areInvalidNumbers; } });
|
|
75
70
|
Object.defineProperty(exports, "isEven", { enumerable: true, get: function () { return guards_2.isEven; } });
|
|
76
71
|
Object.defineProperty(exports, "isEvenNumber", { enumerable: true, get: function () { return guards_2.isEven; } });
|
|
77
72
|
Object.defineProperty(exports, "isFibonacci", { enumerable: true, get: function () { return guards_2.isFibonacci; } });
|
|
73
|
+
Object.defineProperty(exports, "isInvalidNumbers", { enumerable: true, get: function () { return guards_2.areInvalidNumbers; } });
|
|
78
74
|
Object.defineProperty(exports, "isMultiple", { enumerable: true, get: function () { return guards_2.isMultiple; } });
|
|
79
75
|
Object.defineProperty(exports, "isOdd", { enumerable: true, get: function () { return guards_2.isOdd; } });
|
|
80
76
|
Object.defineProperty(exports, "isOddNumber", { enumerable: true, get: function () { return guards_2.isOdd; } });
|
|
@@ -5,9 +5,6 @@ exports.sumDigits = sumDigits;
|
|
|
5
5
|
exports.sumNumbers = sumNumbers;
|
|
6
6
|
exports.reverseNumber = reverseNumber;
|
|
7
7
|
exports.getAverage = getAverage;
|
|
8
|
-
exports.getPercentage = getPercentage;
|
|
9
|
-
exports.getValueFromPercentage = getValueFromPercentage;
|
|
10
|
-
exports.getOriginalFromPercentage = getOriginalFromPercentage;
|
|
11
8
|
const helpers_1 = require("./helpers");
|
|
12
9
|
/**
|
|
13
10
|
* * Utility to generate a random number between a given range.
|
|
@@ -137,69 +134,17 @@ function reverseNumber(num) {
|
|
|
137
134
|
* * Calculates the average of a set of numbers.
|
|
138
135
|
*
|
|
139
136
|
* @param numbers - A list of numbers for which to calculate the average.
|
|
140
|
-
* @returns The average of the provided numbers. Returns `NaN` if no numbers are
|
|
137
|
+
* @returns The average of the provided numbers. Returns `NaN` if no numbers are valid.
|
|
141
138
|
*/
|
|
142
139
|
function getAverage(...numbers) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
140
|
+
let sum = 0;
|
|
141
|
+
let count = 0;
|
|
142
|
+
for (const n of numbers) {
|
|
143
|
+
const num = Number(n);
|
|
144
|
+
if (typeof num === 'number' && !isNaN(num)) {
|
|
145
|
+
sum += num;
|
|
146
|
+
count++;
|
|
147
|
+
}
|
|
147
148
|
}
|
|
148
|
-
|
|
149
|
-
const validNumbers = cNumbers.filter((num) => typeof num === 'number' && !isNaN(num));
|
|
150
|
-
// Edge case: check if the valid numbers array is empty after filtering
|
|
151
|
-
if (validNumbers.length === 0) {
|
|
152
|
-
return NaN; // Return NaN if no valid numbers are found
|
|
153
|
-
}
|
|
154
|
-
// Calculate the sum of the valid numbers
|
|
155
|
-
const sum = validNumbers.reduce((acc, curr) => acc + curr, 0);
|
|
156
|
-
// Return the average
|
|
157
|
-
return sum / validNumbers.length;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* * Calculates the percentage of a number based on a given part and total.
|
|
161
|
-
*
|
|
162
|
-
* @param part - The part of the total value.
|
|
163
|
-
* @param total - The total value.
|
|
164
|
-
* @returns The percentage of the part in relation to the total, rounded to two decimal places.
|
|
165
|
-
* Returns `NaN` if the total is zero or invalid.
|
|
166
|
-
*/
|
|
167
|
-
function getPercentage(part, total) {
|
|
168
|
-
if (total === 0 || !Number.isFinite(total) || !Number.isFinite(part)) {
|
|
169
|
-
return NaN; // Prevent division by zero or invalid values
|
|
170
|
-
}
|
|
171
|
-
return (part / total) * 100;
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* * Calculates the number that a given percentage corresponds to based on a total value.
|
|
175
|
-
*
|
|
176
|
-
* @param percentage - The percentage of the total value.
|
|
177
|
-
* @param total - The total value.
|
|
178
|
-
* @returns The number corresponding to the given percentage of the total, rounded to two decimal places.
|
|
179
|
-
* Returns `NaN` if the total is zero or invalid.
|
|
180
|
-
*/
|
|
181
|
-
function getValueFromPercentage(percentage, total) {
|
|
182
|
-
if (total === 0 ||
|
|
183
|
-
!Number.isFinite(total) ||
|
|
184
|
-
!Number.isFinite(percentage)) {
|
|
185
|
-
return NaN; // Prevent division by zero or invalid values
|
|
186
|
-
}
|
|
187
|
-
return (percentage / 100) * total;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* * Calculates the original number based on the given percentage and the corresponding value.
|
|
191
|
-
*
|
|
192
|
-
* @param percentage - The percentage value (e.g., 10 for 10%).
|
|
193
|
-
* @param percentageValue - The value corresponding to the given percentage.
|
|
194
|
-
* @returns The original number from which the percentage value is derived, or `NaN` if invalid inputs are provided.
|
|
195
|
-
*/
|
|
196
|
-
function getOriginalFromPercentage(percentage, percentageValue) {
|
|
197
|
-
if (percentage === 0 ||
|
|
198
|
-
percentageValue === 0 ||
|
|
199
|
-
!Number.isFinite(percentage) ||
|
|
200
|
-
!Number.isFinite(percentageValue)) {
|
|
201
|
-
return NaN; // Prevent division by zero or invalid values
|
|
202
|
-
}
|
|
203
|
-
const original = (percentageValue / percentage) * 100;
|
|
204
|
-
return Math.round(original * 100) / 100;
|
|
149
|
+
return count === 0 ? NaN : Math.round((sum / count) * 1000) / 1000;
|
|
205
150
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.isMultiple = exports.isOdd = exports.isEven = void 0;
|
|
4
4
|
exports.isPerfectSquare = isPerfectSquare;
|
|
5
5
|
exports.isFibonacci = isFibonacci;
|
|
6
|
+
exports.areInvalidNumbers = areInvalidNumbers;
|
|
6
7
|
/**
|
|
7
8
|
* * Check if a number is even or not.
|
|
8
9
|
*
|
|
@@ -52,3 +53,12 @@ function isPerfectSquare(num) {
|
|
|
52
53
|
function isFibonacci(num) {
|
|
53
54
|
return (isPerfectSquare(5 * num * num + 4) || isPerfectSquare(5 * num * num - 4));
|
|
54
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* * Checks whether any input is not a finite number.
|
|
58
|
+
*
|
|
59
|
+
* @param numbers - The list of numbers to validate.
|
|
60
|
+
* @returns `true` if any input is not finite.
|
|
61
|
+
*/
|
|
62
|
+
function areInvalidNumbers(...numbers) {
|
|
63
|
+
return numbers.some((n) => !Number.isFinite(n));
|
|
64
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculatePercentage = calculatePercentage;
|
|
4
|
+
const basics_1 = require("./basics");
|
|
5
|
+
const guards_1 = require("./guards");
|
|
6
|
+
/**
|
|
7
|
+
* * Performs a percentage-related calculation based on the given mode and inputs.
|
|
8
|
+
*
|
|
9
|
+
* - `get-percent`: Calculates what percentage the `part` is of the `total`.
|
|
10
|
+
* - `get-value`: Calculates the value from a given `percentage` of a `total`.
|
|
11
|
+
* - `get-original`: Calculates the original value from a known `value` and `percentage`.
|
|
12
|
+
* - `get-change-percent`: Percent increase/decrease from `oldValue` to `newValue`.
|
|
13
|
+
* - `apply-percent-change`: Applies increase/decrease by `percentage` to `baseValue`.
|
|
14
|
+
* - `get-percent-difference`: Absolute percent difference between two values.
|
|
15
|
+
* - `inverse-percent`: What percent `total` is of `part`.
|
|
16
|
+
*
|
|
17
|
+
* @param options - The calculation mode and inputs required for the operation.
|
|
18
|
+
* @returns The calculated number rounded to three decimal places, or `NaN` if input is invalid.
|
|
19
|
+
*/
|
|
20
|
+
function calculatePercentage(options) {
|
|
21
|
+
switch (options?.mode) {
|
|
22
|
+
case 'get-percent': {
|
|
23
|
+
const { part, total } = options;
|
|
24
|
+
if ((0, guards_1.areInvalidNumbers)(part, total) || total === 0) {
|
|
25
|
+
return NaN;
|
|
26
|
+
}
|
|
27
|
+
return Math.round((part / total) * 100 * 1000) / 1000;
|
|
28
|
+
}
|
|
29
|
+
case 'get-value': {
|
|
30
|
+
const { percentage, total } = options;
|
|
31
|
+
if ((0, guards_1.areInvalidNumbers)(percentage, total) || total === 0) {
|
|
32
|
+
return NaN;
|
|
33
|
+
}
|
|
34
|
+
return Math.round((percentage / 100) * total * 1000) / 1000;
|
|
35
|
+
}
|
|
36
|
+
case 'get-original': {
|
|
37
|
+
const { percentage, value } = options;
|
|
38
|
+
if ((0, guards_1.areInvalidNumbers)(percentage, value) || percentage === 0) {
|
|
39
|
+
return NaN;
|
|
40
|
+
}
|
|
41
|
+
return Math.round((value / percentage) * 100 * 1000) / 1000;
|
|
42
|
+
}
|
|
43
|
+
case 'get-change-percent': {
|
|
44
|
+
const { oldValue, newValue } = options;
|
|
45
|
+
if ((0, guards_1.areInvalidNumbers)(oldValue, newValue) || oldValue === 0) {
|
|
46
|
+
return NaN;
|
|
47
|
+
}
|
|
48
|
+
const change = ((newValue - oldValue) / oldValue) * 100;
|
|
49
|
+
return Math.round(change * 1000) / 1000;
|
|
50
|
+
}
|
|
51
|
+
case 'apply-percent-change': {
|
|
52
|
+
const { baseValue, percentage } = options;
|
|
53
|
+
if ((0, guards_1.areInvalidNumbers)(baseValue, percentage)) {
|
|
54
|
+
return NaN;
|
|
55
|
+
}
|
|
56
|
+
const value = baseValue * (1 + percentage / 100);
|
|
57
|
+
return Math.round(value * 1000) / 1000;
|
|
58
|
+
}
|
|
59
|
+
case 'get-percent-difference': {
|
|
60
|
+
const { value1, value2 } = options;
|
|
61
|
+
if ((0, guards_1.areInvalidNumbers)(value1, value2)) {
|
|
62
|
+
return NaN;
|
|
63
|
+
}
|
|
64
|
+
const avg = (0, basics_1.getAverage)(value1, value2);
|
|
65
|
+
if (avg === 0) {
|
|
66
|
+
return NaN;
|
|
67
|
+
}
|
|
68
|
+
const diff = (Math.abs(value1 - value2) / avg) * 100;
|
|
69
|
+
return Math.round(diff * 1000) / 1000;
|
|
70
|
+
}
|
|
71
|
+
case 'inverse-percent': {
|
|
72
|
+
const { part, total } = options;
|
|
73
|
+
if ((0, guards_1.areInvalidNumbers)(part, total) || part === 0) {
|
|
74
|
+
return NaN;
|
|
75
|
+
}
|
|
76
|
+
return Math.round((total / part) * 100 * 1000) / 1000;
|
|
77
|
+
}
|
|
78
|
+
default:
|
|
79
|
+
return NaN;
|
|
80
|
+
}
|
|
81
|
+
}
|
package/dist/dts/index.d.ts
CHANGED
|
@@ -3,9 +3,10 @@ export { generateAnagrams } from './string/anagram';
|
|
|
3
3
|
export { isCamelCase, isEmojiOnly, isKebabCase, isPalindrome, isPascalCase, isSnakeCase, } from './string/guards';
|
|
4
4
|
export { convertStringCase, extractEmails, extractURLs, formatUnitWithPlural as formatNumberWithPluralUnit, formatUnitWithPlural, formatUnitWithPlural as formatWithPlural, maskString, normalizeString, replaceAllInString, reverseString, slugifyString, } from './string/convert';
|
|
5
5
|
export { extractNumbersFromString, getLevenshteinDistance, getLevenshteinDistance as levenshteinDistance, } from './string/utilities';
|
|
6
|
-
export { getAverage as calculateAverage, calculateHCF as calculateGCD, calculateHCF, calculateLCM as calculateLCD, calculateLCM,
|
|
6
|
+
export { getAverage as calculateAverage, calculateHCF as calculateGCD, calculateHCF, calculateLCM as calculateLCD, calculateLCM, convertToDecimal, getAverage, getAverage as getAverageOfNumbers, getRandomNumber, sumNumbers as getSumOfNumbers, reverseNumber, sumDigits, sumNumbers, sumNumbers as sumOfNumbers, } from './number/basics';
|
|
7
|
+
export { calculatePercentage } from './number/percent';
|
|
7
8
|
export { fibonacciGenerator, fibonacciGenerator as generateFibonacci, getFibonacciSeries as getFibonacci, getFibonacciSeries as getFibonacciNumbers, getFibonacciSeries, getFibonacciSeriesMemo, getFibonacciSeriesMemo as getMemoizedFibonacci, getFibonacciSeriesMemo as getMemoizedFibonacciSeries, getNthFibonacci, } from './number/fibonacci';
|
|
8
|
-
export { isEven, isEven as isEvenNumber, isFibonacci, isMultiple, isOdd, isOdd as isOddNumber, isFibonacci as isParOfFibonacci, isFibonacci as isParOfFibonacciSeries, isPerfectSquare, } from './number/guards';
|
|
9
|
+
export { areInvalidNumbers, isEven, isEven as isEvenNumber, isFibonacci, areInvalidNumbers as isInvalidNumbers, isMultiple, isOdd, isOdd as isOddNumber, isFibonacci as isParOfFibonacci, isFibonacci as isParOfFibonacciSeries, isPerfectSquare, } from './number/guards';
|
|
9
10
|
export { numberToWords as convertNumberToWords, convertToRomanNumerals, numberToWords, } from './number/convert';
|
|
10
11
|
export { findPrimeNumbers, findPrimeNumbers as getPrimeNumbers, isPrime, isPrime as isPrimeNumber, } from './number/prime';
|
|
11
12
|
export { getOrdinal as cardinalToOrdinal, clampNumber, formatCurrency as convertNumberToCurrency, getOrdinal as convertNumberToOrdinal, getOrdinal as convertToOrdinal, formatCurrency, getOrdinal, getOrdinal as getOrdinalNumber, getRandomFloat as getRandomDecimal, getRandomFloat, getOrdinal as numberToOrdinal, roundToNearest as roundNumber, roundToNearest as roundNumberToNearestInterval, roundToNearest, roundToNearest as roundToNearestInterval, } from './number/utilities';
|
package/dist/dts/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACV,cAAc,GACd,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,EACN,WAAW,EACX,WAAW,EACX,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,WAAW,GACX,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACN,iBAAiB,EACjB,aAAa,EACb,WAAW,EACX,oBAAoB,IAAI,0BAA0B,EAClD,oBAAoB,EACpB,oBAAoB,IAAI,gBAAgB,EACxC,UAAU,EACV,eAAe,EACf,kBAAkB,EAClB,aAAa,EACb,aAAa,GACb,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACN,wBAAwB,EACxB,sBAAsB,EACtB,sBAAsB,IAAI,mBAAmB,GAC7C,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EACN,UAAU,IAAI,gBAAgB,EAC9B,YAAY,IAAI,YAAY,EAC5B,YAAY,EACZ,YAAY,IAAI,YAAY,EAC5B,YAAY,EACZ,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACV,cAAc,GACd,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,EACN,WAAW,EACX,WAAW,EACX,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,WAAW,GACX,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACN,iBAAiB,EACjB,aAAa,EACb,WAAW,EACX,oBAAoB,IAAI,0BAA0B,EAClD,oBAAoB,EACpB,oBAAoB,IAAI,gBAAgB,EACxC,UAAU,EACV,eAAe,EACf,kBAAkB,EAClB,aAAa,EACb,aAAa,GACb,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACN,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BAAkB,EACnC,OAAO,EACP,UAAU,EACV,MAAM,EACN,MAAM,IAAI,YAAY,EACtB,KAAK,EACL,kBAAkB,IAAI,QAAQ,EAC9B,kBAAkB,EAClB,gBAAgB,EAChB,QAAQ,EACR,aAAa,IAAI,aAAa,EAC9B,gBAAgB,EAChB,SAAS,EACT,QAAQ,EACR,QAAQ,IAAI,mBAAmB,EAC/B,kBAAkB,EAClB,KAAK,EACL,YAAY,EACZ,MAAM,IAAI,WAAW,EACrB,KAAK,IAAI,UAAU,EACnB,gBAAgB,IAAI,aAAa,EACjC,KAAK,IAAI,UAAU,GACnB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EACN,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,OAAO,EACP,YAAY,EACZ,aAAa,EACb,aAAa,IAAI,iBAAiB,EAClC,WAAW,EACX,MAAM,EACN,aAAa,IAAI,SAAS,EAC1B,aAAa,IAAI,iBAAiB,EAClC,eAAe,EACf,aAAa,EACb,KAAK,EACL,MAAM,EACN,OAAO,IAAI,YAAY,EACvB,KAAK,IAAI,UAAU,GACnB,MAAM,mBAAmB,CAAC"}
|
|
@@ -56,33 +56,7 @@ export declare function reverseNumber(num: Numeric): number;
|
|
|
56
56
|
* * Calculates the average of a set of numbers.
|
|
57
57
|
*
|
|
58
58
|
* @param numbers - A list of numbers for which to calculate the average.
|
|
59
|
-
* @returns The average of the provided numbers. Returns `NaN` if no numbers are
|
|
59
|
+
* @returns The average of the provided numbers. Returns `NaN` if no numbers are valid.
|
|
60
60
|
*/
|
|
61
61
|
export declare function getAverage(...numbers: Numeric[]): number;
|
|
62
|
-
/**
|
|
63
|
-
* * Calculates the percentage of a number based on a given part and total.
|
|
64
|
-
*
|
|
65
|
-
* @param part - The part of the total value.
|
|
66
|
-
* @param total - The total value.
|
|
67
|
-
* @returns The percentage of the part in relation to the total, rounded to two decimal places.
|
|
68
|
-
* Returns `NaN` if the total is zero or invalid.
|
|
69
|
-
*/
|
|
70
|
-
export declare function getPercentage(part: number, total: number): number;
|
|
71
|
-
/**
|
|
72
|
-
* * Calculates the number that a given percentage corresponds to based on a total value.
|
|
73
|
-
*
|
|
74
|
-
* @param percentage - The percentage of the total value.
|
|
75
|
-
* @param total - The total value.
|
|
76
|
-
* @returns The number corresponding to the given percentage of the total, rounded to two decimal places.
|
|
77
|
-
* Returns `NaN` if the total is zero or invalid.
|
|
78
|
-
*/
|
|
79
|
-
export declare function getValueFromPercentage(percentage: number, total: number): number;
|
|
80
|
-
/**
|
|
81
|
-
* * Calculates the original number based on the given percentage and the corresponding value.
|
|
82
|
-
*
|
|
83
|
-
* @param percentage - The percentage value (e.g., 10 for 10%).
|
|
84
|
-
* @param percentageValue - The value corresponding to the given percentage.
|
|
85
|
-
* @returns The original number from which the percentage value is derived, or `NaN` if invalid inputs are provided.
|
|
86
|
-
*/
|
|
87
|
-
export declare function getOriginalFromPercentage(percentage: number, percentageValue: number): number;
|
|
88
62
|
//# sourceMappingURL=basics.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"basics.d.ts","sourceRoot":"","sources":["../../../src/number/basics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAExC,OAAO,KAAK,EACX,gBAAgB,EAChB,cAAc,EACd,mBAAmB,EACnB,MAAM,SAAS,CAAC;AAEjB;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,aAAc,mBAAmB,KAAG,MA+C/D,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,OAAO,GAAG,SAAS,iBACtD,OAAO,YACJ,cAAc,CAAC,CAAC,CAAC,KACzB,gBAAgB,CAAC,CAAC,CAQpB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY,eAAgB,OAAO,EAAE,KAAG,MAYpD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY,eAAgB,OAAO,EAAE,KAAG,MAY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AU,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE,GAAG,MAAM,CAaxD"}
|
|
@@ -34,4 +34,11 @@ export declare function isPerfectSquare(num: number): boolean;
|
|
|
34
34
|
* @returns `true` if the number is a Fibonacci number, otherwise `false`.
|
|
35
35
|
*/
|
|
36
36
|
export declare function isFibonacci(num: number): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* * Checks whether any input is not a finite number.
|
|
39
|
+
*
|
|
40
|
+
* @param numbers - The list of numbers to validate.
|
|
41
|
+
* @returns `true` if any input is not finite.
|
|
42
|
+
*/
|
|
43
|
+
export declare function areInvalidNumbers(...numbers: number[]): boolean;
|
|
37
44
|
//# sourceMappingURL=guards.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../../src/number/guards.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,MAAM,UAAW,MAAM,KAAG,OAEtC,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,KAAK,UAAW,MAAM,KAAG,OAErC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,UAAW,MAAM,cAAc,MAAM,KAAG,OAE9D,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAIhD"}
|
|
1
|
+
{"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../../src/number/guards.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,MAAM,UAAW,MAAM,KAAG,OAEtC,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,KAAK,UAAW,MAAM,KAAG,OAErC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,UAAW,MAAM,cAAc,MAAM,KAAG,OAE9D,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAIhD;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAE/D"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { PercentageOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* * Performs a percentage-related calculation based on the given mode and inputs.
|
|
4
|
+
*
|
|
5
|
+
* - `get-percent`: Calculates what percentage the `part` is of the `total`.
|
|
6
|
+
* - `get-value`: Calculates the value from a given `percentage` of a `total`.
|
|
7
|
+
* - `get-original`: Calculates the original value from a known `value` and `percentage`.
|
|
8
|
+
* - `get-change-percent`: Percent increase/decrease from `oldValue` to `newValue`.
|
|
9
|
+
* - `apply-percent-change`: Applies increase/decrease by `percentage` to `baseValue`.
|
|
10
|
+
* - `get-percent-difference`: Absolute percent difference between two values.
|
|
11
|
+
* - `inverse-percent`: What percent `total` is of `part`.
|
|
12
|
+
*
|
|
13
|
+
* @param options - The calculation mode and inputs required for the operation.
|
|
14
|
+
* @returns The calculated number rounded to three decimal places, or `NaN` if input is invalid.
|
|
15
|
+
*/
|
|
16
|
+
export declare function calculatePercentage(options: PercentageOptions): number;
|
|
17
|
+
//# sourceMappingURL=percent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"percent.d.ts","sourceRoot":"","sources":["../../../src/number/percent.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,iBAAiB,GAAG,MAAM,CAuFtE"}
|
|
@@ -38,4 +38,69 @@ export type RangedNumbers<T extends GetAs> = T extends 'array' ? number[] : stri
|
|
|
38
38
|
export type CurrencyCode = keyof typeof CURRENCY_LOCALES | (typeof CURRENCY_CODES)[number];
|
|
39
39
|
/** - List of all supported BCP 47 locales */
|
|
40
40
|
export type LocaleCode = (typeof CURRENCY_LOCALES)[keyof typeof CURRENCY_LOCALES] | (typeof LOCALE_CODES)[number];
|
|
41
|
+
/** * Options to calculate what percentage a `part` is of a `total`. */
|
|
42
|
+
export interface GetPercentOptions {
|
|
43
|
+
/** Mode to calculate percentage from `part` and `total` */
|
|
44
|
+
mode: 'get-percent';
|
|
45
|
+
/** The part value (e.g., 25 out of 100) */
|
|
46
|
+
part: number;
|
|
47
|
+
/** The total value representing 100% */
|
|
48
|
+
total: number;
|
|
49
|
+
}
|
|
50
|
+
/** * Options to calculate a value from a `percentage` of a `total`. */
|
|
51
|
+
export interface GetValueOptions {
|
|
52
|
+
/** Mode to calculate value from `percentage` and `total` */
|
|
53
|
+
mode: 'get-value';
|
|
54
|
+
/** The percentage (e.g., 25%) */
|
|
55
|
+
percentage: number;
|
|
56
|
+
/** The total value representing 100% */
|
|
57
|
+
total: number;
|
|
58
|
+
}
|
|
59
|
+
/** * Options to calculate the original total from a known `value` and `percentage`. */
|
|
60
|
+
export interface GetOriginalOptions {
|
|
61
|
+
/** Mode to calculate original total from `value` and `percentage` */
|
|
62
|
+
mode: 'get-original';
|
|
63
|
+
/** The percentage the `value` represents */
|
|
64
|
+
percentage: number;
|
|
65
|
+
/** The known value that is a percentage of the original total */
|
|
66
|
+
value: number;
|
|
67
|
+
}
|
|
68
|
+
/** * Calculates the percentage change from `oldValue` to `newValue`. */
|
|
69
|
+
export interface GetChangeOptions {
|
|
70
|
+
/** Mode to calculate percentage change from `oldValue` to `newValue` */
|
|
71
|
+
mode: 'get-change-percent';
|
|
72
|
+
/** The original value before the change */
|
|
73
|
+
oldValue: number;
|
|
74
|
+
/** The new value after the change */
|
|
75
|
+
newValue: number;
|
|
76
|
+
}
|
|
77
|
+
/** * Applies a percentage increase or decrease to a `baseValue`. */
|
|
78
|
+
export interface ApplyChangeOptions {
|
|
79
|
+
/** Mode to apply percentage change to `baseValue` */
|
|
80
|
+
mode: 'apply-percent-change';
|
|
81
|
+
/** The base value to apply the percentage change to */
|
|
82
|
+
baseValue: number;
|
|
83
|
+
/** The percentage change to apply (positive or negative) */
|
|
84
|
+
percentage: number;
|
|
85
|
+
}
|
|
86
|
+
/** * Calculates the absolute percentage difference between two values. */
|
|
87
|
+
export interface GetDifferenceOptions {
|
|
88
|
+
/** Mode to calculate percentage difference between `value1` and `value2` */
|
|
89
|
+
mode: 'get-percent-difference';
|
|
90
|
+
/** The first value to compare */
|
|
91
|
+
value1: number;
|
|
92
|
+
/** The second value to compare */
|
|
93
|
+
value2: number;
|
|
94
|
+
}
|
|
95
|
+
/** * Calculates the inverse percentage: what percent `total` is of `part`. */
|
|
96
|
+
export interface InversePercentageOptions {
|
|
97
|
+
/** Mode to calculate inverse percentage from `part` and `total` */
|
|
98
|
+
mode: 'inverse-percent';
|
|
99
|
+
/** The part value to calculate inverse percentage from */
|
|
100
|
+
part: number;
|
|
101
|
+
/** The total value to calculate inverse percentage of */
|
|
102
|
+
total: number;
|
|
103
|
+
}
|
|
104
|
+
/** * Options for calculating percentages and related values. */
|
|
105
|
+
export type PercentageOptions = GetPercentOptions | GetValueOptions | GetOriginalOptions | GetChangeOptions | ApplyChangeOptions | GetDifferenceOptions | InversePercentageOptions;
|
|
41
106
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/number/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,MAAM,aAAa,CAAC;AAErB,4CAA4C;AAC5C,MAAM,WAAW,mBAAmB;IACnC,oCAAoC;IACpC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,kCAAkC;IAClC,GAAG,CAAC,EAAE,MAAM,CAAC;I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AAM,CAAC;CACnB;AAED,0EAA0E;AAC1E,MAAM,WAAW,oBAAoB;IACpC,4EAA4E;IAC5E,IAAI,EAAE,wBAAwB,CAAC;IAC/B,iCAAiC;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,kCAAkC;IAClC,MAAM,EAAE,MAAM,CAAC;CACf;AAED,8EAA8E;AAC9E,MAAM,WAAW,wBAAwB;IACxC,mEAAmE;IACnE,IAAI,EAAE,iBAAiB,CAAC;IACxB,0DAA0D;IAC1D,IAAI,EAAE,MAAM,CAAC;IACb,yDAAyD;IACzD,KAAK,EAAE,MAAM,CAAC;CACd;AAED,gEAAgE;AAChE,MAAM,MAAM,iBAAiB,GAC1B,iBAAiB,GACjB,eAAe,GACf,kBAAkB,GAClB,gBAAgB,GAChB,kBAAkB,GAClB,oBAAoB,GACpB,wBAAwB,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -5,9 +5,10 @@ export { isCamelCase, isEmojiOnly, isKebabCase, isPalindrome, isPascalCase, isSn
|
|
|
5
5
|
export { convertStringCase, extractEmails, extractURLs, formatUnitWithPlural as formatNumberWithPluralUnit, formatUnitWithPlural, formatUnitWithPlural as formatWithPlural, maskString, normalizeString, replaceAllInString, reverseString, slugifyString, } from './string/convert';
|
|
6
6
|
export { extractNumbersFromString, getLevenshteinDistance, getLevenshteinDistance as levenshteinDistance, } from './string/utilities';
|
|
7
7
|
// ! Number Utilities
|
|
8
|
-
export { getAverage as calculateAverage, calculateHCF as calculateGCD, calculateHCF, calculateLCM as calculateLCD, calculateLCM,
|
|
8
|
+
export { getAverage as calculateAverage, calculateHCF as calculateGCD, calculateHCF, calculateLCM as calculateLCD, calculateLCM, convertToDecimal, getAverage, getAverage as getAverageOfNumbers, getRandomNumber, sumNumbers as getSumOfNumbers, reverseNumber, sumDigits, sumNumbers, sumNumbers as sumOfNumbers, } from './number/basics';
|
|
9
|
+
export { calculatePercentage } from './number/percent';
|
|
9
10
|
export { fibonacciGenerator, fibonacciGenerator as generateFibonacci, getFibonacciSeries as getFibonacci, getFibonacciSeries as getFibonacciNumbers, getFibonacciSeries, getFibonacciSeriesMemo, getFibonacciSeriesMemo as getMemoizedFibonacci, getFibonacciSeriesMemo as getMemoizedFibonacciSeries, getNthFibonacci, } from './number/fibonacci';
|
|
10
|
-
export { isEven, isEven as isEvenNumber, isFibonacci, isMultiple, isOdd, isOdd as isOddNumber, isFibonacci as isParOfFibonacci, isFibonacci as isParOfFibonacciSeries, isPerfectSquare, } from './number/guards';
|
|
11
|
+
export { areInvalidNumbers, isEven, isEven as isEvenNumber, isFibonacci, areInvalidNumbers as isInvalidNumbers, isMultiple, isOdd, isOdd as isOddNumber, isFibonacci as isParOfFibonacci, isFibonacci as isParOfFibonacciSeries, isPerfectSquare, } from './number/guards';
|
|
11
12
|
export { numberToWords as convertNumberToWords, convertToRomanNumerals, numberToWords, } from './number/convert';
|
|
12
13
|
export { findPrimeNumbers, findPrimeNumbers as getPrimeNumbers, isPrime, isPrime as isPrimeNumber, } from './number/prime';
|
|
13
14
|
export { getOrdinal as cardinalToOrdinal, clampNumber, formatCurrency as convertNumberToCurrency, getOrdinal as convertNumberToOrdinal, getOrdinal as convertToOrdinal, formatCurrency, getOrdinal, getOrdinal as getOrdinalNumber, getRandomFloat as getRandomDecimal, getRandomFloat, getOrdinal as numberToOrdinal, roundToNearest as roundNumber, roundToNearest as roundNumberToNearestInterval, roundToNearest, roundToNearest as roundToNearestInterval, } from './number/utilities';
|
|
@@ -123,69 +123,17 @@ export function reverseNumber(num) {
|
|
|
123
123
|
* * Calculates the average of a set of numbers.
|
|
124
124
|
*
|
|
125
125
|
* @param numbers - A list of numbers for which to calculate the average.
|
|
126
|
-
* @returns The average of the provided numbers. Returns `NaN` if no numbers are
|
|
126
|
+
* @returns The average of the provided numbers. Returns `NaN` if no numbers are valid.
|
|
127
127
|
*/
|
|
128
128
|
export function getAverage(...numbers) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
129
|
+
let sum = 0;
|
|
130
|
+
let count = 0;
|
|
131
|
+
for (const n of numbers) {
|
|
132
|
+
const num = Number(n);
|
|
133
|
+
if (typeof num === 'number' && !isNaN(num)) {
|
|
134
|
+
sum += num;
|
|
135
|
+
count++;
|
|
136
|
+
}
|
|
133
137
|
}
|
|
134
|
-
|
|
135
|
-
const validNumbers = cNumbers.filter((num) => typeof num === 'number' && !isNaN(num));
|
|
136
|
-
// Edge case: check if the valid numbers array is empty after filtering
|
|
137
|
-
if (validNumbers.length === 0) {
|
|
138
|
-
return NaN; // Return NaN if no valid numbers are found
|
|
139
|
-
}
|
|
140
|
-
// Calculate the sum of the valid numbers
|
|
141
|
-
const sum = validNumbers.reduce((acc, curr) => acc + curr, 0);
|
|
142
|
-
// Return the average
|
|
143
|
-
return sum / validNumbers.length;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* * Calculates the percentage of a number based on a given part and total.
|
|
147
|
-
*
|
|
148
|
-
* @param part - The part of the total value.
|
|
149
|
-
* @param total - The total value.
|
|
150
|
-
* @returns The percentage of the part in relation to the total, rounded to two decimal places.
|
|
151
|
-
* Returns `NaN` if the total is zero or invalid.
|
|
152
|
-
*/
|
|
153
|
-
export function getPercentage(part, total) {
|
|
154
|
-
if (total === 0 || !Number.isFinite(total) || !Number.isFinite(part)) {
|
|
155
|
-
return NaN; // Prevent division by zero or invalid values
|
|
156
|
-
}
|
|
157
|
-
return (part / total) * 100;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* * Calculates the number that a given percentage corresponds to based on a total value.
|
|
161
|
-
*
|
|
162
|
-
* @param percentage - The percentage of the total value.
|
|
163
|
-
* @param total - The total value.
|
|
164
|
-
* @returns The number corresponding to the given percentage of the total, rounded to two decimal places.
|
|
165
|
-
* Returns `NaN` if the total is zero or invalid.
|
|
166
|
-
*/
|
|
167
|
-
export function getValueFromPercentage(percentage, total) {
|
|
168
|
-
if (total === 0 ||
|
|
169
|
-
!Number.isFinite(total) ||
|
|
170
|
-
!Number.isFinite(percentage)) {
|
|
171
|
-
return NaN; // Prevent division by zero or invalid values
|
|
172
|
-
}
|
|
173
|
-
return (percentage / 100) * total;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* * Calculates the original number based on the given percentage and the corresponding value.
|
|
177
|
-
*
|
|
178
|
-
* @param percentage - The percentage value (e.g., 10 for 10%).
|
|
179
|
-
* @param percentageValue - The value corresponding to the given percentage.
|
|
180
|
-
* @returns The original number from which the percentage value is derived, or `NaN` if invalid inputs are provided.
|
|
181
|
-
*/
|
|
182
|
-
export function getOriginalFromPercentage(percentage, percentageValue) {
|
|
183
|
-
if (percentage === 0 ||
|
|
184
|
-
percentageValue === 0 ||
|
|
185
|
-
!Number.isFinite(percentage) ||
|
|
186
|
-
!Number.isFinite(percentageValue)) {
|
|
187
|
-
return NaN; // Prevent division by zero or invalid values
|
|
188
|
-
}
|
|
189
|
-
const original = (percentageValue / percentage) * 100;
|
|
190
|
-
return Math.round(original * 100) / 100;
|
|
138
|
+
return count === 0 ? NaN : Math.round((sum / count) * 1000) / 1000;
|
|
191
139
|
}
|
|
@@ -44,3 +44,12 @@ export function isPerfectSquare(num) {
|
|
|
44
44
|
export function isFibonacci(num) {
|
|
45
45
|
return (isPerfectSquare(5 * num * num + 4) || isPerfectSquare(5 * num * num - 4));
|
|
46
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* * Checks whether any input is not a finite number.
|
|
49
|
+
*
|
|
50
|
+
* @param numbers - The list of numbers to validate.
|
|
51
|
+
* @returns `true` if any input is not finite.
|
|
52
|
+
*/
|
|
53
|
+
export function areInvalidNumbers(...numbers) {
|
|
54
|
+
return numbers.some((n) => !Number.isFinite(n));
|
|
55
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { getAverage } from './basics';
|
|
2
|
+
import { areInvalidNumbers } from './guards';
|
|
3
|
+
/**
|
|
4
|
+
* * Performs a percentage-related calculation based on the given mode and inputs.
|
|
5
|
+
*
|
|
6
|
+
* - `get-percent`: Calculates what percentage the `part` is of the `total`.
|
|
7
|
+
* - `get-value`: Calculates the value from a given `percentage` of a `total`.
|
|
8
|
+
* - `get-original`: Calculates the original value from a known `value` and `percentage`.
|
|
9
|
+
* - `get-change-percent`: Percent increase/decrease from `oldValue` to `newValue`.
|
|
10
|
+
* - `apply-percent-change`: Applies increase/decrease by `percentage` to `baseValue`.
|
|
11
|
+
* - `get-percent-difference`: Absolute percent difference between two values.
|
|
12
|
+
* - `inverse-percent`: What percent `total` is of `part`.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The calculation mode and inputs required for the operation.
|
|
15
|
+
* @returns The calculated number rounded to three decimal places, or `NaN` if input is invalid.
|
|
16
|
+
*/
|
|
17
|
+
export function calculatePercentage(options) {
|
|
18
|
+
switch (options?.mode) {
|
|
19
|
+
case 'get-percent': {
|
|
20
|
+
const { part, total } = options;
|
|
21
|
+
if (areInvalidNumbers(part, total) || total === 0) {
|
|
22
|
+
return NaN;
|
|
23
|
+
}
|
|
24
|
+
return Math.round((part / total) * 100 * 1000) / 1000;
|
|
25
|
+
}
|
|
26
|
+
case 'get-value': {
|
|
27
|
+
const { percentage, total } = options;
|
|
28
|
+
if (areInvalidNumbers(percentage, total) || total === 0) {
|
|
29
|
+
return NaN;
|
|
30
|
+
}
|
|
31
|
+
return Math.round((percentage / 100) * total * 1000) / 1000;
|
|
32
|
+
}
|
|
33
|
+
case 'get-original': {
|
|
34
|
+
const { percentage, value } = options;
|
|
35
|
+
if (areInvalidNumbers(percentage, value) || percentage === 0) {
|
|
36
|
+
return NaN;
|
|
37
|
+
}
|
|
38
|
+
return Math.round((value / percentage) * 100 * 1000) / 1000;
|
|
39
|
+
}
|
|
40
|
+
case 'get-change-percent': {
|
|
41
|
+
const { oldValue, newValue } = options;
|
|
42
|
+
if (areInvalidNumbers(oldValue, newValue) || oldValue === 0) {
|
|
43
|
+
return NaN;
|
|
44
|
+
}
|
|
45
|
+
const change = ((newValue - oldValue) / oldValue) * 100;
|
|
46
|
+
return Math.round(change * 1000) / 1000;
|
|
47
|
+
}
|
|
48
|
+
case 'apply-percent-change': {
|
|
49
|
+
const { baseValue, percentage } = options;
|
|
50
|
+
if (areInvalidNumbers(baseValue, percentage)) {
|
|
51
|
+
return NaN;
|
|
52
|
+
}
|
|
53
|
+
const value = baseValue * (1 + percentage / 100);
|
|
54
|
+
return Math.round(value * 1000) / 1000;
|
|
55
|
+
}
|
|
56
|
+
case 'get-percent-difference': {
|
|
57
|
+
const { value1, value2 } = options;
|
|
58
|
+
if (areInvalidNumbers(value1, value2)) {
|
|
59
|
+
return NaN;
|
|
60
|
+
}
|
|
61
|
+
const avg = getAverage(value1, value2);
|
|
62
|
+
if (avg === 0) {
|
|
63
|
+
return NaN;
|
|
64
|
+
}
|
|
65
|
+
const diff = (Math.abs(value1 - value2) / avg) * 100;
|
|
66
|
+
return Math.round(diff * 1000) / 1000;
|
|
67
|
+
}
|
|
68
|
+
case 'inverse-percent': {
|
|
69
|
+
const { part, total } = options;
|
|
70
|
+
if (areInvalidNumbers(part, total) || part === 0) {
|
|
71
|
+
return NaN;
|
|
72
|
+
}
|
|
73
|
+
return Math.round((total / part) * 100 * 1000) / 1000;
|
|
74
|
+
}
|
|
75
|
+
default:
|
|
76
|
+
return NaN;
|
|
77
|
+
}
|
|
78
|
+
}
|
package/package.json
CHANGED