shevchenko 3.0.8 → 3.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +2 -2
  3. package/dist/cjs/anthroponym-declension/anthroponym-inflector.js +3 -3
  4. package/dist/cjs/anthroponym-declension/bootstrap.js +3 -3
  5. package/dist/cjs/anthroponym-declension/family-name-classifier/artifacts/model.bundle.json.js +3 -3
  6. package/dist/cjs/anthroponym-declension/family-name-classifier/cache/incorrect-predictions.json.js +3 -3
  7. package/dist/cjs/anthroponym-declension/family-name-classifier/family-name-class-transformer.js +3 -3
  8. package/dist/cjs/anthroponym-declension/family-name-classifier/family-name-classifer.js +3 -3
  9. package/dist/cjs/anthroponym-declension/family-name-classifier/model-bundle-loader.js +3 -3
  10. package/dist/cjs/anthroponym-declension/family-name-classifier/model-config.js +3 -3
  11. package/dist/cjs/anthroponym-declension/family-name-classifier/word-transformer.js +3 -3
  12. package/dist/cjs/anthroponym-declension/family-name-inflector.js +4 -4
  13. package/dist/cjs/anthroponym-declension/family-name-inflector.js.map +1 -1
  14. package/dist/cjs/anthroponym-declension/given-name-inflector.js +4 -4
  15. package/dist/cjs/anthroponym-declension/given-name-inflector.js.map +1 -1
  16. package/dist/cjs/anthroponym-declension/name-inflector.js +3 -3
  17. package/dist/cjs/anthroponym-declension/patronymic-name-inflector.js +5 -5
  18. package/dist/cjs/anthroponym-declension/patronymic-name-inflector.js.map +1 -1
  19. package/dist/cjs/gender-detection/detect-gender.js +3 -3
  20. package/dist/cjs/gender-detection/given-names-genders.json.js +3 -3
  21. package/dist/cjs/index.js +5 -3
  22. package/dist/cjs/index.js.map +1 -1
  23. package/dist/cjs/input-validation.js +54 -49
  24. package/dist/cjs/input-validation.js.map +1 -1
  25. package/dist/cjs/language/alphabet.js +3 -3
  26. package/dist/cjs/language/grammatical-case.js +3 -3
  27. package/dist/cjs/language/grammatical-gender.js +3 -3
  28. package/dist/cjs/language/letter-case.js +3 -3
  29. package/dist/cjs/language/linguistics.js +3 -3
  30. package/dist/cjs/language/word-class.js +3 -3
  31. package/dist/cjs/shevchenko.js +97 -18
  32. package/dist/cjs/shevchenko.js.map +1 -1
  33. package/dist/cjs/word-declension/append-command-runner.js +3 -3
  34. package/dist/cjs/word-declension/bootstrap.js +4 -4
  35. package/dist/cjs/word-declension/command-runner-factory.js +3 -3
  36. package/dist/cjs/word-declension/declension-rule-inflector.js +3 -3
  37. package/dist/cjs/word-declension/declension-types.js +3 -3
  38. package/dist/cjs/word-declension/declension-types.js.map +1 -1
  39. package/dist/cjs/word-declension/replace-command-runner.js +3 -3
  40. package/dist/cjs/word-declension/{declension-rules.json.js → rules/artifacts/declension-rules.json.js} +97 -97
  41. package/dist/cjs/word-declension/word-inflector.js +7 -7
  42. package/dist/cjs/word-declension/word-inflector.js.map +1 -1
  43. package/dist/es/anthroponym-declension/anthroponym-inflector.js +3 -3
  44. package/dist/es/anthroponym-declension/anthroponym.d.ts +2 -2
  45. package/dist/es/anthroponym-declension/anthroponym.d.ts.map +1 -1
  46. package/dist/es/anthroponym-declension/bootstrap.js +3 -3
  47. package/dist/es/anthroponym-declension/family-name-classifier/artifacts/model.bundle.json.js +3 -3
  48. package/dist/es/anthroponym-declension/family-name-classifier/cache/incorrect-predictions.json.js +3 -3
  49. package/dist/es/anthroponym-declension/family-name-classifier/data-utils.d.ts +3 -3
  50. package/dist/es/anthroponym-declension/family-name-classifier/data-utils.d.ts.map +1 -1
  51. package/dist/es/anthroponym-declension/family-name-classifier/family-name-class-transformer.js +3 -3
  52. package/dist/es/anthroponym-declension/family-name-classifier/family-name-class.d.ts +2 -2
  53. package/dist/es/anthroponym-declension/family-name-classifier/family-name-class.d.ts.map +1 -1
  54. package/dist/es/anthroponym-declension/family-name-classifier/family-name-classifer.js +3 -3
  55. package/dist/es/anthroponym-declension/family-name-classifier/model-bundle-loader.js +3 -3
  56. package/dist/es/anthroponym-declension/family-name-classifier/model-config.js +3 -3
  57. package/dist/es/anthroponym-declension/family-name-classifier/scripts/train-model.d.ts +2 -2
  58. package/dist/es/anthroponym-declension/family-name-classifier/scripts/train-model.d.ts.map +1 -1
  59. package/dist/es/anthroponym-declension/family-name-classifier/word-transformer.js +3 -3
  60. package/dist/es/anthroponym-declension/family-name-inflector.js +4 -4
  61. package/dist/es/anthroponym-declension/family-name-inflector.js.map +1 -1
  62. package/dist/es/anthroponym-declension/given-name-inflector.js +4 -4
  63. package/dist/es/anthroponym-declension/given-name-inflector.js.map +1 -1
  64. package/dist/es/anthroponym-declension/name-inflector.js +3 -3
  65. package/dist/es/anthroponym-declension/patronymic-name-inflector.d.ts.map +1 -1
  66. package/dist/es/anthroponym-declension/patronymic-name-inflector.js +5 -5
  67. package/dist/es/anthroponym-declension/patronymic-name-inflector.js.map +1 -1
  68. package/dist/es/contracts.d.ts +10 -4
  69. package/dist/es/contracts.d.ts.map +1 -1
  70. package/dist/es/gender-detection/detect-gender.js +3 -3
  71. package/dist/es/gender-detection/given-names-genders.json.js +3 -3
  72. package/dist/es/index.js +4 -3
  73. package/dist/es/index.js.map +1 -1
  74. package/dist/es/input-validation.d.ts +6 -4
  75. package/dist/es/input-validation.d.ts.map +1 -1
  76. package/dist/es/input-validation.js +54 -50
  77. package/dist/es/input-validation.js.map +1 -1
  78. package/dist/es/language/alphabet.js +3 -3
  79. package/dist/es/language/grammatical-case.js +3 -3
  80. package/dist/es/language/grammatical-gender.js +3 -3
  81. package/dist/es/language/letter-case.js +3 -3
  82. package/dist/es/language/linguistics.js +3 -3
  83. package/dist/es/language/word-class.js +3 -3
  84. package/dist/es/shevchenko.d.ts +87 -15
  85. package/dist/es/shevchenko.d.ts.map +1 -1
  86. package/dist/es/shevchenko.js +97 -18
  87. package/dist/es/shevchenko.js.map +1 -1
  88. package/dist/es/word-declension/append-command-runner.js +3 -3
  89. package/dist/es/word-declension/bootstrap.js +4 -4
  90. package/dist/es/word-declension/command-runner-factory.js +3 -3
  91. package/dist/es/word-declension/declension-rule-inflector.js +3 -3
  92. package/dist/es/word-declension/declension-rule-matchers.d.ts +5 -5
  93. package/dist/es/word-declension/declension-rule-matchers.d.ts.map +1 -1
  94. package/dist/es/word-declension/declension-types.d.ts +12 -11
  95. package/dist/es/word-declension/declension-types.d.ts.map +1 -1
  96. package/dist/es/word-declension/declension-types.js +3 -3
  97. package/dist/es/word-declension/declension-types.js.map +1 -1
  98. package/dist/es/word-declension/replace-command-runner.js +3 -3
  99. package/dist/es/word-declension/{declension-rules.json.js → rules/artifacts/declension-rules.json.js} +97 -97
  100. package/dist/es/word-declension/rules/scripts/build-declension-rules.d.ts +2 -0
  101. package/dist/es/word-declension/rules/scripts/build-declension-rules.d.ts.map +1 -0
  102. package/dist/es/word-declension/word-inflector.d.ts +5 -7
  103. package/dist/es/word-declension/word-inflector.d.ts.map +1 -1
  104. package/dist/es/word-declension/word-inflector.js +7 -7
  105. package/dist/es/word-declension/word-inflector.js.map +1 -1
  106. package/dist/umd/shevchenko.min.js +188 -188
  107. package/dist/umd/shevchenko.min.js.map +1 -1
  108. package/package.json +4 -5
  109. package/src/anthroponym-declension/anthroponym.ts +2 -2
  110. package/src/anthroponym-declension/family-name-classifier/data-utils.ts +3 -3
  111. package/src/anthroponym-declension/family-name-classifier/family-name-class.ts +2 -2
  112. package/src/anthroponym-declension/family-name-classifier/model-bundle-saver.ts +4 -4
  113. package/src/anthroponym-declension/family-name-classifier/scripts/cache-incorrect-predictions.ts +6 -6
  114. package/src/anthroponym-declension/family-name-classifier/scripts/train-model.ts +7 -7
  115. package/src/anthroponym-declension/family-name-inflector.ts +1 -1
  116. package/src/anthroponym-declension/given-name-inflector.ts +1 -1
  117. package/src/anthroponym-declension/patronymic-name-inflector.ts +3 -2
  118. package/src/contracts.ts +12 -4
  119. package/src/input-validation.ts +65 -51
  120. package/src/shevchenko.ts +127 -23
  121. package/src/word-declension/README.md +1 -1
  122. package/src/word-declension/bootstrap.ts +1 -1
  123. package/src/word-declension/declension-rule-matchers.ts +10 -7
  124. package/src/word-declension/declension-types.ts +13 -11
  125. package/src/word-declension/rules/adjective-declension-rules.json +649 -0
  126. package/src/word-declension/{declension-rules.json → rules/artifacts/declension-rules.json} +94 -94
  127. package/src/word-declension/rules/exception-declension-rules.json +842 -0
  128. package/src/word-declension/rules/noun-declension-rules.json +6301 -0
  129. package/src/word-declension/rules/scripts/build-declension-rules.ts +40 -0
  130. package/src/word-declension/word-inflector.ts +13 -11
  131. package/dist/es/word-declension/scripts/load-declension-rules.d.ts +0 -2
  132. package/dist/es/word-declension/scripts/load-declension-rules.d.ts.map +0 -1
  133. package/src/word-declension/scripts/load-declension-rules.ts +0 -14
  134. /package/dist/cjs/word-declension/{declension-rules.json.js.map → rules/artifacts/declension-rules.json.js.map} +0 -0
  135. /package/dist/es/word-declension/{declension-rules.json.js.map → rules/artifacts/declension-rules.json.js.map} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shevchenko",
