Skip to content

Instantly share code, notes, and snippets.

@JonCatmull
Last active January 12, 2024 16:09
Show Gist options
  • Save JonCatmull/da9c8bb7367f1f55a4080bbc525c09ac to your computer and use it in GitHub Desktop.
Save JonCatmull/da9c8bb7367f1f55a4080bbc525c09ac to your computer and use it in GitHub Desktop.

Revisions

  1. JonCatmull revised this gist Mar 13, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ordinal.pipe.ts
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ import { Pipe, PipeTransform } from '@angular/core';
    const ordinals: string[] = ['th','st','nd','rd'];

    /*
    * Turn Date into realtive date (e.g. 5 days ago)
    * Append ordinal to number (e.g. "1st" position)
    * Usage:
    * value | ordinal:keepNumber
    * Example:
  2. JonCatmull revised this gist Mar 13, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ordinal.pipe.ts
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ const ordinals: string[] = ['th','st','nd','rd'];
    export class OrdinalPipe implements PipeTransform {

    transform(n: number, keepNumber: boolean = true) {
    var v = n % 100;
    let v = n % 100;
    return (keepNumber?n:'') + (ordinals[(v-20)%10]||ordinals[v]||ordinals[0]);
    }
    }
  3. JonCatmull revised this gist Mar 13, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ordinal.pipe.ts
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import { Pipe, PipeTransform } from '@angular/core';

    const ordinals: string[] = ["th","st","nd","rd"];
    const ordinals: string[] = ['th','st','nd','rd'];

    /*
    * Turn Date into realtive date (e.g. 5 days ago)
  4. JonCatmull created this gist Mar 13, 2017.
    23 changes: 23 additions & 0 deletions ordinal.pipe.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import { Pipe, PipeTransform } from '@angular/core';

    const ordinals: string[] = ["th","st","nd","rd"];

    /*
    * Turn Date into realtive date (e.g. 5 days ago)
    * Usage:
    * value | ordinal:keepNumber
    * Example:
    * {{ 23 | ordinal}}
    * formats to: '23rd'
    * Example:
    * {{ 23 | ordinal:false}}
    * formats to: 'rd'
    */
    @Pipe({name: 'ordinal'})
    export class OrdinalPipe implements PipeTransform {

    transform(n: number, keepNumber: boolean = true) {
    var v = n % 100;
    return (keepNumber?n:'') + (ordinals[(v-20)%10]||ordinals[v]||ordinals[0]);
    }
    }