shevchenko
Version:
JavaScript library for declension of Ukrainian anthroponyms
72 lines (68 loc) • 2.94 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}
*/
;
var extension = require('./extension.js');
require('./language/alphabet.js');
require('./language/word-class.js');
require('./language/grammatical-case.js');
var grammaticalGender = require('./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.GrammaticalGender).includes(input.gender);
if (!isGenderValid) {
throw new InputValidationError(`The "gender" parameter must be one of the following:` +
` "${Object.values(grammaticalGender.GrammaticalGender).join('", "')}".`);
}
const mergedFieldNames = [...fieldNames, ...extension.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;
}
exports.InputValidationError = InputValidationError;
exports.validateDeclensionInput = validateDeclensionInput;
exports.validateGenderDetectionInput = validateGenderDetectionInput;