Skip to content

Commit

Permalink
Convert to TypeScript (wojtekmaj#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj committed Jan 9, 2023
1 parent 60376bb commit 7e36e7e
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 186 deletions.
7 changes: 1 addition & 6 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{
"presets": ["@babel/env"],
"env": {
"production-esm": {
"presets": [["@babel/env", { "modules": false }]]
}
}
"presets": ["@babel/typescript", "@babel/env"]
}
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"extends": "wojtekmaj"
"extends": [
"wojtekmaj",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"]
}
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
"description": "A function that returns a flag emoji given IETF language tag.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"source": "src/index.js",
"source": "src/index.ts",
"types": "src/index.ts",
"sideEffects": false,
"scripts": {
"build": "yarn build-esm && yarn build-cjs",
"build-esm": "BABEL_ENV=production-esm babel src -d dist/esm --ignore \"**/*.spec.js\"",
"build-cjs": "BABEL_ENV=production-cjs babel src -d dist/cjs --ignore \"**/*.spec.js\"",
"build-esm": "tsc --project tsconfig.build.json --outDir dist/esm --module esnext",
"build-cjs": "tsc --project tsconfig.build.json --outDir dist/cjs --module commonjs",
"clean": "rimraf dist",
"jest": "jest",
"lint": "eslint src",
"lint": "eslint src --ext .js,.ts",
"postinstall": "husky install",
"prepack": "yarn clean && yarn build",
"prettier": "prettier --check . --cache",
"test": "yarn lint && yarn prettier && yarn jest"
"test": "yarn lint && yarn tsc && yarn prettier && yarn jest",
"tsc": "tsc --noEmit"
},
"keywords": [
"emoji"
Expand All @@ -27,16 +29,20 @@
},
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.15.0",
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@babel/preset-typescript": "^7.18.6",
"@types/jest": "^29.0.0",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.44.0",
"eslint": "^8.26.0",
"eslint-config-wojtekmaj": "^0.7.1",
"husky": "^8.0.0",
"jest": "^29.0.0",
"prettier": "^2.7.0",
"pretty-quick": "^3.1.0",
"rimraf": "^3.0.0"
"rimraf": "^3.0.0",
"typescript": "^4.9.4"
},
"resolutions": {
"semver@7.0.0": "^7.0.0"
Expand Down
18 changes: 0 additions & 18 deletions src/index.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/index.spec.js → src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import countryCodeToFlagEmoji from './index';

describe('countryCodeToFlagEmoji', () => {
it('returns nothing given nothing', () => {
// @ts-expect-error-next-line
const result = countryCodeToFlagEmoji();

expect(result).toBe();
expect(result).toBe(null);
});

it('returns proper emoji from a full IETF language tag', () => {
Expand Down
20 changes: 20 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Translates 'a' to '🇦', 'b' to '🇧' and so on.
function letterToLetterEmoji(letter: string): string {
return String.fromCodePoint(letter.toLowerCase().charCodeAt(0) + 127365);
}

// Translates 'pl' to 'PL', 'en-US' to 'US' and so on.
function countryCodeToCountry(countryCode: string): string {
const country = countryCode.split('-').pop() as string;

return country.toUpperCase();
}

// Translates 'pl-PL' to 🇵🇱 and so on.
export default function countryCodeToFlagEmoji(string: string): string | null {
if (!string) {
return null;
}

return Array.from(countryCodeToCountry(string)).map(letterToLetterEmoji).join('');
}
4 changes: 4 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["src/**/*.spec.ts"]
}
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"declaration": true,
"esModuleInterop": true,
"isolatedModules": true,
"moduleResolution": "node",
"outDir": "dist",
"strict": true
},
"include": ["src"]
}
Loading

0 comments on commit 7e36e7e

Please sign in to comment.