Skip to content

Instantly share code, notes, and snippets.

@behnammodi
Last active July 5, 2019 18:50
Show Gist options
  • Save behnammodi/fe3e92a17b302f6b01c730a6f1c13043 to your computer and use it in GitHub Desktop.
Save behnammodi/fe3e92a17b302f6b01c730a6f1c13043 to your computer and use it in GitHub Desktop.
utils file
/**
* @private
*/
const FA = 0;
/**
* @private
*/
const EN = 1;
/**
* @private
*/
const AR = 2;
/**
* @private
*/
const numbers = [
['۰', '0', '٠'],
['۱', '1', '١'],
['۲', '2', '٢'],
['۳', '3', '٣'],
['۴', '4', '٤'],
['۵', '5', '٥'],
['۶', '6', '٦'],
['۷', '7', '٧'],
['۸', '8', '٨'],
['۹', '9', '٩']
];
/**
* @description Convert value form a language to another language
* @private
* @param {string} value
* @param {number} from
* @param {number} to
* @returns {string} converted value
*/
const convert = (value, from, to) =>
numbers.reduce(
(acc, digit) => acc.split(digit[from]).join(digit[to]),
(typeof value === 'string' || typeof value === 'number'
? value
: ''
).toString()
);
/**
* @description Convert english number to farsi number
* @public
* @param {string} value
* @returns {string} converted value
*/
export const convertEnNumToFaNum = value => convert(value, EN, FA);
/**
* @description Convert english number to arabic number
* @public
* @param {string} value
* @returns {string} converted value
*/
export const convertEnNumToArNum = value => convert(value, EN, AR);
/**
* @description Convert farsi number to english number
* @public
* @param {string} value
* @returns {string} converted value
*/
export const convertFaNumToEnNum = value => convert(value, FA, EN);
/**
* @description Convert farsi number to arabic number
* @public
* @param {string} value
* @returns {string} converted value
*/
export const convertFaNumToArNum = value => convert(value, FA, AR);
/**
* @description Convert arabic number to farsi number
* @public
* @param {string} value
* @returns {string} converted value
*/
export const convertArNumToFaNum = value => convert(value, AR, FA);
/**
* @description Convert arabic number to english number
* @public
* @param {string} value
* @returns {string} converted value
*/
export const convertArNumToEnNum = value => convert(value, AR, EN);
/**
* @description check is numeric
* @public
* @param {number|string} num
* @returns {boolean} is numeric
*/
export const isNumeric = num => !isNaN(parseFloat(num)) && isFinite(num);
export const isMobile = () => {
if (navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
) {
return true;
}
else {
return false;
}
}
export const log = (...args) => {
console.log('→', ...args);
}
export const toCamelCase = (str) =>
str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment