diff options
-rw-r--r-- | it.class | 24 | ||||
-rwxr-xr-x | tests/it.t | 7 |
2 files changed, 17 insertions, 14 deletions
@@ -950,22 +950,18 @@ static function date($format = "", $stamp = null) * Iterate over an array, replacing every element by function value or expression * @param $code The function or expression to apply. Expression may contain $k for keys and $v for values * @param $array The array to iterate over - * @param $p['keys'] Only modify elements with given keys (keys can be array or comma separated) */ -static function map($code, $array, $p = null) +static function map($code, $array) { static $cache = array(); - if (is_string($code) && it::match('^[\w:]+$', $code)) - $code .= '($v)'; - if (!is_callable($func = $code) && !($func = $cache[$code])) $func = $cache[$code] = create_function('$k,$v', "return $code;"); - foreach ($p['keys'] ? it::filter_keys($array, $p['keys']) : $array as $k => $v) - $array[$k] = $func($k, $v); + foreach ($array as $k => $v) + $result[$k] = $func($k, $v); - return (array)$array; + return (array)$result; } /** @@ -1086,4 +1082,16 @@ static function mod($a, $n) return (($a % $n) + $n) % $n; } +/** + * Same as php tempnam() but $dir defaults to tmp/ in service home + */ +static function tempnam($dir, $prefix) +{ + $dir = $dir ?: $GLOBALS['ULTRAHOME'] . "/tmp"; + if (!is_writable($dir)) + it::error("tempnam: '$dir' not writable"); + + return tempnam($dir, $prefix); +} + } @@ -12,6 +12,7 @@ $oldlocale = setlocale(LC_CTYPE, 0); ini_set('default_charset', 'utf-8'); setlocale(LC_CTYPE, 'de_CH'); # required becuase we're checking German umlauts in latin1 mode + function match($regex, $string, $expect, $name, $p = array()) { $GLOBALS['TEST_MORE_LEVEL'] = 1; @@ -417,9 +418,3 @@ is(it::mod(7, 4), 3); is(it::map('5*$k+$v', array(0 => 1, 1 => 2)), array(1, 7)); is(it::map(create_function('$k,$v', 'return 5*$k+$v;'), array(0 => 1, 1 => 2)), array(1, 7)); is(it::map(function($k, $v) {return 5*$k+$v;}, array(0 => 1, 1 => 2)), array(1, 7)); -is(it::map('strlen', array("aaa", "aa")), array(3, 2)); -is(it::map('it::ucfirst', array("aaa")), array("Aaa")); -# Only map selected keys -is(it::map('2*$v', [3, 4, 5], ['keys' => '0,1']), [6, 8, 5]); -is(it::map('2*$v', [3, 4, 5], ['keys' => [0,1]]), [6, 8, 5]); -is(it::map('2*$v', ['foo' => 1, 'bar' => 2], ['keys' => 'foo']), ['foo' => 2, 'bar' =>2]); |