UNPKG

react-hichestan-numberinput

Version:

A serie of react components to numbers in input specially persian numbers.

64 lines (54 loc) 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NUMBER_FORMAT_LATIN = exports.NUMBER_FORMAT_FARSI = void 0; exports.hasStringACharToGoToNext = hasStringACharToGoToNext; exports.mapToFarsi = mapToFarsi; exports.mapToLatin = mapToLatin; exports.repeatStr = repeatStr; exports.stripAnyThingButDigits = stripAnyThingButDigits; var NUMBER_FORMAT_FARSI = 'FARSI'; exports.NUMBER_FORMAT_FARSI = NUMBER_FORMAT_FARSI; var NUMBER_FORMAT_LATIN = 'LATIN'; exports.NUMBER_FORMAT_LATIN = NUMBER_FORMAT_LATIN; function mapToFarsi(str) { if (!str) return str; return str.toString().replace(/[1234567890١٢٣٤٥٦٧٨٩٠]/gi, function (e) { var c = e.charCodeAt(0); return String.fromCharCode(c + (c < 60 ? 1728 : 144)); }); } function mapToLatin(str) { if (!str) return str; return str.toString().replace(/[۱۲۳۴۵۶۷۸۹۰١٢٣٤٥٦٧٨٩٠]/gi, function (e) { var c = e.charCodeAt(0); return String.fromCharCode(c - (c < 1770 ? 1584 : 1728)); }); } function stripAnyThingButDigits(str) { if (!str) return str; return str.toString().replace(/[^1234567890۱۲۳۴۵۶۷۸۹۰١٢٣٤٥٦٧٨٩٠]/gi, ''); } function hasStringACharToGoToNext(str) { if (str.indexOf('.') >= 0) return true; if (str.indexOf(',') >= 0) return true; // if(str.indexOf('/')>=0) return true; if (str.indexOf('-') >= 0) return true; if (str.indexOf(';') >= 0) return true; if (str.indexOf('*') >= 0) return true; if (str.indexOf('#') >= 0) return true; if (str.indexOf(' ') >= 0) return true; if (str.indexOf('،') >= 0) return true; return false; } /** * @param {string} str * @param {number} n */ function repeatStr(str, n) { var r = ''; for (var i = 0; i < n; i++) { r += str; } return r; }