|
8 | 8 | isEmptyObject, |
9 | 9 | isFunction, |
10 | 10 | isMap, |
| 11 | + isMobilePhone, |
11 | 12 | isNaN, |
12 | 13 | isNull, |
13 | 14 | isNullOrUndefined, |
@@ -398,3 +399,85 @@ describe('isPlainObject', () => { |
398 | 399 | expect(isPlainObject(() => {})).toBe(false) |
399 | 400 | }) |
400 | 401 | }) |
| 402 | + |
| 403 | +describe('isMobilePhone', () => { |
| 404 | + it('应该识别有效的手机号码', () => { |
| 405 | + // 移动号段 - 字符串格式 |
| 406 | + expect(isMobilePhone('13812345678')).toBe(true) |
| 407 | + expect(isMobilePhone('13987654321')).toBe(true) |
| 408 | + expect(isMobilePhone('15012345678')).toBe(true) |
| 409 | + expect(isMobilePhone('15987654321')).toBe(true) |
| 410 | + |
| 411 | + // 移动号段 - 数字格式 |
| 412 | + expect(isMobilePhone(13812345678)).toBe(true) |
| 413 | + expect(isMobilePhone(13987654321)).toBe(true) |
| 414 | + expect(isMobilePhone(15012345678)).toBe(true) |
| 415 | + expect(isMobilePhone(15987654321)).toBe(true) |
| 416 | + |
| 417 | + // 联通号段 - 字符串格式 |
| 418 | + expect(isMobilePhone('13012345678')).toBe(true) |
| 419 | + expect(isMobilePhone('13112345678')).toBe(true) |
| 420 | + expect(isMobilePhone('18512345678')).toBe(true) |
| 421 | + expect(isMobilePhone('18612345678')).toBe(true) |
| 422 | + |
| 423 | + // 联通号段 - 数字格式 |
| 424 | + expect(isMobilePhone(13012345678)).toBe(true) |
| 425 | + expect(isMobilePhone(13112345678)).toBe(true) |
| 426 | + expect(isMobilePhone(18512345678)).toBe(true) |
| 427 | + expect(isMobilePhone(18612345678)).toBe(true) |
| 428 | + |
| 429 | + // 电信号段 - 字符串格式 |
| 430 | + expect(isMobilePhone('18012345678')).toBe(true) |
| 431 | + expect(isMobilePhone('18112345678')).toBe(true) |
| 432 | + expect(isMobilePhone('19012345678')).toBe(true) |
| 433 | + expect(isMobilePhone('19912345678')).toBe(true) |
| 434 | + |
| 435 | + // 电信号段 - 数字格式 |
| 436 | + expect(isMobilePhone(18012345678)).toBe(true) |
| 437 | + expect(isMobilePhone(18112345678)).toBe(true) |
| 438 | + expect(isMobilePhone(19012345678)).toBe(true) |
| 439 | + expect(isMobilePhone(19912345678)).toBe(true) |
| 440 | + }) |
| 441 | + |
| 442 | + it('应该拒绝无效的手机号码', () => { |
| 443 | + // 长度不对 - 字符串格式 |
| 444 | + expect(isMobilePhone('1381234567')).toBe(false) // 10位 |
| 445 | + expect(isMobilePhone('138123456789')).toBe(false) // 12位 |
| 446 | + |
| 447 | + // 长度不对 - 数字格式 |
| 448 | + expect(isMobilePhone(1381234567)).toBe(false) // 10位 |
| 449 | + expect(isMobilePhone(138123456789)).toBe(false) // 12位 |
| 450 | + |
| 451 | + // 不是以1开头 |
| 452 | + expect(isMobilePhone('23812345678')).toBe(false) |
| 453 | + expect(isMobilePhone('01812345678')).toBe(false) |
| 454 | + expect(isMobilePhone(23812345678)).toBe(false) |
| 455 | + |
| 456 | + // 第二位不是3-9 |
| 457 | + expect(isMobilePhone('10812345678')).toBe(false) |
| 458 | + expect(isMobilePhone('11812345678')).toBe(false) |
| 459 | + expect(isMobilePhone('12812345678')).toBe(false) |
| 460 | + expect(isMobilePhone(10812345678)).toBe(false) |
| 461 | + expect(isMobilePhone(11812345678)).toBe(false) |
| 462 | + expect(isMobilePhone(12812345678)).toBe(false) |
| 463 | + |
| 464 | + // 包含非数字字符(只对字符串有效) |
| 465 | + expect(isMobilePhone('138-1234-5678')).toBe(false) |
| 466 | + expect(isMobilePhone('138 1234 5678')).toBe(false) |
| 467 | + expect(isMobilePhone('138a1234567')).toBe(false) |
| 468 | + |
| 469 | + // 非字符串非数字类型 |
| 470 | + expect(isMobilePhone(null)).toBe(false) |
| 471 | + expect(isMobilePhone(undefined)).toBe(false) |
| 472 | + expect(isMobilePhone({})).toBe(false) |
| 473 | + expect(isMobilePhone([])).toBe(false) |
| 474 | + expect(isMobilePhone(true)).toBe(false) |
| 475 | + |
| 476 | + // 空字符串 |
| 477 | + expect(isMobilePhone('')).toBe(false) |
| 478 | + |
| 479 | + // 特殊数字情况 |
| 480 | + expect(isMobilePhone(Number.NaN)).toBe(false) |
| 481 | + expect(isMobilePhone(Infinity)).toBe(false) |
| 482 | + }) |
| 483 | +}) |
0 commit comments