Created
September 20, 2010 19:48
-
-
Save dustMason/588535 to your computer and use it in GitHub Desktop.
Measurement Parsing and Display in cfscript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfoutput> | |
<cfscript> | |
if (isDefined("measurement")) { | |
arguments = measurement.properties(); | |
} | |
var mcount = 0; | |
var measurements = {}; | |
if (isDefined("arguments.w") AND arguments.w NEQ "") { | |
measurements["w"] = arguments.w; | |
measurements["wsystem"] = arguments.wsystem; | |
mcount++; | |
} | |
if (isDefined("arguments.d") AND arguments.d NEQ "") { | |
measurements["d"] = arguments.d; | |
measurements["dsystem"] = arguments.dsystem; | |
mcount++; | |
} | |
if (isDefined("arguments.h") AND arguments.h NEQ "") { | |
measurements["h"] = arguments.h; | |
measurements["hsystem"] = arguments.hsystem; | |
mcount++; | |
} | |
// to make sure we go in order: | |
var keys = ["w","d","h"]; | |
// build the strings of dimensions: | |
var c = 1; | |
var primary = ""; | |
var secondary = ""; | |
for (i=1; i LTE ArrayLen(keys); i++) { | |
if (isDefined("measurements[keys[i]]")) { | |
var systemName = "#keys[i]#system"; | |
var system = measurements[systemName]; | |
if (system EQ "us") { | |
primary = primary&showusmeasurement(measurements[keys[i]]); | |
secondary = secondary&inchestometric(measurements[keys[i]]); | |
} else if (system EQ "metric") { | |
primary = primary&showmetricmesurement(measurements[keys[i]]); | |
secondary = secondary¢imeterstous(measurements[keys[i]]); | |
} | |
// if we have more than one dimension, include the "W", "D", or "H" | |
if (mcount GT 1) { | |
primary = primary&" <span class='dimension'>"&UCase(keys[i])&"</span>"; | |
secondary = secondary&" <span class='dimension'>"&UCase(keys[i])&"</span>"; | |
} | |
// if we aren't on the last one, stick in a "x" | |
if (c NEQ mcount) { | |
primary = primary&" <span class='by'>×</span> "; | |
secondary = secondary&" <span class='by'>×</span> "; | |
} | |
c++; | |
} | |
} | |
</cfscript> | |
<tr class="measurement subItem"> | |
<td class="field"> | |
<p>#arguments.label#</p> | |
</td> | |
<td class="value"> | |
<p class="primary-measurement">#primary#</p> | |
<p class="secondary-measurement">#secondary#</p> | |
</td> | |
</tr> | |
</cfoutput> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
function showmetricmesurement(cm) { | |
if (cm NEQ "") { | |
if (cm LT 100) { | |
return NumberFormat(cm,"9.99")&"cm"; | |
} else { | |
return NumberFormat((cm/100),"9.99")&"m"; | |
} | |
} else { | |
return ""; | |
} | |
} | |
function showusmeasurement(in) { | |
if (in NEQ "") { | |
m = in / 12; | |
feet = Int(m); | |
fraction = m-feet; | |
inches = (12*fraction); | |
if (feet EQ 0) { | |
feet = ""; | |
} else { | |
feet = feet&"′ "; | |
} | |
// because isValid(integer) is broken! | |
if (isValid("regex", inches, "^-{0,1}[1-9]+[\d]*")) { | |
return feet&inches&"″"; | |
} else { | |
wholeinches = Int(inches); | |
partinches = inches-wholeinches; | |
fractionPrecision = 32; | |
// force the fractions to be of the correct precision | |
partinches = Int(partinches*fractionPrecision)/fractionPrecision; | |
// round tiny values up to the minimum | |
if (partinches LT (1/fractionPrecision) AND partinches GT 0) { partinches = (1/fractionPrecision); } | |
am = createObject("java", "org.apache.commons.math.fraction.Fraction"); | |
f = Replace(am.init(JavaCast("double", partinches)).toString(), " ", "", "all"); | |
if (f NEQ "0") { | |
fl = ListToArray(f, "/"); | |
num = fl[1]; | |
if (IsDefined("fl[2]")) { | |
den = fl[2]; | |
} else { | |
den = 1; | |
} | |
fractionStr = '<sup class="fracNum">#num#</sup>/<sub class="fracDen">#den#</sub>'; | |
} else { | |
fractionStr = ""; | |
} | |
if (wholeinches EQ 0) { wholeinches = ""; } | |
out = feet&wholeinches&fractionStr; | |
if (wholeinches NEQ "" OR fractionStr NEQ "") { | |
out = out&"″"; | |
} | |
return out; | |
} | |
} else { | |
return ""; | |
} | |
} | |
function inchestometric(inches) { | |
if (inches NEQ "") { | |
cm = inches * 2.54; | |
return showmetricmesurement(cm); | |
} else { | |
return ""; | |
} | |
} | |
function centimeterstous(cm) { | |
if (cm NEQ "") { | |
inches = cm * 0.393700787; | |
return showusmeasurement(inches); | |
} else { | |
return ""; | |
} | |
} | |
</cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
function parseDimension(input) { | |
input = arguments.input; | |
subs = REFind("(?x) | |
(?: | |
(?: ## feet | |
([0-9]*\.?[0-9]*) | |
[ ]* ## any number of spaces | |
(?:'|ft|feet) ## feet indicator | |
) | |
{0,1} | |
[ ]* ## any number of spaces | |
(\d*(?![/\\w])) ## i have no idea what this means | |
{0,1} | |
(?:[ ,\-]) ## ? | |
{0,1} | |
((\d*)\/(\d*)) ## a fraction | |
{0,1} | |
(\.\d*) ## decimal point? | |
{0,1} | |
(?:\x22| in|in| inches|inches) ## inch indicator | |
)|(?: | |
([0-9]*\.?[0-9]*) ## number | |
[ ]* ## any number of spaces | |
(?:'|ft|feet) ## feet indicator | |
[ ]* ## any number of spaces | |
) | |
{1}",input,1,true); | |
if (subs.pos[1] eq 0) { | |
// it failed the imperial regex test | |
// now parse for metric values | |
// search for mm, then cm, then m | |
msubs = REFind("(?i)([0-9]*\.?[0-9]*)?(mm|cm|m)$",input,1,true); | |
if (msubs.pos[1] NEQ 0) { | |
metrics = {}; | |
metrics.value = Mid(input,msubs.pos[2],msubs.len[2]); | |
metrics.unit = Mid(input,msubs.pos[3],msubs.len[3]); | |
if (metrics.unit EQ "mm") { | |
cm = metrics.value/100; | |
} else if (metrics.unit EQ "m") { | |
cm = metrics.value*100; | |
} else if (metrics.unit EQ "cm") { | |
cm = metrics.value; | |
} | |
return { | |
"system":"metric", | |
"value":cm | |
}; | |
} | |
} else { | |
imperials = {}; | |
if (subs.pos[2] NEQ 0 AND subs.len[2] NEQ 0) { imperials.feet = Mid(input,subs.pos[2],subs.len[2]); } else { imperials.feet = 0; } | |
if (subs.pos[3] NEQ 0 AND subs.len[3] NEQ 0) { imperials.inches = Mid(input,subs.pos[3],subs.len[3]); } else { imperials.inches = 0; } | |
// if (subs.pos[4] NEQ 0 AND subs.len[4] NEQ 0) { imperials.fraction = Mid(input,subs.pos[4],subs.len[4]); } else { imperials.fraction = 0; } | |
if (subs.pos[5] NEQ 0 AND subs.len[5] NEQ 0) { imperials.fracNum = Mid(input,subs.pos[5],subs.len[5]); } else { imperials.fracNum = 0; } | |
if (subs.pos[6] NEQ 0 AND subs.len[6] NEQ 0) { imperials.fracDem = Mid(input,subs.pos[6],subs.len[6]); } else { imperials.fracDem = 1; } | |
if (subs.pos[7] NEQ 0 AND subs.len[7] NEQ 0) { imperials.decimal = Mid(input,subs.pos[7],subs.len[7]); } else { imperials.decimal = 0; } | |
if (subs.pos[8] NEQ 0 AND subs.len[8] NEQ 0) { | |
// Use the _other_ feet | |
imperials.feet = Mid(input,subs.pos[8],subs.len[8]); | |
} | |
// formula: | |
if (imperials.fracNum/imperials.fracDem NEQ 0) { | |
d = imperials.fracNum/imperials.fracDem; | |
} else if (imperials.decimal NEQ 0) { | |
d = imperials.decimal; | |
} else { | |
d = 0; | |
} | |
totalinches = (imperials.feet*12)+imperials.inches+d; | |
return { | |
"system":"imperial", | |
"value":totalinches | |
}; | |
} | |
} | |
</cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcomponent extends="Controller" output="false" hint="A set of functions to interface with the jQuery validator plugin"> | |
<cfscript> | |
function measurement() { | |
// gets either params.w, params.d or params.h | |
// m = params.w OR params.d OR params.h; | |
if (isDefined("params.w")) { m = params.w; } | |
if (isDefined("params.d")) { m = params.d; } | |
if (isDefined("params.h")) { m = params.h; } | |
try { | |
out = parseDimension(m); | |
} catch (Any e) { | |
renderText("false"); | |
return false; | |
} | |
if (isDefined("out.unit")) { | |
renderText("true"); | |
} else { | |
renderText("false"); | |
} | |
} | |
</cfscript> | |
</cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment