Sahi Pro - Utility APIs
_random
Since Sahi Pro: 3.5Since Sahi OS: 2
_random($maxNumber)
Arguments
$maxNumber | integer | maximum number |
Details
Returns a random number between 0 and maxNumber.
Returns a random number between 0 and maxNumber.
warning
Two calls to _random() may return the same number. DO NOT use it for uniqueness.
_extract
Since Sahi Pro: 4.3Since Sahi OS: NA
_extract($str, $pattern[, $onlyGroups])
Arguments
$str | s | String to find pattern in |
$pattern | string|regular expression | Regular expression as native regex (/a/) or as a string ("/a/") |
$onlyGroups | b optional | if true, returns only the groups matched. |
Details
This API helps extract specific content from strings.
Example
This API helps extract specific content from strings.
Example
// To get value "250" from "Rs. 250"
_extract("Rs. 250", "/Rs. (.*)/", true); // returns "250"
_extract("Rs. 250", "/Rs. (.*)/"); // returns an array ["Rs. 250", "250"]
// Multiple groups with and without onlyGroups
var $str = "The traffic light changed from red to green";
var $pattern = "/from (.*) to (.*)/";
var $extracted = _extract($str, $pattern);
// $extracted now has the overall match "from red to green" and the individual groups "red" and "green"
_assertEqual(["from red to green", "red", "green"], $extracted);
// Setting onlyGroups=true, we do not get back the overall match.
var $extracted = _extract($str, $pattern, true);
// $extracted now has only the individual groups "red" and "green"
_assertEqual(["red", "green"], $extracted);
_trim
Since Sahi Pro: 5.1.0Since Sahi OS: NA
_trim($arg)
Arguments
$arg | string | array | String or 1-d or 2-d array. |
Details
Returns a trimmed string or 1-d or 2-d array.
Returns a trimmed string or 1-d or 2-d array.
var $s = " sahi pro ";
$s = _trim($s);
_assertEqual("sahi pro", $s);
var $arr = ["",,,"sahi", "pro",,,"",,];
$arr = _trim($arr);
_assertEqual("sahi", $arr[0]);
_assertEqual("pro", $arr[1]);
_assertEqual(2, $arr.length);
var $array2D = [[],[],[],["","",""],["abc", "xyz", "c"], ["1", "2", "3"], ["1", "2", "3"], ["1", "2", "3"], ["1", "2", "3"],[],[""],[]];
$array2D = _trim($array2D);
_assertEqual("xyz", $array2D[0][1]);
_assertEqual(5, $array2D.length);
_toJSON
Since Sahi Pro: 6.0.0Since Sahi OS: NA
_toJSON($object)
Arguments
$object | JSON Object | JSON to convert to string. |
Details
It converts a JSON object to String.
Example
It converts a JSON object to String.
Example
var $libObj = {
"name":"John Johnson",
"street":"Oslo West 16",
"age":33,
"phone":"555 1234567"};
// Gives string corresponding to json object.
var $jsonString = _toJSON($libObj);