3
- "version": "3.0.8",
3
+ "version": "3.0.9",
4
4
  "description": "JavaScript library for declension of Ukrainian anthroponyms",
5
5
  "browser": "./dist/umd/shevchenko.min.js",
6
6
  "main": "./dist/cjs/shevchenko.js",
@@ -37,11 +37,10 @@
37
37
  "type": "git",
38
38
  "url": "git+https://github.com/tooleks/shevchenko-js.git"
39
39
  },
40
- "author": "Oleksandr Tolochko <tooleks@gmail.com>",
40
+ "author": "Oleksandr Tolochko <shevchenko-js@tooleks.com>",
41
41
  "contributors": [
42
- "Anna Tolochko <anya.pogorelova@gmail.com>",
43
- "Tetyana Tolochko",
44
- "spetrik <sergei.petrik@gmail.com> (https://github.com/spetrik)"
42
+ "Anna Tolochko",
43
+ "Tetyana Tolochko"
45
44
  ],
46
45
  "license": "MIT",
47
46
  "bugs": {
@@ -1,5 +1,5 @@
1
- export interface Anthroponym {
1
+ export type Anthroponym = {
2
2
  givenName?: string;
3
3
  patronymicName?: string;
4
4
  familyName?: string;
5
- }
5
+ };
@@ -1,10 +1,10 @@
1
1
  /**
2
- * An interface representing a split of a dataset into training and validation sets.
2
+ * Represents a split of a dataset into training and validation sets.
3
3
  */
4
- export interface SplitData<T> {
4
+ export type SplitData<T> = {
5
5
  trainingData: T[];
6
6
  validationData: T[];
7
- }
7
+ };
8
8
 
9
9
  /**
10
10
  * Splits an array of objects into training and validation datasets, grouped by a specific feature and shuffled randomly within each group.
@@ -1,8 +1,8 @@
1
1
  import { WordClass } from '../../language';
2
2
 
3
- export interface FamilyNameClass {
3
+ export type FamilyNameClass = {
4
4
  /**
5
5
  * The word class of the classified family name.
6
6
  */
7
7
  wordClass: WordClass;
8
- }
8
+ };
@@ -1,18 +1,18 @@
1
- import { writeFile } from 'fs/promises';
2
- import * as path from 'path';
1
+ import { writeFile } from 'node:fs/promises';
2
+ import * as path from 'node:path';
3
3
  import * as tf from '@tensorflow/tfjs';
