UNPKG

@datastax/astra-db-ts

Version:
145 lines (144 loc) 4.15 kB
"use strict"; // Copyright Datastax, Inc // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryState = exports.numDigits = exports.anyInstanceOf = exports.function_ = exports.oneOrMany = exports.EqualityProof = exports.forJSEnv = void 0; exports.isNullish = isNullish; exports.jsonTryStringify = jsonTryStringify; exports.jsonTryParse = jsonTryParse; exports.buildAstraEndpoint = buildAstraEndpoint; exports.toArray = toArray; exports.isBigNumber = isBigNumber; exports.findLast = findLast; exports.isNonEmpty = isNonEmpty; exports.splitWithIncludesCheck = splitWithIncludesCheck; const bignumber_js_1 = require("bignumber.js"); const decoders_1 = require("decoders"); function isNullish(t) { return t === null || t === undefined; } function jsonTryStringify(value, otherwise) { try { return JSON.stringify(value); } catch (_) { return otherwise; } } function jsonTryParse(json, otherwise, reviver) { try { return JSON.parse(json, reviver); } catch (_) { return otherwise; } } function buildAstraEndpoint(id, region, env = 'prod') { return 'https://' + id + '-' + region + `.apps${env === 'prod' ? '' : `-${env}`}.astra.datastax.com`; } function toArray(t) { return Array.isArray(t) ? t : [t]; } const getJSEnv = () => (typeof globalThis.window !== 'undefined') ? 'browser' : (typeof globalThis.Buffer !== 'undefined') ? 'server' : 'unknown'; const env = getJSEnv(); exports.forJSEnv = (typeof process !== 'undefined' && typeof process.env === 'object' && process.env.CLIENT_DYNAMIC_JS_ENV_CHECK) ? (fns) => (...args) => fns[getJSEnv()](...args) : (fns) => fns[env]; function isBigNumber(value) { return bignumber_js_1.BigNumber.isBigNumber(value) && 's' in value && 'e' in value && 'c' in value; } const EqualityProof = () => { }; exports.EqualityProof = EqualityProof; const oneOrMany = (decoder) => { return (0, decoders_1.either)(decoder, (0, decoders_1.array)(decoder)); }; exports.oneOrMany = oneOrMany; exports.function_ = (0, decoders_1.define)((fn, ok, err) => { if (typeof fn === 'function') { return ok(fn); } else { return err('Input must be a function'); } }); const anyInstanceOf = (cls) => (0, decoders_1.instanceOf)(cls); exports.anyInstanceOf = anyInstanceOf; const numDigits = (n) => { return (n !== 0) ? Math.floor(Math.log10(Math.abs(n))) + 1 : 1; }; exports.numDigits = numDigits; function findLast(predicate, orElse) { return (arr) => { for (let i = arr.length - 1; i >= 0; i--) { if (predicate(arr[i], i)) { return arr[i]; } } return orElse; }; } class QueryState { constructor() { Object.defineProperty(this, "_state", { enumerable: true, configurable: true, writable: true, value: QueryState.Unattempted }); Object.defineProperty(this, "_value", { enumerable: true, configurable: true, writable: true, value: null }); } get state() { return this._state; } swap(value) { if (isNullish(value)) { this._state = QueryState.NotFound; this._value = null; } else { this._state = QueryState.Found; this._value = value; } return this; } unwrap() { return this._value; } } exports.QueryState = QueryState; Object.defineProperty(QueryState, "Unattempted", { enumerable: true, configurable: true, writable: true, value: 0 }); Object.defineProperty(QueryState, "Found", { enumerable: true, configurable: true, writable: true, value: 1 }); Object.defineProperty(QueryState, "NotFound", { enumerable: true, configurable: true, writable: true, value: 2 }); function isNonEmpty(arr) { return arr.length > 0; } function splitWithIncludesCheck(str, sep) { if (!str.includes(sep)) { return [str]; } return str.split(sep); }