Permalink
Browse files

refine dir; add unittest; up to 0.1.1

  • Loading branch information...
1 parent 29ca001 commit 7a85d64b43ddf9e3d1b1c2e74cc6f22ae352067b @fengmk2 fengmk2 committed Jun 25, 2012
View
@@ -1,4 +1,6 @@
lib-cov
+coverage.html
+.DS_Store
*.seed
*.log
*.csv
@@ -12,4 +14,4 @@ logs
results
node_modules
-npm-debug.log
+npm-debug.log
View
@@ -0,0 +1,27 @@
+TESTS = test/*.js
+TESTTIMEOUT = 1000
+REPORTER = dot
+
+complie:
+ @python bin/create_emoji_js.py
+
+test: complie
+ @NODE_ENV=test ./node_modules/.bin/mocha -R $(REPORTER) --timeout $(TESTTIMEOUT) $(TESTS)
+
+test-cov: lib-cov
+ @EMOJI_COV=1 $(MAKE) test REPORTER=progress
+ @EMOJI_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
+ @$(MAKE) test-results
+
+test-results:
+ @$(MAKE) test REPORTER=markdown > test_results.md
+
+lib-cov:
+ @rm -rf ./$@
+ @jscoverage lib $@
+
+clean:
+ @rm -rf lib-cov
+ @rm -f coverage.html
+
+.PHONY: complie test test-cov clean lib-cov test-results
View
@@ -1,6 +1,8 @@
emoji
=====
+[![Build Status](https://secure.travis-ci.org/fengmk2/emoji.png)](http://travis-ci.org/fengmk2/emoji)
+
## Usage
### Browser
@@ -21,8 +23,6 @@ $text.html(jEmoji.unifiedToHTML(html));
### Nodejs
-#### Install
-
```sh
$ npm install emoji
```
@@ -32,6 +32,14 @@ var emoji = require('emoji');
console.log('😜', emoji.unifiedToHTML('😜'));
```
+test on nodejs:
+
+```bash
+$ make test
+```
+
+jscoverage: [**98%**](http://fengmk2.github.com/coverage/emoji.html)
+
## Sync data
Source data come from [php-emoji](https://github.com/iamcal/php-emoji).
View
@@ -9,7 +9,7 @@
project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# d = pq(url='https://raw.github.com/iamcal/php-emoji/master/table.htm')
-d = pq(open(project_dir + '/table.htm', 'rb').read())
+d = pq(open(project_dir + '/lib/table.htm', 'rb').read())
tr = d('table tr')
@@ -53,6 +53,6 @@
content += open(project_dir + '/tpl/emoji_footer.js', 'rb').read().decode('utf-8')
-f = open(project_dir + '/emoji.js', 'wb')
+f = open(project_dir + '/lib/emoji.js', 'wb')
f.write(content.encode('utf-8'))
f.close()
View
@@ -1,7 +1,7 @@
#!/bin/sh
-curl -O https://github.com/iamcal/php-emoji/raw/master/iphone_emoji.png
-curl -O https://raw.github.com/iamcal/php-emoji/master/emoji.css
-curl -O https://raw.github.com/iamcal/php-emoji/master/table.htm
+curl -o lib/iphone_emoji.png https://github.com/iamcal/php-emoji/raw/master/iphone_emoji.png
+curl -o lib/emoji.css https://raw.github.com/iamcal/php-emoji/master/emoji.css
+curl -o lib/table.htm https://raw.github.com/iamcal/php-emoji/master/table.htm
python bin/create_emoji_js.py
View
@@ -3,9 +3,9 @@
<head>
<meta charset="utf-8" />
<title>emojs.js demo</title>
- <link href="./emoji.css" rel="stylesheet" type="text/css" />
- <script src="./jquery.js"></script>
- <script src="./emoji.js"></script>
+ <link href="https://raw.github.com/fengmk2/emoji/master/lib/emoji.css" rel="stylesheet" type="text/css" />
+ <script src="http://fengmk2.github.com/js/jquery-1.7.min.js"></script>
+ <script src="https://raw.github.com/fengmk2/emoji/master/lib/emoji.js"></script>
</head>
<body>
<div class="emojstext">
View
@@ -0,0 +1 @@
+module.exports = process.env.EMOJI_COV ? require('./lib-cov/emoji') : require('./lib/emoji');
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,4 +1,4 @@
-.emoji { background: url("iphone_emoji.png") top left no-repeat; width: 20px; height: 20px; display: -moz-inline-stack; display: inline-block; vertical-align: top; zoom: 1; *display: inline; }
+.emoji { background: url("https://github.com/fengmk2/emoji/raw/master/lib/iphone_emoji.png") top left no-repeat; width: 20px; height: 20px; display: -moz-inline-stack; display: inline-block; vertical-align: top; zoom: 1; *display: inline; }
.emoji2600 { background-position: 0px -260px; }
.emoji2601 { background-position: 0px -20px; }
.emoji2614 { background-position: 0px -40px; }
View
@@ -25,7 +25,7 @@ if (typeof module === 'undefined') {
*
* @type {Object}
*/
-var EMOJI_MAP = {
+var EMOJI_MAP = jEmoji.EMOJI_MAP = {
"": ["U+2600", "black sun with rays", "2600", ["", "U+E63E"], ["", "U+E488"], ["", "U+E04A"], ["󾀀", "U+FE000"]],
"": ["U+2601", "cloud", "2601", ["", "U+E63F"], ["", "U+E48D"], ["", "U+E049"], ["󾀁", "U+FE001"]],
"": ["U+2614", "umbrella with rain drops", "2614", ["", "U+E640"], ["", "U+E48C"], ["", "U+E04B"], ["󾀂", "U+FE002"]],
@@ -751,17 +751,31 @@ var EMOJI_MAP = {
"👐": ["U+1F450", "open hands sign", "1f450", ["", "U+E695"], ["", "U+EAD6"], ["", "U+E422"], ["󾮡", "U+FEBA1"]]
};
-var EMOJI_RE = null;
/**
+ * Create map keys rexgep, keys sort by key's length desc.
+ *
+ * @param {Object} map
+ * @return {RegExp}
+ */
+function _createRegexp(map) {
+ var keys = Object.keys(map);
+ keys.sort(function (a, b) {
+ return b.length - a.length;
+ });
+ return new RegExp('(' + keys.join('|') + ')', 'g');
+}
+
+var EMOJI_RE = null;
+/**
* Convert unified code to HTML.
*
* @param {String} text
* @return {String} html with emoji classname.
*/
function unifiedToHTML(text) {
if (!EMOJI_RE) {
- EMOJI_RE = new RegExp('(' + Object.keys(EMOJI_MAP).join('|') + ')', 'g');
+ EMOJI_RE = _createRegexp(EMOJI_MAP);
}
return text.replace(EMOJI_RE, function (_, m) {
var em = EMOJI_MAP[m];
@@ -779,10 +793,12 @@ for (var k in EMOJI_MAP) {
var em = EMOJI_MAP[k];
for (var i = 0; i < _maps.length; i++) {
var index = i + 3;
- if (em[index][0] === '-') {
+ var code = em[index][0];
+ var map = _maps[i];
+ if (code === '-' || map[code]) { // use first code
continue;
}
- _maps[i][em[index][0]] = k;
+ map[code] = k;
}
}
@@ -795,7 +811,7 @@ var EMOJI_DOCOMO_RE = null;
*/
function docomoToUnified(text) {
if (!EMOJI_DOCOMO_RE) {
- EMOJI_DOCOMO_RE = new RegExp('(' + Object.keys(EMOJI_DOCOMO_MAP).join('|') + ')', 'g');
+ EMOJI_DOCOMO_RE = _createRegexp(EMOJI_DOCOMO_MAP);
}
return text.replace(EMOJI_DOCOMO_RE, function (_, m) {
return EMOJI_DOCOMO_MAP[m];
@@ -812,7 +828,7 @@ var EMOJI_KDDI_RE = null;
*/
function kddiToUnified(text) {
if (!EMOJI_KDDI_RE) {
- EMOJI_KDDI_RE = new RegExp('(' + Object.keys(EMOJI_KDDI_MAP).join('|') + ')', 'g')
+ EMOJI_KDDI_RE = _createRegexp(EMOJI_KDDI_MAP);
}
return text.replace(EMOJI_KDDI_RE, function (_, m) {
return EMOJI_KDDI_MAP[m];
@@ -829,7 +845,7 @@ var EMOJI_SOFTBANK_RE = null;
*/
function softbankToUnified(text) {
if (!EMOJI_SOFTBANK_RE) {
- EMOJI_SOFTBANK_RE = new RegExp('(' + Object.keys(EMOJI_SOFTBANK_MAP).join('|') + ')', 'g')
+ EMOJI_SOFTBANK_RE = _createRegexp(EMOJI_SOFTBANK_MAP);
}
return text.replace(EMOJI_SOFTBANK_RE, function (_, m) {
return EMOJI_SOFTBANK_MAP[m];
@@ -846,7 +862,7 @@ var EMOJI_GOOGLE_RE = null;
*/
function googleToUnified(text) {
if (!EMOJI_GOOGLE_RE) {
- EMOJI_GOOGLE_RE = new RegExp('(' + Object.keys(EMOJI_GOOGLE_MAP).join('|') + ')', 'g')
+ EMOJI_GOOGLE_RE = _createRegexp(EMOJI_GOOGLE_MAP);
}
return text.replace(EMOJI_GOOGLE_RE, function (_, m) {
return EMOJI_GOOGLE_MAP[m];
File renamed without changes
File renamed without changes.
View
@@ -2,17 +2,21 @@
"author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com/)",
"name": "emoji",
"description": "This library allows the handling and conversion of Emoji in Javascript.",
- "version": "0.1.0",
+ "version": "0.1.1",
"homepage": "https://github.com/fengmk2/emoji",
"repository": {
"type": "git",
"url": "git://github.com/fengmk2/emoji.git"
},
- "main": "emoji.js",
+ "main": "index.js",
"scripts": {
+ "test": "make test"
},
"dependencies": {},
- "devDependencies": {},
+ "devDependencies": {
+ "mocha": "*",
+ "should": "*"
+ },
"optionalDependencies": {},
"engines": {
"node": "*"
View
@@ -0,0 +1,110 @@
+/*!
+ * emoji - test/emoji.js
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var should = require('should');
+var emoji = require('../');
+
+describe('emoji.js', function () {
+
+ var EMOJI_MAP = emoji.EMOJI_MAP;
+
+ describe('unifiedToHTML()', function () {
+ it('should convert all unified code to html format', function () {
+ for (var key in EMOJI_MAP) {
+ var em = EMOJI_MAP[key];
+ emoji.unifiedToHTML(key).should.equal('<span class="emoji emoji' + em[2] + '" title="' + em[1] + '"></span>');
+ }
+ });
+
+ it('should not covert normal string', function () {
+ var text = 'I am not unified string...';
+ emoji.unifiedToHTML(text).should.equal(text);
+ });
+ });
+
+ describe('docomoToUnified()', function () {
+ it('should convert all DoCoMo code to html format', function () {
+ for (var key in EMOJI_MAP) {
+ var em = EMOJI_MAP[key];
+ if (em[3][0] === '-') {
+ continue;
+ }
+ var unified = emoji.docomoToUnified(em[3][0]);
+ should.ok(unified);
+ should.ok(EMOJI_MAP[unified]);
+ }
+ });
+
+ it('should not covert normal string', function () {
+ var text = 'I am not DoCoMo string...🌑';
+ emoji.docomoToUnified(text).should.equal(text);
+ });
+ });
+
+ describe('kddiToUnified()', function () {
+ it('should convert all KDDI code to html format', function () {
+ for (var key in EMOJI_MAP) {
+ var em = EMOJI_MAP[key];
+ if (em[4][0] === '-') {
+ continue;
+ }
+ var unified = emoji.kddiToUnified(em[4][0]);
+ should.ok(unified);
+ should.ok(EMOJI_MAP[unified]);
+ }
+ });
+
+ it('should not covert normal string', function () {
+ var text = 'I am not KDDI string...🌑';
+ emoji.kddiToUnified(text).should.equal(text);
+ });
+ });
+
+ describe('softbankToUnified()', function () {
+ it('should convert all SoftBank code to html format', function () {
+ for (var key in EMOJI_MAP) {
+ var em = EMOJI_MAP[key];
+ if (em[5][0] === '-') {
+ continue;
+ }
+ var unified = emoji.softbankToUnified(em[5][0]);
+ should.ok(unified);
+ should.ok(EMOJI_MAP[unified]);
+ }
+ });
+
+ it('should not covert normal string', function () {
+ var text = 'I am not KDDI string...🌑';
+ emoji.softbankToUnified(text).should.equal(text);
+ });
+ });
+
+ describe('googleToUnified()', function () {
+ it('should convert all Google code to html format', function () {
+ for (var key in EMOJI_MAP) {
+ var em = EMOJI_MAP[key];
+ if (em[6][0] === '-') {
+ continue;
+ }
+ var unified = emoji.googleToUnified(em[6][0]);
+ should.ok(unified);
+ should.ok(EMOJI_MAP[unified]);
+ }
+ });
+
+ it('should not covert normal string', function () {
+ var text = 'I am not Google string...🌑';
+ emoji.googleToUnified(text).should.equal(text);
+ });
+ });
+
+});
View
@@ -0,0 +1,2 @@
+--require should
+--growl
Oops, something went wrong.

0 comments on commit 7a85d64

Please sign in to comment.