Skip to content

Commit

Permalink
Convert project to esm
Browse files Browse the repository at this point in the history
In this diff I migrated the project to esm and provided module field
which is useful for bundlers.

My usecase is rollup. It is able to handle only esm. And this package
requires me to add commonjs and json plugins.

As a side bonus this module can be used in browsers without bundlers
similar to this https://unpkg.com/jss@next?module.

For `tap` I wasn't able to setup esm so I replaced it with `tape`.
  • Loading branch information
TrySound committed Aug 12, 2019
1 parent e1753c5 commit d214c24
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 82 deletions.
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
}
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
"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",
"esm": "^3.2.25",
"rollup": "^1.15.6",
"rollup-plugin-json": "^4.0.0",
"standard": "^12.0.1",
"tap": "^12.0.1"
"tape": "^4.10.2"
},
"scripts": {
"pretest": "standard",
"test": "tap test/*.js --100"
"test": "tape -r esm test/*.js",
"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()]
}
]
10 changes: 4 additions & 6 deletions test/get-code-list.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict'

var { test } = require('tap')
var { getCodeList } = require('../')
var data = require('../data.json')
import { test } from 'tape'
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()

t.type(list, Object, 'country list is an object')
t.ok(list instanceof Object, 'country list is an object')
t.equal(Object.keys(list).length, data.length, 'country list should be as many as countries')
t.equal(list['is'], 'Iceland', 'IS code must be equal to Iceland')
t.end()
Expand Down
20 changes: 9 additions & 11 deletions test/get-code.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict'

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

test('get code from country name', function (t) {
t.equal(getCode('Iceland'), 'IS', 'name "Iceland" should return IS')
t.equal(getCode('iceland'), 'IS', 'name "iceland" should return IS')
t.equal(getCode('ICELAND'), 'IS', 'name "ICELAND" should return IS')
t.type(getCode('Iceland'), 'string', 'type of name "Iceland" is string')
t.type(getCode('IS'), 'undefined', 'type of name "IS" is undefined')
t.type(getCode('FarFarAwayLand'), 'undefined', 'name "FarFarAwayLand" should return undefined')
t.type(getCode(''), 'undefined', 'empty name should return undefined')
t.equal(countries.getCode('Iceland'), 'IS', 'name "Iceland" should return IS')
t.equal(countries.getCode('iceland'), 'IS', 'name "iceland" should return IS')
t.equal(countries.getCode('ICELAND'), 'IS', 'name "ICELAND" should return IS')
t.equal(typeof countries.getCode('Iceland'), 'string', 'type of name "Iceland" is string')
t.equal(typeof countries.getCode('IS'), 'undefined', 'type of name "IS" is undefined')
t.equal(typeof countries.getCode('FarFarAwayLand'), 'undefined', 'name "FarFarAwayLand" should return undefined')
t.equal(typeof countries.getCode(''), 'undefined', 'empty name should return undefined')
t.end()
})
16 changes: 7 additions & 9 deletions test/get-codes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict'

var { test } = require('tap')
var { getCodes } = require('../')
var data = require('../data.json')
import { test } from 'tape'
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')
t.ok(getCodes().indexOf('IS') > -1, 'codes should include IS')
t.ok(getCodes().indexOf('is') === -1, 'codes is case-sensitive, does not include is')
t.type(getCodes(), Array, 'code list is an array')
t.equal(countries.getCodes().length, data.length, 'codes should be as many as countries')
t.ok(countries.getCodes().indexOf('IS') > -1, 'codes should include IS')
t.ok(countries.getCodes().indexOf('is') === -1, 'codes is case-sensitive, does not include is')
t.ok(countries.getCodes() instanceof Array, 'code list is an array')
t.end()
})
14 changes: 6 additions & 8 deletions test/get-data.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

var { test } = require('tap')
var { getData } = require('../')
var data = require('../data.json')
import { test } from 'tape'
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')
t.type(getData(), Array, 'data list is an array')
t.type(getData()[0], Object, 'first item in list should be object')
t.equal(countries.getData().length, data.length, 'data list should be the same lenght as data.json')
t.ok(countries.getData() instanceof Array, 'data list is an array')
t.ok(countries.getData()[0] instanceof Object, 'first item in list should be object')
t.end()
})
10 changes: 4 additions & 6 deletions test/get-name-list.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict'

var { test } = require('tap')
var { getNameList } = require('../')
var data = require('../data.json')
import { test } from 'tape'
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()

t.type(list, Object, 'country name list is an object')
t.ok(list instanceof Object, 'country name list is an object')
t.equal(Object.keys(list).length, data.length, 'country name list should be as many as countries')
t.equal(list['iceland'], 'IS', 'Iceland must be equal to IS code')
t.end()
Expand Down
18 changes: 8 additions & 10 deletions test/get-name.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict'

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

test('get name from country code', function (t) {
t.equal(getName('IS'), 'Iceland', 'code "IS" should return Iceland')
t.equal(getName('is'), 'Iceland', 'code "is" should return Iceland')
t.type(getName('IS'), 'string', 'type of code "IS" is string')
t.type(getName('Iceland'), 'undefined', 'code "Iceland" should return undefined')
t.type(getName('XX'), 'undefined', 'code "XX" should return undefined')
t.type(getName(''), 'undefined', 'empty code should return undefined')
t.equal(countries.getName('IS'), 'Iceland', 'code "IS" should return Iceland')
t.equal(countries.getName('is'), 'Iceland', 'code "is" should return Iceland')
t.equal(typeof countries.getName('IS'), 'string', 'type of code "IS" is string')
t.equal(typeof countries.getName('Iceland'), 'undefined', 'code "Iceland" should return undefined')
t.equal(typeof countries.getName('XX'), 'undefined', 'code "XX" should return undefined')
t.equal(typeof countries.getName(''), 'undefined', 'empty code should return undefined')
t.end()
})
16 changes: 7 additions & 9 deletions test/get-names.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict'

var { test } = require('tap')
var { getNames } = require('../')
var data = require('../data.json')
import { test } from 'tape'
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')
t.ok(getNames().indexOf('Iceland') > -1, 'country names should include Iceland')
t.ok(getNames().indexOf('iceland') === -1, 'country names is case-sensitive, does not include iceland')
t.type(getNames(), Array, 'name list is an array')
t.equal(countries.getNames().length, data.length, 'country names should be as many as countries')
t.ok(countries.getNames().indexOf('Iceland') > -1, 'country names should include Iceland')
t.ok(countries.getNames().indexOf('iceland') === -1, 'country names is case-sensitive, does not include iceland')
t.ok(countries.getNames() instanceof Array, 'name list is an array')
t.end()
})
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 'tape'
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 'tape'
import { overwrite, getData, getName } from '../country-list.js'

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

0 comments on commit d214c24

Please sign in to comment.