Skip to content

Instantly share code, notes, and snippets.

View shootermv's full-sized avatar
😀
working on rewrite MESI app with react and redux

Moshe shootermv

😀
working on rewrite MESI app with react and redux
View GitHub Profile
@shootermv
shootermv / product-list.component.ts
Created August 22, 2023 11:14
product-list.component.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'product-list',
template: `
<h1>Products List</h1>
<div *ngFor="let product of products">{{product.name}}<div>
`,
styles: [`:host{border: 1px solid #000;}`]
})
@shootermv
shootermv / shopping-cart.component.ts
Created August 14, 2023 11:49
shopping-cart.component
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'shopping-cart',
template: `
<h1>Shopping Cart ({{calcTotal()}})</h1>
<cart-product *ngFor="let product of products" [product]="product" (productRemoved)="removeProduct($event)"><cart-product>
`,
styles: [`:host{border: 1px solid #000;}`]
})
@shootermv
shootermv / app.component.ts
Created August 13, 2023 13:36
addProductToCart method
addProductToCart(product) {
const productExistInCart = this.cartProductList.find(
({ name }) => name === product.name
); // find product by name
if (!productExistInCart) {
this.cartProductList.push({ ...product, num: 1 }); // enhance "porduct" object with "num" property
return;
}
productExistInCart.num += 1;
}
@shootermv
shootermv / app.module.ts
Last active August 13, 2023 13:31
app.module.ts - starting
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { ProductListComponent } from './product-list.component'; // hello replaced
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [
@shootermv
shootermv / formatMessage.util.ts
Created September 15, 2022 06:41
tuil for formatting message
export function formatMessage(str: string, term: string, showMore: boolean) {
if (term) {
str = str.replace(
new RegExp(term, "ig"),
`<b style="color:red">${term}</b>`
);
}
if (showMore) return str;
return `${str.slice(0, 100)}`;
}
@shootermv
shootermv / formatMessage.test.js
Created September 15, 2022 05:29
test for show-more util
import { formatMessage } from "./util";
test("pass empty args", () => {
const showMore = false;
expect(formatMessage("", "", showMore)).toBe("");
});
test("pass 10 chars long str", () => {
const showMore = false;
expect(formatMessage("1".repeat(10), "", showMore).length).toBe(10);
});
@shootermv
shootermv / es5-outdated-correct-answer.js
Created April 29, 2022 09:32
es5 outdated question "correct" answer
document.getElementById("app").innerHTML = `
<h1>Hello Vanilla!</h1>
<ul id="ul">
<li>1 </li>
<li>2 </li>
<li>3 </li>
<li>4 </li>
<li>5 </li>
</ul>
`;
@shootermv
shootermv / es5-outdated.js
Created April 29, 2022 09:24
es5 outdated question
document.getElementById("app").innerHTML = `
<h1>Hello Vanilla!</h1>
<ul id="ul">
<li>1 </li>
<li>2 </li>
<li>3 </li>
<li>4 </li>
<li>5 </li>
</ul>
`;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>objectVsArray</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@shootermv
shootermv / gist:2f15daadb479e176b50108297b4fa214
Last active March 6, 2020 05:42
implementation of "flat" array using recursion and using stack
// using recursion
const myFlat1 = arr => arr.reduce((acc, item) => [...acc, ...(Array.isArray(item) ? myFlat1(item) : [item])], []);
// because recursion is less performant (space complexity)
// it is better to use "while" loop and stack for traversing the array
const myFlat2 = arr => {
let results = [], stack = []; // using stack to store pointer to outer array
while(true) {
let item = arr.shift(); // each time we take next item we cutting it from the array