Guest User

Untitled

a guest
Mar 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. /********************************************************************************************
  4.  *                                                                                          *
  5.  * Plese read the following tutorial before implementing tasks:                             *
  6.  * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String  *
  7.  *                                                                                          *
  8.  ********************************************************************************************/
  9.  
  10.  
  11. /**
  12.  * Returns the result of concatenation of two strings.
  13.  *
  14.  * @param {string} value1
  15.  * @param {string} value2
  16.  * @return {string}
  17.  *
  18.  * @example
  19.  *   'aa', 'bb' => 'aabb'
  20.  *   'aa',''    => 'aa'
  21.  *   '',  'bb'  => 'bb'
  22.  */
  23. function concatenateStrings(value1, value2) {
  24.     return value1 + value2;
  25. }
  26.  
  27.  
  28. /**
  29.  * Returns the length of given string.
  30.  *
  31.  * @param {string} value
  32.  * @return {number}
  33.  *
  34.  * @example
  35.  *   'aaaaa' => 5
  36.  *   'b'     => 1
  37.  *   ''      => 0
  38.  */
  39. function getStringLength(value) {
  40.     return value.length;
  41. }
  42.  
  43. /**
  44.  * Returns the result of string template and given parameters firstName and lastName.
  45.  * Please do not use concatenation, use template string :
  46.  * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
  47.  *
  48.  * @param {string} firstName
  49.  * @param {string} lastName
  50.  * @return {string}
  51.  *
  52.  * @example
  53.  *   'John','Doe'      => 'Hello, John Doe!'
  54.  *   'Chuck','Norris'  => 'Hello, Chuck Norris!'
  55.  */
  56. function getStringFromTemplate(firstName, lastName) {
  57.     return `Hello, ${firstName} ${lastName}!`
  58. }
  59.  
  60. /**
  61.  * Extracts a name from template string 'Hello, First_Name Last_Name!'.
  62.  *
  63.  * @param {string} value
  64.  * @return {string}
  65.  *
  66.  * @example
  67.  *   'Hello, John Doe!' => 'John Doe'
  68.  *   'Hello, Chuck Norris!' => 'Chuck Norris'
  69.  */
  70. function extractNameFromTemplate(value) {
  71.     return value.replace('Hello, ', '').replace('!', '');
  72. }
  73.  
  74.  
  75. /**
  76.  * Returns a first char of the given string.
  77.  *
  78.  * @param {string} value
  79.  * @return {string}
  80.  *
  81.  * @example
  82.  *   'John Doe'  => 'J'
  83.  *   'cat'       => 'c'
  84.  */
  85. function getFirstChar(value) {
  86.     return value[0];
  87. }
  88.  
  89. /**
  90.  * Removes a leading and trailing whitespace characters from string.
  91.  *
  92.  * @param {string} value
  93.  * @return {string}
  94.  *
  95.  * @example
  96.  *   '  Abracadabra'    => 'Abracadabra'
  97.  *   'cat'              => 'cat'
  98.  *   '\tHello, World! ' => 'Hello, World!'
  99.  */
  100. function removeLeadingAndTrailingWhitespaces(value) {
  101.     return value.trim();
  102. }
  103.  
  104. /**
  105.  * Returns a string that repeated the specified number of times.
  106.  *
  107.  * @param {string} value
  108.  * @param {string} count
  109.  * @return {string}
  110.  *
  111.  * @example
  112.  *   'A', 5  => 'AAAAA'
  113.  *   'cat', 3 => 'catcatcat'
  114.  */
  115. function repeatString(value, count) {
  116.     return value.repeat(count);
  117. }
  118.  
  119. /**
  120.  * Remove the first occurrence of string inside another string
  121.  *
  122.  * @param {string} str
  123.  * @param {string} value
  124.  * @return {string}
  125.  *
  126.  * @example
  127.  *   'To be or not to be', 'not'  => 'To be or to be'
  128.  *   'I like legends', 'end' => 'I like legs',
  129.  *   'ABABAB','BA' => 'ABAB'
  130.  */
  131. function removeFirstOccurrences(str, value) {
  132.     return str.replace(value, '');
  133. }
  134.  
  135. /**
  136.  * Remove the first and last angle brackets from tag string
  137.  *
  138.  * @param {string} str
  139.  * @return {string}
  140.  *
  141.  * @example
  142.  *   '<div>' => 'div'
  143.  *   '<span>' => 'span'
  144.  *   '<a>' => 'a'
  145.  */
  146. function unbracketTag(str) {
  147.     return str.slice(1, -1);
  148. }
  149.  
  150.  
  151. /**
  152.  * Converts all characters of the specified string into the upper case
  153.  *
  154.  * @param {string} str
  155.  * @return {string}
  156.  *
  157.  * @example
  158.  *   'Thunderstruck' => 'THUNDERSTRUCK'
  159.  *  'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  160.  */
  161. function convertToUpperCase(str) {
  162.     return str.toUpperCase();
  163. }
  164.  
  165. /**
  166.  * Extracts e-mails from single string with e-mails list delimeted by semicolons
  167.  *
  168.  * @param {string} str
  169.  * @return {array}
  170.  *
  171.  * @example
  172.  *   'angus.young@gmail.com;brian.johnson@hotmail.com;bon.scott@yahoo.com' => ['angus.young@gmail.com', 'brian.johnson@hotmail.com', 'bon.scott@yahoo.com']
  173.  *   'info@gmail.com' => ['info@gmail.com']
  174.  */
  175. function extractEmails(str) {
  176.     return str.split(';');
  177. }
  178.  
  179. /**
  180.  * Returns the string representation of rectangle with specified width and height
  181.  * using pseudograhic chars
  182.  *
  183.  * @param {number} width
  184.  * @param {number} height
  185.  * @return {string}
  186.  *
  187.  * @example
  188.  *
  189.  *            '┌────┐\n'+
  190.  *  (6,4) =>  '│    │\n'+
  191.  *            '│    │\n'+
  192.  *            '└────┘\n'
  193.  *
  194.  *  (2,2) =>  '┌┐\n'+
  195.  *            '└┘\n'
  196.  *
  197.  *             '┌──────────┐\n'+
  198.  *  (12,3) =>  '│          │\n'+
  199.  *             '└──────────┘\n'
  200.  *
  201.  */
  202. function getRectangleString(width, height) {
  203.     let dashStr = '─'.repeat(width - 2);
  204.     return `┌${dashStr}┐\n` + `│${' '.repeat(width - 2)}│\n`.repeat(height - 2) + `└${dashStr}┘\n`;
  205. }
  206.  
  207.  
  208. /**
  209.  * Encode specified string with ROT13 cipher
  210.  * See details:  https://en.wikipedia.org/wiki/ROT13
  211.  *
  212.  * @param {string} str
  213.  * @return {string}
  214.  *
  215.  * @example
  216.  *
  217.  *   'hello' => 'uryyb'
  218.  *   'Why did the chicken cross the road?' => 'Jul qvq gur puvpxra pebff gur ebnq?'
  219.  *   'Gb trg gb gur bgure fvqr!' => 'To get to the other side!'
  220.  *   'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' => 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
  221.  *
  222.  */
  223. function encodeToRot13(str) {
  224.     return str.replace(/[a-zA-Z]/g, function (c) {
  225.         return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
  226.     });
  227. }
  228.  
  229. /**
  230.  * Returns true if the value is string; otherwise false.
  231.  * @param {string} value
  232.  * @return {boolean}
  233.  *
  234.  * @example
  235.  *   isString() => false
  236.  *   isString(null) => false
  237.  *   isString([]) => false
  238.  *   isString({}) => false
  239.  *   isString('test') => true
  240.  *   isString(new String('test')) => true
  241.  */
  242. function isString(value) {
  243.     return value ? typeof value.valueOf() == 'string' : false;
  244. }
  245.  
  246.  
  247. /**
  248.  * Returns playid card id.
  249.  *
  250.  * Playing cards inittial deck inclides the cards in the following order:
  251.  *
  252.  *  'A♣','2♣','3♣','4♣','5♣','6♣','7♣','8♣','9♣','10♣','J♣','Q♣','K♣',
  253.  *  'A♦','2♦','3♦','4♦','5♦','6♦','7♦','8♦','9♦','10♦','J♦','Q♦','K♦',
  254.  *  'A♥','2♥','3♥','4♥','5♥','6♥','7♥','8♥','9♥','10♥','J♥','Q♥','K♥',
  255.  *  'A♠','2♠','3♠','4♠','5♠','6♠','7♠','8♠','9♠','10♠','J♠','Q♠','K♠'
  256.  *
  257.  * (see https://en.wikipedia.org/wiki/Standard_52-card_deck)
  258.  * Function returns the zero-based index of specified card in the initial deck above.
  259.  *
  260.  * @param {string} value
  261.  * @return {number}
  262.  *
  263.  * @example
  264.  *   'A♣' => 0
  265.  *   '2♣' => 1
  266.  *   '3♣' => 2
  267.  *     ...
  268.  *   'Q♠' => 50
  269.  *   'K♠' => 51
  270.  */
  271. function getCardId(value) {
  272.     let cards = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
  273.     let suits = ['♣', '♦', '♥', '♠'];
  274.     return cards.indexOf(value.slice(0, -1)) + suits.indexOf(value.slice(-1)) * cards.length;
  275. }
  276.  
  277.  
  278. module.exports = {
  279.     concatenateStrings: concatenateStrings,
  280.     getStringLength: getStringLength,
  281.     getStringFromTemplate: getStringFromTemplate,
  282.     extractNameFromTemplate: extractNameFromTemplate,
  283.     getFirstChar: getFirstChar,
  284.     removeLeadingAndTrailingWhitespaces: removeLeadingAndTrailingWhitespaces,
  285.     repeatString: repeatString,
  286.     removeFirstOccurrences: removeFirstOccurrences,
  287.     unbracketTag: unbracketTag,
  288.     convertToUpperCase: convertToUpperCase,
  289.     extractEmails: extractEmails,
  290.     getRectangleString: getRectangleString,
  291.     encodeToRot13: encodeToRot13,
  292.     isString: isString,
  293.     getCardId: getCardId
  294. };
Add Comment
Please, Sign In to add comment