@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
95 lines (94 loc) • 2.48 kB
JavaScript
"use strict";
/**
* @author WMXPY
* @namespace Util_Error
* @description Assert
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.assert = void 0;
const error_code_1 = require("../../declare/error-code");
const error_1 = require("../error/error");
class Assert {
constructor(element) {
this._elements = [element];
this._reverse = false;
}
get is() {
return this;
}
get to() {
return this;
}
get be() {
return this;
}
get not() {
this._reverse = true;
return this;
}
and(element) {
this._elements.push(element);
return this;
}
exist(code = error_code_1.ERROR_CODE.ASSERT_EXIST_ELEMENT_NOT_EXIST) {
const result = this.eachElement((value) => {
return value !== null && value !== undefined;
});
if (!result) {
throw (0, error_1.error)(code);
}
return this;
}
true(code = error_code_1.ERROR_CODE.ASSERT_BOOLEAN_OPPOSITE) {
const result = this.eachElement((value) => {
return Boolean(value);
});
if (!result) {
throw (0, error_1.error)(code);
}
return this;
}
array(code = error_code_1.ERROR_CODE.ASSERT_TYPE_NOT_MATCHED) {
const result = this.eachElement((value) => {
return value instanceof Array;
});
if (!result) {
throw (0, error_1.error)(code);
}
return this;
}
number(code = error_code_1.ERROR_CODE.ASSERT_TYPE_NOT_MATCHED) {
const result = this.eachElement((value) => {
return typeof value === "number";
});
if (!result) {
throw (0, error_1.error)(code);
}
return this;
}
string(code = error_code_1.ERROR_CODE.ASSERT_TYPE_NOT_MATCHED) {
const result = this.eachElement((value) => {
return typeof value === "string";
});
if (!result) {
throw (0, error_1.error)(code);
}
return this;
}
firstValue() {
return this._elements[0];
}
eachElement(func) {
for (const element of this._elements) {
const result = func(element);
if (this._reverse === result) {
return false;
}
}
return true;
}
}
const assert = (element) => {
return new Assert(element);
};
exports.assert = assert;