shevchenko
Version:
JavaScript library for declension of Ukrainian anthroponyms
68 lines (65 loc) • 2.8 kB
JavaScript
/**
* @file JavaScript library for declension of Ukrainian anthroponyms
* @module shevchenko
* @version 3.1.4
* @author Oleksandr Tolochko <shevchenko-js@tooleks.com>
* @license MIT
* @copyright 2017-2025 Oleksandr Tolochko <shevchenko-js@tooleks.com>
* @see {@link git+https://github.com/tooleks/shevchenko-js.git}
*/
import { getCustomFieldNames } from './extension.js';
import './language/alphabet.js';
import './language/word-class.js';
import './language/grammatical-case.js';
import { GrammaticalGender } from './language/grammatical-gender.js';
class InputValidationError extends TypeError {
}
const fieldNames = ['givenName', 'patronymicName', 'familyName'];
/**
* Validates if a given value is a valid input for declension.
*
* @throws {InputValidationError}
*/
function validateDeclensionInput(input) {
if (!isObject(input)) {
throw new InputValidationError('The input type must be an object.');
}
const isGenderValid = Object.values(GrammaticalGender).includes(input.gender);
if (!isGenderValid) {
throw new InputValidationError(`The "gender" parameter must be one of the following:` +
` "${Object.values(GrammaticalGender).join('", "')}".`);
}
const mergedFieldNames = [...fieldNames, ...getCustomFieldNames()];
const hasFields = mergedFieldNames.some((fieldName) => fieldName in input && typeof input[fieldName] !== 'undefined');
if (!hasFields) {
throw new InputValidationError(`At least one of the following parameters must present: "${mergedFieldNames.join('", "')}".`);
}
for (const fieldName of mergedFieldNames) {
if (typeof input[fieldName] !== 'undefined' && typeof input[fieldName] !== 'string') {
throw new InputValidationError(`The "${fieldName}" parameter must be a string.`);
}
}
}
/**
* Validates if a given value is a valid input for gender detection.
*
* @throws {InputValidationError}
*/
function validateGenderDetectionInput(input) {
if (!isObject(input)) {
throw new InputValidationError('The input type must be an object.');
}
const hasFields = fieldNames.some((fieldName) => fieldName in input && typeof input[fieldName] !== 'undefined');
if (!hasFields) {
throw new InputValidationError(`At least one of the following parameters must present: "${fieldNames.join('", "')}".`);
}
for (const fieldName of fieldNames) {
if (typeof input[fieldName] !== 'undefined' && typeof input[fieldName] !== 'string') {
throw new InputValidationError(`The "${fieldName}" parameter must be a string.`);
}
}
}
function isObject(value) {
return typeof value === 'object' && value != null;
}
export { InputValidationError, validateDeclensionInput, validateGenderDetectionInput };