4
4
 
5
5
  const MODEL_BUNDLE_FILEPATH = path.join(__dirname, 'artifacts/model.bundle.json');
6
6
 
7
7
  type ModelMetadata = Omit<tf.io.ModelJSON, 'modelTopology' | 'weightsManifest'>;
8
8
 
9
- interface ModelBundle {
9
+ type ModelBundle = {
10
10
  modelInfo: tf.io.ModelArtifactsInfo;
11
11
  modelTopology: Record<string, unknown>;
12
12
  weightSpecs: tf.io.WeightsManifestEntry[];
13
13
  weightData: string;
14
14
  modelMetadata: ModelMetadata;
15
- }
15
+ };
16
16
 
17
17
  // Use Buffer on Node.js instead of Blob/atob/btoa
18
18
  const useNodeBuffer =
@@ -1,6 +1,6 @@
1
- import { createReadStream } from 'fs';
2
- import { writeFile } from 'fs/promises';
3
- import { join as joinPath } from 'path';
1
+ import { createReadStream } from 'node:fs';
2
+ import * as fs from 'node:fs/promises';
3
+ import * as path from 'node:path';
4
4
  import * as tf from '@tensorflow/tfjs-node';
5
5
  import { parse as createCsvParser } from 'csv';
6
6
  import { FamilyNameClassTransformer } from '../family-name-class-transformer';
