UNPKG

@deno/kv

Version:

A Deno KV client library optimized for Node.js.

68 lines (67 loc) 2.51 kB
// Copyright 2023 the Deno authors. All rights reserved. MIT license. export function isRecord(obj) { return typeof obj === "object" && obj !== null && !Array.isArray(obj) && obj.constructor === Object; } export function isDateTime(value) { return typeof value === "string" && /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(\.\d+)?Z$/.test(value); } export function checkKeyNotEmpty(key) { if (key.length === 0) throw new TypeError(`Key cannot be empty`); } export function checkExpireIn(expireIn) { const valid = expireIn === undefined || typeof expireIn === "number" && expireIn > 0 && Number.isSafeInteger(expireIn); if (!valid) { throw new TypeError(`Bad 'expireIn', expected optional positive integer, found ${expireIn}`); } } export function checkMatches(name, value, pattern) { const m = pattern.exec(value); if (!m) throw new TypeError(`Bad '${name}': ${value}`); return m; } export function checkString(name, value) { if (typeof value !== "string") { throw new TypeError(`Bad '${name}': expected string, found ${value}`); } } export function checkOptionalString(name, value) { if (!(value === undefined || typeof value === "string")) { throw new TypeError(`Bad '${name}': expected optional string, found ${value}`); } } export function checkRecord(name, value) { if (!isRecord(value)) { throw new TypeError(`Bad '${name}': expected simple object, found ${value}`); } } export function checkOptionalBoolean(name, value) { if (!(value === undefined || typeof value === "boolean")) { throw new TypeError(`Bad '${name}': expected optional boolean, found ${value}`); } } export function checkOptionalNumber(name, value) { if (!(value === undefined || typeof value === "number")) { throw new TypeError(`Bad '${name}': expected optional number, found ${value}`); } } // deno-lint-ignore ban-types export function checkOptionalFunction(name, value) { if (!(value === undefined || typeof value === "function")) { throw new TypeError(`Bad '${name}': expected optional function, found ${value}`); } } export function checkOptionalObject(name, value) { if (!(value === undefined || typeof value === "object")) { throw new TypeError(`Bad '${name}': expected optional object, found ${value}`); } } export function check(name, value, valid) { if (!valid) throw new TypeError(`Bad '${name}': ${value}`); }