Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

@azure/core-util

Package Overview
Dependencies
Maintainers
1
Versions
214
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@azure/core-util - npm Package Compare versions

Comparing version 1.0.0-alpha.20220406.3 to 1.0.0-alpha.20220415.2

dist-esm/src/error.js

7

CHANGELOG.md
# 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

4

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc