predictype
Version:
PredicType is a library of pre-built and tested predicates for TypeScript, covering various data types and operations.
41 lines (40 loc) • 1.47 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringState = stringState;
const strings_js_1 = require("../../enums/strings.js");
/**
* Checks the state of a string (empty, not empty, blank, not blank) using the specified operation.
*
* @param source The string to check.
* @param oper The state operation to perform (e.g. 'is_empty', 'is_not_blank').
* @returns True if the state check is valid according to the operator, otherwise false.
*
* @throws {Error} If the operation is not recognized.
*
* @example
* const str = '';
* const str2 = ' ';
*
* stringState(str, 'is_empty'); // true
* stringState(str2, 'is_blank'); // true
*
* @remarks
* Supported Operators:
* - **IS_EMPTY**: String is empty
* - **IS_NOT_EMPTY**: String is not empty
* - **IS_BLANK**: String is blank (only whitespace)
* - **IS_NOT_BLANK**: String is not blank
*/
function stringState(source, oper) {
const operators = {
[strings_js_1.StringStateEnum.IS_EMPTY]: (a) => a.length === 0,
[strings_js_1.StringStateEnum.IS_NOT_EMPTY]: (a) => a.length > 0,
[strings_js_1.StringStateEnum.IS_BLANK]: (a) => /^\s*$/.test(a),
[strings_js_1.StringStateEnum.IS_NOT_BLANK]: (a) => !/^\s*$/.test(a),
};
const enumOper = typeof oper === 'string' ? oper : oper;
const fn = operators[enumOper];
if (!fn)
throw new Error(`Unknown StringState operation: ${oper}`);
return fn(source);
}
;