Best JavaScript code snippet using storybook-root
muut-widget-latest-comments.js
Source:muut-widget-latest-comments.js
1/**2 * Contains the objects that are used for Muut Latest Comments widget.3 * Version 1.04 * Requires jQuery5 *6 * Copyright (c) 2014 Moot, Inc.7 * Licensed under MIT8 * http://www.opensource.org/licenses/mit-license.php9 */10jQuery(document).ready(function($) {11 // Once Muut is loaded...12 $('body').on('muut_loaded', function() {13 // Set the latest comments wrapper object.14 var widget_latest_comments_wrapper = $('#muut-widget-latest-comments-wrapper');15 // If the widget exists...16 if ( widget_latest_comments_wrapper.length > 0 ) {17 var widget_latest_comments_current_list_elements = $(widget_latest_comments_wrapper).find('.muut_recentcomments');18 var widget_latest_comments_num_showing = widget_latest_comments_current_list_elements.length;19 var widget_muut_base_path = muutObj().path;20 var muut_comments_root_paths = muut_latest_comments_path.map(function(cur) {21 return widget_muut_base_path + '/' + cur;22 });23 //var muut_comments_root_path = muutObj().path + '/' + muut_latest_comments_path;24 // Init all of the facelink functionality (tooltips and such).25 widget_latest_comments_wrapper.facelinkinit();26 // Get new results from the server.27 var muut_poll_wordpress_cache = function( timeout ) {28 setTimeout( function() {29 jQuery.ajax({30 url: muut_latest_comments_request_endpoint,31 async: false,32 cache: false,33 dataType: 'json',34 success: function(data) {35 var old_data = muut_latest_comments_json;36 muut_latest_comments_json = data;37 widget_latest_comments_wrapper.trigger('json_update', [ muut_latest_comments_json, old_data ] );38 if ( timeout >= 1000 ) {39 muut_poll_wordpress_cache( timeout );40 }41 }42 });43 }, timeout);44 };45 // Check to update the timestamps every minute.46 var update_time_displays = function() {47 setTimeout( function() {48 if ( Date.now() - last_update < 60000 ) {49 widget_latest_comments_current_list_elements = $(widget_latest_comments_current_list_elements.selector);50 }51 widget_latest_comments_current_list_elements.each(function(index, element) {52 var difference_timestamp = Date.now() - ( muut_latest_comments_json.latest_comments_posts[index].timestamp * 1000 );53 var new_display_time = muut_time_format( difference_timestamp );54 $(this).find('.muut-post-time-since').text(new_display_time);55 });56 update_time_displays();57 }, 60000);58 };59 var last_update = Date.now();60 update_time_displays();61 // Find out if a thread path is a WP Post commenting path.62 // Return the WP post id on success, or false on failure.63 var muut_is_wp_commenting_thread = function(path) {64 // The commenting base path.65 for ( i=0; i<muut_comments_root_paths.length; i++ ) {66 var path_post_id_re = new RegExp(muut_comments_root_paths[i] + '/([0-9]+)');67 // If the string lines up with the commenting base path.68 if (path.search(muut_comments_root_paths[i]) != -1) {69 // Do the regular expression comparison to get the WP Post id the path references.70 matches = path_post_id_re.exec(path);71 if (matches && typeof matches[1] != 'undefined') {72 return parseInt(matches[1]);73 }74 }75 }76 return false;77 };78 // Get the array of currently locally cached WP *post ids* (in same order as they are stored).79 var muut_get_cached_post_ids = function() {80 var current_post_ids = [];81 for (i = 0; i < muut_latest_comments_json.latest_comments_posts.length; i++) {82 current_post_ids.push(muut_latest_comments_json.latest_comments_posts[i].post_id);83 }84 return current_post_ids;85 };86 // Check if a given post id is already stored locally, and if so what the index is in the locally cached array.87 // Return the index OR -1 if there is no match.88 var muut_post_id_is_cached_locally = function(post_id){89 var current_post_ids = muut_get_cached_post_ids();90 return $.inArray(post_id, current_post_ids);;91 };92 // If we are going to poll the server for new posts...93 // The poll time must be greater than 1 second (1000 milliseconds).94 if ( muut_latest_comments_poll_time >= 1000 ) {95 // Poll the WP server to get the new JSON for the widget every <timeout> seconds.96 muut_poll_wordpress_cache( muut_latest_comments_poll_time );97 } else {98 // When a reply event comes through the websocket.99 muutRpc.on('reply', function( path, reply_object ) {100 var post_id = muut_is_wp_commenting_thread(path);101 if (post_id != 'false') {102 var new_data = $.extend(true,{},muut_latest_comments_json);103 var index_match = muut_post_id_is_cached_locally(post_id);104 if ( index_match >= 0 ) {105 var post_data = new_data.latest_comments_posts[index_match];106 new_object = {107 post_id: post_id,108 post_permalink: post_data.post_permalink,109 post_title: post_data.post_title,110 timestamp: Math.floor(Date.now() / 1000).toString(),111 user: {112 displayname: reply_object.user.displayname,113 img: reply_object.user.img,114 path: reply_object.user.path115 }116 };117 new_data.latest_comments_posts.splice( index_match, 1 );118 new_data.latest_comments_posts.unshift(new_object);119 widget_latest_comments_wrapper.trigger('json_update', [ new_data, muut_latest_comments_json ] );120 } else {121 setTimeout( function() {122 muut_poll_wordpress_cache(0);123 }, 4000124 );125 }126 }127 });128 muutRpc.on('post', function( location, post_object ) {129 // If the path shows that it is a comment on a post...130 var post_id = muut_is_wp_commenting_thread(location.path);131 if (post_id != 'false') {132 var new_data = $.extend(true,{},muut_latest_comments_json);133 var index_match = muut_post_id_is_cached_locally(post_id);134 if ( index_match >= 0 ) {135 var post_data = new_data.latest_comments_posts[index_match];136 new_object = {137 post_id: post_id,138 post_permalink: post_data.post_permalink,139 post_title: post_data.post_title,140 timestamp: Math.floor(Date.now() / 1000).toString(),141 user: {142 displayname: post_object.user.displayname,143 img: post_object.user.img,144 path: post_object.user.path145 }146 };147 new_data.latest_comments_posts.splice( index_match, 1 );148 new_data.latest_comments_posts.unshift(new_object);149 widget_latest_comments_wrapper.trigger('json_update', [ new_data, muut_latest_comments_json ] );150 } else {151 setTimeout( function() {152 muut_poll_wordpress_cache(0);153 }, 4000154 );155 }156 }157 });158 muutRpc.on('send', function(event, object) {159 if ( ( event == 'reply' || event == 'createMoot' ) && typeof object[0] != 'undefined' && typeof muut_wp_post_id != 'undefined' ) {160 if(muut_is_wp_commenting_thread(object[0].path)) {161 var new_data = $.extend(true,{},muut_latest_comments_json);162 var current_post_ids = [];163 for (i = 0; i < muut_latest_comments_json.latest_comments_posts.length; i++) {164 current_post_ids.push(muut_latest_comments_json.latest_comments_posts[i].post_id);165 }166 var index_match = muut_post_id_is_cached_locally(muut_wp_post_id);167 if ( index_match >= 0 ) {168 new_data.latest_comments_posts.splice( index_match, 1 );169 } else if (new_data.latest_comments_posts.length == 10 ) {170 new_data.latest_comments_posts.splice( 9, 1 );171 }172 new_object = {173 post_id: muut_wp_post_id,174 post_permalink: muut_wp_post_permalink,175 post_title: muut_wp_post_title,176 timestamp: Math.floor(Date.now() / 1000).toString(),177 user: {178 displayname: muutObj().user.displayname,179 img: muutObj().user.img,180 path: muutObj().user.path181 }182 };183 new_data.latest_comments_posts.unshift(new_object);184 widget_latest_comments_wrapper.trigger('json_update', [ new_data, muut_latest_comments_json ] );185 }186 }187 });188 }189 muutRpc.event('enter', function(user) {190 widget_latest_comments_wrapper.facelinkinit( true );191 });192 muutRpc.event('leave', function(user) {193 widget_latest_comments_wrapper.facelinkinit( true );194 });195 // Listen for the json_update event so that we can compare data and act accordingly.196 widget_latest_comments_wrapper.on('json_update', function( event, new_obj, old_obj ) {197 last_update = Date.now();198 widget_latest_comments_num_showing = widget_latest_comments_current_list_elements.length;199 muut_latest_comments_json = new_obj;200 new_obj = new_obj.latest_comments_posts;201 old_obj = old_obj.latest_comments_posts;202 var new_post_ids = [];203 for (i = 0; i < new_obj.length; i++) {204 new_post_ids.push(new_obj[i].post_id);205 }206 var num_new_items = false;207 var post_ids_to_delete = [];208 for(i = 0; i < widget_latest_comments_num_showing; i++) {209 var index_match = $.inArray(old_obj[i].post_id, new_post_ids);210 var old_string = JSON.stringify(old_obj[i]);211 var new_string = JSON.stringify(new_obj[index_match]);212 // If there is a match and it is identical to the old one, we know that the list has new items above it, but213 // it remains the top relating to the "old list".214 if(index_match >= 0 && old_string == new_string && num_new_items === false){215 num_new_items = index_match;216 // If there is a match, but it is different, then the element has had a new post. Continue to the next one to217 // see if it should be functioning as the new "top of previous list" item.218 } else if(index_match >= 0 && old_string != new_string) {219 post_ids_to_delete.push(old_obj[i].post_id);220 // If there is no post id match, that means ALL elements have been replaced by the new list.221 // Skip the rest of the loop.222 } else if (index_match == '-1' && num_new_items == 0){223 num_new_items = old_obj.length;224 break;225 }226 }227 // Delete the posts that are being replaced by updates to them (i.e. being moved to the top).228 for(i = 0; i < post_ids_to_delete.length; i++) {229 widget_latest_comments_wrapper.find('.muut_recentcomments[data-post-id="' + post_ids_to_delete[i] + '"]').remove();230 }231 // Refresh the current list of items and get figure out how many to remove from the bottom.232 widget_latest_comments_num_showing = widget_latest_comments_num_showing - post_ids_to_delete.length;233 var difference_count_from_new_items = muut_latest_comments_num_posts - num_new_items;234 for(i = 0; i < widget_latest_comments_num_showing - difference_count_from_new_items; i++) {235 $(widget_latest_comments_current_list_elements.get(-1)).remove();236 }237 // Generate the HTML for the new elements and prepend it to the list.238 var new_item_html = '';239 for(i = 0; i < num_new_items; i++) {240 // Get the pretty timestamp.241 var timestamp = Date.now() - (new_obj[i].timestamp * 1000);242 var list_time = muut_time_format( timestamp );243 // Generate the HTML.244 new_item_html += muut_latest_comments_row_template.replace(/%USER_PATH%/g, new_obj[i].user.path)245 .replace(/%USER_DISPLAYNAME%/g, new_obj[i].user.displayname)246 .replace(/%USER_IMAGEURL%/g, new_obj[i].user.img)247 .replace(/%POSTID%/g, new_obj[i].post_id)248 .replace(/%TIMESTAMP%/g, timestamp)249 .replace(/%LISTTIME%/g, list_time)250 .replace(/%POST_PERMALINK%/g, new_obj[i].post_permalink)251 .replace(/%POST_TITLE%/g, new_obj[i].post_title);252 }253 // Append the HTML.254 $('#muut-recentcomments').prepend(new_item_html).facelinkinit();255 });256 }257 });...
get_name_from_filename.ts
Source:get_name_from_filename.ts
1export default function get_name_from_filename(filename: string) {2 if (!filename) return null;3 const parts = filename.split(/[/\\]/).map(encodeURI);4 if (parts.length > 1) {5 const index_match = parts[parts.length - 1].match(/^index(\.\w+)/);6 if (index_match) {7 parts.pop();8 parts[parts.length - 1] += index_match[1];9 }10 }11 const base = parts.pop()12 .replace(/%/g, 'u')13 .replace(/\.[^.]+$/, '')14 .replace(/[^a-zA-Z_$0-9]+/g, '_')15 .replace(/^_/, '')16 .replace(/_$/, '')17 .replace(/^(\d)/, '_$1');18 if (!base) {19 throw new Error(`Could not derive component name from file ${filename}`);20 }21 return base[0].toUpperCase() + base.slice(1);...
Using AI Code Generation
1import React from 'react';2import { storiesOf } from '@storybook/react';3import { action } from '@storybook/addon-actions';4import { linkTo } from '@storybook/addon-links';5import { Welcome } from '@storybook/react/demo';6import Button from './Button';7import Welcome from './Welcome';8import { withKnobs, text, boolean, number } from '@storybook/addon-knobs/react';9storiesOf('Welcome', module).add('to Storybook', () => <Welcome showApp={linkTo('Button')} />);10storiesOf('Button', module)11 .addDecorator(withKnobs)12 .add('with text', () => (13 <Button onClick={action('clicked')}>{text('Label', 'Hello Button')}</Button>14 .add('with some emoji', () => (15 <Button onClick={action('clicked')} disabled={boolean('Disabled', false)}>16 .add('with some emoji and action', () => (17 <Button onClick={action('clicked')} disabled={boolean('Disabled', false)}>18 {text('Label', 'Hello Button')}19 .add('with some emoji and action', () => (20 <Button onClick={action('clicked')} disabled={boolean('Disabled', false)}>21 {text('Label', 'Hello Button')}22 .add('with some emoji and action', () => (23 <Button onClick={action('clicked')} disabled={boolean('Disabled', false)}>24 {text('Label', 'Hello Button')}25 .add('with some emoji and action', () => (26 <Button onClick={action('clicked')} disabled={boolean('Disabled', false)}>27 {text('Label', 'Hello Button')}28 .add('with some emoji and action', () => (29 <Button onClick={action('clicked')} disabled={boolean('Disabled', false)}>
Using AI Code Generation
1import { index_match } from 'storybook-root';2import { storiesOf } from '@storybook/react';3const stories = storiesOf('storybook-root', module);4stories.add('index_match', () => {5 const [index, setIndex] = useState(0);6 const [match, setMatch] = useState(false);7 const [path, setPath] = useState('/');8 const [pathArray, setPathArray] = useState(['/', '/home', '/about', '/contact']);9 const [pathIndex, setPathIndex] = useState(0);10 const [pathMatch, setPathMatch] = useState(false);11 const [pathMatchIndex, setPathMatchIndex] = useState(0);12 const [pathMatchMatch, setPathMatchMatch] = useState(false);13 const [pathMatchPath, setPathMatchPath] = useState('/');14 const [pathMatchPathArray, setPathMatchPathArray] = useState(['/', '/home', '/about', '/contact']);15 const [pathMatchPathIndex, setPathMatchPathIndex] = useState(0);16 const [pathMatchPathMatch, setPathMatchPathMatch] = useState(false);17 const [pathMatchPathMatchIndex, setPathMatchPathMatchIndex] = useState(0);18 const [pathMatchPathMatchMatch, setPathMatchPathMatchMatch] = useState(false);19 const [pathMatchPathMatchPath, setPathMatchPathMatchPath] = useState('/');20 const [pathMatchPathMatchPathArray, setPathMatchPathMatchPathArray] = useState(['/', '/home', '/about', '/contact']);21 const [pathMatchPathMatchPathIndex, setPathMatchPathMatchPathIndex] = useState(0);22 const [pathMatchPathMatchPathMatch, setPathMatchPathMatchPathMatch] = useState(false);23 const [pathMatchPathMatchPathMatchIndex, setPathMatchPathMatchPathMatchIndex] = useState(0);24 const [pathMatchPathMatchPathMatchMatch, setPathMatchPathMatchPathMatchMatch] = useState(false);25 const [pathMatchPathMatchPathMatchPath, setPathMatchPathMatchPathMatchPath] = useState('/');26 const [pathMatchPathMatchPathMatchPathArray, setPathMatchPathMatchPathMatchPathArray] = useState(['/', '/home', '/about', '/contact']);
Using AI Code Generation
1import { index_match } from 'storybook-root';2console.log(index_match(2, 2));3import { index_match } from './lib/index_match';4export { index_match };5import { index_match } from './index_match';6export { index_match };7export function index_match(index, length) {8 if (index < 0) {9 index = length + index;10 }11 if (index < 0 || index >= length) {12 throw new Error('Index out of bounds');13 }14 return index;15}16export function index_match(index, length) {17 if (index < 0) {18 index = length + index;19 }20 if (index < 0 || index >= length) {21 throw new Error('Index out of bounds');22 }23 return index;24}25export function index_match(index, length) {26 if (index < 0) {27 index = length + index;28 }29 if (index < 0 || index >= length) {30 throw new Error('Index out of bounds');31 }32 return index;33}34export function index_match(index, length) {35 if (index < 0) {36 index = length + index;37 }38 if (index < 0 || index >= length) {39 throw new Error('Index out of bounds');40 }41 return index;42}43export function index_match(index, length) {44 if (index < 0) {45 index = length + index;46 }47 if (index < 0 || index >= length) {48 throw new Error('Index out of bounds');49 }50 return index;51}52export function index_match(index, length) {53 if (index < 0) {54 index = length + index;55 }
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!!