@azure/core-util
Advanced tools
Comparing version 1.0.0-alpha.20220406.3 to 1.0.0-alpha.20220415.2
# Release History | ||
## 1.0.0-beta.2 (Unreleased) | ||
## 1.0.0 (Unreleased) | ||
### Features Added | ||
- Add helpers `isObject`, `isError`, `getErrorMessage` for handling unknown Error objects. | ||
- Add helper `getRandomIntegerInclusive` for randomly selecting a whole integer value from a given range. | ||
### Other Changes | ||
@@ -6,0 +11,0 @@ |
@@ -5,2 +5,5 @@ // Copyright (c) Microsoft Corporation. | ||
export { delay } from "./delay"; | ||
export { getRandomIntegerInclusive } from "./random"; | ||
export { isObject } from "./object"; | ||
export { isError, getErrorMessage } from "./error"; | ||
//# sourceMappingURL=index.js.map |
@@ -24,4 +24,83 @@ 'use strict'; | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
/** | ||
* Returns a random integer value between a lower and upper bound, | ||
* inclusive of both bounds. | ||
* Note that this uses Math.random and isn't secure. If you need to use | ||
* this for any kind of security purpose, find a better source of random. | ||
* @param min - The smallest integer value allowed. | ||
* @param max - The largest integer value allowed. | ||
*/ | ||
function getRandomIntegerInclusive(min, max) { | ||
// Make sure inputs are integers. | ||
min = Math.ceil(min); | ||
max = Math.floor(max); | ||
// Pick a random offset from zero to the size of the range. | ||
// Since Math.random() can never return 1, we have to make the range one larger | ||
// in order to be inclusive of the maximum value after we take the floor. | ||
const offset = Math.floor(Math.random() * (max - min + 1)); | ||
return offset + min; | ||
} | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
/** | ||
* Helper to determine when an input is a generic JS object. | ||
* @returns true when input is an object type that is not null, Array, RegExp, or Date. | ||
*/ | ||
function isObject(input) { | ||
return (typeof input === "object" && | ||
input !== null && | ||
!Array.isArray(input) && | ||
!(input instanceof RegExp) && | ||
!(input instanceof Date)); | ||
} | ||
// Copyright (c) Microsoft Corporation. | ||
/** | ||
* Typeguard for an error object shape (has name and message) | ||
* @param e - Something caught by a catch clause. | ||
*/ | ||
function isError(e) { | ||
if (isObject(e)) { | ||
const hasName = typeof e.name === "string"; | ||
const hasMessage = typeof e.message === "string"; | ||
return hasName && hasMessage; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Given what is thought to be an error object, return the message if possible. | ||
* If the message is missing, returns a stringified version of the input. | ||
* @param e - Something thrown from a try block | ||
* @returns The error message or a string of the input | ||
*/ | ||
function getErrorMessage(e) { | ||
if (isError(e)) { | ||
return e.message; | ||
} | ||
else { | ||
let stringified; | ||
try { | ||
if (typeof e === "object" && e) { | ||
stringified = JSON.stringify(e); | ||
} | ||
else { | ||
stringified = String(e); | ||
} | ||
} | ||
catch (err) { | ||
stringified = "[unable to stringify input]"; | ||
} | ||
return `Unknown error ${stringified}`; | ||
} | ||
} | ||
exports.delay = delay; | ||
exports.getErrorMessage = getErrorMessage; | ||
exports.getRandomIntegerInclusive = getRandomIntegerInclusive; | ||
exports.isError = isError; | ||
exports.isNode = isNode; | ||
exports.isObject = isObject; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@azure/core-util", | ||
"version": "1.0.0-alpha.20220406.3", | ||
"version": "1.0.0-alpha.20220415.2", | ||
"description": "Core library for shared utility methods", | ||
@@ -32,3 +32,3 @@ "sdk-type": "client", | ||
"extract-api": "tsc -p . && api-extractor run --local", | ||
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"", | ||
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", | ||
"integration-test:browser": "echo skipped", | ||
@@ -35,0 +35,0 @@ "integration-test:node": "echo skipped", |
@@ -8,5 +8,37 @@ /** | ||
/** | ||
* Given what is thought to be an error object, return the message if possible. | ||
* If the message is missing, returns a stringified version of the input. | ||
* @param e - Something thrown from a try block | ||
* @returns The error message or a string of the input | ||
*/ | ||
export declare function getErrorMessage(e: unknown): string; | ||
/** | ||
* Returns a random integer value between a lower and upper bound, | ||
* inclusive of both bounds. | ||
* Note that this uses Math.random and isn't secure. If you need to use | ||
* this for any kind of security purpose, find a better source of random. | ||
* @param min - The smallest integer value allowed. | ||
* @param max - The largest integer value allowed. | ||
*/ | ||
export declare function getRandomIntegerInclusive(min: number, max: number): number; | ||
/** | ||
* Typeguard for an error object shape (has name and message) | ||
* @param e - Something caught by a catch clause. | ||
*/ | ||
export declare function isError(e: unknown): e is Error; | ||
/** | ||
* A constant that indicates whether the environment the code is running is Node.JS. | ||
*/ | ||
export declare const isNode: boolean; | ||
/** | ||
* Helper to determine when an input is a generic JS object. | ||
* @returns true when input is an object type that is not null, Array, RegExp, or Date. | ||
*/ | ||
export declare function isObject(input: unknown): input is UnknownObject; | ||
/** | ||
* A generic shape for a plain JS object. | ||
*/ | ||
export declare type UnknownObject = { | ||
[s: string]: unknown; | ||
}; | ||
export {}; |
@@ -9,2 +9,26 @@ /** | ||
/** | ||
* Given what is thought to be an error object, return the message if possible. | ||
* If the message is missing, returns a stringified version of the input. | ||
* @param e - Something thrown from a try block | ||
* @returns The error message or a string of the input | ||
*/ | ||
export declare function getErrorMessage(e: unknown): string; | ||
/** | ||
* Returns a random integer value between a lower and upper bound, | ||
* inclusive of both bounds. | ||
* Note that this uses Math.random and isn't secure. If you need to use | ||
* this for any kind of security purpose, find a better source of random. | ||
* @param min - The smallest integer value allowed. | ||
* @param max - The largest integer value allowed. | ||
*/ | ||
export declare function getRandomIntegerInclusive(min: number, max: number): number; | ||
/** | ||
* Typeguard for an error object shape (has name and message) | ||
* @param e - Something caught by a catch clause. | ||
*/ | ||
export declare function isError(e: unknown): e is Error; | ||
/** | ||
* A constant that indicates whether the environment the code is running is Node.JS. | ||
@@ -14,2 +38,15 @@ */ | ||
/** | ||
* Helper to determine when an input is a generic JS object. | ||
* @returns true when input is an object type that is not null, Array, RegExp, or Date. | ||
*/ | ||
export declare function isObject(input: unknown): input is UnknownObject; | ||
/** | ||
* A generic shape for a plain JS object. | ||
*/ | ||
export declare type UnknownObject = { | ||
[s: string]: unknown; | ||
}; | ||
export { } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
30426
22
288
1