Skip to content

Commit

Permalink
feat: rework additional minimal data files
Browse files Browse the repository at this point in the history
  • Loading branch information
dmythro committed Sep 12, 2023
1 parent 77de6e9 commit 45690bd
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 25 deletions.
12 changes: 12 additions & 0 deletions dist/minimal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
This directory contains simplified data for each list,
converting `Object` with fields to `Array` with fields in predefined order to decrease the file size (and traffic).

## Country codes: ISO 3166-1 **alpha-2** to **alpha-3** ~2.7KB

Example: `{"UA":"UKR"}`

## Country codes: ISO 3166-1 **alpha-3** to **alpha-2** ~2.7KB

Example: `{"UKR":"UA"}`

## Country emoji by **ISO 3166-1 alpha-2** code ~3.9KB

Example: `{"UA":"🇺🇦"}`

## Country names (English) by **ISO 3166-1 alpha-2** code ~4.3KB

Example: `{"UA":"Ukraine"}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="../index.d.ts" />
import type { TCountryToString } from '../'

declare const countries2to3: TCountryToString
export default countries2to3
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="../index.d.ts" />
import type { TStringToCountry } from '../'

declare const countries3to2: TStringToCountry
export default countries3to2
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="../index.d.ts" />
import type { TCountryToString } from '../'

declare const countriesEmoji: TCountryToString
export default countriesEmoji
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions dist/minimal/countries.en.min.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { TCountryToString } from '../'

declare const countriesEn: TCountryToString
export default countriesEn

5 changes: 5 additions & 0 deletions dist/minimal/countries.native.min.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { TCountryToString } from '../'

declare const countriesNative: TCountryToString
export default countriesNative

5 changes: 5 additions & 0 deletions dist/minimal/languages.en.min.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { TLanguageToString } from '../'

declare const languagesEn: TLanguageToString
export default languagesEn

5 changes: 5 additions & 0 deletions dist/minimal/languages.native.min.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { TLanguageToString } from '../'

declare const languagesNative: TLanguageToString
export default languagesNative

2 changes: 0 additions & 2 deletions dist/more/countries.2to3.min.js

This file was deleted.

2 changes: 0 additions & 2 deletions dist/more/countries.3to2.min.js

This file was deleted.

2 changes: 0 additions & 2 deletions dist/more/countries.emoji.min.js

This file was deleted.

11 changes: 8 additions & 3 deletions packages/scripts/tasks/generateTypings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from 'chalk'
import fs from 'node:fs'

import { MINIMAL_DIR } from 'scripts/constants.ts'
import { continents, countries, languages } from 'scripts/data.ts'
import { saveTextFile } from 'scripts/utils.ts'

Expand Down Expand Up @@ -32,13 +33,17 @@ export const generateTypings = (): void => {
saveTextFile('index.d.ts', typings.trim())
}

export const generateMoreTypings = (fileName: string, varName: string, type: string): void => {
export const generateMinimalDataTypings = (
fileName: string,
varName: string,
type: string
): void => {
const fileContents = [
'/// <reference path="../index.d.ts" />',
`import type { ${type} } from '../'`,
'',
`declare const ${varName}: ${type}`,
`export default ${varName}`,
].join('\n')

saveTextFile(`more/${fileName}.min.d.ts`, fileContents + '\n')
saveTextFile(`${MINIMAL_DIR}/${fileName}.min.d.ts`, fileContents + '\n')
}
26 changes: 15 additions & 11 deletions packages/scripts/tasks/minifyJsonData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'

import { ALL, CONTINENTS, COUNTRIES, MINIMAL_DIR, MORE_DIR, LANGUAGES } from 'scripts/constants.ts'
import { ALL, CONTINENTS, COUNTRIES, MINIMAL_DIR, LANGUAGES } from 'scripts/constants.ts'
import {
continents,
countries,
Expand All @@ -18,7 +18,7 @@ import {
TLanguageToString,
} from 'countries/types.ts'

import { generateMoreTypings } from 'scripts/tasks/generateTypings.ts'
import { generateMinimalDataTypings } from 'scripts/tasks/generateTypings.ts'

export const minifyJsonData = (): void => {
console.log(chalk.bold('\nMinifying main JSON files:\n'))
Expand All @@ -28,13 +28,13 @@ export const minifyJsonData = (): void => {
saveJsonFile(LANGUAGES, languagesInUse)
saveJsonFile(`${LANGUAGES}${ALL}`, languages)

console.log(chalk.bold('\nMinifying data JSON files:\n'))
console.log(chalk.bold('\nGenerating minimal data JSON files:\n'))

saveJsonFile(`${MORE_DIR}${COUNTRIES}.2to3`, countries2to3)
generateMoreTypings(`${COUNTRIES}.2to3`, `${COUNTRIES}2to3`, 'TCountryToString')
saveJsonFile(`${MINIMAL_DIR}${COUNTRIES}.2to3`, countries2to3)
generateMinimalDataTypings(`${COUNTRIES}.2to3`, `${COUNTRIES}2to3`, 'TCountryToString')

saveJsonFile(`${MORE_DIR}${COUNTRIES}.3to2`, countries3to2)
generateMoreTypings(`${COUNTRIES}.3to2`, `${COUNTRIES}3to2`, 'TStringToCountry')
saveJsonFile(`${MINIMAL_DIR}${COUNTRIES}.3to2`, countries3to2)
generateMinimalDataTypings(`${COUNTRIES}.3to2`, `${COUNTRIES}3to2`, 'TStringToCountry')

const countryCodes = Object.keys(countries) as TCountryCode[]
const languageCodes = Object.keys(languagesInUse) as TLanguageCode[]
Expand All @@ -57,14 +57,18 @@ export const minifyJsonData = (): void => {
languagesNative[lang] = languages[lang].native
}

saveJsonFile(`${MORE_DIR}${COUNTRIES}.emoji`, countriesEmoji)
generateMoreTypings(`${COUNTRIES}.emoji`, `${COUNTRIES}Emoji`, 'TCountryToString')

console.log(chalk.bold('\nGenerating minimal data JSON files:\n'))
saveJsonFile(`${MINIMAL_DIR}${COUNTRIES}.emoji`, countriesEmoji)
generateMinimalDataTypings(`${COUNTRIES}.emoji`, `${COUNTRIES}Emoji`, 'TCountryToString')

saveJsonFile(`${MINIMAL_DIR}${COUNTRIES}.en`, countriesEn)
generateMinimalDataTypings(`${COUNTRIES}.en`, `${COUNTRIES}En`, 'TCountryToString')

saveJsonFile(`${MINIMAL_DIR}${COUNTRIES}.native`, countriesNative)
generateMinimalDataTypings(`${COUNTRIES}.native`, `${COUNTRIES}Native`, 'TCountryToString')

saveJsonFile(`${MINIMAL_DIR}${LANGUAGES}.en`, languagesEn)
generateMinimalDataTypings(`${LANGUAGES}.en`, `${LANGUAGES}En`, 'TLanguageToString')

saveJsonFile(`${MINIMAL_DIR}${LANGUAGES}.native`, languagesNative)
generateMinimalDataTypings(`${LANGUAGES}.native`, `${LANGUAGES}Native`, 'TLanguageToString')
}
9 changes: 7 additions & 2 deletions packages/scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export const getLanguagesInUse = (
})
}
})
console.log('Languages in use:', inUseList.length)
inUseList.sort()

console.log('Languages in use:', inUseList.length, `(${inUseList.join(', ')})\n`)

const languagesInUse: Partial<TLanguages> = {}
inUseList.sort().forEach((lang) => {
inUseList.forEach((lang) => {
languagesInUse[lang] = Object.assign({}, languages[lang])
})

Expand All @@ -49,6 +51,8 @@ export const getLanguagesInUse = (
notInUseList.push(lang)
}
})
notInUseList.sort()

console.log('Unused languages:', notInUseList.length, `(${notInUseList.join(', ')})`)

return languagesInUse
Expand Down Expand Up @@ -87,6 +91,7 @@ export const saveTextFile = (fileName: string, data: string): boolean => {
console.log(
'Saved',
chalk.blue(path.relative('../../', filePath)),
'-',
chalk.bold(prettyBytes(stats.size))
)
return true
Expand Down

0 comments on commit 45690bd

Please sign in to comment.