bitwarden-estensione-browser/libs/common/src/models/view/cardView.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
2.8 KiB
TypeScript
Raw Normal View History

import { CardLinkedId as LinkedId } from "../../enums/linkedIdType";
import { linkedFieldOption } from "../../misc/linkedFieldOption.decorator";
2022-02-22 15:39:11 +01:00
import { ItemView } from "./itemView";
export class CardView extends ItemView {
@linkedFieldOption(LinkedId.CardholderName)
2019-01-25 15:30:21 +01:00
cardholderName: string = null;
@linkedFieldOption(LinkedId.ExpMonth, "expirationMonth")
2018-01-25 22:16:02 +01:00
expMonth: string = null;
@linkedFieldOption(LinkedId.ExpYear, "expirationYear")
2019-01-25 15:30:21 +01:00
expYear: string = null;
@linkedFieldOption(LinkedId.Code, "securityCode")
2019-01-25 15:30:21 +01:00
code: string = null;
2018-01-24 17:33:15 +01:00
2018-01-25 22:16:02 +01:00
private _brand: string = null;
2019-01-25 15:30:21 +01:00
private _number: string = null;
private _subTitle: string = null;
2018-01-24 22:58:34 +01:00
2022-02-22 15:39:11 +01:00
constructor() {
super();
2018-01-24 17:33:15 +01:00
}
2018-01-24 22:58:34 +01:00
get maskedCode(): string {
return this.code != null ? "•".repeat(this.code.length) : null;
}
2021-04-29 13:31:21 +02:00
get maskedNumber(): string {
return this.number != null ? "•".repeat(this.number.length) : null;
}
@linkedFieldOption(LinkedId.Brand)
2018-01-24 22:58:34 +01:00
get brand(): string {
return this._brand;
}
set brand(value: string) {
this._brand = value;
this._subTitle = null;
}
@linkedFieldOption(LinkedId.Number)
2018-01-24 22:58:34 +01:00
get number(): string {
return this._number;
}
set number(value: string) {
this._number = value;
this._subTitle = null;
}
get subTitle(): string {
if (this._subTitle == null) {
this._subTitle = this.brand;
if (this.number != null && this.number.length >= 4) {
if (this._subTitle != null && this._subTitle !== "") {
this._subTitle += ", ";
} else {
2019-01-05 20:19:21 +01:00
this._subTitle = "";
2018-01-25 20:26:09 +01:00
}
// Show last 5 on amex, last 4 for all others
const count =
this.number.length >= 5 && this.number.match(new RegExp("^3[47]")) != null ? 5 : 4;
this._subTitle += "*" + this.number.substr(this.number.length - count);
2021-12-16 13:36:21 +01:00
}
2018-01-25 20:26:09 +01:00
}
2018-01-24 22:58:34 +01:00
return this._subTitle;
2021-12-16 13:36:21 +01:00
}
2018-01-25 20:26:09 +01:00
get expiration(): string {
if (!this.expMonth && !this.expYear) {
return null;
}
2021-12-16 13:36:21 +01:00
2018-01-25 20:26:09 +01:00
let exp = this.expMonth != null ? ("0" + this.expMonth).slice(-2) : "__";
exp += " / " + (this.expYear != null ? this.formatYear(this.expYear) : "____");
return exp;
2021-12-16 13:36:21 +01:00
}
2018-01-25 20:26:09 +01:00
private formatYear(year: string): string {
return year.length === 2 ? "20" + year : year;
2021-12-16 13:36:21 +01:00
}
2022-06-24 02:38:55 +02:00
2022-06-29 06:00:05 +02:00
toJSON(): Partial<CardView> {
// Needed to serialize getters which are not included by JSON.stringify
2022-06-29 02:23:01 +02:00
return {
cardholderName: this.cardholderName,
brand: this.brand,
number: this.number,
expMonth: this.expMonth,
expYear: this.expYear,
code: this.code,
};
2022-06-24 02:38:55 +02:00
}
2022-06-29 06:00:05 +02:00
static fromJSON(obj: Partial<CardView>): CardView {
2022-06-29 02:23:01 +02:00
const view = new CardView();
view.cardholderName = obj.cardholderName;
view.brand = obj.brand;
view.number = obj.number;
view.expMonth = obj.expMonth;
view.expYear = obj.expYear;
view.code = obj.code;
return view;
2022-06-24 02:38:55 +02:00
}
2018-01-24 17:33:15 +01:00
}