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.

101 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-06-24 02:38:55 +02:00
import { Utils } from "@bitwarden/common/misc/utils";
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";
const propertyMap: any = {
cardholderName: null,
brand: null,
number: null,
expMonth: null,
expYear: null,
code: null,
};
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-27 23:50:26 +02:00
toJSON() {
return Utils.copyToNewObject(this, propertyMap);
2022-06-24 02:38:55 +02:00
}
static fromJSON(obj: any): CardView {
return Utils.copyToNewObject(obj, propertyMap, CardView);
2022-06-24 02:38:55 +02:00
}
2018-01-24 17:33:15 +01:00
}