Skip to content

Commit

Permalink
Dev new EM function unique(args) which returns true if all non-empty …
Browse files Browse the repository at this point in the history
…responses are unique
  • Loading branch information
TMSWhite committed Aug 1, 2012
1 parent b432af4 commit cc8d897
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions classes/expressions/ExpressionManager.php
Expand Up @@ -216,6 +216,7 @@ function __construct()
'time' => array('time', 'time', $this->gT('Return current UNIX timestamp'), 'number time()', 'http://www.php.net/manual/en/function.time.php', 0),
'trim' => array('trim', 'trim', $this->gT('Strip whitespace (or other characters) from the beginning and end of a string'), 'string trim(string [, charlist])', 'http://www.php.net/manual/en/function.trim.php', 1,2),
'ucwords' => array('ucwords', 'ucwords', $this->gT('Uppercase the first character of each word in a string'), 'string ucwords(string)', 'http://www.php.net/manual/en/function.ucwords.php', 1),
'unique' => array('exprmgr_unique', 'LEMunique', $this->gT('Returns true if all non-empty responses are unique'), 'boolean unique(arg1, ..., argN)', '', -1),
);

}
Expand Down Expand Up @@ -3461,4 +3462,25 @@ function exprmgr_fixnum($value)
return $value;
}

/**
* Returns true if all non-empty values are unique
* @param type $args
*/
function exprmgr_unique($args)
{
$uniqs = array();
foreach ($args as $arg)
{
if (trim($arg)=='')
{
continue; // ignore blank answers
}
if (isset($uniqs[$arg]))
{
return false;
}
$uniqs[$arg]=1;
}
return true;
}
?>
14 changes: 14 additions & 0 deletions scripts/em_javascript.js
Expand Up @@ -15,6 +15,20 @@ function LEMcount()
return result;
}

function LEMunique()
{
var uniqs = new Array();
for (i=0;i<arguments.length;++i) {
var arg = arguments[i];
if (trim(arg)=='')
continue;
if (typeof uniqs[arg] !== 'undefined')
return false;
uniqs[arg] = 1;
}
return true;
}

function LEMcountif()
{
// takes variable number of arguments - returns count of those arguments that match the first parameter
Expand Down

0 comments on commit cc8d897

Please sign in to comment.