Skip to content

Commit

Permalink
Lesson code
Browse files Browse the repository at this point in the history
  • Loading branch information
tomer79sagi committed Feb 11, 2024
1 parent 48e819e commit 409f382
Show file tree
Hide file tree
Showing 4 changed files with 360 additions and 0 deletions.
102 changes: 102 additions & 0 deletions 26 - 11.2.24 - TS Class, DOM, REST API/exercise/class/app1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _User_id, _User_name, _User_email, _User_password, _User_createdAt, _User_isAdmin, _User_isBusiness;
class User {
constructor(name, address, email, phone, password) {
_User_id.set(this, void 0);
_User_name.set(this, void 0);
_User_email.set(this, void 0);
_User_password.set(this, void 0);
_User_createdAt.set(this, void 0);
_User_isAdmin.set(this, void 0);
_User_isBusiness.set(this, void 0);
__classPrivateFieldSet(this, _User_id, User.generateId(), "f");
this.name = name; // Uses the 'name' setter, re-using the capitalization functionality
this.address = address;
__classPrivateFieldSet(this, _User_email, email, "f");
this.phone = phone;
this.password = password;
__classPrivateFieldSet(this, _User_createdAt, new Date(), "f");
__classPrivateFieldSet(this, _User_isAdmin, false, "f");
__classPrivateFieldSet(this, _User_isBusiness, false, "f");
}
static generateId() {
return Math.floor(Math.random() * 8999999 + 1000000);
}
static capitalizeWord(str) {
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
get id() {
return __classPrivateFieldGet(this, _User_id, "f");
}
get name() {
return __classPrivateFieldGet(this, _User_name, "f");
}
set name(name) {
__classPrivateFieldSet(this, _User_name, `${User.capitalizeWord(name.first)} ${User.capitalizeWord(name.last)}`, "f");
}
get email() {
return __classPrivateFieldGet(this, _User_email, "f");
}
get password() {
return __classPrivateFieldGet(this, _User_password, "f");
}
set password(password) {
// This regex checks for at least one uppercase letter, one lowercase letter, exactly four digits, and one special character
const passwordRegex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d{4,})(?=.*[!@#$%^&*-]).*$/;
if (passwordRegex.test(password))
__classPrivateFieldSet(this, _User_password, password, "f");
else
throw new Error(`Password provided is invalid.`);
}
get createdAt() {
return __classPrivateFieldGet(this, _User_createdAt, "f");
}
get isAdmin() {
return __classPrivateFieldGet(this, _User_isAdmin, "f");
}
set isAdmin(isAdmin) {
__classPrivateFieldSet(this, _User_isAdmin, isAdmin, "f");
}
get isBusiness() {
return __classPrivateFieldGet(this, _User_isBusiness, "f");
}
toggleIsBusiness() {
if (__classPrivateFieldGet(this, _User_isAdmin, "f"))
__classPrivateFieldSet(this, _User_isBusiness, !__classPrivateFieldGet(this, _User_isBusiness, "f"), "f");
else
throw new Error(`You are not an Admin, and not authorized to perform this action.`);
}
}
_User_id = new WeakMap(), _User_name = new WeakMap(), _User_email = new WeakMap(), _User_password = new WeakMap(), _User_createdAt = new WeakMap(), _User_isAdmin = new WeakMap(), _User_isBusiness = new WeakMap();
try {
const userTomer = new User({
first: "tomer",
last: "sagi"
}, {
state: "Israel",
country: "Israel",
city: "Haifa",
street: "Kadish Luz",
houseNumber: 9,
zip: "3215907"
}, "[email protected]", "0528684411", "111111");
// userTomer.isAdmin = true;
// userTomer.password = '12345'; // Should throw an Error
userTomer.password = 'Tomer5555$';
console.log(userTomer);
userTomer.toggleIsBusiness();
}
catch (error) {
alert(`Error: ${error.message}`);
}
131 changes: 131 additions & 0 deletions 26 - 11.2.24 - TS Class, DOM, REST API/exercise/class/app1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

interface IName {
first: string;
last: string;
}

interface IAddress {
state: string;
country: string;
city: string;
street: string;
houseNumber: number;
zip: string;
}

class User {
#id: number;
#name?: string;
address: IAddress;
phone: string;
#email: string;
#password?: string;
#createdAt: Date;
#isAdmin: boolean;
#isBusiness: boolean;

constructor(name: IName, address: IAddress, email: string, phone: string, password: string) {
this.#id = User.generateId();
this.name = name; // Uses the 'name' setter, re-using the capitalization functionality
this.address = address;
this.#email = email;
this.phone = phone;
this.password = password;
this.#createdAt = new Date();
this.#isAdmin = false;
this.#isBusiness = false;
}

static generateId(): number {
return Math.floor(Math.random() * 8999999 + 1000000);
}

static capitalizeWord(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}

get id() {
return this.#id;
}

get name(): string {
return this.#name!;
}

set name(name: IName) {
this.#name = `${User.capitalizeWord(name.first)} ${User.capitalizeWord(name.last)}`;
}

get email() {
return this.#email;
}

get password() {
return this.#password!;
}

set password(password: string) {
// This regex checks for at least one uppercase letter, one lowercase letter, exactly four digits, and one special character
const passwordRegex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d{4,})(?=.*[!@#$%^&*-]).*$/;
if (passwordRegex.test(password))
this.#password = password;
else
throw new Error(`Password provided is invalid.`);
}

get createdAt() {
return this.#createdAt;
}

get isAdmin() {
return this.#isAdmin;
}

set isAdmin(isAdmin: boolean) {
this.#isAdmin = isAdmin;
}

get isBusiness() {
return this.#isBusiness;
}

toggleIsBusiness(): void {
if (this.#isAdmin)
this.#isBusiness = !this.#isBusiness;
else
throw new Error(`You are not an Admin, and not authorized to perform this action.`);
}
}


try {

const userTomer = new User(
{
first: "tomer",
last: "sagi"
},
{
state: "Israel",
country: "Israel",
city: "Haifa",
street: "Kadish Luz",
houseNumber: 9,
zip: "3215907"
},
"[email protected]",
"0528684411",
"111111"
);
// userTomer.isAdmin = true;

// userTomer.password = '12345'; // Should throw an Error
userTomer.password = 'Tomer5555$';
console.log(userTomer);
userTomer.toggleIsBusiness();

} catch(error: any) {
alert(`Error: ${error.message}`);
}


18 changes: 18 additions & 0 deletions 26 - 11.2.24 - TS Class, DOM, REST API/exercise/class/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>11.2.24</title>

<script defer src="./app1.js"></script>
</head>
<body>
<h1>11.2.24</h1>
<h2>Classes</h2>

<hr/>

<div id="output"></div>
</body>
</html>
Loading

0 comments on commit 409f382

Please sign in to comment.