Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. /**
  4.  * @description сравнение элементов двух массивов. Элементы сравниваются через оператор ===
  5.  * @param {Object[]} arr1
  6.  * @param {Object[]} arr2
  7.  * @returns {boolean}
  8.  */
  9. function arrayElementsEquals(arr1, arr2) {
  10.     if (arr1 === arr2) {
  11.         return true;
  12.     }
  13.     if (arr1.length !== arr2.length) {
  14.         return false;
  15.     }
  16.     for (let e of arr1) {
  17.         if (!arr2.includes(e)) {
  18.             return false;
  19.         }
  20.     }
  21.  
  22.     return true;
  23. }
  24.  
  25. function containsKeysExtension(instance, keys) {
  26.     for (let key of keys) {
  27.         if (!instance.hasOwnProperty(key)) {
  28.             return false;
  29.         }
  30.     }
  31.  
  32.     return true;
  33. }
  34.  
  35. function hasKeysExtension(instance, keys) {
  36.     let instanceKeys = Array.from(Object.keys(instance));
  37.  
  38.     return arrayElementsEquals(instanceKeys, keys);
  39. }
  40.  
  41. function containValuesExtension(instance, values) {
  42.     let instanceValues = Array.from(Object.values(instance));
  43.     for (let value of values) {
  44.         if (!instanceValues.includes(value)) {
  45.             return false;
  46.         }
  47.     }
  48.  
  49.     return true;
  50. }
  51.  
  52. function hasValueTypeExtension(instance, key, expectedType) {
  53.     let objectForCheckType = instance[key];
  54.  
  55.     switch (typeof objectForCheckType) {
  56.         case 'number':
  57.             return expectedType === Number;
  58.         case 'function':
  59.             return expectedType === Function;
  60.         case 'string':
  61.             return expectedType === String;
  62.         case 'object':
  63.             return expectedType === Array;
  64.         default:
  65.             return false;
  66.     }
  67. }
  68.  
  69. function hasLengthExtension(instance, expectedLength) {
  70.     return instance.length === expectedLength;
  71. }
  72.  
  73. function hasValuesExtension(instance, values) {
  74.     let instanceValues = Array.from(Object.values(instance));
  75.  
  76.     return arrayElementsEquals(instanceValues, values);
  77. }
  78.  
  79. function hasWordsCountExtension(instance, expectedWordsCount) {
  80.     let wordsCount = instance.split(' ').length;
  81.  
  82.     return wordsCount === expectedWordsCount;
  83. }
  84.  
  85. /**
  86.  * Проверяет, верно ли, что у функции expectedParamsCount параметров
  87.  * @param {Function} instance
  88.  * @param {Number} expectedParamsCount
  89.  * @returns {Boolean}
  90.  */
  91. function hasParamsCountExtension(instance, expectedParamsCount) {
  92.     return instance.length === expectedParamsCount;
  93. }
  94.  
  95. function initObjectExtensions() {
  96.     Object.defineProperty(Object.prototype, 'check', {
  97.         get() {
  98.             const instance = this;
  99.  
  100.             return {
  101.                 containsKeys(keys) {
  102.                     return containsKeysExtension(instance, keys);
  103.                 },
  104.                 hasKeys(keys) {
  105.                     return hasKeysExtension(instance, keys);
  106.                 },
  107.                 containsValues(values) {
  108.                     return containValuesExtension(instance, values);
  109.                 },
  110.                 hasValues(values) {
  111.                     return hasValuesExtension(instance, values);
  112.                 },
  113.                 hasValueType(key, expectedType) {
  114.                     return hasValueTypeExtension(instance, key, expectedType);
  115.                 },
  116.                 not: {
  117.                     containsKeys(keys) {
  118.                         return !containsKeysExtension(instance, keys);
  119.                     },
  120.                     hasKeys(keys) {
  121.                         return !hasKeysExtension(instance, keys);
  122.                     },
  123.                     containsValues(values) {
  124.                         return !containValuesExtension(instance, values);
  125.                     },
  126.                     hasValues(values) {
  127.                         return !hasValuesExtension(instance, values);
  128.                     },
  129.                     hasValueType(key, expectedType) {
  130.                         return !hasValueTypeExtension(instance, key, expectedType);
  131.                     }
  132.                 }
  133.             };
  134.         }
  135.     });
  136.  
  137. }
  138.  
  139. function initStringExtensions() {
  140.     Object.defineProperty(String.prototype, 'check', {
  141.         get() {
  142.             const instance = this;
  143.  
  144.             return {
  145.                 hasLength(expectedLength) {
  146.                     return hasLengthExtension(instance, expectedLength);
  147.                 },
  148.                 hasWordsCount(expectedCount) {
  149.                     return hasWordsCountExtension(instance, expectedCount);
  150.                 },
  151.                 not: {
  152.                     hasLength(expectedLength) {
  153.                         return !hasLengthExtension(instance, expectedLength);
  154.                     },
  155.                     hasWordsCount(expectedCount) {
  156.                         return !hasWordsCountExtension(instance, expectedCount);
  157.                     }
  158.                 }
  159.             };
  160.         }
  161.     });
  162. }
  163.  
  164.  
  165. function initArrayExtensions() {
  166.     Object.defineProperty(Array.prototype, 'check', {
  167.         get() {
  168.             const instance = this;
  169.  
  170.             return {
  171.                 containsKeys(keys) {
  172.                     return containsKeysExtension(instance, keys);
  173.                 },
  174.                 hasKeys(keys) {
  175.                     return hasKeysExtension(instance, keys);
  176.                 },
  177.                 containsValues(values) {
  178.                     return containValuesExtension(instance, values);
  179.                 },
  180.                 hasValues(values) {
  181.                     return hasValuesExtension(instance, values);
  182.                 },
  183.                 hasLength(expectedLength) {
  184.                     return hasLengthExtension(instance, expectedLength);
  185.                 },
  186.                 hasValueType(key, expectedType) {
  187.                     return hasValueTypeExtension(instance, key, expectedType);
  188.                 },
  189.                 not: {
  190.                     containsKeys(keys) {
  191.                         return !containsKeysExtension(instance, keys);
  192.                     },
  193.                     hasKeys(keys) {
  194.                         return !hasKeysExtension(instance, keys);
  195.                     },
  196.                     containsValues(values) {
  197.                         return !containValuesExtension(instance, values);
  198.                     },
  199.                     hasValues(values) {
  200.                         return !hasValuesExtension(instance, values);
  201.                     },
  202.                     hasLength(expectedLength) {
  203.                         return !hasLengthExtension(instance, expectedLength);
  204.                     },
  205.                     hasValueType(key, expectedType) {
  206.                         return !hasValueTypeExtension(instance, key, expectedType);
  207.                     }
  208.                 }
  209.             };
  210.         }
  211.     });
  212. }
  213.  
  214. function initFunctionExtensions() {
  215.     Object.defineProperty(Function.prototype, 'check', {
  216.         get() {
  217.             const instance = this;
  218.  
  219.             return {
  220.                 hasParamsCount(paramsCount) {
  221.                     return hasParamsCountExtension(instance, paramsCount);
  222.                 },
  223.                 not: {
  224.                     hasParamsCount(paramsCount) {
  225.                         return !hasParamsCountExtension(instance, paramsCount);
  226.                     }
  227.                 }
  228.             };
  229.         }
  230.     });
  231. }
  232.  
  233. function wrap(instance) {
  234.     const isNull = (instance === null);
  235.     const typeOfInstance = typeof instance;
  236.  
  237.     return {
  238.         containsKeys(keys) {
  239.             return !isNull && typeOfInstance === 'object'
  240.                 ? instance.check.containsKeys(keys)
  241.                 : false;
  242.         },
  243.         isNull() {
  244.             return isNull;
  245.         },
  246.         hasKeys(keys) {
  247.             return !isNull && typeOfInstance === 'object'
  248.                 ? instance.check.hasKeys(keys)
  249.                 : false;
  250.         },
  251.         containsValues(values) {
  252.             return !isNull && typeOfInstance === 'object'
  253.                 ? instance.check.containsValues(values)
  254.                 : false;
  255.         },
  256.         hasValues(values) {
  257.             return !isNull && typeOfInstance === 'object'
  258.                 ? instance.check.hasValues(values)
  259.                 : false;
  260.         },
  261.         hasValueType(key, type) {
  262.             return !isNull && typeOfInstance === 'object'
  263.                 ? instance.check.hasValueType(key, type)
  264.                 : false;
  265.         },
  266.         hasLength(expectedLength) {
  267.             return !isNull && (typeOfInstance === 'object' || typeOfInstance === 'string')
  268.                 ? instance.check.hasLength(expectedLength)
  269.                 : false;
  270.         },
  271.         hasParamsCount(expectedCount) {
  272.             return !isNull && (typeOfInstance === 'function')
  273.                 ? instance.check.hasParamsCount(expectedCount)
  274.                 : false;
  275.         },
  276.         hasWordsCount(expectedCount) {
  277.             return !isNull && (typeOfInstance === 'string')
  278.                 ? instance.check.hasWordsCount(expectedCount)
  279.                 : false;
  280.         },
  281.         not: {
  282.             containsKeys(keys) {
  283.                 return !isNull && typeOfInstance === 'object'
  284.                     ? !instance.check.containsKeys(keys)
  285.                     : false;
  286.             },
  287.             isNull() {
  288.                 return isNull;
  289.             },
  290.             hasKeys(keys) {
  291.                 return !isNull && typeOfInstance === 'object'
  292.                     ? !instance.check.hasKeys(keys)
  293.                     : false;
  294.             },
  295.             containsValues(values) {
  296.                 return !isNull && typeOfInstance === 'object'
  297.                     ? !instance.check.containsValues(values)
  298.                     : false;
  299.             },
  300.             hasValues(values) {
  301.                 return !isNull && typeOfInstance === 'object'
  302.                     ? !instance.check.hasValues(values)
  303.                     : false;
  304.             },
  305.             hasValueType(key, type) {
  306.                 return !isNull && typeOfInstance === 'object'
  307.                     ? !instance.check.hasValueType(key, type)
  308.                     : false;
  309.             },
  310.             hasLength(expectedLength) {
  311.                 return !isNull && (typeOfInstance === 'object' || typeOfInstance === 'string')
  312.                     ? !instance.check.hasLength(expectedLength)
  313.                     : false;
  314.             },
  315.             hasParamsCount(expectedCount) {
  316.                 return !isNull && (typeOfInstance === 'function')
  317.                     ? !instance.check.hasParamsCount(expectedCount)
  318.                     : false;
  319.             },
  320.             hasWordsCount(expectedCount) {
  321.                 return !isNull && (typeOfInstance === 'string')
  322.                     ? !instance.check.hasWordsCount(expectedCount)
  323.                     : false;
  324.             }
  325.         },
  326.         check: {
  327.             containsKeys(keys) {
  328.                 return !isNull && typeOfInstance === 'object'
  329.                     ? instance.check.containsKeys(keys)
  330.                     : false;
  331.             },
  332.             isNull() {
  333.                 return isNull;
  334.             },
  335.             hasKeys(keys) {
  336.                 return !isNull && typeOfInstance === 'object'
  337.                     ? instance.check.hasKeys(keys)
  338.                     : false;
  339.             },
  340.             containsValues(values) {
  341.                 return !isNull && typeOfInstance === 'object'
  342.                     ? instance.check.containsValues(values)
  343.                     : false;
  344.             },
  345.             hasValues(values) {
  346.                 return !isNull && typeOfInstance === 'object'
  347.                     ? instance.check.hasValues(values)
  348.                     : false;
  349.             },
  350.             hasValueType(key, type) {
  351.                 return !isNull && typeOfInstance === 'object'
  352.                     ? instance.check.hasValueType(key, type)
  353.                     : false;
  354.             },
  355.             hasLength(expectedLength) {
  356.                 return !isNull && (typeOfInstance === 'object' || typeOfInstance === 'string')
  357.                     ? instance.check.hasLength(expectedLength)
  358.                     : false;
  359.             },
  360.             hasParamsCount(expectedCount) {
  361.                 return !isNull && (typeOfInstance === 'function')
  362.                     ? instance.check.hasParamsCount(expectedCount)
  363.                     : false;
  364.             },
  365.             hasWordsCount(expectedCount) {
  366.                 return !isNull && (typeOfInstance === 'string')
  367.                     ? instance.check.hasWordsCount(expectedCount)
  368.                     : false;
  369.             },
  370.             not: {
  371.                 containsKeys(keys) {
  372.                     return !isNull && typeOfInstance === 'object'
  373.                         ? !instance.check.containsKeys(keys)
  374.                         : false;
  375.                 },
  376.                 isNull() {
  377.                     return isNull;
  378.                 },
  379.                 hasKeys(keys) {
  380.                     return !isNull && typeOfInstance === 'object'
  381.                         ? !instance.check.hasKeys(keys)
  382.                         : false;
  383.                 },
  384.                 containsValues(values) {
  385.                     return !isNull && typeOfInstance === 'object'
  386.                         ? !instance.check.containsValues(values)
  387.                         : false;
  388.                 },
  389.                 hasValues(values) {
  390.                     return !isNull && typeOfInstance === 'object'
  391.                         ? !instance.check.hasValues(values)
  392.                         : false;
  393.                 },
  394.                 hasValueType(key, type) {
  395.                     return !isNull && typeOfInstance === 'object'
  396.                         ? !instance.check.hasValueType(key, type)
  397.                         : false;
  398.                 },
  399.                 hasLength(expectedLength) {
  400.                     return !isNull && (typeOfInstance === 'object' || typeOfInstance === 'string')
  401.                         ? !instance.check.hasLength(expectedLength)
  402.                         : false;
  403.                 },
  404.                 hasParamsCount(expectedCount) {
  405.                     return !isNull && (typeOfInstance === 'function')
  406.                         ? !instance.check.hasParamsCount(expectedCount)
  407.                         : false;
  408.                 },
  409.                 hasWordsCount(expectedCount) {
  410.                     return !isNull && (typeOfInstance === 'string')
  411.                         ? !instance.check.hasWordsCount(expectedCount)
  412.                         : false;
  413.                 }
  414.             }
  415.         }
  416.     };
  417. }
  418.  
  419. exports.init = function () {
  420.     initObjectExtensions();
  421.     initArrayExtensions();
  422.     initStringExtensions();
  423.     initFunctionExtensions();
  424. };
  425.  
  426. exports.wrap = wrap;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement