diff --git a/integration/integration-tests.config.js b/integration/integration-tests.config.js index 504fc13..24753cd 100644 --- a/integration/integration-tests.config.js +++ b/integration/integration-tests.config.js @@ -4,5 +4,5 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', - resetModules: true + resetModules: true, }; diff --git a/integration/tests/Plugin.test.ts b/integration/tests/Plugin.test.ts index 1d83be7..18c2a1c 100644 --- a/integration/tests/Plugin.test.ts +++ b/integration/tests/Plugin.test.ts @@ -1,4 +1,8 @@ -import { getStandard, getValueGenerator, registerPlugin } from '@falbricate/base'; +import { + getStandard, + getValueGenerator, + registerPlugin, +} from '@falbricate/base'; import { GeographyPlugin } from '../../src'; describe('Not failing Plugin registration', () => { @@ -10,7 +14,7 @@ describe('Not failing Plugin registration', () => { describe('Registering contents', () => { it('should register Random Country Generator', () => { const config = { - countries: ['no', 'it'] + countries: ['no', 'it'], }; expect(() => getValueGenerator('random-country', config)).not.toThrow(); @@ -20,4 +24,3 @@ describe('Registering contents', () => { expect(() => getStandard('country-abbr-eu')).not.toThrow(); }); }); - diff --git a/src/data/base.ts b/src/data/base.ts index cdf0640..7dcef9e 100644 --- a/src/data/base.ts +++ b/src/data/base.ts @@ -9,7 +9,7 @@ export type NamedGeography = { type: GeographyType; name: string; abbr: string; -} +}; /** Declaration of regions and groups */ export interface Region extends NamedGeography { diff --git a/src/data/countries/european.ts b/src/data/countries/european.ts index 9857a86..b7149ee 100644 --- a/src/data/countries/european.ts +++ b/src/data/countries/european.ts @@ -8,7 +8,12 @@ export const EUROPEAN_COUNTRIES: Country[] = [ { type: 'country', abbr: 'az', name: 'Azerbaijan', capital: 'Baku' }, { type: 'country', abbr: 'by', name: 'Belarus', capital: 'Minsk' }, { type: 'country', abbr: 'be', name: 'Belgium', capital: 'Brussels' }, - { type: 'country', abbr: 'ba', name: 'Bosnia and Herzegovina', capital: 'Sarajevo' }, + { + type: 'country', + abbr: 'ba', + name: 'Bosnia and Herzegovina', + capital: 'Sarajevo', + }, { type: 'country', abbr: 'bg', name: 'Bulgaria', capital: 'Sofia' }, { type: 'country', abbr: 'hr', name: 'Croatia', capital: 'Zagreb' }, { type: 'country', abbr: 'cy', name: 'Cyprus', capital: 'Nicosia' }, @@ -28,7 +33,7 @@ export const EUROPEAN_COUNTRIES: Country[] = [ { type: 'country', abbr: 'li', name: 'Liechtenstein', capital: 'Vaduz' }, { type: 'country', abbr: 'lt', name: 'Lithuania', capital: 'Vilnius' }, { type: 'country', abbr: 'lu', name: 'Luxembourg', capital: 'Luxembourg' }, - { type: 'country', abbr: 'mt', name: 'Malta', capital: 'St. Paul\'s Bay' }, + { type: 'country', abbr: 'mt', name: 'Malta', capital: "St. Paul's Bay" }, { type: 'country', abbr: 'md', name: 'Moldova', capital: 'Chisinau' }, { type: 'country', abbr: 'mc', name: 'Monaco', capital: 'Monaco' }, { type: 'country', abbr: 'me', name: 'Montenegro', capital: 'Podgorica' }, @@ -49,5 +54,5 @@ export const EUROPEAN_COUNTRIES: Country[] = [ { type: 'country', abbr: 'tr', name: 'Turkey', capital: 'Ankara' }, { type: 'country', abbr: 'ua', name: 'Ukraine', capital: 'Kyiv' }, { type: 'country', abbr: 'uk', name: 'United Kingdom', capital: 'London' }, - { type: 'country', abbr: 'va', name: 'Vatican', capital: 'Vatican City' } + { type: 'country', abbr: 'va', name: 'Vatican', capital: 'Vatican City' }, ]; diff --git a/src/data/regions/european/index.ts b/src/data/regions/european/index.ts index f16a4d2..ed2c5b0 100644 --- a/src/data/regions/european/index.ts +++ b/src/data/regions/european/index.ts @@ -6,95 +6,168 @@ export const EUROPEAN_REGIONS: Region[] = [ abbr: 'eu', name: 'European Union', subs: [ - 'at', 'be', 'bg', 'hr', 'cy', 'cz', 'dk', 'fi', 'ee', 'fr', 'de', - 'gr', 'hu', 'ie', 'it', 'lv', 'lt', 'lu', 'mt', 'nl', 'pl', 'pt', - 'ro', 'sk', 'si', 'es', 'se' - ] - }, { + 'at', + 'be', + 'bg', + 'hr', + 'cy', + 'cz', + 'dk', + 'fi', + 'ee', + 'fr', + 'de', + 'gr', + 'hu', + 'ie', + 'it', + 'lv', + 'lt', + 'lu', + 'mt', + 'nl', + 'pl', + 'pt', + 'ro', + 'sk', + 'si', + 'es', + 'se', + ], + }, + { type: 'region', abbr: 'not-eu', name: 'European Countries not in EU', subs: [ - 'al', 'ad', 'am', 'az', 'by', 'ba', 'ge', 'is', 'li', 'md', 'mc', - 'me', 'mkd', 'no', 'ru', 'sm', 'rs', 'ch', 'tr', 'ua', 'uk', 'va' - ] - }, { + 'al', + 'ad', + 'am', + 'az', + 'by', + 'ba', + 'ge', + 'is', + 'li', + 'md', + 'mc', + 'me', + 'mkd', + 'no', + 'ru', + 'sm', + 'rs', + 'ch', + 'tr', + 'ua', + 'uk', + 'va', + ], + }, + { type: 'region', abbr: 'europe', name: 'Europe', - subs: ['eu', 'not-eu'] - }, { + subs: ['eu', 'not-eu'], + }, + { type: 'region', abbr: 'nordics', name: 'Nordic countries', - subs: ['dk', 'fi', 'no', 'se', 'is'] - }, { + subs: ['dk', 'fi', 'no', 'se', 'is'], + }, + { type: 'region', abbr: 'baltics', name: 'Baltic states', - subs: ['ee', 'lv', 'lt'] - }, { + subs: ['ee', 'lv', 'lt'], + }, + { type: 'region', abbr: 'v4', name: 'Visegrád Group', - subs: ['cz', 'hu', 'pl', 'sk'] - }, { + subs: ['cz', 'hu', 'pl', 'sk'], + }, + { type: 'region', abbr: 'benelux', name: 'Benelux', - subs: ['be', 'nl', 'lu'] - }, { + subs: ['be', 'nl', 'lu'], + }, + { type: 'region', abbr: 'iberia', name: 'Iberian Penisula', - subs: ['es', 'pt'] - }, { + subs: ['es', 'pt'], + }, + { type: 'region', abbr: 'balkans', name: 'Balkan Countries', - subs: ['al', 'ba', 'bg', 'hr', 'me', 'mkd', 'rs'] - }, { + subs: ['al', 'ba', 'bg', 'hr', 'me', 'mkd', 'rs'], + }, + { type: 'region', abbr: 'balkans-broad', name: 'Broad Balkan Countries', - subs: ['balkans', 'gr', 'ro', 'si', 'tr'] - }, { + subs: ['balkans', 'gr', 'ro', 'si', 'tr'], + }, + { type: 'region', abbr: 'n-europe', name: 'Northern Europe', - subs: ['dk', 'ee', 'fi', 'is', 'ie', 'lv', 'lt', 'no', 'se', 'uk'] - }, { + subs: ['dk', 'ee', 'fi', 'is', 'ie', 'lv', 'lt', 'no', 'se', 'uk'], + }, + { type: 'region', abbr: 's-europe', name: 'Southwest Europe', subs: [ - 'al', 'ad', 'ba', 'hr', 'gr', 'va', 'it', 'mt', 'me', 'pt', 'sm', - 'rs', 'si', 'es', 'mkd' - ] - }, { + 'al', + 'ad', + 'ba', + 'hr', + 'gr', + 'va', + 'it', + 'mt', + 'me', + 'pt', + 'sm', + 'rs', + 'si', + 'es', + 'mkd', + ], + }, + { type: 'region', abbr: 'w-europe', name: 'Western Europe', - subs: ['au', 'be', 'fr', 'de', 'li', 'lu', 'mc', 'nl', 'ch'] - }, { + subs: ['au', 'be', 'fr', 'de', 'li', 'lu', 'mc', 'nl', 'ch'], + }, + { type: 'region', abbr: 'e-europe', name: 'Eastern Europe', - subs: ['bg', 'by', 'cz', 'hu', 'md', 'pl', 'ro', 'ru', 'sk', 'ua'] - }, { + subs: ['bg', 'by', 'cz', 'hu', 'md', 'pl', 'ro', 'ru', 'sk', 'ua'], + }, + { type: 'region', abbr: 'c-europe', name: 'Central Europe', - subs: ['at', 'de', 'cz', 'sk', 'pl', 'hu'] - }, { + subs: ['at', 'de', 'cz', 'sk', 'pl', 'hu'], + }, + { type: 'region', abbr: 'sw-europe', name: 'Southwest Europe', - subs: ['es', 'pt'] - }, { + subs: ['es', 'pt'], + }, + { type: 'region', abbr: 'se-europe', name: 'Southeast Europe', - subs: ['balkans-broad', 'it'] - } -]; \ No newline at end of file + subs: ['balkans-broad', 'it'], + }, +]; diff --git a/src/data/registry.ts b/src/data/registry.ts index 8edea41..43b4df8 100644 --- a/src/data/registry.ts +++ b/src/data/registry.ts @@ -1,4 +1,4 @@ -import {Country, GeographyElement, NamedGeography, Region} from "./base"; +import { Country, GeographyElement, NamedGeography, Region } from './base'; import { EUROPEAN_COUNTRIES } from './countries'; import { EUROPEAN_REGIONS } from './regions'; @@ -16,24 +16,26 @@ const REGISTRY: GeographyRegistry = {}; */ export const hasGeographyAbbreviation = (abbr: string): boolean => { return allGeographyAbbreviations().includes(abbr); -} +}; export const allGeographyAbbreviations = (): string[] => { - return Object.keys(REGISTRY) -} + return Object.keys(REGISTRY); +}; export const addGeography = (geography: GeographyElement) => { if (allGeographyAbbreviations().includes(geography.abbr)) { - throw new Error(`Geography region with abbreviation '${geography.abbr}' already exists`); + throw new Error( + `Geography region with abbreviation '${geography.abbr}' already exists`, + ); } if (geography.type === 'region') { - geography = geography as Region + geography = geography as Region; if (!geography.subs || !Array.isArray(geography.subs)) { throw new Error(`Region's subregions must be a non-empty array`); } } else if (geography.type === 'country') { - geography = geography as Country + geography = geography as Country; if (!geography.capital) { throw new Error(`Country has to have a capital city assigned`); } @@ -56,13 +58,13 @@ export const getGeography = (abbreviation: string): Country[] => { region.subs .map((subregion) => getGeography(subregion)) .map((subregion) => { - return subregion + return subregion; }) .forEach((subregion) => { subregion.forEach((country) => result.push(country)); - }) + }); - return [...new Set(result)] + return [...new Set(result)]; } else if (geography.type === 'country') { const country = geography as Country; return [country]; @@ -76,5 +78,5 @@ export const getGeography = (abbreviation: string): Country[] => { ...EUROPEAN_COUNTRIES, // Register Regions - ...EUROPEAN_REGIONS + ...EUROPEAN_REGIONS, ].forEach((region) => addGeography(region)); diff --git a/src/generators/RandomCountryGenerator.ts b/src/generators/RandomCountryGenerator.ts index fd3b98d..9f095fb 100644 --- a/src/generators/RandomCountryGenerator.ts +++ b/src/generators/RandomCountryGenerator.ts @@ -1,18 +1,23 @@ -import { GeneratedValue, randomItemFromArray, ValueGenerator, ValueGeneratorConfig } from '@falbricate/base'; +import { + GeneratedValue, + randomItemFromArray, + ValueGenerator, + ValueGeneratorConfig, +} from '@falbricate/base'; import { Country, getGeography } from '../data'; -export type CountryAs = 'object' | 'name' | 'abbr' | 'capital' +export type CountryAs = 'object' | 'name' | 'abbr' | 'capital'; export type RandomCountryConfig = { countries: string | string[]; as?: CountryAs; -} & ValueGeneratorConfig & any; +} & ValueGeneratorConfig & + any; export class RandomCountryGenerator extends ValueGenerator< GeneratedValue, RandomCountryConfig > { - private readonly countries: Country[]; constructor(config: RandomCountryConfig) { @@ -37,8 +42,8 @@ export class RandomCountryGenerator extends ValueGenerator< } else { throw new Error( `Unexpected type of countries specification: ` + - `'${this.config.countries}'` - ) + `'${this.config.countries}'`, + ); } } @@ -56,7 +61,9 @@ export class RandomCountryGenerator extends ValueGenerator< return country.capital; default: - throw new Error(`Unknown type of country property: '${this.config.as}'`); + throw new Error( + `Unknown type of country property: '${this.config.as}'`, + ); } }; -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index b83ad89..93e9fe8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,16 +2,16 @@ import { FalbricatePlugin } from '@falbricate/base'; import { RandomCountryConfig, RandomCountryGenerator } from './generators'; import { standardsPlugin } from './standards'; - export const GeographyPlugin = (): FalbricatePlugin => ({ valueGenerators: [ { key: 'random-country', - builder: (config: RandomCountryConfig) => new RandomCountryGenerator(config) - } + builder: (config: RandomCountryConfig) => + new RandomCountryGenerator(config), + }, ], - standards: standardsPlugin -}) + standards: standardsPlugin, +}); export * from './data'; export * from './generators'; diff --git a/src/standards/index.ts b/src/standards/index.ts index b6cff4c..cc0e06f 100644 --- a/src/standards/index.ts +++ b/src/standards/index.ts @@ -1,22 +1,36 @@ -import { StandardPlugin, StandardValueGeneratorBuilder } from '@falbricate/base'; +import { + StandardPlugin, + StandardValueGeneratorBuilder, +} from '@falbricate/base'; import { CountryAs, RandomCountryGenerator } from '../generators'; -export const standardsPlugin: StandardPlugin[] = [] +export const standardsPlugin: StandardPlugin[] = []; const add = (key: string, builder: StandardValueGeneratorBuilder) => { - standardsPlugin.push({ key, builder }) -} + standardsPlugin.push({ key, builder }); +}; -const asPossibilities: CountryAs[] = ['object', 'name', 'abbr', 'capital'] +const asPossibilities: CountryAs[] = ['object', 'name', 'abbr', 'capital']; const regions = [ // European - 'eu', 'non-eu', 'europe', 'n-europe', 's-europe', 'e-europe', 'w-europe' -] + 'eu', + 'non-eu', + 'europe', + 'n-europe', + 's-europe', + 'e-europe', + 'w-europe', +]; asPossibilities.forEach((as) => { regions.forEach((region) => { - add(`country-${as}-${region}`, () => new RandomCountryGenerator({ - as, countries: region - })); + add( + `country-${as}-${region}`, + () => + new RandomCountryGenerator({ + as, + countries: region, + }), + ); }); }); diff --git a/tests/data/geographyRegistry.test.ts b/tests/data/geographyRegistry.test.ts index a4da4bf..ac814f1 100644 --- a/tests/data/geographyRegistry.test.ts +++ b/tests/data/geographyRegistry.test.ts @@ -26,7 +26,7 @@ describe('getGeography function', () => { type: 'country', name: 'Gondor', capital: 'Minas Tirith', - abbr: 'gndr' + abbr: 'gndr', }; addGeography(country); @@ -38,16 +38,26 @@ describe('getGeography function', () => { it('should be able to add another region', () => { const countries: Country[] = [ - { type: 'country', name: 'Gondor', capital: 'Minas Tirith', abbr: 'gondor' }, + { + type: 'country', + name: 'Gondor', + capital: 'Minas Tirith', + abbr: 'gondor', + }, { type: 'country', name: 'Rohan', capital: 'Edoras', abbr: 'rohan' }, - { type: 'country', name: 'Rivendell', capital: 'Rivendell', abbr: 'rivendell' } + { + type: 'country', + name: 'Rivendell', + capital: 'Rivendell', + abbr: 'rivendell', + }, ]; const middleEarth: Region = { type: 'region', abbr: 'middle-earth-region', name: 'Middle Earth', - subs: ['gondor', 'rohan', 'rivendell'] + subs: ['gondor', 'rohan', 'rivendell'], }; countries.forEach((c) => addGeography(c)); diff --git a/tests/generators/RandomCountryGenerator.test.ts b/tests/generators/RandomCountryGenerator.test.ts index e74e1ff..10cb76f 100644 --- a/tests/generators/RandomCountryGenerator.test.ts +++ b/tests/generators/RandomCountryGenerator.test.ts @@ -1,24 +1,28 @@ -import { Country, RandomCountryConfig, RandomCountryGenerator } from '../../src'; +import { + Country, + RandomCountryConfig, + RandomCountryGenerator, +} from '../../src'; describe('Random Country Generator', () => { it('should return a single country when region given', () => { const generator = new RandomCountryGenerator({ countries: 'eu', - as: 'object' - }) + as: 'object', + }); const country = generator.get() as Country; expect(Array.isArray(country)).toBe(false); expect(country).not.toBeUndefined(); - expect(country.type).toBe('country') + expect(country.type).toBe('country'); }); it('should return the country when single country given', () => { const generator = new RandomCountryGenerator({ countries: 'no', - as: 'object' - }) + as: 'object', + }); const country = generator.get() as Country; @@ -31,8 +35,8 @@ describe('Random Country Generator', () => { it('should return name of the country', () => { const generator = new RandomCountryGenerator({ countries: 'no', - as: 'name' - }) + as: 'name', + }); const country = generator.get() as Country; expect(country).toBe('Norway'); @@ -41,8 +45,8 @@ describe('Random Country Generator', () => { it('should return abbreviation of the country', () => { const generator = new RandomCountryGenerator({ countries: 'no', - as: 'abbr' - }) + as: 'abbr', + }); const country = generator.get() as Country; expect(country).toBe('no'); @@ -51,8 +55,8 @@ describe('Random Country Generator', () => { it('should return capital of the country', () => { const generator = new RandomCountryGenerator({ countries: 'no', - as: 'capital' - }) + as: 'capital', + }); const country = generator.get() as Country; expect(country).toBe('Oslo'); @@ -60,8 +64,8 @@ describe('Random Country Generator', () => { it('should return name of the country by default', () => { const generator = new RandomCountryGenerator({ - countries: 'no' - }) + countries: 'no', + }); const country = generator.get() as Country; expect(country).toBe('Norway'); @@ -70,10 +74,10 @@ describe('Random Country Generator', () => { it('should return a country from given list', () => { const config: RandomCountryConfig = { countries: ['no', 'se'], - as: 'abbr' - } + as: 'abbr', + }; - const generator = new RandomCountryGenerator(config) + const generator = new RandomCountryGenerator(config); const country = generator.get() as Country; expect(config.countries).toContain(country); diff --git a/unit-test.config.js b/unit-test.config.js index 6116cfa..48f2502 100644 --- a/unit-test.config.js +++ b/unit-test.config.js @@ -5,5 +5,5 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', modulePathIgnorePatterns: ['/integration'], - resetModules: true + resetModules: true, };