@caidrive/shared
Version:
caidrive.shared.components
97 lines (96 loc) • 2.64 kB
JavaScript
;
/**
* What it does.
*
* @param name - Parameter description.
* @returns Type and description of the returned object.
*
* @example
* ```
* Write me later.
* ```
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Guard = void 0;
const result_1 = require("./result");
/**
*
*/
class Guard {
/**
*
*/
static Numeric(argument, argumentName) {
/**
*
*/
if (Number.isInteger(argument)) {
return result_1.Result.ok();
}
if (!Number.isNaN(Number.parseInt(argument))) {
return result_1.Result.ok();
}
return result_1.Result.fail(`${argumentName} is non numeric value, can not be parsed`);
}
/**
*
*/
static EmptyOrBlank(argument, argumentName) {
const nullGuardResult = Guard.NullOrUndefined(argument, "EmbptyOrBlank:argument");
if (nullGuardResult.isSuccess) {
if (!(argument instanceof Date) && typeof argument === "object") {
if (Object.keys(argument).length) {
return result_1.Result.ok();
}
}
else {
return result_1.Result.ok();
}
}
return result_1.Result.fail(`${argumentName} is null or undefined`);
}
/**
*
*/
static NullOrUndefined(argument, argumentName) {
if (argument === null || argument === undefined) {
return result_1.Result.fail(`${argumentName} is null or undefined`);
}
return result_1.Result.ok();
}
/**
*
*/
static NullOrUndefinedBulk(args) {
const nullGuardResult = Guard.NullOrUndefined(args, "args");
if (nullGuardResult.isSuccess) {
for (const arg of args) {
const result = this.NullOrUndefined(arg.value, arg.name);
if (result.isFailure) {
return result;
}
}
return result_1.Result.ok();
}
return nullGuardResult;
}
/**
*
*
*/
static AtLeast(length, text) {
return (text === null || text === void 0 ? void 0 : text.length) >= length
? result_1.Result.ok()
: result_1.Result.fail(`Value length must be at least ${length}`);
}
/*
*
*
*/
static AtMost(length, text) {
return (text === null || text === void 0 ? void 0 : text.length) <= length
? result_1.Result.ok()
: result_1.Result.fail(`Value length must be at most ${length}`);
}
}
exports.Guard = Guard;