number-helper-functions
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -1,7 +0,101 @@ | ||
import { checkDecimals, convertToNumber, prefixWithZeros, processNumber } from './modules/format'; | ||
import { isEven, isFloat, isInt, isNumber, isOdd, isPrime } from './modules/checks'; | ||
import { enforceNumber } from './modules/base'; | ||
import { getRandom } from './modules/random'; | ||
import { rangeBetween } from './modules/utilities'; | ||
export { checkDecimals, convertToNumber, enforceNumber, getRandom, isEven, isFloat, isInt, isNumber, isOdd, isPrime, prefixWithZeros, processNumber, rangeBetween, }; | ||
declare type NumberLike = string | number | null | undefined; | ||
/** | ||
* Prefixes a number with zeros until desired length | ||
* | ||
* @param {number} number Number to pad | ||
* @param {number} length Desired number length | ||
* @returns {string} Padded number as a string | ||
*/ | ||
declare function prefixWithZeros(number: number, length: number): string; | ||
/** | ||
* Checks the decimals of a number | ||
* @param {NumberLike} number Number to check the amount of decimals of | ||
* @return {number} The number of decimals | ||
*/ | ||
declare function checkDecimals(number: NumberLike): number; | ||
/** | ||
* Converts string to number | ||
* | ||
* @param {NumberLike} toCheck Number-like to convert | ||
* @param {number} [decimals=2] Number of decimals to truncate | ||
* @returns {number} Converted number | ||
*/ | ||
declare function convertToNumber(toCheck: NumberLike, decimals?: number): number; | ||
/** | ||
* Processes number to get clean value | ||
* | ||
* @param {NumberLike} value String or number to process | ||
* @param {number} [decimals=2] No. of decimals to truncate to | ||
* @returns {number} Parsed and processed number | ||
*/ | ||
declare function processNumber(value: NumberLike, decimals?: number): number; | ||
/** | ||
* Checks if element is a number | ||
* | ||
* @param {NumberLike} n The element to check | ||
* @returns {boolean} Whether the element is a number or not | ||
*/ | ||
declare function isNumber(n: NumberLike): boolean; | ||
/** | ||
* Checks if element is an integer | ||
* | ||
* @param {NumberLike} n Number to check | ||
* @returns {boolean} Whether the number is an integer or not | ||
*/ | ||
declare function isInt(n: NumberLike): boolean; | ||
/** | ||
* Checks if element is a float number | ||
* | ||
* @param {NumberLike} n Number to check | ||
* @returns {boolean} Whether the number is a float or not | ||
*/ | ||
declare function isFloat(n: NumberLike): boolean; | ||
/** | ||
* Checks if number is even | ||
* Any integer that can be divided exactly by 2 is an even number | ||
* | ||
* @param {NumberLike} n Number to check | ||
* @returns {boolean} Whether the number is even or not | ||
*/ | ||
declare function isEven(n: NumberLike): boolean; | ||
/** | ||
* Checks if number is odd | ||
* Any integer that cannot be divided exactly by 2 is an odd number. | ||
* | ||
* @param {NumberLike} n Number to check | ||
* @returns {boolean} Whether the number is odd or not | ||
*/ | ||
declare function isOdd(n: NumberLike): boolean; | ||
/** | ||
* Checks if number is prime | ||
* Natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers | ||
* | ||
* @param {(number|string)} num Number to check | ||
* @returns {boolean} Whether the numbere is prime or not | ||
*/ | ||
declare function isPrime(num: number): boolean; | ||
declare function enforceNumber(n: NumberLike): number; | ||
/** | ||
* Gets random integer number in a range | ||
* | ||
* @param {number} min Minimum number | ||
* @param {number} max Maximum number | ||
* @returns {number} Random number in range | ||
*/ | ||
declare function getRandom(min: number, max: number): number; | ||
/** | ||
* Creates a range between start and end, including them, according to step | ||
* | ||
* @param {number} start Starting value | ||
* @param {number} end End value | ||
* @param {number} step Step to use for the range | ||
* @return {number[]} The range | ||
*/ | ||
declare function rangeBetween(start: number, end: number, step: number): number[]; | ||
declare const _default: { | ||
@@ -22,2 +116,3 @@ checkDecimals: typeof checkDecimals; | ||
}; | ||
export default _default; | ||
export { checkDecimals, convertToNumber, _default as default, enforceNumber, getRandom, isEven, isFloat, isInt, isNumber, isOdd, isPrime, prefixWithZeros, processNumber, rangeBetween }; |
@@ -0,8 +1,156 @@ | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __reExport = (target, module2, copyDefault, desc) => { | ||
if (module2 && typeof module2 === "object" || typeof module2 === "function") { | ||
for (let key of __getOwnPropNames(module2)) | ||
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default")) | ||
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); | ||
} | ||
return target; | ||
}; | ||
var __toCommonJS = /* @__PURE__ */ ((cache) => { | ||
return (module2, temp) => { | ||
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp); | ||
}; | ||
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0); | ||
'use strict' | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
checkDecimals: () => checkDecimals, | ||
convertToNumber: () => convertToNumber, | ||
default: () => src_default, | ||
enforceNumber: () => enforceNumber, | ||
getRandom: () => getRandom, | ||
isEven: () => isEven, | ||
isFloat: () => isFloat, | ||
isInt: () => isInt, | ||
isNumber: () => isNumber, | ||
isOdd: () => isOdd, | ||
isPrime: () => isPrime, | ||
prefixWithZeros: () => prefixWithZeros, | ||
processNumber: () => processNumber, | ||
rangeBetween: () => rangeBetween | ||
}); | ||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./number-helper-functions.cjs.production.min.js') | ||
} else { | ||
module.exports = require('./number-helper-functions.cjs.development.js') | ||
// src/modules/base.ts | ||
function enforceNumber(n) { | ||
return Number(n); | ||
} | ||
// src/modules/checks.ts | ||
function isNumber(n) { | ||
return !isNaN(enforceNumber(n)); | ||
} | ||
function isInt(n) { | ||
if (isNumber(n)) { | ||
return enforceNumber(n) % 1 === 0; | ||
} else { | ||
return false; | ||
} | ||
} | ||
function isFloat(n) { | ||
if (isNumber(n)) { | ||
return enforceNumber(n).toString().includes("."); | ||
} else { | ||
return false; | ||
} | ||
} | ||
function isEven(n) { | ||
return enforceNumber(n) % 2 === 0; | ||
} | ||
function isOdd(n) { | ||
return enforceNumber(n) % 2 !== 0; | ||
} | ||
function isPrime(num) { | ||
return num > 1 && Array(Math.floor(Math.sqrt(num)) - 1).fill(0).map((_, i) => i + 2).every((i) => num % i !== 0); | ||
} | ||
// src/modules/format.ts | ||
function prefixWithZeros(number, length) { | ||
return String(number).padStart(length, "0"); | ||
} | ||
function checkDecimals(number) { | ||
if (isNumber(number)) { | ||
const decimals = enforceNumber(number).toString().split(".").pop(); | ||
return decimals !== void 0 ? decimals.length : 0; | ||
} else { | ||
throw new Error("Entered number is NaN"); | ||
} | ||
} | ||
function convertToNumber(toCheck, decimals = 2) { | ||
let number = 0; | ||
if (isInt(toCheck)) { | ||
number = enforceNumber(toCheck); | ||
} else if (isFloat(toCheck)) { | ||
number = enforceNumber(enforceNumber(toCheck).toFixed(decimals)); | ||
} | ||
return number; | ||
} | ||
function processNumber(value, decimals = 2) { | ||
return convertToNumber(value, decimals); | ||
} | ||
// src/modules/random.ts | ||
function getRandom(min, max) { | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
} | ||
// src/modules/utilities.ts | ||
function rangeBetween(start, end, step) { | ||
const stepIsBiggerThanDifference = step > end - start; | ||
const stepIsZero = step === 0; | ||
const numberOfItems = (end - start) / step; | ||
const stepWillNotCoverRange = isFloat(numberOfItems); | ||
if (start > end) { | ||
throw new Error("Start should be smaller than end"); | ||
} | ||
if (stepIsBiggerThanDifference || stepIsZero || stepWillNotCoverRange) { | ||
throw new Error("Invalid step value"); | ||
} | ||
return Array(numberOfItems + 1).fill(0).map((_, index) => { | ||
const valueToAdd = step * index; | ||
return processNumber(start + valueToAdd, checkDecimals(step)); | ||
}); | ||
} | ||
// src/index.ts | ||
var src_default = { | ||
checkDecimals, | ||
convertToNumber, | ||
enforceNumber, | ||
getRandom, | ||
isEven, | ||
isFloat, | ||
isInt, | ||
isNumber, | ||
isOdd, | ||
isPrime, | ||
prefixWithZeros, | ||
processNumber, | ||
rangeBetween | ||
}; | ||
module.exports = __toCommonJS(src_exports); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
checkDecimals, | ||
convertToNumber, | ||
enforceNumber, | ||
getRandom, | ||
isEven, | ||
isFloat, | ||
isInt, | ||
isNumber, | ||
isOdd, | ||
isPrime, | ||
prefixWithZeros, | ||
processNumber, | ||
rangeBetween | ||
}); |
{ | ||
"name": "number-helper-functions", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Helper with multiple number-related methods", | ||
"main": "dist/index.js", | ||
"module": "dist/number-helper-functions.esm.js", | ||
"typings": "dist/index.d.ts", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"files": [ | ||
@@ -17,30 +24,30 @@ "dist", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=12" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^4.9.2", | ||
"@typescript-eslint/eslint-plugin": "^4.15.2", | ||
"@typescript-eslint/parser": "^4.15.2", | ||
"babel-jest": "^26.6.3", | ||
"coveralls": "^3.1.0", | ||
"eslint": "^7.20.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
"eslint-plugin-jest": "^23.9.0", | ||
"husky": "^5.1.1", | ||
"jest": "^25.5.4", | ||
"size-limit": "^4.9.2", | ||
"tsdx": "^0.14.1", | ||
"tslib": "^2.1.0", | ||
"typedoc": "^0.20.28", | ||
"typedoc-plugin-markdown": "^3.5.0", | ||
"typescript": "^4.2.2" | ||
"@size-limit/preset-small-lib": "^7.0.5", | ||
"@typescript-eslint/eslint-plugin": "^5.10.1", | ||
"@typescript-eslint/parser": "^5.10.1", | ||
"c8": "^7.11.0", | ||
"coveralls": "^3.1.1", | ||
"eslint": "^8.8.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-jest": "^26.0.0", | ||
"husky": "^7.0.4", | ||
"size-limit": "^7.0.5", | ||
"tsup": "^5.11.13", | ||
"typedoc": "^0.22.11", | ||
"typedoc-plugin-markdown": "^3.11.12", | ||
"typescript": "^4.5.5", | ||
"vite": "^2.7.13", | ||
"vitest": "^0.2.5" | ||
}, | ||
"size-limit": [ | ||
{ | ||
"path": "dist/number-helper-functions.cjs.production.min.js", | ||
"path": "dist/index.js", | ||
"limit": "10 KB" | ||
}, | ||
{ | ||
"path": "dist/number-helper-functions.esm.js", | ||
"path": "dist/index.mjs", | ||
"limit": "10 KB" | ||
@@ -50,11 +57,11 @@ } | ||
"scripts": { | ||
"start": "tsdx watch", | ||
"build": "tsdx build", | ||
"test": "tsdx test", | ||
"lint": "tsdx lint", | ||
"prepare": "tsdx build", | ||
"start": "pnpm run build -- --watch src", | ||
"build": "tsup src/index.ts --format cjs,esm --dts --clean", | ||
"test": "vitest run", | ||
"coverage": "vitest run --coverage", | ||
"lint": "eslint --cache --fix --ignore-path .gitignore --ext .ts,.js src", | ||
"prepare": "pnpm run build", | ||
"size": "size-limit", | ||
"analyze": "size-limit --why", | ||
"docs": "typedoc src/index.ts --readme", | ||
"coveralls": "tsdx test --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" | ||
"coveralls": "vitest run --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" | ||
}, | ||
@@ -61,0 +68,0 @@ "husky": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
1
25990
13
570