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

A top level forgiving validate method #146

Merged
merged 8 commits into from
May 29, 2014

Conversation

zeusdeux
Copy link
Contributor

@zeusdeux zeusdeux commented Mar 5, 2014

Numeral.js is quite forgiving and very fault tolerant. This can be seen quite evidently in methods like unformat. For example:

numeral().unformat("asd1000"); //1000
numeral().unformat("1asd1000"); //11000
numeral().unformat(",1,000"); //1000
numeral().unformat(",1.,000"); //1

In the examples above we can notice that Numeral.js is very fault tolerant and forgiving. But that makes it quite hard, for example, to visually guess what unformat might return.
More often than not, we need to be able to soft validate numerals before, say, unformatting them. This is required so that we can make educated guesses about the values that a method like unformat returns while debugging.

I came across multiple scenarios where I wished there was a top-level validation function in Numeral.js that was made for Numeral.js. By this what I mean is, the validator returns true if the value that is passed to it matches the rules given in the current numeral.language(). For example, it doesn't strictly validate if the thousand separator ( ',' comma for en-US) is placed after 3 digits from the unit position, etc. It just validates if the thousands separator is used and if used, then only before the decimal separator and not after. This is what I mean by soft validations.

It enables the programmer to visually correlate the value returned by, for example unformat, and the value passed to it. By this what I mean is, if the programmer sees that:

numeral.validate('$1,00.123', 'en') is true then he can guess that numeral().unformat('$1,00.123') will return 100.123.

This, in my experience, can make debugging a lot easier when using Numeral.js heavily (especially if you're using it to help in localization/i18n).

Hence I've added a top-level method called validate to the numeral object that supports soft validations over the abbreviations, currency and delimiters properties in the object returned by numeral.languageData(). I have also added the tests for it in tests/numeral/validate.js.

numeral.validate

Syntax:

numeral.validate( value[, culture/language] )

  • value is the localized numeral string. example: $100,000.123
  • culture or language is a language code understood by numeral.language. example en.
    It is optional.
    If not provided, then numeral.validate uses the current language which is given by numeral.language().
Example usage:
numeral.validate('$100.00k','en'); //true
numeral.validate('$100.00k','de'); //false
numeral.validate('100,00.123','de'); //true
numeral.validate('100.00,123','en'); //false

EDIT: The build seems to be breaking as grunt-contrib-uglify@0.4.0 expects node >= 0.10.0 but the version used for the build is v0.8.26. This is causing npm install to fail and hence the build to fail.

@zeusdeux zeusdeux mentioned this pull request Mar 7, 2014
@msaglietto
Copy link

nice... I could use a method like this .. it seem logical if you can enforce a format be able to validate it

what do you say @adamwdraper ?

@zeusdeux
Copy link
Contributor Author

Oh and fyi, we are using this method in production already so its seen a lot of usage.

@adamwdraper @msaglietto

@adamwdraper adamwdraper merged commit 1693d44 into adamwdraper:develop May 29, 2014
@adamwdraper
Copy link
Owner

Nice work. Thanks

@zeusdeux
Copy link
Contributor Author

No problem.

@fmoyses
Copy link

fmoyses commented Jun 11, 2014

I was looking something like this to validate data in our application.
@adamwdraper, do you know when this will be available in a dist version?

Thanks!

@acds
Copy link

acds commented Jun 20, 2019

any plan to add this to the distro ?

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

Successfully merging this pull request may close these issues.

None yet

5 participants