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

convert Arabic numbers to Latin numbers, how can I do that? #3685

Closed
IbraheemAlSaady opened this issue Dec 27, 2016 · 3 comments
Closed

convert Arabic numbers to Latin numbers, how can I do that? #3685

IbraheemAlSaady opened this issue Dec 27, 2016 · 3 comments

Comments

@IbraheemAlSaady
Copy link

I used to convert Arabic numbers to Latin numbers by doing the following

      moment.updateLocale('ar', <MomentLanguage>{
        preparse: (str) => {
            return str.replace(/\u200f/g, '');
        },
        postformat: (str) => {
            return str;
        }
      });

but now, MomentLanguage is not a module in moment, what should I do?
PS: I'm using version 2.15.2

@butterflyhug
Copy link
Contributor

Your code is written with Typescript, right? I think the interface you're using is called Locale now.

@maggiepint
Copy link
Member

Closing since we haven't heard back since Lucas' comment. Reopen if you still need help.

@abusada
Copy link

abusada commented Jan 26, 2017

@IbraheemAlSaady this should work

const symbolMap = {
        '1': '1',
        '2': '2',
        '3': '3',
        '4': '4',
        '5': '5',
        '6': '6',
        '7': '7',
        '8': '8',
        '9': '9',
        '0': '0'
    };
    const numberMap = {
        '١': '1',
        '٢': '2',
        '٣': '3',
        '٤': '4',
        '٥': '5',
        '٦': '6',
        '٧': '7',
        '٨': '8',
        '٩': '9',
        '٠': '0'
    }
  moment.updateLocale('ar', {
      preparse: function (string) {
          return string.replace(/\u200f/g, '').replace(/[١٢٣٤٥٦٧٨٩٠]/g, function (match) {
              return numberMap[match];
          }).replace(/،/g, ',');
      },
      postformat: function(string) {
        return string.replace(/\d/g, function(match) {
          return symbolMap[match];
        }).replace(/,/g, '،');
      },
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants