Conditions | 1 |
Paths | 1 |
Total Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | define(function(require) { |
||
2 | 'use strict'; |
||
3 | |||
4 | var _ = require('underscore'); |
||
5 | var $ = require('jquery'); |
||
6 | var __ = require('orotranslation/js/translator'); |
||
7 | var messenger = require('oroui/js/messenger'); |
||
8 | |||
9 | var defaultParam = { |
||
10 | message: 'All product prices should be unique.' |
||
11 | }; |
||
12 | |||
13 | /** |
||
14 | * @param {HTMLElement} element |
||
15 | * @return {string} |
||
16 | */ |
||
17 | function getStringifiedValues(element) { |
||
18 | var price = $(element); |
||
19 | |||
20 | var priceList = price.find('input[name$="[priceList]"]').val(); |
||
21 | var quantity = price.find('input[name$="[quantity]"]').val(); |
||
22 | var unit = price.find('select[name$="[unit]"] option:selected').val(); |
||
23 | var currency = price.find('select[name$="[currency]"] option:selected').val(); |
||
24 | |||
25 | if ( |
||
26 | !priceList || !priceList.trim() || |
||
27 | !quantity || !quantity.trim() || |
||
28 | !unit || !unit.trim() || |
||
29 | !currency || !currency.trim() |
||
30 | ) { |
||
31 | return ''; |
||
32 | } |
||
33 | |||
34 | return [priceList, parseFloat(quantity), unit, currency].join(':'); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @export oropricing/js/validator/unique-product-prices |
||
39 | */ |
||
40 | return [ |
||
41 | 'Oro\\Bundle\\PricingBundle\\Validator\\Constraints\\UniqueProductPrices', |
||
42 | function(value, element) { |
||
43 | var processedPrices = []; |
||
44 | var $container = $(element).closest('.oro-item-collection'); |
||
45 | |||
46 | var duplicate = _.find($container.find('.oro-multiselect-holder'), function(price) { |
||
47 | var stringifiedPrice = getStringifiedValues(price); |
||
48 | |||
49 | if (stringifiedPrice === '') { |
||
50 | return; |
||
51 | } |
||
52 | if (processedPrices.indexOf(stringifiedPrice) !== -1) { |
||
53 | // duplicates are found |
||
54 | return true; |
||
55 | } else { |
||
56 | processedPrices.push(stringifiedPrice); |
||
|
|||
57 | } |
||
58 | }); |
||
59 | |||
60 | return duplicate === void 0; |
||
61 | }, |
||
62 | function(param) { |
||
63 | param = _.extend({}, defaultParam, param); |
||
64 | messenger.notificationFlashMessage('error', __(param.message, {})); |
||
65 | |||
66 | return false; |
||
67 | } |
||
68 | ]; |
||
69 | }); |
||
70 |