* Mock.js
function Mock() {}
Mock.mobile_prefix = ["134", "135", "136", "137", "138", "139", "150", "151",
"152", "157", "158", "159", "130","131", "132", "155", "156", "133", "153"];
Mock.numeric = "0123456789";
Mock.lowerCase = "abcdefghijklmnopqrstuvwxyz"
Mock.upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Mock.email_suffix = ["@gmail.com", "@yahoo.com", "@msn.com", "@hotmail.com",
"@aol.com", "@ask.com","@live.com", "@qq.com", "@0355.net", "@163.com",
"@163.net", "@263.net", "@3721.net", "@yeah.net", "@126.com", "@sina.com",
"@sohu.com", "@yahoo.com.cn"];
Mock.grade = ['高三','高二','高一','初三','初二','初一','小六','六年级','七年级','八年级','九年级','高中','初中','小学'];
Mock.gradeValue = ["03-2016", "03-2017", "03-2018", "02-2016", "02-2017", "02-2018", "02-2015", "02-2016", "02-2017", "02-2018", "01-2013"];
Mock.random = function(len, list) {
if (len <= 1) { len = 1; }
var s = "";
var n = list.length;
if (typeof list === "string") {
while (len-- > 0 ){
s += list.charAt(Math.random() * n)
}
} else if (list instanceof Array) {
while (len-- > 0 ){
s += list[ Math.floor(Math.random() * n) ]
}
}
return s;
}
Mock.randomInt = function(min, max) {
return min + Math.floor(Math.random() * (max-min+1))
}
Mock.pick = function(list) {
if (! list.length ) {
return undefined;
}
return list[ Math.floor(Math.random() * list.length) ]
}
Mock.getUsername = function() {
return Mock.random(8, Mock.lowerCase)
}
Mock.getMobile = function() {
return Mock.random(1, Mock.mobile_prefix)
+ Mock.random(8, Mock.numeric);
}
Mock.getGrade = function() {
return Mock.random(1, Mock.grade);
}
Mock.getEmail = function() {
var opt = Mock.numeric + Mock.lowerCase + Mock.upperCase
return Mock.random( Mock.randomInt(4, 10), opt) +
Mock.random(1, Mock.email_suffix);
}
* MockValue.php
<?php
class MockValue {
public static $letters = "abcdefghijklmnopqrstuvwxyz";
public static $numeric = "0123456789";
public static $email_suffix =
["@gmail.com", "@yahoo.com", "@msn.com", "@hotmail.com", "@aol.com", "@ask.com",
"@live.com", "@qq.com", "@0355.net", "@163.com", "@163.net",
"@263.net", "@3721.net", "@yeah.net", "@126.com", "@sina.com", "@sohu.com", "@yahoo.com.cn"];
public static $mobile_prefix = ["134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159", "130",
"131", "132", "155", "156", "133", "153"];
public static $grade = ['高三','高二','高一','初三','初二','初一','小六','六年级','七年级','八年级','九年级','高中','初中','小学'];
public static $gradeValue = ["03-2016", "03-2017", "03-2018", "02-2016", "02-2017", "02-2018", "02-2015", "02-2016", "02-2017", "02-2018", "01-2013"];
public static function getNumber(/* int */$width) /* int */ {
$min = 1;
if ($width <= 1) {
$width = 1;
return rand(0, 9);
}
$width -= 1;
for ($i = 0; $i <$width; $i++) {
$min *= 10;
}
$max = $min * 10 - 1;
return rand($min, $max);
}
public static function getMobile() {
return self::random(1, self::$mobile_prefix) . self::random(8, self::$numeric);
}
public static function getGrade() {
return self::random(1, self::$grade);
}
public static function getGradeValue() {
return self::pick(self::$gradeValue);
}
public static function getElement($list) {
if (is_string($list)) {
$n = strlen($list);
} else if (is_array($list)) {
$n = count($list);
} else {
throw new InvalidArgumentException("list must string or array");
}
return $list[rand(0, $n-1)];
}
public static function getName() {
return self::random(8, self::$letters);
}
private static function random(/*int */$length, /* ArrayAccess */ $list) {
if ($length <= 1) {
$length = 1;
}
$s = "";
if (is_string($list)) {
$n = strlen($list);
} else if (is_array($list)) {
$n = count($list);
} else {
throw new InvalidArgumentException("list must string or array");
}
while ($length--) {
$s .= $list[ rand(0, $n-1) ]; // inclusive $n-1
}
return $s;
}
public static function pick($list) {
$n = count($list);
if ($n < 1) {
throw new RunTimeException("Empty list");
}
return $list[ rand(0, $n-1) ];
}
public static function main() {
echo self::getNumber(5).PHP_EOL;
echo self::getMobile().PHP_EOL;
echo self::getGrade().PHP_EOL;
}
}
// MockValue::main();