Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- /**
- * @description сравнение элементов двух массивов. Элементы сравниваются через оператор ===
- * @param {Object[]} arr1
- * @param {Object[]} arr2
- * @returns {boolean}
- */
- function arrayElementsEquals(arr1, arr2) {
- if (arr1 === arr2) {
- return true;
- }
- if (arr1.length !== arr2.length) {
- return false;
- }
- for (let e of arr1) {
- if (!arr2.includes(e)) {
- return false;
- }
- }
- return true;
- }
- function containsKeysExtension(instance, keys) {
- for (let key of keys) {
- if (!instance.hasOwnProperty(key)) {
- return false;
- }
- }
- return true;
- }
- function hasKeysExtension(instance, keys) {
- let instanceKeys = Array.from(Object.keys(instance));
- return arrayElementsEquals(instanceKeys, keys);
- }
- function containValuesExtension(instance, values) {
- let instanceValues = Array.from(Object.values(instance));
- for (let value of values) {
- if (!instanceValues.includes(value)) {
- return false;
- }
- }
- return true;
- }
- function hasValueTypeExtension(instance, key, expectedType) {
- let objectForCheckType = instance[key];
- switch (typeof objectForCheckType) {
- case 'number':
- return expectedType === Number;
- case 'function':
- return expectedType === Function;
- case 'string':
- return expectedType === String;
- case 'object':
- return expectedType === Array;
- default:
- return false;
- }
- }
- function hasLengthExtension(instance, expectedLength) {
- return instance.length === expectedLength;
- }
- function hasValuesExtension(instance, values) {
- let instanceValues = Array.from(Object.values(instance));
- return arrayElementsEquals(instanceValues, values);
- }
- function hasWordsCountExtension(instance, expectedWordsCount) {
- let wordsCount = instance.split(' ').length;
- return wordsCount === expectedWordsCount;
- }
- /**
- * Проверяет, верно ли, что у функции expectedParamsCount параметров
- * @param {Function} instance
- * @param {Number} expectedParamsCount
- * @returns {Boolean}
- */
- function hasParamsCountExtension(instance, expectedParamsCount) {
- return instance.length === expectedParamsCount;
- }
- function initObjectExtensions() {
- Object.defineProperty(Object.prototype, 'check', {
- get() {
- const instance = this;
- return {
- containsKeys(keys) {
- return containsKeysExtension(instance, keys);
- },
- hasKeys(keys) {
- return hasKeysExtension(instance, keys);
- },
- containsValues(values) {
- return containValuesExtension(instance, values);
- },
- hasValues(values) {
- return hasValuesExtension(instance, values);
- },
- hasValueType(key, expectedType) {
- return hasValueTypeExtension(instance, key, expectedType);
- },
- not: {
- containsKeys(keys) {
- return !containsKeysExtension(instance, keys);
- },
- hasKeys(keys) {
- return !hasKeysExtension(instance, keys);
- },
- containsValues(values) {
- return !containValuesExtension(instance, values);
- },
- hasValues(values) {
- return !hasValuesExtension(instance, values);
- },
- hasValueType(key, expectedType) {
- return !hasValueTypeExtension(instance, key, expectedType);
- }
- }
- };
- }
- });
- }
- function initStringExtensions() {
- Object.defineProperty(String.prototype, 'check', {
- get() {
- const instance = this;
- return {
- hasLength(expectedLength) {
- return hasLengthExtension(instance, expectedLength);
- },
- hasWordsCount(expectedCount) {
- return hasWordsCountExtension(instance, expectedCount);
- },
- not: {
- hasLength(expectedLength) {
- return !hasLengthExtension(instance, expectedLength);
- },
- hasWordsCount(expectedCount) {
- return !hasWordsCountExtension(instance, expectedCount);
- }
- }
- };
- }
- });
- }
- function initArrayExtensions() {
- Object.defineProperty(Array.prototype, 'check', {
- get() {
- const instance = this;
- return {
- containsKeys(keys) {
- return containsKeysExtension(instance, keys);
- },
- hasKeys(keys) {
- return hasKeysExtension(instance, keys);
- },
- containsValues(values) {
- return containValuesExtension(instance, values);
- },
- hasValues(values) {
- return hasValuesExtension(instance, values);
- },
- hasLength(expectedLength) {
- return hasLengthExtension(instance, expectedLength);
- },
- hasValueType(key, expectedType) {
- return hasValueTypeExtension(instance, key, expectedType);
- },
- not: {
- containsKeys(keys) {
- return !containsKeysExtension(instance, keys);
- },
- hasKeys(keys) {
- return !hasKeysExtension(instance, keys);
- },
- containsValues(values) {
- return !containValuesExtension(instance, values);
- },
- hasValues(values) {
- return !hasValuesExtension(instance, values);
- },
- hasLength(expectedLength) {
- return !hasLengthExtension(instance, expectedLength);
- },
- hasValueType(key, expectedType) {
- return !hasValueTypeExtension(instance, key, expectedType);
- }
- }
- };
- }
- });
- }
- function initFunctionExtensions() {
- Object.defineProperty(Function.prototype, 'check', {
- get() {
- const instance = this;
- return {
- hasParamsCount(paramsCount) {
- return hasParamsCountExtension(instance, paramsCount);
- },
- not: {
- hasParamsCount(paramsCount) {
- return !hasParamsCountExtension(instance, paramsCount);
- }
- }
- };
- }
- });
- }
- function wrap(instance) {
- const isNull = (instance === null);
- const typeOfInstance = typeof instance;
- return {
- containsKeys(keys) {
- return !isNull && typeOfInstance === 'object'
- ? instance.check.containsKeys(keys)
- : false;
- },
- isNull() {
- return isNull;
- },
- hasKeys(keys) {
- return !isNull && typeOfInstance === 'object'
- ? instance.check.hasKeys(keys)
- : false;
- },
- containsValues(values) {
- return !isNull && typeOfInstance === 'object'
- ? instance.check.containsValues(values)
- : false;
- },
- hasValues(values) {
- return !isNull && typeOfInstance === 'object'
- ? instance.check.hasValues(values)
- : false;
- },
- hasValueType(key, type) {
- return !isNull && typeOfInstance === 'object'
- ? instance.check.hasValueType(key, type)
- : false;
- },
- hasLength(expectedLength) {
- return !isNull && (typeOfInstance === 'object' || typeOfInstance === 'string')
- ? instance.check.hasLength(expectedLength)
- : false;
- },
- hasParamsCount(expectedCount) {
- return !isNull && (typeOfInstance === 'function')
- ? instance.check.hasParamsCount(expectedCount)
- : false;
- },
- hasWordsCount(expectedCount) {
- return !isNull && (typeOfInstance === 'string')
- ? instance.check.hasWordsCount(expectedCount)
- : false;
- },
- not: {
- containsKeys(keys) {
- return !isNull && typeOfInstance === 'object'
- ? !instance.check.containsKeys(keys)
- : false;
- },
- isNull() {
- return isNull;
- },
- hasKeys(keys) {
- return !isNull && typeOfInstance === 'object'
- ? !instance.check.hasKeys(keys)
- : false;
- },
- containsValues(values) {
- return !isNull && typeOfInstance === 'object'
- ? !instance.check.containsValues(values)
- : false;
- },
- hasValues(values) {
- return !isNull && typeOfInstance === 'object'
- ? !instance.check.hasValues(values)
- : false;
- },
- hasValueType(key, type) {
- return !isNull && typeOfInstance === 'object'
- ? !instance.check.hasValueType(key, type)
- : false;
- },
- hasLength(expectedLength) {
- return !isNull && (typeOfInstance === 'object' || typeOfInstance === 'string')
- ? !instance.check.hasLength(expectedLength)
- : false;
- },
- hasParamsCount(expectedCount) {
- return !isNull && (typeOfInstance === 'function')
- ? !instance.check.hasParamsCount(expectedCount)
- : false;
- },
- hasWordsCount(expectedCount) {
- return !isNull && (typeOfInstance === 'string')
- ? !instance.check.hasWordsCount(expectedCount)
- : false;
- }
- },
- check: {
- containsKeys(keys) {
- return !isNull && typeOfInstance === 'object'
- ? instance.check.containsKeys(keys)
- : false;
- },
- isNull() {
- return isNull;
- },
- hasKeys(keys) {
- return !isNull && typeOfInstance === 'object'
- ? instance.check.hasKeys(keys)
- : false;
- },
- containsValues(values) {
- return !isNull && typeOfInstance === 'object'
- ? instance.check.containsValues(values)
- : false;
- },
- hasValues(values) {
- return !isNull && typeOfInstance === 'object'
- ? instance.check.hasValues(values)
- : false;
- },
- hasValueType(key, type) {
- return !isNull && typeOfInstance === 'object'
- ? instance.check.hasValueType(key, type)
- : false;
- },
- hasLength(expectedLength) {
- return !isNull && (typeOfInstance === 'object' || typeOfInstance === 'string')
- ? instance.check.hasLength(expectedLength)
- : false;
- },
- hasParamsCount(expectedCount) {
- return !isNull && (typeOfInstance === 'function')
- ? instance.check.hasParamsCount(expectedCount)
- : false;
- },
- hasWordsCount(expectedCount) {
- return !isNull && (typeOfInstance === 'string')
- ? instance.check.hasWordsCount(expectedCount)
- : false;
- },
- not: {
- containsKeys(keys) {
- return !isNull && typeOfInstance === 'object'
- ? !instance.check.containsKeys(keys)
- : false;
- },
- isNull() {
- return isNull;
- },
- hasKeys(keys) {
- return !isNull && typeOfInstance === 'object'
- ? !instance.check.hasKeys(keys)
- : false;
- },
- containsValues(values) {
- return !isNull && typeOfInstance === 'object'
- ? !instance.check.containsValues(values)
- : false;
- },
- hasValues(values) {
- return !isNull && typeOfInstance === 'object'
- ? !instance.check.hasValues(values)
- : false;
- },
- hasValueType(key, type) {
- return !isNull && typeOfInstance === 'object'
- ? !instance.check.hasValueType(key, type)
- : false;
- },
- hasLength(expectedLength) {
- return !isNull && (typeOfInstance === 'object' || typeOfInstance === 'string')
- ? !instance.check.hasLength(expectedLength)
- : false;
- },
- hasParamsCount(expectedCount) {
- return !isNull && (typeOfInstance === 'function')
- ? !instance.check.hasParamsCount(expectedCount)
- : false;
- },
- hasWordsCount(expectedCount) {
- return !isNull && (typeOfInstance === 'string')
- ? !instance.check.hasWordsCount(expectedCount)
- : false;
- }
- }
- }
- };
- }
- exports.init = function () {
- initObjectExtensions();
- initArrayExtensions();
- initStringExtensions();
- initFunctionExtensions();
- };
- exports.wrap = wrap;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement