Skip to content

Commit

Permalink
refactor: refactored code duplication (#23)
Browse files Browse the repository at this point in the history
* Refactored code duplication

* Fixed description in package.json
  • Loading branch information
akaSybe authored Oct 15, 2019
1 parent 2bafa23 commit c721570
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lvovich",
"version": "0.0.0-semantic-release",
"description": "GraphQL schema builder from different data sources with middleware extensions.",
"description": "Library to decline people & city names in Russian, and to detect gender by name",
"engines": {
"node": ">= 6"
},
Expand Down
2 changes: 1 addition & 1 deletion src/city.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export function cityIn(name: string, gender?: GenderStrT): string;
// родительный, из какого города приехали?
export function cityFrom(name: string, gender?: GenderStrT): string;

// в какой город направляетесь?
// винительный, в какой город направляетесь?
export function cityTo(name: string): string;
30 changes: 11 additions & 19 deletions src/city.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
applyRule,
applyMod,
} from './inclineRules';
import type { DeclentionT } from './inclineRules';
import { inclineFirstname } from './incline';
import { frozenWords, frozenParts, frozenPartsAfter, cityRules } from './rules/cityRules';
import type { GenderStrT } from './gender';

constantizeGenderInRules(cityRules);

// предложный, в каком городе живете/находитесь?
export function cityIn(name: string, gender?: GenderStrT) {
function declineTo(name: string, wordCase: DeclentionT, gender?: GenderStrT) {
if (isFrozen(name, frozenWords)) return name;
return name
.split(/(\s|-)/g)
Expand All @@ -27,33 +27,25 @@ export function cityIn(name: string, gender?: GenderStrT) {

const rule = findRule(part, ANDROGYNOUS, cityRules);
if (rule) {
return applyRule(rule, part, PREPOSITIONAL);
return applyRule(rule, part, wordCase);
}

return inclineFirstname(part, PREPOSITIONAL, gender) || part;
return inclineFirstname(part, wordCase, gender) || part;
})
.join('');
}

// предложный, в каком городе живете/находитесь?
export function cityIn(name: string, gender?: GenderStrT) {
return declineTo(name, PREPOSITIONAL, gender);
}

// родительный, из какого города приехали?
export function cityFrom(name: string, gender?: GenderStrT) {
if (isFrozen(name, frozenWords)) return name;
return name
.split(/(\s|-)/g)
.map((part, i, parts) => {
if (isFrozenPart(part, i, parts)) return part;

const rule = findRule(part, ANDROGYNOUS, cityRules);
if (rule) {
return applyRule(rule, part, GENITIVE);
}

return inclineFirstname(part, GENITIVE, gender) || part;
})
.join('');
return declineTo(name, GENITIVE, gender);
}

// в какой город направляетесь?
// винительный, в какой город направляетесь?
export function cityTo(name: string) {
if (!name) return name;
return name
Expand Down

0 comments on commit c721570

Please sign in to comment.