Skip to content

Commit

Permalink
Log learning 03/02/2022 on modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Nguyen committed Mar 3, 2022
1 parent 9d1076b commit e237a85
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 8-using-inheritance/exercises/exercise-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

class FunctionalSet extends Set {
filter(predicate) {
return new FunctionalSet([...this].filter(predicate));
}
map(mapper) {
return new FunctionalSet([...this].map(mapper));
}
reduce(accumulator, identity) {
return [...this].reduce(accumulator, identity);
}
}

const set = new FunctionalSet(["Jack", "Jill", "Tom", "Jerry"]).add("Bill");
console.log(set);
45 changes: 45 additions & 0 deletions 8-using-inheritance/exercises/exercise-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";

class BoundedSet extends Set {
constructor(capacity, initialValues) {
super();
this.capacity = capacity;

if (initialValues.length <= capacity) {
initialValues.forEach((value) => this.add(value));
}
}

add(value) {
if (this.has(value)) return;

if (this.size < this.capacity) {
super.add(value);
} else {
throw new Error(`exceeded vapacity of ${this.capacity} elements`);
}
}

static get [Symbol.species]() {
return BoundedSet;
}
}

const set = new BoundedSet(5, ["Apple", "Banana", "Grape", "Mangoe"]);

set.add("Orange");
set.add("Apple");

try {
set.add("Tangerine");
} catch (ex) {
console.log(ex.message); // exceeded capacity of 5 elements
}

set.delete("Grape");
set.add("Peach");
console.log(set.size); // 5

const set2 = new BoundedSet(2, ["Apple", "Banana", "Grape"]);
console.log(set2.size); // 0
console.log(set2); // BoundedSet {capacity: 2}
23 changes: 23 additions & 0 deletions 8-using-inheritance/exercises/exercise-4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";
class Base {
copy() {
const constructor =
Reflect.getPrototypeOf(this).constructor[Symbol.species] ||
Reflect.getPrototypeOf(this).constructor;
return new constructor();
}
}
class Derived1 extends Base {
static get [Symbol.species]() {
return Base;
}
}
class Derived2 extends Base {
static get [Symbol.species]() {
return Derived2;
}
}
const derived1 = new Derived1();
const derived2 = new Derived2();
console.log(derived1.copy()); //Base {}
console.log(derived2.copy()); //Derived2 {}
5 changes: 5 additions & 0 deletions 9-using-modules/left.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { right } from "./right.mjs";
import { middle } from "./middle.mjs";

middle();
right();
7 changes: 7 additions & 0 deletions 9-using-modules/middle.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { right } from "./right.mjs";

console.log("executing middle module");

export const middle = function () {
console.log("middle called");
};
6 changes: 6 additions & 0 deletions 9-using-modules/right.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
console.log("executing right module");
const message = "right called";

export const right = function () {
console.log(message);
};
32 changes: 32 additions & 0 deletions 9-using-modules/temperature.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const FREEZING_POINT = 0;

export function f2c(fahrenheit) {
return (fahrenheit - 32) / 1.8;
}

export const temperaturePoints = { freezing: 0, boiling: 100 };

export class Thermostat {
constructor() {}
}

const FREEZING_POINT_IN_F = 32;

function c2f(celsius) {
return celsius * 1.8 + 32;
}

const FREEZING_POINT_IN_K = 237.15,
BOILING_POINT_IN_K = 373.15;

export { c2f, FREEZING_POINT_IN_K };

function c2k(celsius) {
return celsius + 273.15;
}

export { c2k as celsiusToKelvin };

export default function unitsOfMeasures() {
return ["Celsius", "Delisle scale", "Fahrenheit", "Kelvin"];
}

0 comments on commit e237a85

Please sign in to comment.