refactor(ivy): make assert conditions more readable (#34938)

PR Close #34938
This commit is contained in:
Misko Hevery 2020-01-26 11:01:41 -08:00 committed by Andrew Kushnir
parent 17650523a8
commit cf07d428a1
1 changed files with 15 additions and 9 deletions

View File

@ -13,59 +13,65 @@
import {stringify} from './stringify'; import {stringify} from './stringify';
export function assertNumber(actual: any, msg: string) { export function assertNumber(actual: any, msg: string) {
if (typeof actual != 'number') { if (!(typeof actual === 'number')) {
throwError(msg, typeof actual, 'number', '==='); throwError(msg, typeof actual, 'number', '===');
} }
} }
export function assertString(actual: any, msg: string) { export function assertString(actual: any, msg: string) {
if (typeof actual != 'string') { if (!(typeof actual === 'string')) {
throwError(msg, actual === null ? 'null' : typeof actual, 'string', '==='); throwError(msg, actual === null ? 'null' : typeof actual, 'string', '===');
} }
} }
export function assertEqual<T>(actual: T, expected: T, msg: string) { export function assertEqual<T>(actual: T, expected: T, msg: string) {
if (actual != expected) { if (!(actual == expected)) {
throwError(msg, actual, expected, '=='); throwError(msg, actual, expected, '==');
} }
} }
export function assertNotEqual<T>(actual: T, expected: T, msg: string) { export function assertNotEqual<T>(actual: T, expected: T, msg: string) {
if (actual == expected) { if (!(actual != expected)) {
throwError(msg, actual, expected, '!='); throwError(msg, actual, expected, '!=');
} }
} }
export function assertSame<T>(actual: T, expected: T, msg: string) { export function assertSame<T>(actual: T, expected: T, msg: string) {
if (actual !== expected) { if (!(actual === expected)) {
throwError(msg, actual, expected, '==='); throwError(msg, actual, expected, '===');
} }
} }
export function assertNotSame<T>(actual: T, expected: T, msg: string) { export function assertNotSame<T>(actual: T, expected: T, msg: string) {
if (actual === expected) { if (!(actual !== expected)) {
throwError(msg, actual, expected, '!=='); throwError(msg, actual, expected, '!==');
} }
} }
export function assertLessThan<T>(actual: T, expected: T, msg: string) { export function assertLessThan<T>(actual: T, expected: T, msg: string) {
if (actual >= expected) { if (!(actual < expected)) {
throwError(msg, actual, expected, '<'); throwError(msg, actual, expected, '<');
} }
} }
export function assertLessThanOrEqual<T>(actual: T, expected: T, msg: string) { export function assertLessThanOrEqual<T>(actual: T, expected: T, msg: string) {
if (actual > expected) { if (!(actual <= expected)) {
throwError(msg, actual, expected, '<='); throwError(msg, actual, expected, '<=');
} }
} }
export function assertGreaterThan<T>(actual: T, expected: T, msg: string) { export function assertGreaterThan<T>(actual: T, expected: T, msg: string) {
if (actual <= expected) { if (!(actual > expected)) {
throwError(msg, actual, expected, '>'); throwError(msg, actual, expected, '>');
} }
} }
export function assertGreaterThanOrEqual<T>(actual: T, expected: T, msg: string) {
if (!(actual >= expected)) {
throwError(msg, actual, expected, '>=');
}
}
export function assertNotDefined<T>(actual: T, msg: string) { export function assertNotDefined<T>(actual: T, msg: string) {
if (actual != null) { if (actual != null) {
throwError(msg, actual, null, '=='); throwError(msg, actual, null, '==');