UNPKG

unifi-client

Version:

NodeJs client for Unifi products (https://www.ui.com/)

111 lines (110 loc) 4.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Validate = void 0; var stringToBooleanMap = new Map([ ['true', true], ['y', true], ['yes', true], ['oui', true], ['on', true], ['false', false], ['n', false], ['no', false], ['non', false], ['off', false] ]); var Validate = /** @class */ (function () { function Validate() { } Validate.mail = function (mail) { return Validate.isString(mail) && !!mail.match(/^([\w.%+-]+)@([\w-]+\.)+([\w]{2,})$/i); }; Validate.uuid = function (uuid) { return Validate.isString(uuid) && !!uuid.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89AB][0-9a-f]{3}-[0-9a-f]{12}$/i); }; Validate.mac = function (address) { return Validate.isString(address) && !!address.match(/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/); }; Validate.hexColor = function (color) { return Validate.isString(color) && !!color.match(/^#[0-9A-F]{6}$/i); }; // Returns if a value is a string Validate.isString = function (value) { return typeof value === 'string' || value instanceof String; }; // Returns if a value is really a number Validate.isNumber = function (value) { return typeof value === 'number' && isFinite(value); }; // Returns if a value is a function // eslint-disable-next-line @typescript-eslint/ban-types Validate.isFunction = function (value) { return typeof value === 'function'; }; // Returns if a value is an object // eslint-disable-next-line @typescript-eslint/ban-types Validate.isObject = function (value) { // eslint-disable-next-line @typescript-eslint/ban-types return (value !== null && value !== void 0 ? value : false) && typeof value === 'object' && value.constructor === Object; }; // Returns if a value is null Validate.isNull = function (value) { return value === null; }; // Returns if a value is undefined Validate.isUndefined = function (value) { return typeof value === 'undefined'; }; // in typescript, the equivalent is // const a = undefined ?? bar Validate.isDefinedNotNull = function (value) { return value !== undefined && value !== null; }; // Returns if a value is a boolean Validate.isBoolean = function (value) { return typeof value === 'boolean'; }; // Returns if a value is a regexp Validate.isRegExp = function (value) { // eslint-disable-next-line @typescript-eslint/ban-types return (value !== null && value !== void 0 ? value : false) && typeof value === 'object' && value.constructor === RegExp; }; // Returns if value is an error object Validate.isError = function (value) { return value instanceof Error && typeof value.message !== 'undefined'; }; // Returns if value is a date Validate.isDate = function (value, acceptTimestamp) { if (acceptTimestamp === void 0) { acceptTimestamp = true; } return (value instanceof Date || (Validate.isString(value) && !Number.isNaN(Date.parse(value.toString()))) || (acceptTimestamp && Validate.isNumber(value) && !Number.isNaN(new Date(value)))); }; // Returns if value is a Buffer Validate.isBuffer = function (value) { return Buffer.isBuffer(value); }; // Returns if a Symbol Validate.isSymbol = function (value) { return typeof value === 'symbol'; }; Validate.implementsTKeys = function (obj, keys) { if (!obj || !Array.isArray(keys) || Validate.isString(obj) || Validate.isNumber(obj) || Validate.isBoolean(obj)) { return false; } // @ts-ignore return keys.reduce(function (impl, key) { return impl && key in obj; }, true); }; /** * Return a boolean depending on the string . ("true", "y", "yes", "oui", "on" return true, else it's return false) * @param str -the string * @param strict - return null instead of false if not in list */ Validate.stringToBoolean = function (str, strict) { var _a; if (strict === void 0) { strict = false; } return (_a = stringToBooleanMap.get((str || '').toLowerCase())) !== null && _a !== void 0 ? _a : (strict ? null : false); }; return Validate; }()); exports.Validate = Validate;