@@ -8,8 +8,8 @@ import { ModelBundleLoader } from '../model-bundle-loader';
8
8
  import { MODEL_INPUT_SIZE } from '../model-config';
9
9
  import { WordTransformer } from '../word-transformer';
10
10
 
11
- const TRAINING_DATASET_FILEPATH = joinPath(__dirname, '../datasets/training.csv');
12
- const INCORRECT_PREDICTIONS_FILEPATH = joinPath(__dirname, '../cache/incorrect-predictions.json');
11
+ const TRAINING_DATASET_FILEPATH = path.join(__dirname, '../datasets/training.csv');
12
+ const INCORRECT_PREDICTIONS_FILEPATH = path.join(__dirname, '../cache/incorrect-predictions.json');
13
13
 
14
14
  type IncorrectPredictions = Record<string, string>;
15
15
 
@@ -35,7 +35,7 @@ async function retrieveIncorrectPredictions(): Promise<IncorrectPredictions> {
35
35
  }
36
36
 
37
37
  async function saveIncorrectPredictions(cache: IncorrectPredictions): Promise<void> {
38
- await writeFile(INCORRECT_PREDICTIONS_FILEPATH, JSON.stringify(cache, null, 2), 'utf-8');
38
+ await fs.writeFile(INCORRECT_PREDICTIONS_FILEPATH, JSON.stringify(cache, null, 2), 'utf-8');
39
39
  }
40
40
 
