Best JavaScript code snippet using redwood
TwTrk_cmds.js
Source:TwTrk_cmds.js
1TwTrkCmd_Login = function(args) {2 this.delegate = null;3 this.want_input = false;4 this.run = function() {5 this.delegate.send_string("[PROMPT_BOT]");6 this.delegate.send_string("æ£å¨éæ¨å° Twitter èªè...");7 setTimeout(function() {8 location.href = '/twitter_auth';9 }, 260);10 }11};12TwTrkCmd_Logout = function(args) {13 this.delegate = null;14 this.want_input = false;15 this.run = function() {16 this.delegate.send_string("[PROMPT_BOT]");17 this.delegate.send_string("æè¬æ¨ç使ç¨ï¼åè¦ï¼");18 setTimeout(function() {19 location.href = '/logout';20 }, 260);21 }22};23TwTrkCmd_SSL = function(args) {24 this.delegate = null;25 this.want_input = false;26 this.run = function() {27 this.delegate.send_string("[PROMPT_BOT]");28 this.delegate.send_string("æ£å¨ååå å¯é£ç·...");29 setTimeout(function() {30 location.href = 'https://twtrk.heroku.com/';31 }, 260);32 } 33}34TwTrkCmd_Help = function(args) {35 this.delegate = null;36 this.run = function() {37 this.delegate.send_string("[PROMPT_BOT]");38 this.delegate.send_string(" å¯ç¨æ令å表ï¼\r\n");39 this.delegate.send_string(" plurk - ç»å
¥å浪帳è\r\n");40 this.delegate.send_string(" sync (on|off) - åææ¯å¦åæ¥\r\n");41 this.delegate.send_string(" rt (on|off) - åææ¯å¦å
å« rT\r\n");42 this.delegate.send_string(" info - 顯示使ç¨è
è³è¨\r\n");43 this.delegate.send_string(" help - å¯ç¨æ令å表\r\n");44 this.delegate.send_string(" logout - ç»åºç³»çµ±\r\n");45 this.delegate.send_string("\r\n");46 this.delegate.send_string("\033[1;37mTwTrk\033[m, \033[1;32mtwitter\033[m->\033[1;31mplurk\033[m syncing bot\r\n");47 this.delegate.send_string("Written by \033[1;31mZero\033[m 2011\r\n");48 this.delegate._cmd_finished();49 }50}51TwTrkCmd_Info = function(args) {52 this.delegate = null;53 this.args = args;54 this.run = function() {55 this.delegate.send_string("[PROMPT_BOT]");56 this.delegate.send_string("è«ç¨å¾ï¼æ¥è©¢ä¸...");57 var obj = this;58 $.ajax({59 type: 'get',60 url:'/info',61 dataType: 'json',62 success: function(data) {63 obj.delegate.send_string("\033[17D");64 obj.delegate.send_string("\033[K");65 obj.delegate.send_string("親æçæå¡æ¨å¥½ï¼æ¨çè³è¨å¦ä¸ï¼\r\n");66 obj.delegate.send_string(" \033[1;32mTwitter\033[m 帳èï¼ " + data.twitter_username + "\r\n");67 obj.delegate.send_string(" \033[1;31mPlurk\033[m 帳èï¼ " + data.plurk_username + "\r\n");68 obj.delegate.send_string(" åæ¥åè½ï¼ [" + (data.should_sync ? "\033[1;32méå\033[m" : "\033[1;31méé\033[m") + "] " + (data.include_rts ? "\033[1;32må
å« rT\033[m" : "\033[1;31mä¸å
å« rT\033[m") + "\r\n");69 obj.delegate.send_string(" 註åæ¥æï¼ " + data.joined_at + "\r\n");70 obj.delegate.send_string("\r\n");71 obj.delegate.send_string("æå¾äºçåæ¥è¨éï¼\r\n");72 obj.delegate.send_string("çµæ | åæ¥çæ¸ | åæ¥æé\r\n");73 obj.delegate.send_string("=====================================\r\n");74 for(var i=0;i<data.logs.length;i++)75 {76 obj.delegate.send_string((data.logs[i].result ? "\033[1;32mæå\033[m" : "\033[1;31m失æ\033[m") + " |");77 obj.delegate.send_string(" " + data.logs[i].synced_twits + " |");78 obj.delegate.send_string(" " + data.logs[i].synced_at + "\r\n");79 }80 if (data.logs.length == 0)81 obj.delegate.send_string(" æ²æåæ¥è¨é\r\n");82 obj.delegate._cmd_finished();83 },84 error: function(xhr, ts, err) {85 obj.delegate.send_string("\033[17D");86 obj.delegate.send_string("\033[K");87 obj.delegate.send_string("é£ç·\033[1;31m失æ\033[mï¼è«ç¨å¾å試...\r\n");88 obj.delegate._cmd_finished();89 }90 });91 }92}93TwTrkCmd_Sync = function(args) {94 this.delegate = null;95 this.args = args;96 this.run = function() {97 if (this.args.size == 1)98 {99 this.delegate.send_string("[PROMPT_BOT]");100 this.delegate.send_string("說æï¼ä½¿ç¨ sync on éååæ¥ææ¯ sync off ééåæ¥ã")101 }102 else 103 {104 this.delegate.send_string("[PROMPT_BOT]");105 this.delegate.send_string("è«ç¨å¾ï¼è¨å®ä¸...");106 var obj = this;107 if (this.args[1] == "off")108 {109 $.ajax({110 type: 'get',111 url:'/set_sync?val=off',112 dataType: 'json',113 success: function(data) {114 obj.delegate.send_string("\033[17D");115 obj.delegate.send_string("\033[K");116 obj.delegate.send_string("åæ¥åè½å·²ç¶ \033[1;31méé\033[m\r\n");117 obj.delegate._cmd_finished();118 },119 error: function(xhr, ts, err) {120 obj.delegate.send_string("\033[17D");121 obj.delegate.send_string("\033[K");122 obj.delegate.send_string("é£ç·\033[1;31m失æ\033[mï¼è«ç¨å¾å試...\r\n");123 obj.delegate._cmd_finished();124 }125 });126 }127 else128 {129 $.ajax({130 type: 'get',131 url:'/set_sync?val=on',132 dataType: 'json',133 success: function(data) {134 obj.delegate.send_string("\033[17D");135 obj.delegate.send_string("\033[K");136 if (data.status == 'ok')137 obj.delegate.send_string("åæ¥åè½å·²ç¶ \033[1;32måå\033[m\r\n");138 else if (data.status == 'err')139 {140 if (data.msg == 'NEED_PLURK_CREDENTIALS')141 obj.delegate.send_string("ç¡æ³åååæ¥ï¼è«å
ä½¿ç¨ \033[1;31mplurk\033[m\r\næ令ç»å
¥\033[1;31må浪\033[mã\r\n");142 else143 obj.delegate.send_string("ä¸æé¯èª¤ï¼è«ç¨å¾å試...\r\n");144 }145 obj.delegate._cmd_finished();146 },147 error: function(xhr, ts, err) {148 obj.delegate.send_string("\033[17D");149 obj.delegate.send_string("\033[K");150 obj.delegate.send_string("é£ç·\033[1;31m失æ\033[mï¼è«ç¨å¾å試...\r\n");151 obj.delegate._cmd_finished();152 }153 });154 }155 }156 }157}158TwTrkCmd_RT = function(args) {159 this.delegate = null;160 this.args = args;161 this.run = function() {162 if (this.args.size == 1)163 {164 this.delegate.send_string("[PROMPT_BOT]");165 this.delegate.send_string("說æï¼ä½¿ç¨ rt on/off è¨å®æ¯å¦å
å«rTã")166 }167 else 168 {169 this.delegate.send_string("[PROMPT_BOT]");170 this.delegate.send_string("è«ç¨å¾ï¼è¨å®ä¸...");171 var obj = this;172 if (this.args[1] == "off")173 {174 $.ajax({175 type: 'get',176 url:'/set_rts?val=off',177 dataType: 'json',178 success: function(data) {179 obj.delegate.send_string("\033[17D");180 obj.delegate.send_string("\033[K");181 obj.delegate.send_string("rT å·²ç¶ \033[1;31méé\033[m\r\n");182 obj.delegate._cmd_finished();183 },184 error: function(xhr, ts, err) {185 obj.delegate.send_string("\033[17D");186 obj.delegate.send_string("\033[K");187 obj.delegate.send_string("é£ç·\033[1;31m失æ\033[mï¼è«ç¨å¾å試...\r\n");188 obj.delegate._cmd_finished();189 }190 });191 }192 else193 {194 $.ajax({195 type: 'get',196 url:'/set_rts?val=on',197 dataType: 'json',198 success: function(data) {199 obj.delegate.send_string("\033[17D");200 obj.delegate.send_string("\033[K");201 obj.delegate.send_string("rT å·²ç¶ \033[1;32måå\033[m\r\n");202 obj.delegate._cmd_finished();203 },204 error: function(xhr, ts, err) {205 obj.delegate.send_string("\033[17D");206 obj.delegate.send_string("\033[K");207 obj.delegate.send_string("é£ç·\033[1;31m失æ\033[mï¼è«ç¨å¾å試...\r\n");208 obj.delegate._cmd_finished();209 }210 });211 }212 }213 }214}215TwTrkCmd_Plurk = function(args) {216 this.delegate = null;217 var obj = this;218 this.states = {219 WAIT_USERNAME: 'WAIT_USERNAME',220 WAIT_PASSWORD: 'WAIT_PASSWORD',221 WAIT_AUTH: 'WAIT_AUTH'222 };223 this.state = this.states.WAIT_USERNAME;224 this.run = function() {225 this.delegate.send_string("[PROMPT_BOT]");226 this.delegate.send_string("è«è¼¸å
¥å浪使ç¨è
å稱ï¼\r\n > ")227 }228 this.want_input = true;229 this.receiving_password = false;230 this.username = "";231 this.password = "";232 this.recv_line = function(str) {233 switch(this.state) {234 case this.states.WAIT_USERNAME:235 this.username = str;236 this.state = this.states.WAIT_PASSWORD;237 this.receiving_password = true;238 this.delegate.send_string("[PROMPT_BOT]");239 this.delegate.send_string("è«è¼¸å
¥å浪å¯ç¢¼ï¼\r\n è«æ³¨ææ¨çå浪å¯ç¢¼\r\n å°ä»¥\033[1;31mæ碼\033[må²åå¨ä¼ºæå¨ä¸ã\r\n > ")240 break;241 case this.states.WAIT_PASSWORD:242 this.password = str;243 this.state = this.states.WAIT_AUTH;244 this.want_input = false;245 this.delegate.send_string("[PROMPT_BOT]");246 this.delegate.send_string("è«ç¨å¾é©èä¸...\r\n");247 var obj = this;248 $.ajax({249 type: 'post',250 url:'/plurk_auth',251 data: {username: this.username, password: this.password},252 dataType: 'json',253 success: function(data) {254 obj.delegate.send_string("[PROMPT_BOT]");255 obj.delegate.send_string("ç»å
¥\033[1;32mæå\033[mï¼å浪å°éå§åæ¥ï¼\r\n");256 obj.delegate._cmd_finished();257 },258 error: function(xhr, ts, err) {259 obj.delegate.send_string("[PROMPT_BOT]");260 obj.delegate.send_string("ç»å
¥\033[1;31m失æ\033[mï¼è«éæ°ç»å
¥å浪ã\r\n");261 obj.delegate._cmd_finished();262 }263 });264 break;265 }266 }...
TwTrk.js
Source:TwTrk.js
...33 return;34 35 if (data.data == "\r")36 {37 this.send_string("\r\n");38 if (this._running_cmd != null && this._running_cmd.recv_line)39 this._running_cmd.recv_line(this._cmdbuf);40 else41 {42 this._cmdbuf = str_trim(this._cmdbuf);43 var args = this._cmdbuf.split(" ");44 var cmd = args[0];45 if (this._cmdset[cmd] != null)46 {47 var running_cmd = new this._cmdset[cmd](args);48 running_cmd.delegate = this;49 this._running_cmd = running_cmd;50 setTimeout(function() {51 running_cmd.run();52 }, 1);53 }54 else55 {56 this.send_string("[PROMPT_BOT]");57 this.send_string("å... æè½ä¸æä½ å¨èªªä»éº¼è¶ï¼å¯ä»¥åå訴æä¸æ¬¡åï¼\r\n");58 this.send_string("[PROMPT_USER]");59 }60 }61 this._cmdbuf = "";62 }63 else if (data.data == "\b")64 {65 if (this._cmdbuf.length > 0)66 {67 this.send_string("\033[1D");68 this.send_string("\033[K");69 this._cmdbuf = this._cmdbuf.substr(0, this._cmdbuf.length - 1);70 }71 }72 else73 {74 if (this._running_cmd && this._running_cmd.receiving_password)75 this.send_string("*");76 else77 this.send_string(data.data);78 this._cmdbuf += data.data;79 }80 81 this._term.flushBuffer();82 }83 84 this._cmd_finished = function() {85 this._running_cmd = null;86 this.send_string("[PROMPT_USER]");87 }88 this.send_guest_greeting = function()89 {90 this.send_string("[PROMPT_BOT]");91 this.send_string("æ¡è¿ä½¿ç¨\033[1;31må浪\033[m\033[1;32mæ¨ç¹\033[måæ¥æ©å¨äººã\r\n");92 this.send_string("[PROMPT_BOT]");93 this.send_string("è«è¼¸å
¥ \033[1;31mlogin\033[m ä½¿ç¨ Twitter 帳èé©è身åã\r\n");94 if (window.location.protocol != 'https:')95 {96 this.send_string('[PROMPT_BOT]');97 this.send_string('æ³ä½¿ç¨ SSL å®å
¨é£ç·ä¿è·ä½ \r\nçé±ç§ï¼ è«è¼¸å
¥ \033[1;31mSSL\033[m æ令ï¼\r\n');98 }99 this.send_string("[PROMPT_USER]");100 }101 this.send_user_greeting = function()102 {103 this.send_string("[PROMPT_BOT]");104 this.send_string("æ¡è¿ä½¿ç¨\033[1;31må浪\033[m\033[1;32mæ¨ç¹\033[måæ¥æ©å¨äººã\r\n");105 this.send_string("[PROMPT_BOT]");106 this.send_string("Twitter 帳è確èªå®æï¼ä½¿ç¨\r\n\033[1;31mplurk\033[m æ令ç»å
¥å浪å¾å³å¯éå§åæ¥ã\r\n");107 this.send_string("[PROMPT_BOT]");108 this.send_string("è¥æä»»ä½åé¡ï¼è«ç¨ \033[1;31mhelp\033[m æ令ã\r\n");109 this.send_string("[PROMPT_USER]");110 }111 112 this.send_msg_greeting = function(msgid)113 {114 if (msgid == "TWITTER_AUTH_FAILED")115 {116 this.send_string("[PROMPT_BOT]");117 this.send_string("èªè失æï¼");118 this.send_string("[PROMPT_BOT]");119 this.send_string("è«è¼¸å
¥ \033[1;37mlogin\033[m ä½¿ç¨ Twitter 帳èé©è身åã\r\n");120 this.send_string("[PROMPT_USER]");121 }122 }123 124 /* private functions */125 this.send_string = function(str)126 {127 this._send_buf.push(str);128 }129 130 this.text_sending_task = function()131 {132 var obj = this;133 if (this._send_buf.length > 0)134 {135 if (!this._sending_lock)136 this.random_delay_send_text(this._send_buf.shift());137 }138 setTimeout(function() { obj.text_sending_task.apply(obj); }, 100);139 }140 this.text_sending_task();141 142 this.random_delay_send_text = function(str)143 {144 if (str.length == 0)145 {146 this._sending_lock = false;147 return;148 }149 150 this._sending_lock = true;151 var obj = this;152 var nextstr = str.substr(1, str.length);153 if (str == "[PROMPT_BOT]")154 {155 this._term.incomingData("\033[1;34mTwTrk\033[m> ");156 nextstr = "";157 }158 else if (str == "[PROMPT_USER]")159 {160 this._term.incomingData("\033[1;37m" + (this._username == "" ? "Guest> " : this._username + "\033[m> "));161 nextstr = "";162 }163 else164 {165 this._term.incomingData(str[0]);166 }167 this._term.flushBuffer();168 setTimeout(function() { obj.random_delay_send_text.apply(obj, [nextstr]); }, Math.random() * 5);169 }170 /* system init */171 var obj = this;172 this.send_string("è«ç¨å¾ï¼æ£å¨è¼å
¥æ©å¨äººç¨å¼ä¸...");173 $.get('/get_status', {}, function(data) {174 if (data.user_state == 'guest')175 obj._user_state = USER_GUEST;176 else177 {178 obj._user_state = USER_AUTHED;179 obj._username = data.username;180 }181 182 obj._cmdset = obj._cmdsets[obj._user_state];183 184 obj.send_string('\033[1J');185 if (data.send_msg)186 obj.send_msg_greeting.apply(obj, [data.send_msg]);187 else if (obj._user_state == USER_GUEST)188 obj.send_guest_greeting.apply(obj);189 else190 obj.send_user_greeting.apply(obj);191 }, 'json');192}193// Author: Ariel Flesler194// http://flesler.blogspot.com/2008/11/fast-trim-function-for-javascript.html195// Licensed under BSD196function str_trim(str) {197 var start = -1,198 end = str.length;...
casinoService.js
Source:casinoService.js
1// Local requires2const Casino = require('../casino/casino');3let casino_balance = 1000000000;4class CasinoService {5 constructor (client_balance, BET) {6 7 this.client_balance = client_balance;8 this.BET = BET;9 }10 SPIN = async() => {11 let matrix = await Casino.creatMatrix();12 let win_sum = await Casino.check(matrix, this.BET);13 if(this.client_balance < this.BET) {14 throw new Error(`you dont have enough money!`);15 }16 casino_balance += this.BET;17 this.client_balance += win_sum - this.BET;18 casino_balance -= win_sum;19 let send_string = '';20 send_string += 'your balance = ' + this.client_balance + '\n\n' ;21 send_string += matrix[0] + '\n' + matrix[1] + '\n' + matrix[2] + '\n\n\n';22 send_string += `your Win = ${win_sum}`;23 24 return send_string;25 }26}...
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!!