values.mjs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. import { AnthropicError } from "../../core/error.mjs";
  3. // https://url.spec.whatwg.org/#url-scheme-string
  4. const startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;
  5. export const isAbsoluteURL = (url) => {
  6. return startsWithSchemeRegexp.test(url);
  7. };
  8. export let isArray = (val) => ((isArray = Array.isArray), isArray(val));
  9. export let isReadonlyArray = isArray;
  10. /** Returns an object if the given value isn't an object, otherwise returns as-is */
  11. export function maybeObj(x) {
  12. if (typeof x !== 'object') {
  13. return {};
  14. }
  15. return x ?? {};
  16. }
  17. // https://stackoverflow.com/a/34491287
  18. export function isEmptyObj(obj) {
  19. if (!obj)
  20. return true;
  21. for (const _k in obj)
  22. return false;
  23. return true;
  24. }
  25. // https://eslint.org/docs/latest/rules/no-prototype-builtins
  26. export function hasOwn(obj, key) {
  27. return Object.prototype.hasOwnProperty.call(obj, key);
  28. }
  29. export function isObj(obj) {
  30. return obj != null && typeof obj === 'object' && !Array.isArray(obj);
  31. }
  32. export const ensurePresent = (value) => {
  33. if (value == null) {
  34. throw new AnthropicError(`Expected a value to be given but received ${value} instead.`);
  35. }
  36. return value;
  37. };
  38. export const validatePositiveInteger = (name, n) => {
  39. if (typeof n !== 'number' || !Number.isInteger(n)) {
  40. throw new AnthropicError(`${name} must be an integer`);
  41. }
  42. if (n < 0) {
  43. throw new AnthropicError(`${name} must be a positive integer`);
  44. }
  45. return n;
  46. };
  47. export const coerceInteger = (value) => {
  48. if (typeof value === 'number')
  49. return Math.round(value);
  50. if (typeof value === 'string')
  51. return parseInt(value, 10);
  52. throw new AnthropicError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
  53. };
  54. export const coerceFloat = (value) => {
  55. if (typeof value === 'number')
  56. return value;
  57. if (typeof value === 'string')
  58. return parseFloat(value);
  59. throw new AnthropicError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
  60. };
  61. export const coerceBoolean = (value) => {
  62. if (typeof value === 'boolean')
  63. return value;
  64. if (typeof value === 'string')
  65. return value === 'true';
  66. return Boolean(value);
  67. };
  68. export const maybeCoerceInteger = (value) => {
  69. if (value == null) {
  70. return undefined;
  71. }
  72. return coerceInteger(value);
  73. };
  74. export const maybeCoerceFloat = (value) => {
  75. if (value == null) {
  76. return undefined;
  77. }
  78. return coerceFloat(value);
  79. };
  80. export const maybeCoerceBoolean = (value) => {
  81. if (value == null) {
  82. return undefined;
  83. }
  84. return coerceBoolean(value);
  85. };
  86. export const safeJSON = (text) => {
  87. try {
  88. return JSON.parse(text);
  89. }
  90. catch (err) {
  91. return undefined;
  92. }
  93. };
  94. //# sourceMappingURL=values.mjs.map