Skip to content

Commit 35ac4ae

Browse files
committed
refactor: extract utility methods
1 parent 18e561e commit 35ac4ae

File tree

4 files changed

+75
-22
lines changed

4 files changed

+75
-22
lines changed

lib/rules/folder-naming-convention.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
globPatternValidator,
1818
} = require('../utils/settings');
1919
const { getDocUrl } = require('../utils/doc');
20+
const { isNotEmpty } = require('../utils/utility');
2021
const NAMING_CONVENTION = require('../constants/naming-convention');
2122

2223
/** @typedef {module:eslint} ESLint */
@@ -69,9 +70,10 @@ module.exports = {
6970
if (!micromatch.isMatch(path, folderPattern)) {
7071
continue;
7172
} else {
72-
const matchedPaths = micromatch.capture(folderPattern, path);
73+
const matchedPaths =
74+
micromatch.capture(folderPattern, path) || [];
7375
const folders = matchedPaths
74-
.filter((p) => !!p)
76+
.filter(isNotEmpty)
7577
.reduce((s, p) => s.concat(getAllFolders(p)), []);
7678

7779
for (const folder of folders) {

lib/utils/filename.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'use strict';
66

77
const path = require('path');
8+
const { pipe, isNotEmpty } = require('./utility');
89

910
/**
1011
* @returns {string} filename without path
@@ -34,8 +35,7 @@ const getBasename = (filename, ignoreMiddleExtensions = false) =>
3435
* @returns {string[]} all folders
3536
* @param {string} p path of folder in posix style
3637
*/
37-
const getAllFolders = (p) =>
38-
p.split(path.posix.sep).filter((folder) => folder !== '');
38+
const getAllFolders = (p) => p.split(path.posix.sep).filter(isNotEmpty);
3939

4040
/**
4141
* @returns {string[]} all sub paths
@@ -84,21 +84,6 @@ const toPosixPath = (p) => p.split(path.sep).join(path.posix.sep);
8484
*/
8585
const removeDriveLetter = (p) => p.replace(/^[A-Za-z]:\\/, '');
8686

87-
/**
88-
* Callback for file path
89-
*
90-
* @callback callback
91-
* @param {string} p file path
92-
*/
93-
/**
94-
* @returns {callback} piped function
95-
* @param {callback[]} fns callback functions
96-
*/
97-
const pipe =
98-
(...fns) =>
99-
(x) =>
100-
fns.reduce((v, f) => f(v), x);
101-
10287
/** @typedef {module:eslint} ESLint */
10388
/**
10489
* @returns {string} file path in posix style

lib/utils/transform.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
const micromatch = require('micromatch');
9+
const { isNil, isEmpty } = require('../utils/utility');
910
const { PREFINED_MATCH_SYNTAX_REGEXP } = require('../constants/regex');
1011

1112
/**
@@ -28,22 +29,22 @@ const transformRuleWithPrefinedMatchSyntax = (
2829
filenameWithPath
2930
);
3031

31-
if (!keyCaptureGroups) {
32+
if (isNil(keyCaptureGroups)) {
3233
return [filenamePattern, namingPattern];
3334
}
3435

3536
const valueCaptureGroups = [
3637
...namingPattern.matchAll(new RegExp(PREFINED_MATCH_SYNTAX_REGEXP, 'g')),
3738
];
3839

39-
if (!valueCaptureGroups || !valueCaptureGroups.length) {
40+
if (isEmpty(valueCaptureGroups)) {
4041
return [filenamePattern, namingPattern];
4142
}
4243

4344
const newNamingPattern = valueCaptureGroups.reduce((value, group) => {
4445
const groupIndex = +group[1];
4546

46-
if (!keyCaptureGroups || keyCaptureGroups[groupIndex] === undefined) {
47+
if (isNil(keyCaptureGroups[groupIndex])) {
4748
throw new Error(
4849
`The capture group "${namingPattern}" is not found in the glob "${filenamePattern}"`
4950
);

lib/utils/utility.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,71 @@
1212
*/
1313
const isObject = (x) => Object.prototype.toString.call(x) === '[object Object]';
1414

15+
/**
16+
* Checks if the given argument is an array
17+
*
18+
* @param {any} x - The argument to check
19+
* @returns {boolean} - True if the argument is an array, false otherwise
20+
*/
21+
const isArray = (x) =>
22+
x != null &&
23+
x.length >= 0 &&
24+
Object.prototype.toString.call(x) === '[object Array]';
25+
26+
/**
27+
* Checks if a value is undefined or null
28+
*
29+
* @param {any} x - The value to check
30+
* @returns {boolean} - Returns true if the value is undefined or null, false otherwise
31+
*/
32+
const isNil = (x) => x === undefined || x === null;
33+
34+
/**
35+
* Checks if a value is an empty value
36+
*
37+
* @param {any} x - The value to check
38+
* @returns {boolean} - Returns true if the value is an empty value, false otherwise
39+
*/
40+
const isEmpty = (x) =>
41+
x === '' ||
42+
(isArray(x) && x.length) === 0 ||
43+
(isObject(x) && Object.keys(x).length === 0);
44+
45+
/**
46+
* Negates a boolean value
47+
*
48+
* @param {boolean} x - The boolean value to negate
49+
* @returns {boolean} The negated boolean value
50+
*/
51+
const not = (x) => !x;
52+
53+
/**
54+
* Callback for file path
55+
*
56+
* @callback callback
57+
* @param {string} p file path
58+
*/
59+
/**
60+
* @returns {callback} piped function
61+
* @param {callback[]} fns callback functions
62+
*/
63+
const pipe =
64+
(...fns) =>
65+
(x) =>
66+
fns.reduce((v, f) => f(v), x);
67+
68+
/**
69+
* Checks if a value isnt an empty value
70+
*
71+
* @param {any} x - The value to check
72+
* @returns {boolean} - Returns true if the value isnt an empty value, false otherwise
73+
*/
74+
const isNotEmpty = pipe(isEmpty, not);
75+
1576
module.exports = {
1677
isObject,
78+
isNil,
79+
isEmpty,
80+
isNotEmpty,
81+
pipe,
1782
};

0 commit comments

Comments
 (0)