41
41
  async function main(): Promise<void> {
@@ -1,5 +1,5 @@
1
- import { createReadStream } from 'fs';
2
- import { join as joinPath } from 'path';
1
+ import * as fs from 'node:fs';
2
+ import * as path from 'node:path';
3
3
  import * as tf from '@tensorflow/tfjs-node';
4
4
  import { parse as createCsvParser } from 'csv';
5
5
  import { ALPHABET_SIZE, WordClass } from '../../../language';
@@ -10,18 +10,18 @@ import { ModelBundleSaver } from '../model-bundle-saver';
10
10
  import { MODEL_INPUT_SIZE } from '../model-config';
11
11
  import { WordTransformer } from '../word-transformer';
12
12
 
13
- const TRAINING_DATASET_FILEPATH = joinPath(__dirname, '../datasets/training.csv');
14
- const MODEL_ARTIFACTS_SOURCE = 'file://' + joinPath(__dirname, '../artifacts');
13
+ const TRAINING_DATASET_FILEPATH = path.join(__dirname, '../datasets/training.csv');
14
+ const MODEL_ARTIFACTS_SOURCE = 'file://' + path.join(__dirname, '../artifacts');
15
15
 
16
- export interface DataItem extends FamilyNameClass {
16
+ export type DataItem = FamilyNameClass & {
17
17
  familyName: string;
18
18
  wordEnding: string;
19
- }
19
+ };
20
20
 
21
21
  async function loadData(): Promise<SplitData<DataItem>> {
22
22
  const data: DataItem[] = [];
23
23
  const dataParser = createCsvParser({ columns: true });
24
- createReadStream(TRAINING_DATASET_FILEPATH).pipe(dataParser);
24
+ fs.createReadStream(TRAINING_DATASET_FILEPATH).pipe(dataParser);
25
25
 
26
26
  for await (const dataRow of dataParser) {
27
27
  data.push({
@@ -38,7 +38,7 @@ export class FamilyNameInflector extends NameInflector {
38
38
  grammaticalCase: grammaticalCase,
39
39
  gender: gender,
40
40
  wordClass: familyNameClass?.wordClass,
41
- application: 'familyName',
41
+ applicationType: 'familyName',
42
42
  });
43
43
  }
44
44
 
@@ -21,7 +21,7 @@ export class GivenNameInflector extends NameInflector {
21
21
  return this.wordInflector.inflect(givenName, {
22
22
  grammaticalCase: grammaticalCase,
23
23
  gender: gender,
24
- application: 'givenName',
24
+ applicationType: 'givenName',
25
25
  });
26
26
  }
27
27
  }
@@ -21,8 +21,9 @@ export class PatronymicNameInflector extends NameInflector {
21
21
  return this.wordInflector.inflect(patronymicName, {
22
22
  grammaticalCase: grammaticalCase,
23
23
  gender: gender,
24
- application: 'patronymicName',
25
- customRuleFilter: (declensionRule) => declensionRule.application.includes('patronymicName'),
24
+ applicationType: 'patronymicName',
25
+ customRuleFilter: (declensionRule) =>
26
+ declensionRule.applicationType.includes('patronymicName'),
26
27
  });
27
28
  }
28
29
  }
package/src/contracts.ts CHANGED
@@ -1,20 +1,28 @@
1
- import { Anthroponym } from './anthroponym-declension';
2
1
  import { GrammaticalGender } from './language';
3
2
 
4
3
  /**
5
4
  * The input parameters for anthroponym declension.
6
5
  */
7
- export type DeclensionInput = Anthroponym & { gender: GrammaticalGender };
6
+ export type DeclensionInput = {
7
+ gender: GrammaticalGender;
8
+ givenName?: string;
9
+ patronymicName?: string;
10
+ familyName?: string;
11
+ };
8
12
 
9
13
  /**
10
14
  * The output result of anthroponym declension.
11
15
  */
12
- export type DeclensionOutput = Anthroponym;
16
+ export type DeclensionOutput<T extends DeclensionInput = DeclensionInput> = Omit<T, 'gender'>;
13
17
 
14
18
  /**
15
19
  * The input parameters for gender detection.
16
20
  */
17
- export type GenderDetectionInput = Anthroponym;
21
+ export type GenderDetectionInput = {
22
+ givenName?: string;
23
+ patronymicName?: string;
24
+ familyName?: string;
25
+ };
18
26
 
19
27
  /**
20
28
  * The output result of gender detection.
@@ -1,80 +1,94 @@
1
- import { Anthroponym } from './anthroponym-declension';
2
1
  import { DeclensionInput, GenderDetectionInput } from './contracts';
3
2
  import { GrammaticalGender } from './language';
4
3
 
5
- /**
6
- * Validates if a given value is a valid input for declension.
7
- *
8
- * @throws {TypeError}
9
- */
10
- export function validateDeclensionInput(input: DeclensionInput): void {
11
- validateObject(input);
12
- validateGender(input.gender);
13
- validateAnthroponym(input);
14
- }
15
-
16
- /**
17
- * Validates if a given value is a valid input for gender detection.
18
- *
19
- * @throws {TypeError}
20
- */
21
- export function validateGenderDetectionInput(input: GenderDetectionInput): void {
22
- validateObject(input);
23
- validateAnthroponym(input);
24
- }
4
+ export class InputValidationError extends TypeError {}
25
5
 
26
6
  /**
27
- * Validates if a given value is an object.
7
+ * Validates if a given value is a valid input for declension.
28
8
  *
29
- * @throws {TypeError}
9
+ * @throws {InputValidationError}
30
10
  */
31
- function validateObject(object: unknown): void {
32
- if (typeof object !== 'object' || object == null) {
33
- throw new TypeError('The input type must be an object.');
11
+ export function validateDeclensionInput(value: unknown): asserts value is DeclensionInput {
12
+ if (!isObject(value)) {
13
+ throw new InputValidationError('The input type must be an object.');
34
14
  }
35
- }
36
15
 
37
- /**
38
- * Validates if a given value is a valid grammatical gender.
39
- *
40
- * @throws {TypeError}
41
- */
42
- function validateGender(gender: GrammaticalGender): void {
43
- if (![GrammaticalGender.MASCULINE, GrammaticalGender.FEMININE].includes(gender)) {
44
- throw new TypeError(
16
+ if (!isIn(value.gender, Object.values(GrammaticalGender))) {
17
+ throw new InputValidationError(
45
18
  `The "gender" parameter must be one of the following: "${GrammaticalGender.MASCULINE}", "${GrammaticalGender.FEMININE}".`,
46
19
  );
47
20
  }
21
+
22
+ if (
23
+ !isDefined(value.givenName) &&
24
+ !isDefined(value.patronymicName) &&
25
+ !isDefined(value.familyName)
26
+ ) {
27
+ throw new InputValidationError(
28
+ 'At least one of the following parameters must present: "givenName", "patronymicName", "familyName".',
29
+ );
30
+ }
31
+
32
+ if (isDefined(value.givenName) && !isString(value.givenName)) {
33
+ throw new InputValidationError('The "givenName" parameter must be a string.');
34
+ }
35
+
36
+ if (isDefined(value.patronymicName) && !isString(value.patronymicName)) {
37
+ throw new InputValidationError('The "patronymicName" parameter must be a string.');
38
+ }
39
+
40
+ if (isDefined(value.familyName) && !isString(value.familyName)) {
41
+ throw new InputValidationError('The "familyName" parameter must be a string.');
42
+ }
48
43
  }
49
44
 
50
45
  /**
51
- * Validates if a given value is a valid anthroponym.
46
+ * Validates if a given value is a valid input for gender detection.
52
47
  *
53
- * @throws {TypeError}
48
+ * @throws {InputValidationError}
54
49
  */
55
- function validateAnthroponym(anthroponym: Anthroponym): void {
50
+ export function validateGenderDetectionInput(
51
+ value: unknown,
52
+ ): asserts value is GenderDetectionInput {
53
+ if (!isObject(value)) {
54
+ throw new InputValidationError('The input type must be an object.');
55
+ }
56
+
56
57
  if (
57
- typeof anthroponym.givenName === 'undefined' &&
58
- typeof anthroponym.patronymicName === 'undefined' &&
59
- typeof anthroponym.familyName === 'undefined'
58
+ !isDefined(value.givenName) &&
59
+ !isDefined(value.patronymicName) &&
60
+ !isDefined(value.familyName)
60
61
  ) {
61
- throw new TypeError(
62
+ throw new InputValidationError(
62
63
  'At least one of the following parameters must present: "givenName", "patronymicName", "familyName".',
63
64
  );
64
65
  }
65
66
 
66
- if (typeof anthroponym.givenName !== 'undefined' && typeof anthroponym.givenName !== 'string') {
67
- throw new TypeError('The "givenName" parameter must be a string.');
67
+ if (isDefined(value.givenName) && !isString(value.givenName)) {
68
+ throw new InputValidationError('The "givenName" parameter must be a string.');
68
69
  }
69
70
 
70
- if (
71
- typeof anthroponym.patronymicName !== 'undefined' &&
72
- typeof anthroponym.patronymicName !== 'string'
73
- ) {
74
- throw new TypeError('The "patronymicName" parameter must be a string.');
71
+ if (isDefined(value.patronymicName) && !isString(value.patronymicName)) {
72
+ throw new InputValidationError('The "patronymicName" parameter must be a string.');
75
73
  }
76
74
 
77
- if (typeof anthroponym.familyName !== 'undefined' && typeof anthroponym.familyName !== 'string') {
78
- throw new TypeError('The "familyName" parameter must be a string.');
75
+ if (isDefined(value.familyName) && !isString(value.familyName)) {
76
+ throw new InputValidationError('The "familyName" parameter must be a string.');
79
77
  }
80
78
  }
79
+
80
+ function isDefined<T>(value: T | undefined): value is T {
81
+ return typeof value !== 'undefined';
82
+ }
83
+
84
+ function isObject(value: unknown): value is Record<string, unknown> {
85
+ return value != null && typeof value === 'object';
86
+ }
87
+
88
+ function isString(value: unknown): value is string {
89
+ return typeof value === 'string';
90
+ }
91
+
92
+ function isIn<T>(value: unknown, values: T[]): value is T {
93
+ return values.includes(value as T);
94
+ }
package/src/shevchenko.ts CHANGED
@@ -6,88 +6,192 @@ import {
6
6
  GenderDetectionOutput,
7
7
  } from './contracts';
8
8
  import { detectGender as autoDetectGender } from './gender-detection';
9
- import { validateDeclensionInput, validateGenderDetectionInput } from './input-validation';
9
+ import {
10
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
11
+ InputValidationError,
12
+ validateDeclensionInput,
13
+ validateGenderDetectionInput,
14
+ } from './input-validation';
10
15
  import { GrammaticalCase } from './language';
11
16
 
12
17
  export * from './contracts';
18
+ export { InputValidationError } from './input-validation';
13
19
  export { GrammaticalGender, GrammaticalCase } from './language';
14
20
  export { Anthroponym } from './anthroponym-declension';
15
21
 
16
22
  /**
17
23
  * Inflects an anthroponym in nominative grammatical case.
18
24
  *
19
- * @throws {TypeError} Input validation error.
25
+ * @example
26
+ * ```ts
27
+ * const anthroponym = await shevchenko.inNominative({
28
+ * gender: shevchenko.GrammaticalGender.MASCULINE,
29
+ * givenName: 'Тарас',
30
+ * patronymicName: 'Григорович',
31
+ * familyName: 'Шевченко',
32
+ * });
33
+ * ```
34
+ * @throws {InputValidationError} Input validation error.
20
35
  */
21
- export async function inNominative(input: DeclensionInput): Promise<DeclensionOutput> {
36
+ export async function inNominative<T extends DeclensionInput>(
37
+ input: T,
38
+ ): Promise<DeclensionOutput<T>> {
22
39
  validateDeclensionInput(input);
23
- return anthroponymInflector.inflect(input, input.gender, GrammaticalCase.NOMINATIVE);
40
+ const result = await anthroponymInflector.inflect(
41
+ input,
42
+ input.gender,
43
+ GrammaticalCase.NOMINATIVE,
44
+ );
45
+ return result as DeclensionOutput<T>;
24
46
  }
25
47
 
26
48
  /**
27
49
  * Inflects an anthroponym in genitive grammatical case.
28
50
  *
29
- * @throws {TypeError} Input validation error.
51
+ * @example
52
+ * ```ts
53
+ * const anthroponym = await shevchenko.inGenitive({
54
+ * gender: shevchenko.GrammaticalGender.MASCULINE,
55
+ * givenName: 'Тарас',
56
+ * patronymicName: 'Григорович',
57
+ * familyName: 'Шевченко',
58
+ * });
59
+ * ```
60
+ * @throws {InputValidationError} Input validation error.
30
61
  */
31
- export async function inGenitive(input: DeclensionInput): Promise<DeclensionOutput> {
62
+ export async function inGenitive<T extends DeclensionInput>(
63
+ input: T,
64
+ ): Promise<DeclensionOutput<T>> {
32
65
  validateDeclensionInput(input);
33
- return anthroponymInflector.inflect(input, input.gender, GrammaticalCase.GENITIVE);
66
+ const result = await anthroponymInflector.inflect(input, input.gender, GrammaticalCase.GENITIVE);
67
+ return result as DeclensionOutput<T>;
34
68
  }
35
69
 
36
70
  /**
37
71
  * Inflects an anthroponym in dative grammatical case.
38
72
  *
39
- * @throws {TypeError} Input validation error.
73
+ * @example
74
+ * ```ts
75
+ * const anthroponym = await shevchenko.inDative({
76
+ * gender: shevchenko.GrammaticalGender.MASCULINE,
77
+ * givenName: 'Тарас',
78
+ * patronymicName: 'Григорович',
79
+ * familyName: 'Шевченко',
80
+ * });
81
+ * ```
82
+ * @throws {InputValidationError} Input validation error.
40
83
  */
41
- export async function inDative(input: DeclensionInput): Promise<DeclensionOutput> {
84
+ export async function inDative<T extends DeclensionInput>(input: T): Promise<DeclensionOutput<T>> {
42
85
  validateDeclensionInput(input);
43
- return anthroponymInflector.inflect(input, input.gender, GrammaticalCase.DATIVE);
86
+ const result = await anthroponymInflector.inflect(input, input.gender, GrammaticalCase.DATIVE);
87
+ return result as DeclensionOutput<T>;
44
88
  }
45
89
 
46
90
  /**
47
91
  * Inflects an anthroponym in accusative grammatical case.
48
92
  *
49
- * @throws {TypeError} Input validation error.
93
+ * @example
94
+ * ```ts
95
+ * const anthroponym = await shevchenko.inAccusative({
96
+ * gender: shevchenko.GrammaticalGender.MASCULINE,
97
+ * givenName: 'Тарас',
98
+ * patronymicName: 'Григорович',
99
+ * familyName: 'Шевченко',
100
+ * });
101
+ * ```
102
+ * @throws {InputValidationError} Input validation error.
50
103
  */
51
- export async function inAccusative(input: DeclensionInput): Promise<DeclensionOutput> {
104
+ export async function inAccusative<T extends DeclensionInput>(
105
+ input: T,
106
+ ): Promise<DeclensionOutput<T>> {
52
107
  validateDeclensionInput(input);
53
- return anthroponymInflector.inflect(input, input.gender, GrammaticalCase.ACCUSATIVE);
108
+ const result = await anthroponymInflector.inflect(
109
+ input,
110
+ input.gender,
111
+ GrammaticalCase.ACCUSATIVE,
112
+ );
113
+ return result as DeclensionOutput<T>;
54
114
  }
55
115
 
56
116
  /**
57
117
  * Inflects an anthroponym in ablative grammatical case.
58
118
  *
59
- * @throws {TypeError} Input validation error.
119
+ * @example
120
+ * ```ts
121
+ * const anthroponym = await shevchenko.inAblative({
122
+ * gender: shevchenko.GrammaticalGender.MASCULINE,
123
+ * givenName: 'Тарас',
124
+ * patronymicName: 'Григорович',
125
+ * familyName: 'Шевченко',
126
+ * });
127
+ * ```
128
+ * @throws {InputValidationError} Input validation error.
60
129
  */
61
- export async function inAblative(input: DeclensionInput): Promise<DeclensionOutput> {
130
+ export async function inAblative<T extends DeclensionInput>(
131
+ input: T,
132
+ ): Promise<DeclensionOutput<T>> {
62
133
  validateDeclensionInput(input);
63
- return anthroponymInflector.inflect(input, input.gender, GrammaticalCase.ABLATIVE);
134
+ const result = await anthroponymInflector.inflect(input, input.gender, GrammaticalCase.ABLATIVE);
135
+ return result as DeclensionOutput<T>;
64
136
  }
65
137
 
66
138
  /**
67
139
  * Inflects an anthroponym in locative grammatical case.
68
140
  *
69
- * @throws {TypeError} Input validation error.
141
+ * @example
142
+ * ```ts
143
+ * const anthroponym = await shevchenko.inLocative({
144
+ * gender: shevchenko.GrammaticalGender.MASCULINE,
145
+ * givenName: 'Тарас',
146
+ * patronymicName: 'Григорович',
147
+ * familyName: 'Шевченко',
148
+ * });
149
+ * ```
150
+ * @throws {InputValidationError} Input validation error.
70
151
  */
71
- export async function inLocative(input: DeclensionInput): Promise<DeclensionOutput> {
152
+ export async function inLocative<T extends DeclensionInput>(
153
+ input: T,
154
+ ): Promise<DeclensionOutput<T>> {
72
155
  validateDeclensionInput(input);
73
- return anthroponymInflector.inflect(input, input.gender, GrammaticalCase.LOCATIVE);
156
+ const result = await anthroponymInflector.inflect(input, input.gender, GrammaticalCase.LOCATIVE);
157
+ return result as DeclensionOutput<T>;
74
158
  }
75
159
 
76
160
  /**
77
161
  * Inflects an anthroponym in vocative grammatical case.
78
162
  *
79
- * @throws {TypeError} Input validation error.
163
+ * @example
164
+ * ```ts
165
+ * const anthroponym = await shevchenko.inVocative({
166
+ * gender: shevchenko.GrammaticalGender.MASCULINE,
167
+ * givenName: 'Тарас',
168
+ * patronymicName: 'Григорович',
169
+ * familyName: 'Шевченко',
170
+ * });
171
+ * ```
172
+ * @throws {InputValidationError} Input validation error.
80
173
  */
81
- export async function inVocative(input: DeclensionInput): Promise<DeclensionOutput> {
174
+ export async function inVocative<T extends DeclensionInput>(
175
+ input: T,
176
+ ): Promise<DeclensionOutput<T>> {
82
177
  validateDeclensionInput(input);
83
- return anthroponymInflector.inflect(input, input.gender, GrammaticalCase.VOCATIVE);
178
+ const result = await anthroponymInflector.inflect(input, input.gender, GrammaticalCase.VOCATIVE);
179
+ return result as DeclensionOutput<T>;
84
180
  }
85
181
 
86
182
  /**
87
183
  * Returns the grammatical gender of an anthroponym.
88
184
  * Returns null if the grammatical gender cannot be detected.
89
185
  *
90
- * @throws {TypeError} Input validation error.
186
+ * @example
187
+ * ```ts
188
+ * const gender = await shevchenko.detectGender({
189
+ * givenName: 'Тарас',
190
+ * patronymicName: 'Григорович',
191
+ * familyName: 'Шевченко',
192
+ * });
193
+ * ```
194
+ * @throws {InputValidationError} Input validation error.
91
195
  */
92
196
  export async function detectGender(input: GenderDetectionInput): Promise<GenderDetectionOutput> {
93
197
  validateGenderDetectionInput(input);
@@ -1,3 +1,3 @@
1
1
  # Word Declension Module
2
2
 
3
- This module provides services and datatypes for the word declension using the declension rules from the [rules repository](https://github.com/tooleks/shevchenko-rules).
3
+ This module provides services and datatypes for the word declension.
@@ -1,5 +1,5 @@
1
- import untypedDeclensionRules from './declension-rules.json';
2
1
  import { DeclensionRule } from './declension-types';
2
+ import untypedDeclensionRules from './rules/artifacts/declension-rules.json';
3
3
  import { WordInflector } from './word-inflector';
4
4
 
5
5
  const declensionRules = untypedDeclensionRules as DeclensionRule[];
@@ -1,5 +1,5 @@
1
1
  import { GrammaticalGender } from '../language';
2
- import { DeclensionRule } from './declension-types';
2
+ import { ApplicationType, DeclensionRule } from './declension-types';
3
3
 
4
4
  /**
5
5
  * Determines if a given declension rule applies to a specified gender.
@@ -9,17 +9,20 @@ export function isGenderApplicable(rule: DeclensionRule, gender: GrammaticalGend
9
9
  }
10
10
 
11
11
  /**
12
- * Determines if a given declension rule applies to a specified application.
12
+ * Determines if a given declension rule applies to a specified application type.
13
13
  */
14
- export function isApplicable(rule: DeclensionRule, application: string): boolean {
15
- return rule.application.length === 0 || rule.application.includes(application);
14
+ export function isApplicable(rule: DeclensionRule, applicationType: ApplicationType): boolean {
15
+ return rule.applicationType.length === 0 || rule.applicationType.includes(applicationType);
16
16
  }
17
17
 
18
18
  /**
19
- * Determines if a given declension rule strictly applies to a specified application.
19
+ * Determines if a given declension rule strictly applies to a specified application type.
20
20
  */
21
- export function isStriclyApplicable(rule: DeclensionRule, application: string): boolean {
22
- return rule.application.includes(application);
21
+ export function isStriclyApplicable(
22
+ rule: DeclensionRule,
23
+ applicationType: ApplicationType,
24
+ ): boolean {
25
+ return rule.applicationType.includes(applicationType);
23
26
  }
24
27
 
25
28
  /**