Best JavaScript code snippet using storybook-root
relative.js
Source:relative.js
1goog.provide('goog.date.relative'); 2goog.require('goog.i18n.DateTimeFormat'); 3goog.date.relative.MINUTE_MS_ = 60000; 4goog.date.relative.DAY_MS_ = 86400000; 5goog.date.relative.Unit_ = { 6 MINUTES: 0, 7 HOURS: 1, 8 DAYS: 2 9}; 10goog.date.relative.fullDateFormatter_; 11goog.date.relative.shortTimeFormatter_; 12goog.date.relative.monthDateFormatter_; 13goog.date.relative.formatMonth_ = function(date) { 14 if(! goog.date.relative.monthDateFormatter_) { 15 goog.date.relative.monthDateFormatter_ = new goog.i18n.DateTimeFormat('MMM dd'); 16 } 17 return goog.date.relative.monthDateFormatter_.format(date); 18}; 19goog.date.relative.formatShortTime_ = function(date) { 20 if(! goog.date.relative.shortTimeFormatter_) { 21 goog.date.relative.shortTimeFormatter_ = new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.SHORT_TIME); 22 } 23 return goog.date.relative.shortTimeFormatter_.format(date); 24}; 25goog.date.relative.formatFullDate_ = function(date) { 26 if(! goog.date.relative.fullDateFormatter_) { 27 goog.date.relative.fullDateFormatter_ = new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.FULL_DATE); 28 } 29 return goog.date.relative.fullDateFormatter_.format(date); 30}; 31goog.date.relative.format = function(dateMs) { 32 var now = goog.now(); 33 var delta = Math.floor((now - dateMs) / goog.date.relative.MINUTE_MS_); 34 var future = false; 35 if(delta < 0) { 36 future = true; 37 delta *= - 1; 38 } 39 if(delta < 60) { 40 return goog.date.relative.getMessage_(delta, future, goog.date.relative.Unit_.MINUTES); 41 } else { 42 delta = Math.floor(delta / 60); 43 if(delta < 24) { 44 return goog.date.relative.getMessage_(delta, future, goog.date.relative.Unit_.HOURS); 45 } else { 46 var offset = new Date(goog.now()).getTimezoneOffset() * goog.date.relative.MINUTE_MS_; 47 delta = Math.floor((now + offset) / goog.date.relative.DAY_MS_) - Math.floor((dateMs + offset) / goog.date.relative.DAY_MS_); 48 if(future) { 49 delta *= - 1; 50 } 51 if(delta < 14) { 52 return goog.date.relative.getMessage_(delta, future, goog.date.relative.Unit_.DAYS); 53 } else { 54 return ''; 55 } 56 } 57 } 58}; 59goog.date.relative.formatPast = function(dateMs) { 60 var now = goog.now(); 61 if(now < dateMs) { 62 dateMs = now; 63 } 64 return goog.date.relative.format(dateMs); 65}; 66goog.date.relative.formatDay = function(dateMs) { 67 var message; 68 var today = new Date(goog.now()); 69 today.setHours(0); 70 today.setMinutes(0); 71 today.setSeconds(0); 72 today.setMilliseconds(0); 73 var yesterday = new Date(today.getTime() - goog.date.relative.DAY_MS_); 74 if(today.getTime() < dateMs) { 75 var MSG_TODAY = goog.getMsg('Today'); 76 message = MSG_TODAY; 77 } else if(yesterday.getTime() < dateMs) { 78 var MSG_YESTERDAY = goog.getMsg('Yesterday'); 79 message = MSG_YESTERDAY; 80 } else { 81 message = goog.date.relative.formatMonth_(new Date(dateMs)); 82 } 83 return message; 84}; 85goog.date.relative.getDateString = function(date, opt_shortTimeMsg, opt_fullDateMsg) { 86 return goog.date.relative.getDateString_(date, goog.date.relative.format, opt_shortTimeMsg, opt_fullDateMsg); 87}; 88goog.date.relative.getPastDateString = function(date, opt_shortTimeMsg, opt_fullDateMsg) { 89 return goog.date.relative.getDateString_(date, goog.date.relative.formatPast, opt_shortTimeMsg, opt_fullDateMsg); 90}; 91goog.date.relative.getDateString_ = function(date, relativeFormatter, opt_shortTimeMsg, opt_fullDateMsg) { 92 var dateMs = date.getTime(); 93 var relativeDate = relativeFormatter(dateMs); 94 if(relativeDate) { 95 relativeDate = ' (' + relativeDate + ')'; 96 } 97 var delta = Math.floor((goog.now() - dateMs) / goog.date.relative.MINUTE_MS_); 98 if(delta < 60 * 24) { 99 return(opt_shortTimeMsg || goog.date.relative.formatShortTime_(date)) + relativeDate; 100 } else { 101 return(opt_fullDateMsg || goog.date.relative.formatFullDate_(date)) + relativeDate; 102 } 103}; 104goog.date.relative.getMessage_ = function(delta, future, unit) { 105 if(! future && unit == goog.date.relative.Unit_.MINUTES) { 106 var MSG_MINUTES_AGO_SINGULAR = goog.getMsg('{$num} minute ago', { 'num': delta }); 107 var MSG_MINUTES_AGO_PLURAL = goog.getMsg('{$num} minutes ago', { 'num': delta }); 108 return delta == 1 ? MSG_MINUTES_AGO_SINGULAR: MSG_MINUTES_AGO_PLURAL; 109 } else if(future && unit == goog.date.relative.Unit_.MINUTES) { 110 var MSG_IN_MINUTES_SINGULAR = goog.getMsg('in {$num} minute', { 'num': delta }); 111 var MSG_IN_MINUTES_PLURAL = goog.getMsg('in {$num} minutes', { 'num': delta }); 112 return delta == 1 ? MSG_IN_MINUTES_SINGULAR: MSG_IN_MINUTES_PLURAL; 113 } else if(! future && unit == goog.date.relative.Unit_.HOURS) { 114 var MSG_HOURS_AGO_SINGULAR = goog.getMsg('{$num} hour ago', { 'num': delta }); 115 var MSG_HOURS_AGO_PLURAL = goog.getMsg('{$num} hours ago', { 'num': delta }); 116 return delta == 1 ? MSG_HOURS_AGO_SINGULAR: MSG_HOURS_AGO_PLURAL; 117 } else if(future && unit == goog.date.relative.Unit_.HOURS) { 118 var MSG_IN_HOURS_SINGULAR = goog.getMsg('in {$num} hour', { 'num': delta }); 119 var MSG_IN_HOURS_PLURAL = goog.getMsg('in {$num} hours', { 'num': delta }); 120 return delta == 1 ? MSG_IN_HOURS_SINGULAR: MSG_IN_HOURS_PLURAL; 121 } else if(! future && unit == goog.date.relative.Unit_.DAYS) { 122 var MSG_DAYS_AGO_SINGULAR = goog.getMsg('{$num} day ago', { 'num': delta }); 123 var MSG_DAYS_AGO_PLURAL = goog.getMsg('{$num} days ago', { 'num': delta }); 124 return delta == 1 ? MSG_DAYS_AGO_SINGULAR: MSG_DAYS_AGO_PLURAL; 125 } else if(future && unit == goog.date.relative.Unit_.DAYS) { 126 var MSG_IN_DAYS_SINGULAR = goog.getMsg('in {$num} day', { 'num': delta }); 127 var MSG_IN_DAYS_PLURAL = goog.getMsg('in {$num} days', { 'num': delta }); 128 return delta == 1 ? MSG_IN_DAYS_SINGULAR: MSG_IN_DAYS_PLURAL; 129 } else { 130 return ''; 131 } ...
parse-date.js
Source:parse-date.js
1const { parseRelativeDate } = require('@/utils/parse-date');2const MockDate = require('mockdate');3describe('parseRelativeDate', () => {4 const second = 1000;5 const minute = 60 * second;6 const hour = 60 * minute;7 const day = 24 * hour;8 const week = 7 * day;9 const month = 30 * day;10 const year = 365 * day;11 const weekday = (d) => +new Date(date.getFullYear(), date.getMonth(), date.getDate() + d - (date.getDay() > d ? date.getDay() : date.getDay() + 7));12 const date = new Date();13 MockDate.set(date);14 it('sç§éå', () => {15 expect(+new Date(parseRelativeDate('10ç§å'))).toBe(+date - 10 * second);16 });17 it('måéå', () => {18 expect(+new Date(parseRelativeDate('10åéå'))).toBe(+date - 10 * minute);19 });20 it('måéå', () => {21 expect(+new Date(parseRelativeDate('10åéå'))).toBe(+date - 10 * minute);22 });23 it('måéå', () => {24 expect(+new Date(parseRelativeDate('10åéå'))).toBe(+date + 10 * minute);25 });26 it('a minute ago', () => {27 expect(+new Date(parseRelativeDate('a minute ago'))).toBe(+date - 1 * minute);28 });29 it('s minutes ago', () => {30 expect(+new Date(parseRelativeDate('10 minutes ago'))).toBe(+date - 10 * minute);31 });32 it('s mins ago', () => {33 expect(+new Date(parseRelativeDate('10 mins ago'))).toBe(+date - 10 * minute);34 });35 it('in s minutes', () => {36 expect(+new Date(parseRelativeDate('in 10 minutes'))).toBe(+date + 10 * minute);37 });38 it('in an hour', () => {39 expect(+new Date(parseRelativeDate('in an hour'))).toBe(+date + 1 * hour);40 });41 it('Hå°æ¶å', () => {42 expect(+new Date(parseRelativeDate('10å°æ¶å'))).toBe(+date - 10 * hour);43 });44 it('H个å°æ¶å', () => {45 expect(+new Date(parseRelativeDate('10个å°æ¶å'))).toBe(+date - 10 * hour);46 });47 it('D天å', () => {48 expect(+new Date(parseRelativeDate('10天å'))).toBe(+date - 10 * day);49 });50 it('Wå¨å', () => {51 expect(+new Date(parseRelativeDate('10å¨å'))).toBe(+date - 10 * week);52 });53 it('Wææå', () => {54 expect(+new Date(parseRelativeDate('10ææå'))).toBe(+date - 10 * week);55 });56 it('W个ææå', () => {57 expect(+new Date(parseRelativeDate('10个ææå'))).toBe(+date - 10 * week);58 });59 it('Mæå', () => {60 expect(+new Date(parseRelativeDate('1æå'))).toBe(+date - 1 * month);61 });62 it('M个æå', () => {63 expect(+new Date(parseRelativeDate('1个æå'))).toBe(+date - 1 * month);64 });65 it('Yå¹´å', () => {66 expect(+new Date(parseRelativeDate('1å¹´å'))).toBe(+date - 1 * year);67 });68 it('Yå¹´M个æå', () => {69 expect(+new Date(parseRelativeDate('1å¹´1个æå'))).toBe(+date - 1 * year - 1 * month);70 });71 it('D天Hå°æ¶å', () => {72 expect(+new Date(parseRelativeDate('1天1å°æ¶å'))).toBe(+date - 1 * day - 1 * hour);73 });74 it('Hå°æ¶måésç§éå', () => {75 expect(+new Date(parseRelativeDate('1å°æ¶1åé1ç§éå'))).toBe(+date - 1 * hour - 1 * minute - 1 * second);76 });77 it('Hå°æ¶måésç§éå', () => {78 expect(+new Date(parseRelativeDate('1å°æ¶1åé1ç§éå'))).toBe(+date + 1 * hour + 1 * minute + 1 * second);79 });80 it('ä»å¤©', () => {81 expect(+new Date(parseRelativeDate('ä»å¤©'))).toBe(+date.setHours(0, 0, 0, 0));82 });83 it('Today H:m', () => {84 expect(+new Date(parseRelativeDate('Today 08:00'))).toBe(+date + 8 * hour);85 });86 it('Today, h:m a', () => {87 expect(+new Date(parseRelativeDate('Today, 8:00 pm'))).toBe(+date + 20 * hour);88 });89 it('TDA H:m:s', () => {90 expect(+new Date(parseRelativeDate('TDA 08:00:00'))).toBe(+date + 8 * hour);91 });92 it('ä»å¤© H:m', () => {93 expect(+new Date(parseRelativeDate('ä»å¤© 08:00'))).toBe(+date + 8 * hour);94 });95 it('ä»å¤©Hç¹må', () => {96 expect(+new Date(parseRelativeDate('ä»å¤©8ç¹0å'))).toBe(+date + 8 * hour);97 });98 it('æ¨æ¥Hç¹måsç§', () => {99 expect(+new Date(parseRelativeDate('æ¨æ¥20æ¶0å0ç§'))).toBe(+date - 4 * hour);100 });101 it('å天 H:m', () => {102 expect(+new Date(parseRelativeDate('å天 20:00'))).toBe(+date - 1 * day - 4 * hour);103 });104 it('æ天 H:m', () => {105 expect(+new Date(parseRelativeDate('æ天 20:00'))).toBe(+date + 1 * day + 20 * hour);106 });107 it('ææå h:m', () => {108 expect(+new Date(parseRelativeDate('ææä¸ 8:00'))).toBe(weekday(1) + 8 * hour);109 });110 it('å¨å h:m', () => {111 expect(+new Date(parseRelativeDate('å¨äº 8:00'))).toBe(weekday(2) + 8 * hour);112 });113 it('ææ天 h:m', () => {114 expect(+new Date(parseRelativeDate('ææ天 8:00'))).toBe(weekday(7) + 8 * hour);115 });116 it('Invalid', () => {117 expect(parseRelativeDate('RSSHub')).toBe('RSSHub');118 });...
Using AI Code Generation
1import { storiesOf } from '@storybook/react';2import { action } from '@storybook/addon-actions';3storiesOf('Button', module)4 .add('with text', () => (5 <Button onClick={action('clicked')}>Hello Button</Button>6 .add('with some emoji', () => (7 <Button onClick={action('clicked')}>8 ));9{10 "scripts": {11 },12 "devDependencies": {13 },14 "dependencies": {15 }16}17import { configure } from '@storybook/react';18const req = require.context('../src', true, /.stories.js$/);19function loadStories() {20 req.keys().forEach(filename => req(filename));21}22configure(loadStories, module);23import '@storybook/addon-actions/register';24const path = require('path
Using AI Code Generation
1import { storiesOf } from '@storybook/react';2import { withKnobs, text } from '@storybook/addon-knobs';3import Component from './Component';4storiesOf('Component', module)5 .addDecorator(withKnobs)6 .add('Component', () => (7 text={text('Text', 'Hello')}8 ));9import { storiesOf } from '@storybook/react';10import { withKnobs, text } from '@storybook/addon-knobs';11import Component from './Component';12storiesOf('Component', module)13 .addDecorator(withKnobs)14 .add('Component', () => (15 text={text('Text', 'Hello')}16 ));
Using AI Code Generation
1import { storiesOf } from 'storybook-root/src'2storiesOf('Button', module)3 .add('with text', () => (4 <Button onClick={action('clicked')}>Hello Button</Button>5 .add('with some emoji', () => (6 <Button onClick={action('clicked')}>😀 😎 👍 💯</Button>7 ));8import { storiesOf } from 'storybook-root/src'9storiesOf('Button', module)10 .add('with text', () => (11 <Button onClick={action('clicked')}>Hello Button</Button>12 .add('with some emoji', () => (13 <Button onClick={action('clicked')}>😀 😎 👍 💯</Button>14 ));15MIT © [mikechabot](
Using AI Code Generation
1import { configure } from '@storybook/react';2import { setOptions } from '@storybook/addon-options';3import { setDefaults } from 'storybook-addon-jsx';4import { addDecorator } from '@storybook/react';5import { withOptions } from '@storybook/addon-options';6import { withInfo } from '@storybook/addon-info';7import { withKnobs } from '@storybook/addon-knobs';8import { setDefaults as setAddonInfoDefaults } from '@storybook/addon-info';9import { withA11y } from '@storybook/addon-a11y';10import { withTests } from '@storybook/addon-jest';11import results from '../.jest-test-results.json';12import './test.scss';13configure(require.context('../src', true, /\.stories\.js$/), module);14setAddonInfoDefaults({15});16addDecorator(withKnobs);17addDecorator(withTests({ results }));18addDecorator(withInfo);19addDecorator(withA11y);20addDecorator(21 withOptions({
Using AI Code Generation
1import { withKnobs, text } from '@storybook/addon-knobs';2import { storiesOf } from '@storybook/react';3import { withInfo } from '@storybook/addon-info';4import React from 'react';5import MyComponent from 'src/components/MyComponent';6storiesOf('MyComponent', module)7 .addDecorator(withKnobs)8 .add('with some emoji', () => (9 <MyComponent>{text('Label', '😀 😎 👍 💯')}</MyComponent>10 ));11import React from 'react';12import PropTypes from 'prop-types';13const MyComponent = ({ children }) => <div>{children}</div>;14MyComponent.propTypes = {15};16export default MyComponent;17"babel": {18 }19{20}21import '../src/index.css';22import '@storybook/addon-knobs/register';23module.exports = (baseConfig, env, config) => {24 config.module.rules.push({25 include: path.resolve(__dirname, '../'),26 });27 return config;28};29module.exports = (baseConfig, env, config) => {30 config.module.rules.push({31 include: path.resolve(__dirname, '../'),32 });33 return config;34};35module.exports = (baseConfig, env, config) => {36 config.module.rules.push({37 include: path.resolve(__dirname, '../'),38 });39 return config;40};41module.exports = (baseConfig, env, config) => {42 config.module.rules.push({
Using AI Code Generation
1import { storiesOf } from 'storybook-root';2storiesOf('my story', module)3 .add('story 1', () => <div>hello</div>)4 .add('story 2', () => <div>hello</div>);5import { configure } from '@storybook/react';6configure(() => {7 require('storybook-root/test.js');8}, module);
Using AI Code Generation
1const root = require('storybook-root');2module.exports = root(__dirname, '.storybook/webpack.config');3const path = require('path');4module.exports = (baseConfig, env, defaultConfig) => {5 defaultConfig.module.rules.push({6 include: path.resolve(__dirname, '../')7 });8 return defaultConfig;9};10import './Button.scss';
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!!