github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/manage/app/components/rightsFromPartialLicense.pipe.ts (about)

     1  import { Pipe, PipeTransform } from '@angular/core';
     2  import  * as lic from './partialLicense';
     3  
     4  /*
     5   * Return only the rights of a partial license (or undefined)
     6   * Takes partialLicense as a string argument
     7   * Usage:
     8   *   partialLicense | filterRights
     9   */
    10  @Pipe({name: 'FilterRights'})
    11  export class FilterRights implements PipeTransform {
    12  
    13    transform(partialLicense: string): lic.UserRights | undefined  {
    14        let r: lic.UserRights = new lic.UserRights;
    15          let obj: any;
    16          obj = JSON.parse(partialLicense, function (key, value): any  {
    17            if (typeof value === 'string') {
    18              let a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
    19              if (a) {
    20                return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
    21              }
    22            }
    23            return value;
    24          });
    25          if ( obj.rights ) {
    26              console.log(obj.rights);
    27              r = obj.rights;
    28              return r;
    29          }
    30          return undefined;
    31    }
    32  }
    33  
    34  @Pipe({name: 'ShowRights'})
    35  export class ShowRights implements PipeTransform {
    36  
    37    transform(partialLicense: string): string  {
    38        let r: lic.UserRights = new lic.UserRights;
    39          let obj: any;
    40          obj = JSON.parse(partialLicense, function (key, value): any  {
    41            if (typeof value === 'string') {
    42              let a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
    43              if (a) {
    44                return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
    45              }
    46            }
    47            return value;
    48          });
    49          if ( obj.rights ) {
    50              console.log(obj.rights);
    51              r = obj.rights;
    52              let s: string = '';
    53              if ( r.copy >0 ) {
    54                  s = 'copy=' + r.copy + ', print=' + r.print + ' ';
    55              }
    56              return s + 'available from ' + r.start.toLocaleString() + ' to ' + r.end.toLocaleString();
    57          }
    58          return '';
    59    }
    60  }