/ngx-humanize-duration

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

Primary LanguageTypeScriptMIT LicenseMIT

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
    );
  }
}