Skip to content

Commit e10f78f

Browse files
committed
lint code
1 parent f87d3b2 commit e10f78f

File tree

7 files changed

+213
-204
lines changed

7 files changed

+213
-204
lines changed

.eslintrc.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
{
2-
"extends": "airbnb-base"
2+
"extends": "airbnb-base",
3+
"overrides": [
4+
{
5+
"files": [
6+
"test/**/*.js"
7+
],
8+
"env": {
9+
"mocha": true
10+
}
11+
}
12+
]
313
}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ before_install:
55
- yarn install
66
script:
77
- yarn test
8-
- yarn audit
98
- yarn lint
9+
- yarn audit

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ bc.betaCodeToGreek('mh=nin a)/eide qea\\ *phlhi+a/dew *)axilh=os');
4747

4848
<body>
4949
<script>
50-
console.log(greekToBetaCode('χαῖρε ὦ κόσμε'));
50+
console.log(BetaCode.greekToBetaCode('χαῖρε ὦ κόσμε'));
5151
// => 'xai=re w)= ko/sme'
5252
53-
console.log(betaCodeToGreek('mh=nin a)/eide qea\\ *phlhi+a/dew *)axilh=os'));
53+
console.log(BetaCode.betaCodeToGreek('mh=nin a)/eide qea\\ *phlhi+a/dew *)axilh=os'));
5454
// => 'μῆνιν ἄειδε θεὰ Πηληϊάδεω Ἀχιλῆος'
5555
</script>
5656
</body>

bundle/beta-code.js

Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,17 @@
1-
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2-
(function () {
3-
'use strict';
1+
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.BetaCode = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2+
const betaCodeToUnicode = require('./vendor/beta-code-json/beta_code_to_unicode.json');
3+
const unicodeToBetaCode = require('./vendor/beta-code-json/unicode_to_beta_code.json');
44

5-
var beta_code_to_unicode = require('./vendor/beta-code-json/beta_code_to_unicode.json');
6-
var unicode_to_beta_code = require('./vendor/beta-code-json/unicode_to_beta_code.json');
7-
var max_beta_code_character_length = _longestKeyLength(beta_code_to_unicode);
5+
function longestKeyLength(obj) {
6+
const keys = Object.keys(obj);
7+
let ii;
8+
let key;
9+
let length = 0;
810

9-
function greekToBetaCode (greek) {
10-
var greek_characters = _normalize(greek).split('');
11-
var beta_code_characters = [];
12-
var current_character, ii;
11+
for (ii = 0; ii < keys.length; ii += 1) {
12+
key = keys[ii];
1313

14-
for (ii = 0; ii < greek_characters.length; ii++) {
15-
current_character = greek_characters[ii];
16-
17-
if (unicode_to_beta_code.hasOwnProperty(current_character)) {
18-
beta_code_characters.push(unicode_to_beta_code[current_character]);
19-
} else {
20-
beta_code_characters.push(current_character);
21-
}
22-
}
23-
24-
return beta_code_characters.join('');
25-
}
26-
27-
function betaCodeToGreek (beta_code) {
28-
var beta_code_characters = _normalize(beta_code).split('');
29-
var greek_characters = [];
30-
var start = 0;
31-
var end, slice, new_start, current_character, max_length;
32-
33-
while (start <= beta_code_characters.length) {
34-
current_character = beta_code_characters[start];
35-
new_start = start + 1;
36-
max_length = _min(beta_code_characters.length, start + max_beta_code_character_length);
37-
38-
// match the longest possible substring that's valid beta code, from left to right
39-
// for example 'e)' is valid beta code (ἐ) but 'e)/' is also valid beta code (ἕ)
40-
// the string 'e)/' should be interpreted as 'e)/' and not as 'e)' + '/'
41-
for (end = new_start; end <= max_length; end++) {
42-
slice = beta_code_characters.slice(start, end).join('');
43-
44-
if (beta_code_to_unicode.hasOwnProperty(slice)) {
45-
current_character = beta_code_to_unicode[slice];
46-
new_start = end;
47-
}
48-
}
49-
50-
greek_characters.push(current_character);
51-
start = new_start;
52-
}
53-
54-
return _sigmaToEndOfWordSigma(greek_characters.join(''));
55-
}
56-
57-
module.exports = {
58-
greekToBetaCode: greekToBetaCode,
59-
betaCodeToGreek: betaCodeToGreek
60-
};
61-
62-
if (typeof window !== 'undefined') {
63-
window.greekToBetaCode = greekToBetaCode;
64-
window.betaCodeToGreek = betaCodeToGreek;
65-
}
66-
67-
function _longestKeyLength (obj) {
68-
var key;
69-
var length = 0;
70-
71-
for (key in obj) {
72-
if (obj.hasOwnProperty(key)) {
14+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
7315
if (key.length > length) {
7416
length = key.length;
7517
}
@@ -84,22 +26,81 @@ function _longestKeyLength (obj) {
8426
// - followed by whitespace
8527
// - followed by a punctuation character
8628
// REGEX NOTE: word boundary \b doesn't work well with unicode
87-
function _sigmaToEndOfWordSigma (string) {
29+
function sigmaToEndOfWordSigma(string) {
8830
return string.replace(/σ(?=[,.:;·\s]|$)/g, 'ς');
8931
}
9032

91-
function _min (a, b) {
33+
function min(a, b) {
9234
return a < b ? a : b;
9335
}
9436

95-
function _normalize (string) {
37+
function normalize(string) {
9638
if (string.normalize) {
9739
return string.normalize();
9840
}
9941

10042
return string;
10143
}
102-
})();
44+
45+
const maxBetaCodeCharacterLength = longestKeyLength(betaCodeToUnicode);
46+
47+
function greekToBetaCode(greek) {
48+
const greekCharacters = normalize(greek).split('');
49+
const betaCodeCharacters = [];
50+
let currentCharacter;
51+
let ii;
52+
53+
for (ii = 0; ii < greekCharacters.length; ii += 1) {
54+
currentCharacter = greekCharacters[ii];
55+
56+
if (Object.prototype.hasOwnProperty.call(unicodeToBetaCode, currentCharacter)) {
57+
betaCodeCharacters.push(unicodeToBetaCode[currentCharacter]);
58+
} else {
59+
betaCodeCharacters.push(currentCharacter);
60+
}
61+
}
62+
63+
return betaCodeCharacters.join('');
64+
}
65+
66+
function betaCodeToGreek(betaCode) {
67+
const betaCodeCharacters = normalize(betaCode).split('');
68+
const greekCharacters = [];
69+
let start = 0;
70+
let end;
71+
let slice;
72+
let newStart;
73+
let currentCharacter;
74+
let maxLength;
75+
76+
while (start <= betaCodeCharacters.length) {
77+
currentCharacter = betaCodeCharacters[start];
78+
newStart = start + 1;
79+
maxLength = min(betaCodeCharacters.length, start + maxBetaCodeCharacterLength);
80+
81+
// match the longest possible substring that's valid beta code, from left to right
82+
// for example 'e)' is valid beta code (ἐ) but 'e)/' is also valid beta code (ἕ)
83+
// the string 'e)/' should be interpreted as 'e)/' and not as 'e)' + '/'
84+
for (end = newStart; end <= maxLength; end += 1) {
85+
slice = betaCodeCharacters.slice(start, end).join('');
86+
87+
if (Object.prototype.hasOwnProperty.call(betaCodeToUnicode, slice)) {
88+
currentCharacter = betaCodeToUnicode[slice];
89+
newStart = end;
90+
}
91+
}
92+
93+
greekCharacters.push(currentCharacter);
94+
start = newStart;
95+
}
96+
97+
return sigmaToEndOfWordSigma(greekCharacters.join(''));
98+
}
99+
100+
module.exports = {
101+
greekToBetaCode,
102+
betaCodeToGreek,
103+
};
103104

104105
},{"./vendor/beta-code-json/beta_code_to_unicode.json":2,"./vendor/beta-code-json/unicode_to_beta_code.json":3}],2:[function(require,module,exports){
105106
module.exports={
@@ -1110,4 +1111,5 @@ module.exports={
11101111
"—": "_"
11111112
}
11121113

1113-
},{}]},{},[1]);
1114+
},{}]},{},[1])(1)
1115+
});

