From c48eeb01bd726895dc577d8b843b2a75883eee07 Mon Sep 17 00:00:00 2001 From: Ananya Sen Date: Fri, 10 Feb 2012 16:03:52 -0800 Subject: Merge from /joseeight/ninja-internal/tree/FileIO Signed-off-by: Ananya Sen --- js/lib/NJUtils.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'js/lib/NJUtils.js') diff --git a/js/lib/NJUtils.js b/js/lib/NJUtils.js index 74039e64..8daafa42 100755 --- a/js/lib/NJUtils.js +++ b/js/lib/NJUtils.js @@ -183,6 +183,27 @@ exports.NJUtils = Object.create(Object.prototype, { }, + // Returns the numerical value and unit string from a string. + // Useful for element properties. + // 100px will return the following array: [100, px] + getValueAndUnits: { + value: function(input) { + var numberValue = parseFloat(input); + + // Ignore all whitespace, digits, negative sign and "." when looking for units label + // The units must come after one or more digits + var objRegExp = /(\-*\d+\.*\d*)(\s*)(\w*\%*)/; + var unitsString = input.replace(objRegExp, "$3"); + if(unitsString) { + var noSpaces = /(\s*)(\S*)(\s*)/; + // strip out spaces and convert to lower case + var match = (unitsString.replace(noSpaces, "$2")).toLowerCase(); + } + + return [numberValue, match]; + } + }, + /* ================= Style methods ================= */ ///// Get computed height of element -- cgit v1.2.3