Completed
Push — master ( 5ba72b...438040 )
by Xu
383:15 queued 343:30
created

➔ loadImage.getTransformedOptions   C

Complexity

Conditions 17
Paths 98

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
c 1
b 0
f 0
nc 98
nop 3
dl 0
loc 75
rs 5.2934

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like load-image-orientation.js ➔ ... ➔ loadImage.getTransformedOptions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*
2
 * JavaScript Load Image Orientation
3
 * https://github.com/blueimp/JavaScript-Load-Image
4
 *
5
 * Copyright 2013, Sebastian Tschan
6
 * https://blueimp.net
7
 *
8
 * Licensed under the MIT license:
9
 * https://opensource.org/licenses/MIT
10
 */
11
12
/* global define */
13
14
;(function (factory) {
15
  'use strict'
16
  if (typeof define === 'function' && define.amd) {
17
    // Register as an anonymous AMD module:
18
    define(['./load-image', './load-image-scale', './load-image-meta'], factory)
19
  } else if (typeof module === 'object' && module.exports) {
20
    factory(
21
      require('./load-image'),
22
      require('./load-image-scale'),
23
      require('./load-image-meta')
24
    )
25
  } else {
26
    // Browser globals:
27
    factory(window.loadImage)
28
  }
29
})(function (loadImage) {
30
  'use strict'
31
32
  var originalHasCanvasOption = loadImage.hasCanvasOption
33
  var originalHasMetaOption = loadImage.hasMetaOption
34
  var originalTransformCoordinates = loadImage.transformCoordinates
35
  var originalGetTransformedOptions = loadImage.getTransformedOptions
36
37
  // Determines if the target image should be a canvas element:
38
  loadImage.hasCanvasOption = function (options) {
39
    return (
40
      !!options.orientation || originalHasCanvasOption.call(loadImage, options)
41
    )
42
  }
43
44
  // Determines if meta data should be loaded automatically:
45
  loadImage.hasMetaOption = function (options) {
46
    return (
47
      (options && options.orientation === true) ||
48
      originalHasMetaOption.call(loadImage, options)
49
    )
50
  }
51
52
  // Transform image orientation based on
53
  // the given EXIF orientation option:
54
  loadImage.transformCoordinates = function (canvas, options) {
55
    originalTransformCoordinates.call(loadImage, canvas, options)
56
    var ctx = canvas.getContext('2d')
57
    var width = canvas.width
58
    var height = canvas.height
59
    var styleWidth = canvas.style.width
60
    var styleHeight = canvas.style.height
61
    var orientation = options.orientation
62
    if (!orientation || orientation > 8) {
63
      return
64
    }
65
    if (orientation > 4) {
66
      canvas.width = height
67
      canvas.height = width
68
      canvas.style.width = styleHeight
69
      canvas.style.height = styleWidth
70
    }
71
    switch (orientation) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
72
      case 2:
73
        // horizontal flip
74
        ctx.translate(width, 0)
75
        ctx.scale(-1, 1)
76
        break
77
      case 3:
78
        // 180° rotate left
79
        ctx.translate(width, height)
80
        ctx.rotate(Math.PI)
81
        break
82
      case 4:
83
        // vertical flip
84
        ctx.translate(0, height)
85
        ctx.scale(1, -1)
86
        break
87
      case 5:
88
        // vertical flip + 90 rotate right
89
        ctx.rotate(0.5 * Math.PI)
90
        ctx.scale(1, -1)
91
        break
92
      case 6:
93
        // 90° rotate right
94
        ctx.rotate(0.5 * Math.PI)
95
        ctx.translate(0, -height)
96
        break
97
      case 7:
98
        // horizontal flip + 90 rotate right
99
        ctx.rotate(0.5 * Math.PI)
100
        ctx.translate(width, -height)
101
        ctx.scale(-1, 1)
102
        break
103
      case 8:
104
        // 90° rotate left
105
        ctx.rotate(-0.5 * Math.PI)
106
        ctx.translate(-width, 0)
107
        break
108
    }
109
  }
110
111
  // Transforms coordinate and dimension options
112
  // based on the given orientation option:
113
  loadImage.getTransformedOptions = function (img, opts, data) {
114
    var options = originalGetTransformedOptions.call(loadImage, img, opts)
115
    var orientation = options.orientation
116
    var newOptions
117
    var i
118
    if (orientation === true && data && data.exif) {
119
      orientation = data.exif.get('Orientation')
120
    }
121
    if (!orientation || orientation > 8 || orientation === 1) {
122
      return options
123
    }
124
    newOptions = {}
125
    for (i in options) {
126
      if (options.hasOwnProperty(i)) {
127
        newOptions[i] = options[i]
128
      }
129
    }
130
    newOptions.orientation = orientation
131
    switch (orientation) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
132
      case 2:
133
        // horizontal flip
134
        newOptions.left = options.right
135
        newOptions.right = options.left
136
        break
137
      case 3:
138
        // 180° rotate left
139
        newOptions.left = options.right
140
        newOptions.top = options.bottom
141
        newOptions.right = options.left
142
        newOptions.bottom = options.top
143
        break
144
      case 4:
145
        // vertical flip
146
        newOptions.top = options.bottom
147
        newOptions.bottom = options.top
148
        break
149
      case 5:
150
        // vertical flip + 90 rotate right
151
        newOptions.left = options.top
152
        newOptions.top = options.left
153
        newOptions.right = options.bottom
154
        newOptions.bottom = options.right
155
        break
156
      case 6:
157
        // 90° rotate right
158
        newOptions.left = options.top
159
        newOptions.top = options.right
160
        newOptions.right = options.bottom
161
        newOptions.bottom = options.left
162
        break
163
      case 7:
164
        // horizontal flip + 90 rotate right
165
        newOptions.left = options.bottom
166
        newOptions.top = options.right
167
        newOptions.right = options.top
168
        newOptions.bottom = options.left
169
        break
170
      case 8:
171
        // 90° rotate left
172
        newOptions.left = options.bottom
173
        newOptions.top = options.left
174
        newOptions.right = options.top
175
        newOptions.bottom = options.right
176
        break
177
    }
178
    if (newOptions.orientation > 4) {
179
      newOptions.maxWidth = options.maxHeight
180
      newOptions.maxHeight = options.maxWidth
181
      newOptions.minWidth = options.minHeight
182
      newOptions.minHeight = options.minWidth
183
      newOptions.sourceWidth = options.sourceHeight
184
      newOptions.sourceHeight = options.sourceWidth
185
    }
186
    return newOptions
187
  }
188
})
189