Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
131 lines (124 sloc) 3.87 KB
/**
* =====================================
* # ( name ; value )
*
* RETURNS:
* An name-value pair in Let notation.
*
* PARAMETERS:
* name: The name for the returned name-value pair. name can be any value
* that would be a valid Let() variable name.
* value: The value for the returned name-value pair.
*
* EXAMPLE:
* # ( "name"; $value ) & # ( "foo" ; "bar" )
*
* DEPENDENCIES: none
*
* HISTORY:
* MODIFIED on 2014-10-07 by Daniel Smith dansmith65@gmail.com to prevent
* from returning an EvaluationError.
* MODIFIED on 2014-06-06 by Jeremy Bante <http://scr.im/jbante> to fix an
* issue where long sequences of digits could be interpreted as timestamps.
* MODIFIED on 2014-05-25 by Jeremy Bante <http://scr.im/jbante> to detect
* type using a different method suggested by Arnold Kegebein.
* MODIFIED on 2013-12-24 by Jeremy Bante <http://scr.im/jbante> to return
* an error if name contains a carriage return (Char ( 13 )), and removing
* explicit encoding for line feeds.
* MODIFIED on 2013-12-12 by John Jones <john.christopher@alumni.virginia.edu>
* to explicitly encode line feed characters in text values.
* MODIFIED on 2013-09-02 by Daniel Smith dansmith65@gmail.com to fix a
* type misidentification bug.
* MODIFIED on 2013-07-27 by Jeremy Bante <http://scr.im/jbante> to fix a
* type misidentification bug.
* MODIFIED on 2013-04-15 by Jeremy Bante <http://scr.im/jbante> to not
* wrap numbers in GetAsNumber.
* MODIFIED on 2013-01-14 by Daniel Smith dansmith65@gmail.com to include
* trailing return on error result and accept a value of "?"
* MODIFIED on 2012-12-12 by Daniel Smith dansmith65@gmail.com to preserve
* data type of value
* MODIFIED on 2012-12-07 by Jeremy Bante <http://scr.im/jbante> where an
* error result could create invalid sytax for values containing a comment
* close sequence.
* MODIFIED on 2012-11-28 by Jeremy Bante <http://scr.im/jbante> to return
* error feedback, and to prefix names with "$".
* CREATED on 2012-11-10 by Jeremy Bante <http://scr.im/jbante>.
*
* REFERENCES:
* https://github.com/filemakerstandards/fmpstandards/blob/master/Functions/%23Name-Value/%23.fmfn
* =====================================
*/
Let ( [
~name = // strip leading "$$" and "$"
Substitute (
"/*start*/" & name ;
[ "/*start*/$$" ; "" ] ;
[ "/*start*/$" ; "" ] ;
[ "/*start*/" ; "" ]
) ;
~plusOneText = GetAsText ( value + 1 ) ;
~isValidDate = not EvaluationError ( GetAsDate ( value ) ) ;
~isValidTime = not EvaluationError ( GetAsTime ( value ) ) ;
~number = GetAsNumber ( value ) ;
~value =
Case (
value = "" or value = "?" or ~number = "?" ;
Quote ( value ) ;
~isValidDate
and ~isValidTime
and GetAsText ( GetAsTimestamp ( value ) + 1 ) = ~plusOneText ;
"GetAsTimestamp ( " & Quote ( value ) & " )" ;
~isValidTime
and GetAsText ( GetAsTime ( value ) + 1 ) = ~plusOneText ;
"GetAsTime ( " & Quote ( value ) & " )" ;
~isValidDate
and GetAsText ( GetAsDate ( value ) + 1 ) = ~plusOneText ;
"GetAsDate ( " & Quote ( value ) & " )" ;
value ≠ ~number ;
Quote ( value ) ;
/* Else */
~number
) ;
~result =
"$"
& ~name
& " = "
& ~value
& " ;¶" ;
~testExpression =
"Let ( [ "
& ~result
& " ~ = \"\" ]; \"\" )" ;
~error =
Case (
IsEmpty ( ~name ) or Position ( ~name ; ¶ ; 1 ; 1 ) ≠ 0 ;
11 ; // Name is not valid
not IsValidExpression ( ~testExpression ) ;
1200 // Generic calculation error
)
];
If ( ~error ; // prevent bad pairs from affecting evaluation by commenting
"/* Error "
& ~error
& " name: "
& Quote (
Substitute ( // escape comment character sequences
name ;
[ "*/" ; "\*\/" ] ;
[ "/*" ; "\/\*" ]
)
)
& " value: "
& Quote (
Substitute ( // escape comment character sequences
value ;
[ "*/" ; "\*\/" ] ;
[ "/*" ; "\/\*" ]
)
)
& " */"
& ¶ ;
/* Else */
~result
)
)