index.js

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,16 @@
1-
(function () {
2-
'use strict';
1+
const betaCodeToUnicode = require('./vendor/beta-code-json/beta_code_to_unicode.json');
2+
const unicodeToBetaCode = require('./vendor/beta-code-json/unicode_to_beta_code.json');
33

4-
var beta_code_to_unicode = require('./vendor/beta-code-json/beta_code_to_unicode.json');
5-
var unicode_to_beta_code = require('./vendor/beta-code-json/unicode_to_beta_code.json');
6-
var max_beta_code_character_length = _longestKeyLength(beta_code_to_unicode);
4+
function longestKeyLength(obj) {
5+
const keys = Object.keys(obj);
6+
let ii;
7+
let key;
8+
let length = 0;
79

8-
function greekToBetaCode (greek) {
9-
var greek_characters = _normalize(greek).split('');
10-
var beta_code_characters = [];
11-
var current_character, ii;
10+
for (ii = 0; ii < keys.length; ii += 1) {
11+
key = keys[ii];
1212

13-
for (ii = 0; ii < greek_characters.length; ii++) {
14-
current_character = greek_characters[ii];
15-
16-
if (unicode_to_beta_code.hasOwnProperty(current_character)) {
17-
beta_code_characters.push(unicode_to_beta_code[current_character]);
18-
} else {
19-
beta_code_characters.push(current_character);
20-
}
21-
}
22-
23-
return beta_code_characters.join('');
24-
}
25-
26-
function betaCodeToGreek (beta_code) {
27-
var beta_code_characters = _normalize(beta_code).split('');
28-
var greek_characters = [];
29-
var start = 0;
30-
var end, slice, new_start, current_character, max_length;
31-
32-
while (start <= beta_code_characters.length) {
33-
current_character = beta_code_characters[start];
34-
new_start = start + 1;
35-
max_length = _min(beta_code_characters.length, start + max_beta_code_character_length);
36-
37-
// match the longest possible substring that's valid beta code, from left to right
38-
// for example 'e)' is valid beta code (ἐ) but 'e)/' is also valid beta code (ἕ)
39-
// the string 'e)/' should be interpreted as 'e)/' and not as 'e)' + '/'
40-
for (end = new_start; end <= max_length; end++) {
41-
slice = beta_code_characters.slice(start, end).join('');
42-
43-
if (beta_code_to_unicode.hasOwnProperty(slice)) {
44-
current_character = beta_code_to_unicode[slice];
45-
new_start = end;
46-
}
47-
}
48-
49-
greek_characters.push(current_character);
50-
start = new_start;
51-
}
52-
53-
return _sigmaToEndOfWordSigma(greek_characters.join(''));
54-
}
55-
56-
module.exports = {
57-
greekToBetaCode: greekToBetaCode,
58-
betaCodeToGreek: betaCodeToGreek
59-
};
60-
61-
if (typeof window !== 'undefined') {
62-
window.greekToBetaCode = greekToBetaCode;
63-
window.betaCodeToGreek = betaCodeToGreek;
64-
}
65-
66-
function _longestKeyLength (obj) {
67-
var key;
68-
var length = 0;
69-
70-
for (key in obj) {
71-
if (obj.hasOwnProperty(key)) {
13+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
7214
if (key.length > length) {
7315
length = key.length;
7416
}
@@ -83,19 +25,78 @@ function _longestKeyLength (obj) {
8325
// - followed by whitespace
8426
// - followed by a punctuation character
8527
// REGEX NOTE: word boundary \b doesn't work well with unicode
86-
function _sigmaToEndOfWordSigma (string) {
28+
function sigmaToEndOfWordSigma(string) {
8729
return string.replace(/σ(?=[,.:;·\s]|$)/g, 'ς');
8830
}
8931

90-
function _min (a, b) {
32+
function min(a, b) {
9133
return a < b ? a : b;
9234
}
9335

94-
function _normalize (string) {
36+
function normalize(string) {
9537
if (string.normalize) {
9638
return string.normalize();
9739
}
9840

9941
return string;
10042
}
101-
})();
43+
44+
const maxBetaCodeCharacterLength = longestKeyLength(betaCodeToUnicode);
45+
46+
function greekToBetaCode(greek) {
47+
const greekCharacters = normalize(greek).split('');
48+
const betaCodeCharacters = [];
49+
let currentCharacter;
50+
let ii;
51+
52+
for (ii = 0; ii < greekCharacters.length; ii += 1) {
53+
currentCharacter = greekCharacters[ii];
54+
55+
if (Object.prototype.hasOwnProperty.call(unicodeToBetaCode, currentCharacter)) {
56+
betaCodeCharacters.push(unicodeToBetaCode[currentCharacter]);
57+
} else {
58+
betaCodeCharacters.push(currentCharacter);
59+
}
60+
}
61+
62+
return betaCodeCharacters.join('');
63+
}
64+
65+
function betaCodeToGreek(betaCode) {
66+
const betaCodeCharacters = normalize(betaCode).split('');
67+
const greekCharacters = [];
68+
let start = 0;
69+
let end;
70+
let slice;
71+
let newStart;
72+
let currentCharacter;
73+
let maxLength;
74+
75+
while (start <= betaCodeCharacters.length) {
76+
currentCharacter = betaCodeCharacters[start];
77+
newStart = start + 1;
78+
maxLength = min(betaCodeCharacters.length, start + maxBetaCodeCharacterLength);
79+
80+
// match the longest possible substring that's valid beta code, from left to right
81+
// for example 'e)' is valid beta code (ἐ) but 'e)/' is also valid beta code (ἕ)
82+
// the string 'e)/' should be interpreted as 'e)/' and not as 'e)' + '/'
83+
for (end = newStart; end <= maxLength; end += 1) {
84+
slice = betaCodeCharacters.slice(start, end).join('');
85+
86+
if (Object.prototype.hasOwnProperty.call(betaCodeToUnicode, slice)) {
87+
currentCharacter = betaCodeToUnicode[slice];
88+
newStart = end;
89+
}
90+
}
91+
92+
greekCharacters.push(currentCharacter);
93+
start = newStart;
94+
}
95+
96+
return sigmaToEndOfWordSigma(greekCharacters.join(''));
97+
}
98+
99+
module.exports = {
100+
greekToBetaCode,
101+
betaCodeToGreek,
102+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
},
1313
"homepage": "https://github.com/perseids-tools/beta-code-js",
1414
"scripts": {
15-
"test": "mocha --reporter nyan",
16-
"bundle": "browserify index.js > bundle/beta-code.js",
15+
"test": "mocha --reporter dot",
16+
"bundle": "browserify --standalone BetaCode index.js > bundle/beta-code.js",
1717
"lint": "eslint --ext=js --ext=jsx ."
1818
},
1919
"keywords": [

0 commit comments

Comments
 (0)