Best JavaScript code snippet using wpt
shopping-cart.service.ts
Source:shopping-cart.service.ts
1import { Observable, Subscription } from 'rxjs';2import { AngularFireDatabase, AngularFireObject } from '@angular/fire/database';3import { Injectable } from '@angular/core';4import { Product } from 'shared/models/product';5import { ShoppingCartItem } from 'shared/models/shopping-cart-item';6import { take, map } from 'rxjs/operators';7import { ShoppingCart } from 'shared/models/shopping-cart';8@Injectable({9 providedIn: 'root'10})11export class ShoppingCartService {12 sub: Subscription;13 14 constructor(private db: AngularFireDatabase) { 15 }16 async getCart(): Promise<Observable<ShoppingCart>> {17 let cartId = await this.getOrCreateId();18 return this.db.object('/shopping-carts/'+ cartId).valueChanges()19 .pipe(map(x => new ShoppingCart((<any>x).items)));20 }21 async addToCart(product: Product) {22 this.updateItem(product, +1);23 }24 async removeFromCart(product: Product) {25 this.updateItem(product, -1);26 }27 async clearCart() {28 let cartId = await this.getOrCreateId();29 this.db.object('/shopping-carts/' + cartId + '/items').remove();30 }31 32 private getItem(cartId: string, productId: string) {33 return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);34 }35 private create() {36 return this.db.list('/shopping-carts/').push({37 dateCreated: new Date().getTime()38 });39 }40 private async getOrCreateId() {41 let cartId = localStorage.getItem('cartId');42 if(cartId) return cartId;43 44 let result = await this.create();45 localStorage.setItem('cartId', result.key)46 return result.key;47 }48 async insertInCart(product: Product) {49 let cartId = await this.getOrCreateId();50 let item$ = this.getItem(cartId, product.key);51 item$.valueChanges().pipe(take(1))52 .subscribe(() => {53 item$.set({54 title: product.title,55 imageUrl: product.imageUrl,56 price: product.price,57 quantity: 158 })59 })60 }61 private async updateItem(product: Product, change: number) {62 let cartId = await this.getOrCreateId();63 let item$ = this.getItem(cartId, product.key);64 item$.valueChanges().pipe(take(1))65 .subscribe(item => {66 let quantity = (<ShoppingCartItem>item).quantity + change;67 if(quantity === 0) item$.remove();68 69 else item$.update({70 title: product.title,71 imageUrl: product.imageUrl,72 price: product.price,73 quantity: quantity74 })75 })76 }...
cart.service.ts
Source:cart.service.ts
1import { Injectable } from "@angular/core";2import { AngularFireDatabase } from "angularfire2/database";3import { Product } from "shared/model/product.model";4import { ShoppingCart } from "shared/model/shopping-cart";5import { Observable, Subject } from "rxjs";6import "rxjs/add/operator/take";7import "rxjs/add/operator/map";8@Injectable()9export class CartService {10 cartItemsCountCahnged = new Subject<ShoppingCart>();11 constructor(private afdb: AngularFireDatabase) {}12 private create() {13 return this.afdb.list("shopping-carts").push({14 cartCreated: new Date().getTime()15 });16 }17 async getCart(): Promise<Observable<ShoppingCart>> {18 const cartId = await this.getOrCreateId();19 return this.afdb20 .object("/shopping-carts/" + cartId)21 .valueChanges()22 .map((x: any) => {23 if (x === null) {24 this.cartItemsCountCahnged.next(new ShoppingCart({}));25 return new ShoppingCart({});26 } else {27 this.cartItemsCountCahnged.next(new ShoppingCart(x.items));28 return new ShoppingCart(x.items);29 }30 });31 }32 private getItem(cartId: string, productId: string) {33 return this.afdb.object(34 "/shopping-carts/" + cartId + "/items/" + productId35 );36 }37 private async getOrCreateId(): Promise<string> {38 const cartId = localStorage.getItem("cartId");39 if (cartId) return cartId;40 const result = await this.create();41 localStorage.setItem("cartId", result.key);42 return result.key;43 }44 addToCart(product) {45 this.updateCart(product, 1);46 }47 removeFromCart(product) {48 this.updateCart(product, -1);49 }50 async clearItems() {51 const cartId = await this.getOrCreateId();52 this.afdb.object("/shopping-carts/" + cartId + "/items").remove();53 }54 private async updateCart(product, change: number) {55 const cartId = await this.getOrCreateId();56 const item = this.getItem(cartId, product.key);57 item58 .valueChanges()59 .take(1)60 .subscribe((resItem: Product) => {61 if (resItem) {62 const quantity = (resItem.quantity || 0) + change;63 quantity ? item.update({ quantity: quantity }) : item.remove();64 } else {65 item.update({66 title: product.payload.val().title,67 imageUrl: product.payload.val().imageUrl,68 price: product.payload.val().price,69 quantity: change70 });71 }72 });73 }...
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var async = require('async');4var request = require('request');5var cheerio = require('cheerio');6var _ = require('underscore');7var natural = require('natural');8var stopwords = require('stopwords').english;9var stemmer = require('stemmer');10var csv = require('csv');11var keywords = fs.readFileSync('keywords.txt').toString().split("\n");12var politicians = fs.readFileSync('politicians.txt').toString().split("\n");13var politicians = fs.readFileSync('politicians.txt').toString().split("\n");14var data = fs.readFileSync('politicians.txt').toString().split("\n");15var urls = [];16for (var i = 0; i < politicians.length; i++) {17}18var results = [];19var getText = function(url, callback) {20 request(url, function(error, response, body) {21 if (error) { callback(error, null); }22 if (response.statusCode !== 200) { callback(new Error("Status code: " + response.statusCode), null); }23 var $ = cheerio.load(body);24 var text = $('p').text();25 text = text.replace(/\[[0-9]+\]/g, "");26 text = text.replace(/[^a-zA-Z0-9\s]/g, "");27 text = text.replace(/\s\s+/g, " ");28 callback(null, text);29 });30};31var getKeywords = function(text, callback) {
Using AI Code Generation
1var wptools = require("wptools");2var id = wptools.getOrCreateID("Albert Einstein");3var wptools = require("wptools");4var id = wptools.getOrCreateID("Albert Einstein", 12345);5var wptools = require("wptools");6var id = wptools.getOrCreateID("Albert Einstein", "Albert_Einstein");7var wptools = require("wptools");8var id = wptools.getOrCreateID("Albert Einstein", 12345, "Albert_Einstein");9var wptools = require("wptools");10var id = wptools.getOrCreateID("Albert Einstein", 12345, "Albert_Einstein", "en");11var wptools = require("wptools");12var id = wptools.getOrCreateID("Albert Einstein", 12345, "Albert_Einstein", "en", "wikipedia");13var wptools = require("wptools");14var id = wptools.getOrCreateID("Albert Einstein", 12345, "Albert_Einstein", "en", "wikipedia", "wikipedia.org");15var wptools = require("wptools");16var id = wptools.getOrCreateID("Albert Einstein", 12345, "Albert_Einstein", "en", "wikipedia", "wikipedia.org", "https");17var wptools = require("wptools");18var id = wptools.getOrCreateID("Albert Einstein", 12345, "Albert_Einstein", "en", "wikipedia", "wikipedia.org",
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Barack Obama');3page.getOrCreateID(function(err, resp) {4 console.log(resp);5});6| `headers` | `Object` | `{}` | Additional headers to send with the request. |
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.getOrCreateID(function(id) {3 console.log(id);4});5var wpt = require('./wpt.js');6wpt.getOrCreateID(function(id) {7 console.log(id);8 wpt.getTestResults(id, function(results) {9 console.log(results);10 });11});12 wpt.getOrCreateID(function(id) {13 document.write(id);14 wpt.getTestResults(id, function(results) {15 document.write(results);16 });17 });18 wpt.getOrCreateID(function(id) {19 document.write(id);20 wpt.getTestResults(id, function(results) {21 document.write(results);22 }, function() {23 });24 });
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!