adamwdraper/Numeral-js — pull request #146 by zeusdeux (2014-03-05 20:03:49+00:00)
Title: A top level forgiving validate method

Numeral.js is quite forgiving and very fault tolerant. This can be seen quite evidently in methods like `unformat`. For example:
```javascript
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:
```javascript
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
```