Skip to content

Instantly share code, notes, and snippets.

@chl03ks
Created August 11, 2016 15:13
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chl03ks/c7fbd8b7f632f5891e98854b024f0557 to your computer and use it in GitHub Desktop.
Save chl03ks/c7fbd8b7f632f5891e98854b024f0557 to your computer and use it in GitHub Desktop.
An Angular 2 pipe to capitalize the first letter of a string value.
import { Pipe, PipeTransform } from '@angular/core';
/*
* Capitalize the first letter of the string
* Takes a string as a value.
* Usage:
* value | capitalizefirst
* Example:
* // value.name = daniel
* {{ value.name | capitalizefirst }}
* fromats to: Daniel
*/
@Pipe({
name: 'capitalizeFirst'
})
export class CapitalizeFirstPipe implements PipeTransform {
transform(value: string, args: any[]): string {
if (value === null) return 'Not assigned';
return value.charAt(0).toUpperCase() + value.slice(1);
}
}
@Technohacker
Copy link

Thanks for the pipe! Can I use this in my own project?

@sankar4n
Copy link

It should be: return value.charAt(0).toUpperCase() + value.slice(1).toLowerCase();

@Zuhayer
Copy link

Zuhayer commented Sep 12, 2017

@hisankaran Thanks for the addition. (y)

@atn18
Copy link

atn18 commented Oct 4, 2017

Now there is a titlecase in Angular angular/angular#13324

@HaakonH
Copy link

HaakonH commented Oct 24, 2017

@agladkov - Just what I was looking for. Thanks!

@charsi
Copy link

charsi commented Feb 14, 2018

@vijay-pratap you don't need a custom pipe for that as Angular already has one for it. See @agladkov 's comment

@nate-getch
Copy link

@aryehg
Copy link

aryehg commented Mar 14, 2018

@ben-tes - thanks a million!! This is the correct way!!
Simply {{one.two | titlecase}} and that's it!

@chl03ks - thank you too for your pipe as you actually created it earlier than the other one.
So you do deserve all the credit too.

@khaledjendi
Copy link

Line 17:
Use
if (!value) return null;
Instead of
if (value === null) return 'Not assigned';

@vedhaperi
Copy link

Angular has 'titlecase'.

For ex:
envName | titlecase

When used with interpolation, avoid all spaces like
{{envName|titlecase}}

@digeomel
Copy link

digeomel commented Sep 21, 2018

To all those referring to the titlecase pipe in Angular. It is not the same.
For example:
let str = 'fileFormat';
{{str|titlecase}} = 'Fileformat'
{{str|capitalizefirst}} = 'FileFormat'
Notice that titlecase is making every other letter of a word than the first lowercase, while capitalizefirst is only capitalizing the first letter and leaving the rest of the string intact, which is what I need in my case.

@g1llz
Copy link

g1llz commented Oct 14, 2018

thanks bro

@wBatdeveloppement4
Copy link

Line 17:
Use
if (!value) return null;
Instead of
if (value === null) return 'Not assigned';

Nope, that would empty the 0 input.
Use

if (value === null || value === undefined) {
            return '';
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment