Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): remove looseIdentical in favor of built-in Object.is #37191

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/core/src/change_detection/change_detection_util.ts
Expand Up @@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/

import {looseIdentical} from '../util/comparison';
import {getSymbolIterator} from '../util/symbol';

export function devModeEqual(a: any, b: any): boolean {
Expand All @@ -20,7 +19,7 @@ export function devModeEqual(a: any, b: any): boolean {
if (!isListLikeIterableA && isAObject && !isListLikeIterableB && isBObject) {
return true;
} else {
return looseIdentical(a, b);
return Object.is(a, b);
}
}
}
Expand Down
Expand Up @@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/

import {looseIdentical} from '../../util/comparison';
import {stringify} from '../../util/stringify';
import {isListLikeIterable, iterateListLike} from '../change_detection_util';

Expand Down Expand Up @@ -180,15 +179,15 @@ export class DefaultIterableDiffer<V> implements IterableDiffer<V>, IterableChan
for (let index = 0; index < this.length; index++) {
item = collection[index];
itemTrackBy = this._trackByFn(index, item);
if (record === null || !looseIdentical(record.trackById, itemTrackBy)) {
if (record === null || !Object.is(record.trackById, itemTrackBy)) {
record = this._mismatch(record, item, itemTrackBy, index);
mayBeDirty = true;
} else {
if (mayBeDirty) {
// TODO(misko): can we limit this to duplicates only?
record = this._verifyReinsertion(record, item, itemTrackBy, index);
}
if (!looseIdentical(record.item, item)) this._addIdentityChange(record, item);
if (!Object.is(record.item, item)) this._addIdentityChange(record, item);
}

record = record._next;
Expand All @@ -197,15 +196,15 @@ export class DefaultIterableDiffer<V> implements IterableDiffer<V>, IterableChan
index = 0;
iterateListLike(collection, (item: V) => {
itemTrackBy = this._trackByFn(index, item);
if (record === null || !looseIdentical(record.trackById, itemTrackBy)) {
if (record === null || !Object.is(record.trackById, itemTrackBy)) {
record = this._mismatch(record, item, itemTrackBy, index);
mayBeDirty = true;
} else {
if (mayBeDirty) {
// TODO(misko): can we limit this to duplicates only?
record = this._verifyReinsertion(record, item, itemTrackBy, index);
}
if (!looseIdentical(record.item, item)) this._addIdentityChange(record, item);
if (!Object.is(record.item, item)) this._addIdentityChange(record, item);
}
record = record._next;
index++;
Expand Down Expand Up @@ -289,7 +288,7 @@ export class DefaultIterableDiffer<V> implements IterableDiffer<V>, IterableChan
if (record !== null) {
// We have seen this before, we need to move it forward in the collection.
// But first we need to check if identity changed, so we can update in view if necessary
if (!looseIdentical(record.item, item)) this._addIdentityChange(record, item);
if (!Object.is(record.item, item)) this._addIdentityChange(record, item);

this._moveAfter(record, previousRecord, index);
} else {
Expand All @@ -298,7 +297,7 @@ export class DefaultIterableDiffer<V> implements IterableDiffer<V>, IterableChan
if (record !== null) {
// It is an item which we have evicted earlier: reinsert it back into the list.
// But first we need to check if identity changed, so we can update in view if necessary
if (!looseIdentical(record.item, item)) this._addIdentityChange(record, item);
if (!Object.is(record.item, item)) this._addIdentityChange(record, item);

this._reinsertAfter(record, previousRecord, index);
} else {
Expand Down Expand Up @@ -628,7 +627,7 @@ class _DuplicateItemRecordList<V> {
let record: IterableChangeRecord_<V>|null;
for (record = this._head; record !== null; record = record._nextDup) {
if ((atOrAfterIndex === null || atOrAfterIndex <= record.currentIndex!) &&
looseIdentical(record.trackById, trackById)) {
Object.is(record.trackById, trackById)) {
return record;
}
}
Expand Down
Expand Up @@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/

import {looseIdentical} from '../../util/comparison';
import {stringify} from '../../util/stringify';
import {isJsObject} from '../change_detection_util';
import {KeyValueChangeRecord, KeyValueChanges, KeyValueDiffer, KeyValueDifferFactory} from './keyvalue_differs';
Expand Down Expand Up @@ -229,7 +228,7 @@ export class DefaultKeyValueDiffer<K, V> implements KeyValueDiffer<K, V>, KeyVal

// Add the record or a given key to the list of changes only when the value has actually changed
private _maybeAddToChanges(record: KeyValueChangeRecord_<K, V>, newValue: any): void {
if (!looseIdentical(newValue, record.currentValue)) {
if (!Object.is(newValue, record.currentValue)) {
record.previousValue = record.currentValue;
record.currentValue = newValue;
this._addToChanges(record);
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/core_private_export.ts
Expand Up @@ -27,7 +27,6 @@ export {GetterFn as ɵGetterFn, MethodFn as ɵMethodFn, SetterFn as ɵSetterFn}
export {allowSanitizationBypassAndThrow as ɵallowSanitizationBypassAndThrow, BypassType as ɵBypassType, getSanitizationBypassType as ɵgetSanitizationBypassType, SafeHtml as ɵSafeHtml, SafeResourceUrl as ɵSafeResourceUrl, SafeScript as ɵSafeScript, SafeStyle as ɵSafeStyle, SafeUrl as ɵSafeUrl, SafeValue as ɵSafeValue, unwrapSafeValue as ɵunwrapSafeValue} from './sanitization/bypass';
export {_sanitizeHtml as ɵ_sanitizeHtml} from './sanitization/html_sanitizer';
export {_sanitizeUrl as ɵ_sanitizeUrl} from './sanitization/url_sanitizer';
export {looseIdentical as ɵlooseIdentical,} from './util/comparison';
export {makeDecorator as ɵmakeDecorator} from './util/decorators';
export {global as ɵglobal} from './util/global';
export {isObservable as ɵisObservable, isPromise as ɵisPromise} from './util/lang';
Expand Down
8 changes: 1 addition & 7 deletions packages/core/src/util/comparison.ts
Expand Up @@ -8,12 +8,6 @@

import {areIterablesEqual, isListLikeIterable} from './iterable';


// JS has NaN !== NaN
export function looseIdentical(a: any, b: any): boolean {
return a === b || typeof a === 'number' && typeof b === 'number' && isNaN(a) && isNaN(b);
}

export function devModeEqual(a: any, b: any): boolean {
const isListLikeIterableA = isListLikeIterable(a);
const isListLikeIterableB = isListLikeIterable(b);
Expand All @@ -25,7 +19,7 @@ export function devModeEqual(a: any, b: any): boolean {
if (!isListLikeIterableA && isAObject && !isListLikeIterableB && isBObject) {
return true;
} else {
return looseIdentical(a, b);
return Object.is(a, b);
}
}
}
3 changes: 1 addition & 2 deletions packages/core/src/view/util.ts
Expand Up @@ -10,7 +10,6 @@ import {devModeEqual, WrappedValue} from '../change_detection/change_detection';
import {SOURCE} from '../di/injector_compatibility';
import {ViewEncapsulation} from '../metadata/view';
import {RendererType2} from '../render/api';
import {looseIdentical} from '../util/comparison';
import {stringify} from '../util/stringify';

import {expressionChangedAfterItHasBeenCheckedError} from './errors';
Expand Down Expand Up @@ -81,7 +80,7 @@ export function checkBinding(
view: ViewData, def: NodeDef, bindingIdx: number, value: any): boolean {
const oldValues = view.oldValues;
if ((view.state & ViewState.FirstCheck) ||
!looseIdentical(oldValues[def.bindingIndex + bindingIdx], value)) {
!Object.is(oldValues[def.bindingIndex + bindingIdx], value)) {
return true;
}
return false;
Expand Down
3 changes: 0 additions & 3 deletions packages/core/test/bundling/todo/bundle.golden_symbols.json
Expand Up @@ -941,9 +941,6 @@
{
"name": "locateHostElement"
},
{
"name": "looseIdentical"
},
{
"name": "makeMetadataCtor"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/core/test/change_detection/util.ts
Expand Up @@ -9,7 +9,6 @@
import {IterableChangeRecord, IterableChanges} from '@angular/core/src/change_detection/differs/iterable_differs';
import {KeyValueChangeRecord, KeyValueChanges} from '@angular/core/src/change_detection/differs/keyvalue_differs';

import {looseIdentical} from '../../src/util/comparison';
import {stringify} from '../../src/util/stringify';

export function iterableDifferToString<V>(iterableChanges: IterableChanges<V>) {
Expand Down Expand Up @@ -64,7 +63,7 @@ export function iterableChangesAsString({
}

function kvcrAsString(kvcr: KeyValueChangeRecord<string, any>) {
return looseIdentical(kvcr.previousValue, kvcr.currentValue) ?
return Object.is(kvcr.previousValue, kvcr.currentValue) ?
stringify(kvcr.key) :
(stringify(kvcr.key) + '[' + stringify(kvcr.previousValue) + '->' +
stringify(kvcr.currentValue) + ']');
Expand Down
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, ElementRef, forwardRef, Host, Input, OnDestroy, Optional, Renderer2, StaticProvider, ɵlooseIdentical as looseIdentical} from '@angular/core';
import {Directive, ElementRef, forwardRef, Host, Input, OnDestroy, Optional, Renderer2, StaticProvider} from '@angular/core';

import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';

Expand Down Expand Up @@ -121,7 +121,7 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
this._compareWith = fn;
}

private _compareWith: (o1: any, o2: any) => boolean = looseIdentical;
private _compareWith: (o1: any, o2: any) => boolean = Object.is;

constructor(private _renderer: Renderer2, private _elementRef: ElementRef) {}

Expand Down
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, ElementRef, forwardRef, Host, Input, OnDestroy, Optional, Renderer2, StaticProvider, ɵlooseIdentical as looseIdentical} from '@angular/core';
import {Directive, ElementRef, forwardRef, Host, Input, OnDestroy, Optional, Renderer2, StaticProvider} from '@angular/core';

import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';

Expand Down Expand Up @@ -118,7 +118,7 @@ export class SelectMultipleControlValueAccessor implements ControlValueAccessor
this._compareWith = fn;
}

private _compareWith: (o1: any, o2: any) => boolean = looseIdentical;
private _compareWith: (o1: any, o2: any) => boolean = Object.is;

constructor(private _renderer: Renderer2, private _elementRef: ElementRef) {}

Expand Down
4 changes: 2 additions & 2 deletions packages/forms/src/directives/shared.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {isDevMode, ɵlooseIdentical as looseIdentical} from '@angular/core';
import {isDevMode} from '@angular/core';

import {FormArray, FormControl, FormGroup} from '../model';
import {Validators} from '../validators';
Expand Down Expand Up @@ -156,7 +156,7 @@ export function isPropertyUpdated(changes: {[key: string]: any}, viewModel: any)
const change = changes['model'];

if (change.isFirstChange()) return true;
return !looseIdentical(viewModel, change.currentValue);
return !Object.is(viewModel, change.currentValue);
}

const BUILTIN_ACCESSORS = [
Expand Down
4 changes: 2 additions & 2 deletions packages/upgrade/static/src/upgrade_component.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, DoCheck, ElementRef, EventEmitter, Injector, OnChanges, OnDestroy, OnInit, SimpleChanges, ɵlooseIdentical as looseIdentical} from '@angular/core';
import {Directive, DoCheck, ElementRef, EventEmitter, Injector, OnChanges, OnDestroy, OnInit, SimpleChanges} from '@angular/core';

import {IAttributes, IAugmentedJQuery, IDirective, IInjectorService, ILinkFn, IScope, ITranscludeFunction} from '../../src/common/src/angular1';
import {$SCOPE} from '../../src/common/src/constants';
Expand Down Expand Up @@ -206,7 +206,7 @@ export class UpgradeComponent implements OnInit, OnChanges, DoCheck, OnDestroy {
const newValue = this.bindingDestination[propName];
const oldValue = twoWayBoundLastValues[idx];

if (!looseIdentical(newValue, oldValue)) {
if (!Object.is(newValue, oldValue)) {
const outputName = propertyToOutputMap[propName];
const eventEmitter: EventEmitter<any> = (this as any)[outputName];

Expand Down