Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
feature(es6): es6-ifies code
Browse files Browse the repository at this point in the history
and tests, and tests don't break

closes #30
  • Loading branch information
edm00se committed Feb 11, 2020
1 parent 408f181 commit ac37991
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 54 deletions.
6 changes: 3 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var transmogrifier = require('emoji-transmogrifier');
const transmogrifier = require('emoji-transmogrifier');

var beerEmojiUrl = transmogrifier.getImage('beer');
const beerEmojiUrl = transmogrifier.getImage('beer');
console.log('the url of the GitHub emoji image for beer is: ' + beerEmojiUrl);

var beerUniStr = transmogrifier.getUnicode('beer');
const beerUniStr = transmogrifier.getUnicode('beer');
console.log('the unicode string for beer is: ' + beerUniStr);

console.log('beers: ' + String.fromCodePoint(beerUniStr));
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"example.js"
],
"engines": {
"node": ">=4.5.0",
"npm": ">=3.0.0"
"node": ">=10.13.0",
"npm": ">=6.0.0"
},
"main": "./src/server/index.js",
"bin": "./src/cli/index.js",
Expand Down
33 changes: 13 additions & 20 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
'use strict';

var emojisOb = require('../assets/emojis.json');
const emojisOb = require('../assets/emojis.json');
/* RegEx pattern, https://regex101.com/r/hI5qF5/1
* `g` removed, as RegExp in JS is stateful,
* src: http://bjorn.tipling.com/state-and-regular-expressions-in-javascript
*/
var re = /(\:\w+\:)(?=\s|[\!\.\?]|$)/im; // eslint-disable-line no-useless-escape
const re = /(\:\w+\:)(?=\s|[\!\.\?]|$)/im; // eslint-disable-line no-useless-escape
// GitHub API for emoji mapping short name to URL of image
// var emojisUrl = "https://api.github.com/emojis";
var UNI_PREFIX = '0x';
const UNI_PREFIX = '0x';

const getImg = key => emojisOb[key];

// String.prototype.includes is nice, but...
const isRange = val => val.indexOf('-') !== -1;

const arrayMapUnicodePrefix = ar => ar.map(val => UNI_PREFIX + val);

function getImg(key) {
return emojisOb[key];
}
function isRange(val) {
// String.prototype.includes is nice, but...
return val.indexOf('-') !== -1;
}
function arrayMapUnicodePrefix(ar) {
var nw = [];
ar.forEach(function(val) {
nw.push(UNI_PREFIX + val);
});
return nw;
}
function extractEmojiUnicodeFromUrl(val) {
var nwVal;
var tmp = val
let nwVal;
const tmp = val
.split('unicode/')[1]
.split('?')[0]
.split('.png')[0];
if (isRange(tmp)) {
var ar = tmp.split('-');
const ar = tmp.split('-');
nwVal = [ar[0], ar[1]];
} else {
nwVal = tmp;
Expand Down
18 changes: 9 additions & 9 deletions src/api/index.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

var assert = require('assert');
var transmogrify = require('.');
var arEq = require('array-equal');
var verSuffix = '?v8';
const assert = require('assert');
const transmogrify = require('.');
const arEq = require('array-equal');
const verSuffix = '?v8';

describe('api', function() {
it('should get image urls from short code', function() {
describe('api', () => {
it('should get image urls from short code', () => {
assert.equal(
transmogrify.getImage('beers'),
'https://assets-cdn.github.com/images/icons/emoji/unicode/1f37b.png' +
Expand All @@ -20,21 +20,21 @@ describe('api', function() {
'post_office == correct GH url'
);
});
it('should handle unicode conversion from short code', function() {
it('should handle unicode conversion from short code', () => {
assert.equal(
true,
transmogrify.getUnicode('beers') === '0x1f37b',
'beers code is 0x1f37b'
);
});
it('should return null as unicode for bad input', function() {
it('should return null as unicode for bad input', () => {
assert.equal(
true,
transmogrify.getUnicode('i am not a valid emoji') === null,
'bad input returns null unicode'
);
});
it('should have tests for the cli component', function() {
it('should have tests for the cli component', () => {
assert(
true,
"but let's face it, it assumes defaults for all options and just runs"
Expand Down
40 changes: 20 additions & 20 deletions src/cli/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var updateNotifier = require('update-notifier');
var pkg = require('../../package.json');
var prog = require('commander');
var fs = require('fs');
var transmogrify = require('../api');
var re = transmogrify.theRegex;
var emojisOb = require('../assets/emojis.json');
var globber = require('glob');
const updateNotifier = require('update-notifier');
const pkg = require('../../package.json');
const prog = require('commander');
const fs = require('fs');
const transmogrify = require('../api');
const re = transmogrify.theRegex;
const emojisOb = require('../assets/emojis.json');
const globber = require('glob');
module.exports = function() {
updateNotifier({ pkg }).notify(); /* eslint-disable-line */
prog.version(pkg.version).usage('<cmd>');
Expand All @@ -14,32 +14,32 @@ module.exports = function() {
.command('zap')
.usage('<glob>')
.description('convert emoji short codes in specified files to image tags')
.action(function(glob) {
var myGlob = '**/*.md';
.action(glob => {
const myGlob = '**/*.md';
if (typeof glob === 'string') {
myGlob = glob;
}
console.log('glob: ' + myGlob);
globber(myGlob, function(er, files) {
globber(myGlob, (er, files) => {
if (er) {
console.log('error: ' + er);
}
console.log('scanning ' + files);
if (files.length < 1) {
console.log('no files found, matching ' + myGlob);
} else {
files.forEach(function(curVal) {
fs.readFile(curVal, 'utf-8', function(err, data) {
files.forEach(curVal => {
fs.readFile(curVal, 'utf-8', (err, data) => {
if (err) {
console.log(' error: ' + err);
}
if (re.test(data)) {
var foundMatch = false;
for (var prop in emojisOb) {
let foundMatch = false;
for (let prop in emojisOb) {
if (data.indexOf(':' + prop + ':') > -1) {
foundMatch = true;
var nwRe = new RegExp(':' + prop + ':', 'gim');
var url = transmogrify.getImage(prop);
const nwRe = new RegExp(':' + prop + ':', 'gim');
const url = transmogrify.getImage(prop);
data = data.replace(
nwRe,
'<img src=' +
Expand All @@ -51,7 +51,7 @@ module.exports = function() {
}
}
if (foundMatch) {
fs.writeFile(curVal, data, 'utf-8', function(writeErr) {
fs.writeFile(curVal, data, 'utf-8', writeErr => {
if (writeErr) {
console.log(writeErr);
}
Expand All @@ -71,7 +71,7 @@ module.exports = function() {
.description(
'returns the unicode interpretation of the given emoji short code'
)
.action(function(shortCode) {
.action(shortCode => {
const uniCodeStr = transmogrify.getUnicode(shortCode);
if (Array.isArray(uniCodeStr)) {
console.log(String.fromCodePoint(...uniCodeStr));
Expand All @@ -85,7 +85,7 @@ module.exports = function() {
.alias('href')
.usage('<shortCode>')
.description('returns the GitHub url of the given emoji by short code')
.action(function(shortCode) {
.action(shortCode => {
console.log(transmogrify.getImage(shortCode));
});

Expand Down

0 comments on commit ac37991

Please sign in to comment.