How to use addSuffix method in Puppeteer

Best JavaScript code snippet using puppeteer

Run Puppeteer automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

index.js

Source: index.js Github

copy
1var translations = {
2  about: 'körülbelül',
3  over: 'több mint',
4  almost: 'majdnem',
5  lessthan: 'kevesebb mint'
6}
7
8function translate(number, addSuffix, key, comparison) {
9  var num = number
10  switch (key) {
11    case 'xseconds':
12      if (comparison === -1 && addSuffix) return num + ' másodperccel ezelőtt'
13      if (comparison === -1 && !addSuffix) return num + ' másodperce'
14      if (comparison === 1) return num + ' másodperc múlva'
15      return num + ' másodperc'
16
17    case 'halfaminute':
18      if (comparison === -1 && addSuffix) return 'fél perccel ezelőtt'
19      if (comparison === -1 && !addSuffix) return 'fél perce'
20      if (comparison === 1) return 'fél perc múlva'
21      return 'fél perc'
22
23    case 'xminutes':
24      if (comparison === -1 && addSuffix) return num + ' perccel ezelőtt'
25      if (comparison === -1 && !addSuffix) return num + ' perce'
26      if (comparison === 1) return num + ' perc múlva'
27      return num + ' perc'
28
29    case 'xhours':
30      if (comparison === -1 && addSuffix) return num + ' órával ezelőtt'
31      if (comparison === -1 && !addSuffix) return num + ' órája'
32      if (comparison === 1) return num + ' óra múlva'
33      return num + ' óra'
34
35    case 'xdays':
36      if (comparison === -1 && addSuffix) return num + ' nappal ezelőtt'
37      if (comparison === -1 && !addSuffix) return num + ' napja'
38      if (comparison === 1) return num + ' nap múlva'
39      return num + ' nap'
40
41    case 'xmonths':
42      if (comparison === -1 && addSuffix) return num + ' hónappal ezelőtt'
43      if (comparison === -1 && !addSuffix) return num + ' hónapja'
44      if (comparison === 1) return num + ' hónap múlva'
45      return num + ' hónap'
46
47    case 'xyears':
48      if (comparison === -1 && addSuffix) return num + ' évvel ezelőtt'
49      if (comparison === -1 && !addSuffix) return num + ' éve'
50      if (comparison === 1) return num + ' év múlva'
51      return num + ' év'
52  }
53  return ''
54}
55
56export default function formatDistance(token, count, options) {
57  options = options || {}
58  var adverb = token.match(/about|over|almost|lessthan/i)
59  var unit = token.replace(adverb, '')
60
61  var result
62  result = translate(
63    count,
64    options.addSuffix,
65    unit.toLowerCase(),
66    options.comparison
67  )
68
69  if (adverb) {
70    result = translations[adverb[0].toLowerCase()] + ' ' + result
71  }
72
73  return result
74}
75
Full Screen

addSuffix.js

Source: addSuffix.js Github

copy
1/* eslint-env jest */
2import React from 'react';
3import addSuffix from '../addSuffix';
4
5describe('addSuffix', () => {
6  it('adds a suffix to a string with a space', () => {
7    expect(addSuffix('hello', '')).toBe('hello');
8    expect(addSuffix('he', 'llo')).toBe('he llo');
9  });
10
11  it('returns the string unmodified if the string is false-ish', () => {
12    expect(addSuffix(null, 'hello')).toBe(null);
13    expect(addSuffix('', 'hello')).toBe('');
14    expect(addSuffix(undefined, 'hello')).toBeUndefined();
15  });
16
17  it('removes any additional whitespace around the string', () => {
18    expect(addSuffix('        hello world       ', 'something')).toBe('hello world something');
19  });
20
21  it('does not add the suffix if it already exists in the string', () => {
22    expect(addSuffix('hello world', ' ')).toBe('hello world');
23    expect(addSuffix('Required *', '*')).toBe('Required *');
24  });
25
26  it('should return a react component if the "string" is a component', () => {
27    const label = <span>Hello!</span>;
28    expect(addSuffix(label, ' *')).toBe(label);
29  });
30});
31
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Puppeteer on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)