Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 08d9ef79234a1e6b1e958b57c9cc2f7d4253ed4c @rauchg rauchg committed Oct 15, 2012
Showing with 140 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +9 −0 Makefile
  3. +42 −0 Readme.md
  4. +11 −0 component.json
  5. +42 −0 index.js
  6. +13 −0 package.json
  7. +22 −0 test/directionality.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,9 @@
+
+REPORTER = dot
+
+test:
+ @./node_modules/.bin/mocha \
+ --reporter $(REPORTER) \
+ --bail
+
+.PHONY: test
@@ -0,0 +1,42 @@
+
+# directionality
+
+ Determines the writing system's
+ [directionality](http://en.wikipedia.org/wiki/Writing_system#Directionality) based on characters tests.
+
+## Installation
+
+```
+$ component install component/directionality
+```
+
+## Example
+
+```js
+var dir = require('directionality');
+dir('الولايات المتحدة الأمريكية'); // 'right'
+dir('Hello world'); // 'left'
+```
+
+## Method
+
+The decision is made based on the first character of the input string.
+
+## API
+
+### directionality(str)
+
+ Returns `'rtl'` for right-to-left text, and `'ltr'` for
+ left-to-right text.
+
+### directionality.regex.ltr
+
+ Access the regular expression of ltr charecters.
@TooTallNate

TooTallNate Oct 15, 2012

Owner

s/charecters/characters

+
+### directionality.regex.rtl
+
+ Access the regular expression of rtl charecters.
+
+## License
+
+MIT
@@ -0,0 +1,11 @@
+{
+ "name": "directionality",
+ "version": "0.1.0",
+ "devDependencies": {
+ "visionmedia/mocha": "*",
+ "guille/expect.js": "*"
+ },
+ "scripts": [
+ "index.js"
+ ]
+}
@@ -0,0 +1,42 @@
+
+/*!
+ * Regular expressions from `google.i18n.bidi`.
+ *
+ * @see https://code.google.com/p/closure-library/
+ * @license Apache 2.0
+ * @copyright 2007 The Closure Library Authors
+ */
+
+/**
+ * Module exports.
+ */
+
+module.exports = exports = check;
+
+/**
+ * Character ranges definition.
+ */
+
+var ltr = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-' +
+ '\u0590\u0800-\u1FFF\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF';
+
+var rtl = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC';
+
+/**
+ * Regular expressions.
+ */
+
+exports.regex = {};
+exports.regex.ltr = new RegExp('^[^' + rtl + ']*[' + ltr + ']');
+exports.regex.rtl = new RegExp('^[^' + ltr + ']*[' + rtl +' ]');
+
+/**
+ * Finds out the writing system directionality.
+ *
+ * @param {String} text
+ * @return {String} `left` (ltr) or `right` (rtl)
+ */
+
+function check(str){
+ return exports.regex.rtl.test(str) ? 'rtl' : 'ltr';
+}
@@ -0,0 +1,13 @@
+{
+ "name": "directionality",
+ "version": "0.1.0",
+ "devDependencies": {
+ "mocha": "*",
+ "expect.js": "*"
+ },
+ "component": {
+ "scripts": {
+ "directionality/index.js": "index.js"
+ }
+ }
+}
@@ -0,0 +1,22 @@
+
+/* dependencies */
+
+var dir = require('..')
+ , expect = require('expect.js');
+
+/* test */
+
+describe('directonality', function(){
+ it('should detect ltr', function(){
+ expect(dir('hello world')).to.be('ltr');
+ });
+
+ it('should detect rtl', function(){
+ expect(dir('الولايات')).to.be('rtl');
+ });
+
+ it('should expose regexps', function(){
+ expect(dir.regex.ltr.test).to.be.a('function');
+ expect(dir.regex.rtl.test).to.be.a('function');
+ });
+});

0 comments on commit 08d9ef7

Please sign in to comment.