Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 28, 2018
1 parent a04777b commit 5ca31de
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
iso-639-3.js
iso-639-3.min.js
71 changes: 37 additions & 34 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
'use strict';
'use strict'

var fs = require('fs');
var path = require('path');
var https = require('https');
var concat = require('concat-stream');
var unzip = require('unzip');
var dsv = require('d3-dsv');
var bail = require('bail');
var fs = require('fs')
var path = require('path')
var https = require('https')
var concat = require('concat-stream')
var unzip = require('unzip')
var dsv = require('d3-dsv')
var bail = require('bail')

var found;
var found

var SCOPES = {
I: 'individual',
M: 'macrolanguage',
S: 'special'
};
}

var TYPES = {
A: 'ancient',
Expand All @@ -23,48 +23,51 @@ var TYPES = {
H: 'historical',
L: 'living',
S: 'special'
};
}

https
.request('https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3_Code_Tables_20180123.zip', onrequest)
.end();
.request(
'https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3_Code_Tables_20180123.zip',
onrequest
)
.end()

process.on('exit', onexit);
process.on('exit', onexit)

function onexit() {
if (!found) {
throw new Error('Could not find expected file');
throw new Error('Could not find expected file')
}
}

function onrequest(response) {
response
.pipe(new unzip.Parse())
.on('entry', onentry);
response.pipe(new unzip.Parse()).on('entry', onentry)
}

function onentry(entry) {
if (path.basename(entry.path) === 'iso-639-3_20180123.tab') {
found = true;
entry.pipe(concat(onconcat));
found = true
entry.pipe(concat(onconcat))
} else {
entry.autodrain();
entry.autodrain()
}
}

function onconcat(body) {
var data = dsv.tsvParse(String(body)).map(function (language) {
return {
name: language.Ref_Name || null,
type: TYPES[language.Language_Type],
scope: SCOPES[language.Scope],
iso6393: language['Id'], // There’s a `<U+FEFF>`
// in there, I don’t know why, but meh.
iso6392B: language.Part2B || null,
iso6392T: language.Part2T || null,
iso6391: language.Part1 || null
};
});
var data = dsv.tsvParse(String(body)).map(mapper)

fs.writeFile('index.json', JSON.stringify(data, 0, 2) + '\n', bail);
fs.writeFile('index.json', JSON.stringify(data, 0, 2) + '\n', bail)
}

function mapper(language) {
return {
name: language.Ref_Name || null,
type: TYPES[language.Language_Type],
scope: SCOPES[language.Scope],
iso6393: language['Id'], // There’s a `<U+FEFF>`
// in there, I don’t know why, but meh.
iso6392B: language.Part2B || null,
iso6392T: language.Part2T || null,
iso6391: language.Part1 || null
}
}
22 changes: 15 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,32 @@
"concat-stream": "^1.5.1",
"d3-dsv": "^1.0.0",
"esmangle": "^1.0.1",
"prettier": "^1.12.1",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.4.0",
"unzip": "^0.1.11",
"xo": "^0.20.0"
},
"scripts": {
"build-crawl": "node build",
"build-md": "remark . -qfo",
"generate": "node build",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.json --bare -s iso6393 > iso-639-3.js",
"build-mangle": "esmangle iso-639-3.js > iso-639-3.min.js",
"build": "npm run build-crawl && npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"test-api": "node test.js",
"test": "npm run build && npm run lint && npm run test-api"
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test": "npm run generate && npm run format && npm run build && npm run test-api"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ npm install iso-639-3
## Usage

```javascript
var iso6393 = require('iso-639-3');
var iso6393 = require('iso-639-3')

console.log(iso6393.slice(1820, 1825));
console.log(iso6393.slice(1822, 1827))
```

Yields:
Expand Down
34 changes: 17 additions & 17 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
'use strict';
'use strict'

var test = require('tape');
var iso6393 = require('.');
var test = require('tape')
var iso6393 = require('.')

test('iso6393', function (t) {
t.plan(8);
test('iso6393', function(t) {
t.plan(8)

t.ok(Array.isArray(iso6393), 'should be an `array`');
t.ok(Array.isArray(iso6393), 'should be an `array`')

iso6393.forEach(function (language) {
iso6393.forEach(function(language) {
if (language.iso6393 !== 'eng') {
return;
return
}

t.equal(language.iso6393, 'eng', 'should have a 639-3 code');
t.equal(language.iso6392B, 'eng', 'should have a 639-2B code');
t.equal(language.iso6392T, 'eng', 'should have a 639-2T code');
t.equal(language.iso6391, 'en', 'should have a 639-1 code');
t.equal(language.scope, 'individual', 'should have a scope');
t.equal(language.type, 'living', 'should have a type');
t.equal(language.name, 'English', 'should have a name');
});
});
t.equal(language.iso6393, 'eng', 'should have a 639-3 code')
t.equal(language.iso6392B, 'eng', 'should have a 639-2B code')
t.equal(language.iso6392T, 'eng', 'should have a 639-2T code')
t.equal(language.iso6391, 'en', 'should have a 639-1 code')
t.equal(language.scope, 'individual', 'should have a scope')
t.equal(language.type, 'living', 'should have a type')
t.equal(language.name, 'English', 'should have a name')
})
})

0 comments on commit 5ca31de

Please sign in to comment.