Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Latest commit

 

History

History
68 lines (49 loc) · 1.31 KB

extend.md

File metadata and controls

68 lines (49 loc) · 1.31 KB

How to extend

Beside the built-in format methods, you can define your own format methods.

Also you can override existing format methods by providing one with the same name.

Extend formatters

import { extend } from 'riot-format';
extend('yesno', function(value) {
  return !!value ? 'yes' : 'no';
});

let's use this method

<app>
  <p>should display yes: {format(1, 'yesno')}</p>
  <p>should display no: {format(null, 'yesno')}</p>
  <p>or use like this: {format(1).yesno()}</p>
</app>

Note: it should be easy to understand how it works if you are familiar with pipes.

Pipes

define another method

import { extend } from 'riot-format';
extend('isToday', function(value) {
  if (value) {
    var date = (value instanceof Date) ? value : new Date(value);
    if (!isNaN(date)) {
      var now = new Date();
      if (('' + date.getYear() + date.getMonth() + date.getDate()) === ('' + now.getYear() + now.getMonth() + now.getDate())) {
        return true;
      }
    }
  }
  return false;
});

use it

<app>
  <p>should display true: {format(new Date().toString()).date().isToday()}</p>
</app>

More