Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

persianjs jQuery Plugin #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
275 changes: 275 additions & 0 deletions jquery.persian.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
/**
* PersianJs v0.1.0
* https://github.com/usablica/persian.js
* MIT licensed
*
* Copyright (C) 2013 usabli.ca and other contributors
*/
(function () {

//Default config/variables
var VERSION = "0.1.0",
//Check for nodeJS
hasModule = (typeof module !== 'undefined' && module.exports);

/**
* PersianJs main class
*
* @class PersianJs
*/
function PersianJs(str) {
this._str = str;
}

/**
* Used for convert Arabic characters to Persian
*
* @method _toPersianChar
* @param {String} value
* @return {String} Returns Converted string
*/
function _toPersianChar(value) {
if (!value) {
return;
}
var arabicChars = ["ي", "ك", "‍", "دِ", "بِ", "زِ", "ذِ", "ِشِ", "ِسِ", "‌", "ى"],
persianChars = ["ی", "ک", "", "د", "ب", "ز", "ذ", "ش", "س", "", "ی"];

for (var i = 0, charsLen = arabicChars.length; i < charsLen; i++) {
value = value.replace(new RegExp(arabicChars[i], "g"), persianChars[i]);
}
return value;
}

/**
* Used for Change keyboard layout
*
* @method _switchKey
* @param {String} value
* @return {String} Returns Converted char
*/
function _switchKey(value) {
if (!value) {
return;
}
var persianChar = [ "ض", "ص", "ث", "ق", "ف", "غ", "ع", "ه", "خ", "ح", "ج", "چ", "ش", "س", "ی", "ب", "ل", "ا", "ت", "ن", "م", "ک", "گ", "ظ", "ط", "ز", "ر", "ذ", "د", "پ", "و","؟" ],
englishChar = [ "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "z", "x", "c", "v", "b", "n", "m", ",","?" ];

for (var i = 0, charsLen = persianChar.length; i < charsLen; i++) {
value = value.replace(new RegExp(persianChar[i], "g"), englishChar[i]);
}
return value;
}

/**
* Used for convert Arabic numbers to Persian
*
* @method _toPersianNumber
* @param {String} value
* @return {String} Returns Converted numbers
*/
function _toPersianNumber(value) {
if (!value) {
return;
}
var arabicNumbers = ["١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩", "٠"],
persianNumbers = ["۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "۰"];

for (var i = 0, numbersLen = arabicNumbers.length; i < numbersLen; i++) {
value = value.replace(new RegExp(arabicNumbers[i], "g"), persianNumbers[i]);
}
return value;
}

/**
* Used for convert English numbers to Persian
* @method _englishNumber
* @param {String} value
* @return {String} Returns Converted numbers
*/
function _englishNumber(value) {
if (!value) {
return;
}
var englishNumbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
persianNumbers = ["۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "۰"];

for (var i = 0, numbersLen = englishNumbers.length; i < numbersLen; i++) {
value = value.replace(new RegExp(englishNumbers[i], "g"), persianNumbers[i]);
}
return value;
}

/**
* Used for fix Persian Charachters in URL
* https://fa.wikipedia.org/wiki/مدیاویکی:Gadget-Extra-Editbuttons-Functions.js
*
* @param {String} value
* @return {String} Returns fixed URL
* @api private
*/
function _fixURL(value) {
if (!value) {
return;
}
// Replace every %20 with _ to protect them from decodeURI
var old = "";
while (old != value) {
old = value;
value = value.replace(/(http\S+?)\%20/g, '$1\u200c\u200c\u200c_\u200c\u200c\u200c');
}
// Decode URIs
// NOTE: This would convert all %20's to _'s which could break some links
// but we will undo that later on
value = value.replace(/(http\S+)/g, function (s, p) {
return decodeURI(p);
});
// Revive all instances of %20 to make sure no links is broken
value = value.replace(/\u200c\u200c\u200c_\u200c\u200c\u200c/g, '%20');
return value;
}

/**
* jQuery plugin adapter for persian.js
*
* @author @salisa
* @param {?string} param
*/
var PJ = $.fn.persian = function(param) {
return this.each(function () {

var $this = $(this), value, persianValue, persianChar, persianNumber, englishNumber;

if (typeof param === 'string') {
value = PJ.getValue($this);
persianValue = PJ.getFa(value, param);
PJ.setValue($this, persianValue);

// In the case of none param passing: $('element').persian();
// apply three main methods: toPersianChar(), toPersianNumber(), englishNumber()
} else if (typeof param === 'undefined') {
value = PJ.getValue($this);
persianChar = PJ.getFa(value, 'toPersianChar');
PJ.setValue($this, persianChar);

persianNumber = PJ.getFa(persianChar, 'toPersianNumber');
PJ.setValue($this, persianNumber);

englishNumber = PJ.getFa(persianNumber, 'englishNumber');
PJ.setValue($this, englishNumber);
}
});
};

/**
*
* @param {$} $elm
* @returns {string}
*/
PJ.getValue = function ($elm) {
var result;
if ($elm.is('input, textarea')) {
result = $elm.val();
} else {
result = $elm.text();
}
return result;
};

/**
*
* @param {$} $elm
* @param {string} value
*/
PJ.setValue = function ($elm, value) {
if ($elm.is('input, textarea')) {
$elm.val(value);
} else {
$elm.text(value);
}
};

/**
*
* @param {string} value
* @param {string} param
* @returns {string}
*/
PJ.getFa = function (value, param) {
var PersianJs = persianJs(value),
persianValue = value;

try {
persianValue = PersianJs[param].call(PersianJs);
} catch (e) {
console.error('Wrong param used! ->', param);
}
return persianValue;
};

var persianJs = function(inputStr) {
if (inputStr === "" || inputStr === null) {
return null;
}
return new PersianJs(inputStr);
};

/**
* Current PersianJs version
*
* @property version
* @type String
*/
persianJs.version = VERSION;

//Prototype
persianJs.fn = PersianJs.prototype = {
clone: function () {
return persianJs(this);
},
value: function () {
return this._str;
},
toString: function () {
return this._str.toString();
},
set : function (value) {
this._str = String(value);
return this;
},
toPersianChar: function() {
return _toPersianChar(this._str);
},
toPersianNumber: function() {
return _toPersianNumber(this._str);
},
fixURL: function() {
return _fixURL(this._str);
},
englishNumber: function() {
return _englishNumber(this._str);
},
switchKey: function() {
return _switchKey(this._str);
}
};

//Expose PersianJs
//CommonJS module is defined
if (hasModule) {
module.exports = persianJs;
}
//global ender:false
if (typeof ender === 'undefined') {
// here, `this` means `window` in the browser, or `global` on the server
// add `persianJs` as a global object via a string identifier,
// for Closure Compiler "advanced" mode
this['persianJs'] = persianJs;
}
//global define:false
if (typeof define === 'function' && define.amd) {
define('persianJs', [], function () {
return persianJs;
});
}
})();
19 changes: 19 additions & 0 deletions test/testJquery.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="../jquery.persian.js" type="text/javascript"></script>
<script>
$(function(){
$("#txtOne,#txtTwo").persian();
$("#txtThree").persian("toPersianNumber");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have unit tests that our CI, Travis, understand. It's not a good unit test.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afshinm
I think Travis-CI tests javaScript using Node.js with mocha package on the server & there's no DOM out of browser. so how we can wrote a unit test on CI for jquery DOM based plugin?

});
</script>
</head>
<body>
<input type="text" value="1234567890" id="txtOne"/>
<input type="text" value="1234567890" id="txtTwo"/>
<input type="text" value="٥" id="txtThree"/>
</body>
</html>