Conditions | 16 |
Total Lines | 80 |
Code Lines | 65 |
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:
Complex classes like OSPaths.ts ➔ Adapt 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 | // # spell-checker:ignore AllUsersProfile HomeDrive HomePath LocalAppData UserProfile WinDir falsey |
||
24 | |||
25 | function Adapt(adapter_: Platform.Adapter): { readonly OSPaths: OSPaths } { |
||
26 | const { env, os, path } = adapter_; |
||
27 | |||
28 | const isWinOS = /^win/i.test(adapter_.process.platform); |
||
29 | |||
30 | function normalizePath(path_: string | undefined): string | undefined { |
||
31 | return path_ ? adapter_.path.normalize(adapter_.path.join(path_, '.')) : void 0; |
||
32 | } |
||
33 | |||
34 | function home() { |
||
35 | const posix = () => |
||
36 | normalizePath((typeof os.homedir === 'function' ? os.homedir() : void 0) || env.get('HOME')); |
||
37 | |||
38 | const windows = () => { |
||
39 | const priorityList = [ |
||
40 | typeof os.homedir === 'function' ? os.homedir() : void 0, |
||
41 | env.get('USERPROFILE'), |
||
42 | env.get('HOME'), |
||
43 | env.get('HOMEDRIVE') || env.get('HOMEPATH') |
||
44 | ? path.join(env.get('HOMEDRIVE') || '', env.get('HOMEPATH') || '') |
||
45 | : void 0, |
||
46 | ]; |
||
47 | return normalizePath(priorityList.find((v) => !isEmpty(v))); |
||
48 | }; |
||
49 | |||
50 | return isWinOS ? windows() : posix(); |
||
51 | } |
||
52 | |||
53 | function temp() { |
||
54 | function joinPathToBase(base: string | undefined, segments: readonly string[]) { |
||
55 | return base ? path.join(base, ...segments) : void 0; |
||
56 | } |
||
57 | |||
58 | function posix() { |
||
59 | const fallback = '/tmp'; |
||
60 | const priorityList = [ |
||
61 | typeof os.tmpdir === 'function' ? os.tmpdir() : void 0, |
||
62 | env.get('TMPDIR'), |
||
63 | env.get('TEMP'), |
||
64 | env.get('TMP'), |
||
65 | ]; |
||
66 | return normalizePath(priorityList.find((v) => !isEmpty(v))) || fallback; |
||
67 | } |
||
68 | |||
69 | function windows() { |
||
70 | const fallback = 'C:\\Temp'; |
||
71 | const priorityListLazy = [ |
||
72 | os.tmpdir, |
||
73 | () => env.get('TEMP'), |
||
74 | () => env.get('TMP'), |
||
75 | () => joinPathToBase(env.get('LOCALAPPDATA'), ['Temp']), |
||
76 | () => joinPathToBase(home(), ['AppData', 'Local', 'Temp']), |
||
77 | () => joinPathToBase(env.get('ALLUSERSPROFILE'), ['Temp']), |
||
78 | () => joinPathToBase(env.get('SystemRoot'), ['Temp']), |
||
79 | () => joinPathToBase(env.get('windir'), ['Temp']), |
||
80 | () => joinPathToBase(env.get('SystemDrive'), ['\\', 'Temp']), |
||
81 | ]; |
||
82 | const v = priorityListLazy.find((v) => v && !isEmpty(v())); |
||
83 | return (v && normalizePath(v())) || fallback; |
||
84 | } |
||
85 | |||
86 | return isWinOS ? windows() : posix(); |
||
87 | } |
||
88 | |||
89 | // eslint-disable-next-line functional/no-class |
||
90 | class OSPaths_ { |
||
91 | constructor() { |
||
92 | function OSPaths(): OSPaths { |
||
93 | return new OSPaths_() as OSPaths; |
||
94 | } |
||
95 | |||
96 | OSPaths.home = home; |
||
97 | OSPaths.temp = temp; |
||
98 | |||
99 | return OSPaths; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | return { OSPaths: new OSPaths_() as OSPaths }; |
||
104 | } |
||
108 |