The CustomParser uses a custom function to format and another to parse the data.
It receives 2 arguments: formatFunction and parseFunction. They are both functions that take 1 argument (the value to be formatted or parsed), and are called on the parser's format and parse methods, and defaults to a function that returns the received value.
Examples:
//A StringTrimmer parser using the JavaScriptUtil trim function var parser = new CustomParser(null, trim); parser.format(" ABC ") -> "ABC" parser.parse(" ABC ") -> " ABC " //A percentual parser using custom functions var parser2 = new CustomParser(); parser2.formatFunction = function (value) { try { return round(value * 100) + "%"; } catch (exception) { return null; } }; parser2.parseFunction = function (value) { try { var number = replaceAll(value, "%", ""); return isEmpty(number) ? null : parseInt(number, 10) / 100; } catch (exception) { return null; } }; parser.format(0.1) -> "10%" parser.format(1) -> "100%" parser.parse("95%") -> 0.95
Previous: Working with the DateParser |
Table of Contents | Next: Using the WrapperParser |