Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert project to esm #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ node_modules
package-lock.json
.nyc_output
coverage
dist
20 changes: 9 additions & 11 deletions country-list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

var data = require('./data.json')
import data from './data.json'

/** Precompute name and code lookups. */
var nameMap = {}
Expand All @@ -12,7 +10,7 @@ function mapCodeAndName (country) {
codeMap[country.code.toLowerCase()] = country.name
}

exports.overwrite = function overwrite (countries) {
export function overwrite (countries) {
if (!countries || !countries.length) return
countries.forEach(function (country) {
var foundIndex = data.findIndex(function (item) {
Expand All @@ -23,34 +21,34 @@ exports.overwrite = function overwrite (countries) {
})
}

exports.getCode = function getCode (name) {
export function getCode (name) {
return nameMap[name.toLowerCase()]
}

exports.getName = function getName (code) {
export function getName (code) {
return codeMap[code.toLowerCase()]
}

exports.getNames = function getNames () {
export function getNames () {
return data.map(function (country) {
return country.name
})
}

exports.getCodes = function getCodes () {
export function getCodes () {
return data.map(function (country) {
return country.code
})
}

exports.getCodeList = function getCodeList () {
export function getCodeList () {
return codeMap
}

exports.getNameList = function getNameList () {
export function getNameList () {
return nameMap
}

exports.getData = function getData () {
export function getData () {
return data
}
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
"name": "country-list",
"version": "2.1.1",
"description": "Maps ISO 3166-1-alpha-2 codes to English country names and vice versa.",
"main": "country-list.js",
"main": "dist/country-list.cjs.js",
"module": "dist/country-list.esm.js",
"files": [
"data.json"
"data.json",
"dist"
],
"dependencies": {},
"devDependencies": {
"csv-parser": "^1.11.0",
"rollup": "^1.15.6",
"rollup-plugin-json": "^4.0.0",
"standard": "^12.0.1",
"tap": "^12.0.1"
"tap": "^14.6.1"
},
"scripts": {
"pretest": "standard",
"test": "tap test/*.js --100"
"test": "tap test/*.js --100",
"build": "rollup -c",
"prepublishOnly": "rollup -c"
},
"repository": {
"type": "git",
Expand Down
15 changes: 15 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import json from 'rollup-plugin-json'
import pkg from './package.json'

export default [
{
input: './country-list.js',
output: { file: pkg.main, format: 'cjs' },
plugins: [json()]
},
{
input: './country-list.js',
output: { file: pkg.module, format: 'esm' },
plugins: [json()]
}
]
8 changes: 3 additions & 5 deletions test/get-code-list.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

var { test } = require('tap')
var { getCodeList } = require('../')
var data = require('../data.json')
import { test } from 'tap'
import { getCodeList } from '../country-list.js'
import data from '../data.json'

test('get all country names with code as key', function (t) {
var list = getCodeList()
Expand Down
6 changes: 2 additions & 4 deletions test/get-code.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

var { test } = require('tap')
var { getCode } = require('../')
import { test } from 'tap'
import { getCode } from '../country-list.js'

test('get code from country name', function (t) {
t.equal(getCode('Iceland'), 'IS', 'name "Iceland" should return IS')
Expand Down
8 changes: 3 additions & 5 deletions test/get-codes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

var { test } = require('tap')
var { getCodes } = require('../')
var data = require('../data.json')
import { test } from 'tap'
import { getCodes } from '../country-list.js'
import data from '../data.json'

test('get all country codes', function (t) {
t.equal(getCodes().length, data.length, 'codes should be as many as countries')
Expand Down
8 changes: 3 additions & 5 deletions test/get-data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

var { test } = require('tap')
var { getData } = require('../')
var data = require('../data.json')
import { test } from 'tap'
import { getData } from '../country-list.js'
import data from '../data.json'

test('get all country names', function (t) {
t.equal(getData().length, data.length, 'data list should be the same lenght as data.json')
Expand Down
8 changes: 3 additions & 5 deletions test/get-name-list.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

var { test } = require('tap')
var { getNameList } = require('../')
var data = require('../data.json')
import { test } from 'tap'
import { getNameList } from '../country-list.js'
import data from '../data.json'

test('get all country names with code as key', function (t) {
var list = getNameList()
Expand Down
6 changes: 2 additions & 4 deletions test/get-name.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

var { test } = require('tap')
var { getName } = require('../')
import { test } from 'tap'
import { getName } from '../country-list.js'

test('get name from country code', function (t) {
t.equal(getName('IS'), 'Iceland', 'code "IS" should return Iceland')
Expand Down
8 changes: 3 additions & 5 deletions test/get-names.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

var { test } = require('tap')
var { getNames } = require('../')
var data = require('../data.json')
import { test } from 'tap'
import { getNames } from '../country-list.js'
import data from '../data.json'

test('get all country names', function (t) {
t.equal(getNames().length, data.length, 'country names should be as many as countries')
Expand Down
6 changes: 2 additions & 4 deletions test/import.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const { test } = require('tap')
const { getCode, getName } = require('../')
import { test } from 'tap'
import { getCode, getName } from '../country-list.js'

test('get code and name for country', t => {
t.equal(getCode('Iceland'), 'IS', 'name "Iceland" should return IS')
Expand Down
6 changes: 2 additions & 4 deletions test/overwrite.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const { test } = require('tap')
const { overwrite, getData, getName } = require('../')
import { test } from 'tap'
import { overwrite, getData, getName } from '../country-list.js'

const correctedTW = {
code: 'TW',
Expand Down