| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
- import { AnthropicError } from "../../core/error.mjs";
- // https://url.spec.whatwg.org/#url-scheme-string
- const startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;
- export const isAbsoluteURL = (url) => {
- return startsWithSchemeRegexp.test(url);
- };
- export let isArray = (val) => ((isArray = Array.isArray), isArray(val));
- export let isReadonlyArray = isArray;
- /** Returns an object if the given value isn't an object, otherwise returns as-is */
- export function maybeObj(x) {
- if (typeof x !== 'object') {
- return {};
- }
- return x ?? {};
- }
- // https://stackoverflow.com/a/34491287
- export function isEmptyObj(obj) {
- if (!obj)
- return true;
- for (const _k in obj)
- return false;
- return true;
- }
- // https://eslint.org/docs/latest/rules/no-prototype-builtins
- export function hasOwn(obj, key) {
- return Object.prototype.hasOwnProperty.call(obj, key);
- }
- export function isObj(obj) {
- return obj != null && typeof obj === 'object' && !Array.isArray(obj);
- }
- export const ensurePresent = (value) => {
- if (value == null) {
- throw new AnthropicError(`Expected a value to be given but received ${value} instead.`);
- }
- return value;
- };
- export const validatePositiveInteger = (name, n) => {
- if (typeof n !== 'number' || !Number.isInteger(n)) {
- throw new AnthropicError(`${name} must be an integer`);
- }
- if (n < 0) {
- throw new AnthropicError(`${name} must be a positive integer`);
- }
- return n;
- };
- export const coerceInteger = (value) => {
- if (typeof value === 'number')
- return Math.round(value);
- if (typeof value === 'string')
- return parseInt(value, 10);
- throw new AnthropicError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
- };
- export const coerceFloat = (value) => {
- if (typeof value === 'number')
- return value;
- if (typeof value === 'string')
- return parseFloat(value);
- throw new AnthropicError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
- };
- export const coerceBoolean = (value) => {
- if (typeof value === 'boolean')
- return value;
- if (typeof value === 'string')
- return value === 'true';
- return Boolean(value);
- };
- export const maybeCoerceInteger = (value) => {
- if (value == null) {
- return undefined;
- }
- return coerceInteger(value);
- };
- export const maybeCoerceFloat = (value) => {
- if (value == null) {
- return undefined;
- }
- return coerceFloat(value);
- };
- export const maybeCoerceBoolean = (value) => {
- if (value == null) {
- return undefined;
- }
- return coerceBoolean(value);
- };
- export const safeJSON = (text) => {
- try {
- return JSON.parse(text);
- }
- catch (err) {
- return undefined;
- }
- };
- //# sourceMappingURL=values.mjs.map
|