You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
When value to truncate is undefined or null, the TruncatePipe.transform() call throws a TypeError. This error points to line 12 of truncate.pipe.ts where value.length is evaluated. See below for a spec to replicate this error.
Examining the source code of standard Angular 2 pipes that work with strings (e.g. lowercase or uppercase. see: https://github.com/angular/angular/blob/2.1.2/modules/angular/common/src/pipes/uppercase_pipe.ts), shows that the pipe transform() returns null in this case. However, another option is to have transform() return an empty string in the case when value is null or undefined. My opinion is to have TruncatePipe.transform() conform to the standard Angular 2 pipe behavior and return null when the value to truncate is null or undefined. Other opinions are welcome.
/**
* Spec to replicate this issue
*/
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { TruncatePipe } from './truncate.pipe';
describe('TruncatePipe', () => {
let pipe;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TruncatePipe, TestComponent, Test2Component]
})
pipe = new TruncatePipe();
});
it('should return null if value is undefined', () => {
let value = undefined;
let actual = pipe.transform(value);
expect(actual).toBeNull();
});
it('should return null if value is null', () => {
let value = null;
let actual = pipe.transform(value);
expect(actual).toBeNull();
});
it('should return a null string inside a real component template when value to truncate is undefined', () => {
// TestComponent defined below whose template uses the truncate pipe
let fixture = TestBed.createComponent(TestComponent);
let component = fixture.componentInstance;
let message = component.message;
let expected = '';
fixture.detectChanges();
let actual = fixture.nativeElement.querySelector('h2').innerText;
expect(actual).toBeNull();
});
it('should return a null string inside a real component template when value to truncate is null', () => {
// Test2Component defined below whose template uses the truncate pipe
let fixture = TestBed.createComponent(TestComponent);
let component = fixture.componentInstance;
let message = component.message;
fixture.detectChanges();
let actual = fixture.nativeElement.querySelector('h2').innerText;
expect(actual).toBeNull();
});
});
@Component({
selector: 'test',
template: `<h2>{{ message | truncate }}</h2>`
})
class TestComponent {
message = undefined;
}
@Component({
selector: 'test2',
template: `<h2>{{ message | truncate }}</h2>`
})
class Test2Component {
message = null;
}
The text was updated successfully, but these errors were encountered:
@cdoremus Taking the Angular approach sounds like the right call. Would you be willing to make a PR with a fix? (Thanks for all of your other PRs so far - super helpful!)
Fix for null or undefined values to truncate defined in issue #162. Tests for this fix are included in truncate.pipe.spec.ts. Also cleaned up linting errors in the spec file.
* fix: added null check (issue #162)
* Added tests for null/undefined(#162). Fixed lint errors.
Fix for null or undefined values to truncate defined in issue GSA#162. Tests for this fix are included in truncate.pipe.spec.ts. Also cleaned up linting errors in the spec file.
* fix: added null check (issue GSA#162)
* Added tests for null/undefined(GSA#162). Fixed lint errors.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
When value to truncate is undefined or null, the TruncatePipe.transform() call throws a TypeError. This error points to line 12 of truncate.pipe.ts where value.length is evaluated. See below for a spec to replicate this error.
Examining the source code of standard Angular 2 pipes that work with strings (e.g. lowercase or uppercase. see: https://github.com/angular/angular/blob/2.1.2/modules/angular/common/src/pipes/uppercase_pipe.ts), shows that the pipe transform() returns null in this case. However, another option is to have transform() return an empty string in the case when value is null or undefined. My opinion is to have TruncatePipe.transform() conform to the standard Angular 2 pipe behavior and return null when the value to truncate is null or undefined. Other opinions are welcome.
The text was updated successfully, but these errors were encountered: