All files / src/internal/client/dev equality.js

84.29% Statements 102/121
89.28% Branches 25/28
83.33% Functions 5/6
83.89% Lines 99/118

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 1192x 2x 2x 2x 2x 2x 2x 2x 2x 2x 819749x 819749x 819749x 819749x 819749x 18468x 819749x 4x 4x 4x       4x 4x 819749x 2x 2x 2x 2x 2x 37756x 37756x 37756x 37756x 37756x 6x 37756x                 37756x 2x 2x 2x 2x 2x 1079296x 1079296x 1079296x 1079296x 1079296x 463545x 1079296x 114x 114x 114x 110x 110x 110x 114x 114x 1079296x 2x 2x 2x 2x 2x 2x 2x 318x 318x 26x 26x 26x 16x 16x 16x 26x 318x 8x 8x 8x       8x 8x 318x 2x 2x 2x 2x 2x 2x 2x 318x 318x 318x 318x 318x 2x 2x 2x 2x 2x 2x 2x            
import { DEV } from 'esm-env';
import { STATE_SYMBOL } from '../constants';
import * as w from '../warnings.js';
 
if (DEV) {
	const array_prototype = Array.prototype;
 
	const original_index_of = array_prototype.indexOf;
 
	array_prototype.indexOf = function (search_element, from_index) {
		const index = original_index_of.call(this, search_element, from_index);
		if (
			index === -1 &&
			search_element != null &&
			typeof search_element === 'object' &&
			STATE_SYMBOL in search_element
		) {
			const o = search_element[STATE_SYMBOL];
			if (o != null) {
				if (original_index_of.call(this, o.p, from_index) !== -1) {
					// fire warning
					console.warn('TODO: $state referenced non-state');
				}
			}
		}
		return index;
	};
 
	const original_last_index_of = array_prototype.lastIndexOf;
 
	array_prototype.lastIndexOf = function (search_element, from_index) {
		const index = original_last_index_of.call(this, search_element, from_index);
		if (
			index === -1 &&
			search_element != null &&
			typeof search_element === 'object' &&
			STATE_SYMBOL in search_element
		) {
			const o = search_element[STATE_SYMBOL];
			if (o != null) {
				if (original_last_index_of.call(this, o.p, from_index) !== -1) {
					// fire warning
					console.warn('TODO: $state referenced non-state');
				}
			}
		}
		return index;
	};
 
	const original_includes = array_prototype.includes;
 
	array_prototype.includes = function (search_element, from_index) {
		const has = original_includes.call(this, search_element, from_index);
		if (
			has &&
			search_element != null &&
			typeof search_element === 'object' &&
			STATE_SYMBOL in search_element
		) {
			const o = search_element[STATE_SYMBOL];
			if (o != null) {
				if (original_includes.call(this, o.p, from_index)) {
					// fire warning
					console.warn('TODO: $state referenced non-state');
				}
			}
		}
		return has;
	};
}
 
/**
 * @param {any} a
 * @param {any} b
 */
function check_dev_equals(a, b) {
	if (a != null && typeof a === 'object' && STATE_SYMBOL in a) {
		const o = a[STATE_SYMBOL];
		if (o != null) {
			if (o.p === b) {
				// fire warning
				console.warn('TODO: $state referenced non-state');
			}
		}
	} else if (b != null && typeof b === 'object' && STATE_SYMBOL in b) {
		const o = b[STATE_SYMBOL];
		if (o != null) {
			if (o.p === a) {
				// fire warning
				console.warn('TODO: $state referenced non-state');
			}
		}
	}
}
 
/**
 * @param {any} a
 * @param {any} b
 * @returns {boolean}
 */
export function strict_equals(a, b) {
	if (DEV) {
		check_dev_equals(a, b);
	}
	return a === b;
}
 
/**
 * @param {any} a
 * @param {any} b
 * @returns {boolean}
 */
export function equals(a, b) {
	if (DEV) {
		check_dev_equals(a, b);
	}
	return a == b;
}