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

79.78% Statements 75/94
61.53% Branches 8/13
71.42% Functions 5/7
79.34% Lines 73/92

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 932x 2x 2x 2x 4x 4x 4x 4x 4x 4x     4x 4x 4x 4x 837102x 837102x 837102x 301575x 301575x 301575x     301575x 837102x 837102x 4x 4x 4x 37778x 37778x 37778x 6x 6x 6x     6x 37778x 37778x 4x 4x 4x 1089371x 1089371x 1089371x 549555x 549555x 549555x     549555x 1089371x 1089371x 4x 4x 4x 4x       4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 8x     8x 8x 8x 2x 2x 2x 2x 2x 2x 2x 2x              
import * as w from '../warnings.js';
import { get_proxied_value } from '../proxy.js';
 
export function init_array_prototype_warnings() {
	const array_prototype = Array.prototype;
	// The REPL ends up here over and over, and this prevents it from adding more and more patches
	// of the same kind to the prototype, which would slow down everything over time.
	// @ts-expect-error
	const cleanup = Array.__svelte_cleanup;
	if (cleanup) {
		cleanup();
	}
 
	const { indexOf, lastIndexOf, includes } = array_prototype;
 
	array_prototype.indexOf = function (item, from_index) {
		const index = indexOf.call(this, item, from_index);
 
		if (index === -1) {
			const test = indexOf.call(get_proxied_value(this), get_proxied_value(item), from_index);
 
			if (test !== -1) {
				w.state_proxy_equality_mismatch('array.indexOf(...)');
			}
		}
 
		return index;
	};
 
	array_prototype.lastIndexOf = function (item, from_index) {
		const index = lastIndexOf.call(this, item, from_index);
 
		if (index === -1) {
			const test = lastIndexOf.call(get_proxied_value(this), get_proxied_value(item), from_index);
 
			if (test !== -1) {
				w.state_proxy_equality_mismatch('array.lastIndexOf(...)');
			}
		}
 
		return index;
	};
 
	array_prototype.includes = function (item, from_index) {
		const has = includes.call(this, item, from_index);
 
		if (!has) {
			const test = includes.call(get_proxied_value(this), get_proxied_value(item), from_index);
 
			if (test) {
				w.state_proxy_equality_mismatch('array.includes(...)');
			}
		}
 
		return has;
	};
 
	// @ts-expect-error
	Array.__svelte_cleanup = () => {
		array_prototype.indexOf = indexOf;
		array_prototype.lastIndexOf = lastIndexOf;
		array_prototype.includes = includes;
	};
}
 
/**
 * @param {any} a
 * @param {any} b
 * @param {boolean} equal
 * @returns {boolean}
 */
export function strict_equals(a, b, equal = true) {
	if ((a === b) !== (get_proxied_value(a) === get_proxied_value(b))) {
		w.state_proxy_equality_mismatch(equal ? '===' : '!==');
	}
 
	return (a === b) === equal;
}
 
/**
 * @param {any} a
 * @param {any} b
 * @param {boolean} equal
 * @returns {boolean}
 */
export function equals(a, b, equal = true) {
	if ((a == b) !== (get_proxied_value(a) == get_proxied_value(b))) {
		w.state_proxy_equality_mismatch(equal ? '==' : '!=');
	}

	return (a == b) === equal;
}