ngx-humanize-duration
TypeScript icon, indicating that this package has built-in type declarations

1.2.5 • Public • Published

Angular Humanize Duration

npm version   Build

Angular wrapper for Humanize Duration library. Provides Angular pipe and service.

Installation

npm install humanize-duration ngx-humanize-duration

Quick Start

Import ngx-humanize-duration module in Angular app.

import { NgxHumanizeDurationModule } from 'ngx-humanize-duration';
 
@NgModule({
  imports: [
    NgxHumanizeDurationModule
  ],
})

Usage

With pipe in templates

<p>
  {{ yearInMillieSeconds | humanizeDuration:{ delimiter: ' and ', largest:2 } }}
</p>

With a service

import {
  NgxHumanizeDurationOptions,
  NgxHumanizeDurationService,
} from "ngx-humanize-duration";
 
@Injectable({
  providedIn: "root",
})
export class MyService {
  constructor(private ngxHumanizeDurationService: NgxHumanizeDurationService) {}
 
  humanizeDuration(value: number, options?: NgxHumanizeDurationOptions) {
    return this.ngxHumanizeDurationService.humanizeDuration(value, options);
  }
}

Advanced Usage

If you find yourself setting same options over and over again, you can set the defaults using forRoot method, which you can still override at Module or Component or Service level

import { NgxHumanizeDurationModule, NgxHumanizerOptions } from 'ngx-humanize-duration';
 
const defaults: NgxHumanizerOptions = {};
 
@NgModule({
  imports: [
    NgxHumanizeDurationModule.forRoot(defaults)
  ],
})

If you have Lazy Loaded modules and you want to use the defaults that you have set using above method, just import the NgxHumanizeDurationModule

import { NgxHumanizeDurationModule, NgxHumanizerOptions } from 'ngx-humanize-duration';
 
const defaults: NgxHumanizerOptions = {};
 
@NgModule({
  imports: [
    NgxHumanizeDurationModule
  ],
})

If you want to override the defaults at Module Level

import { NgxHumanizeDurationModule, NgxHumanizerOptions } from 'ngx-humanize-duration';
 
const moduleLevelOptions: NgxHumanizerOptions = {};
 
@NgModule({
  imports: [
    NgxHumanizeDurationModule.forFeature(moduleLevelOptions)
  ],
})

If you want to override the defaults at Component level

<p>
  {{ yearInMillieSeconds | humanizeDuration:{ componentLevelOptions } }}
</p>

If you want to override the defaults at Service level

import { NgxHumanizeDurationService } from "ngx-humanize-duration";
 
@Injectable({
  providedIn: "root",
})
export class MyService {
  constructor(private ngxHumanizeDurationService: NgxHumanizeDurationService) {}
 
  humanizeDuration() {
    const humanizedDuration = this.ngxHumanizeDurationService.humanizeDuration(
      value,
      newOptions
    );
  }
}

Package Sidebar

Install

npm i ngx-humanize-duration

Weekly Downloads

105

Version

1.2.5

License

MIT

Unpacked Size

136 kB

Total Files

30

Last publish

Collaborators

  • george3447