Repository navigation
Expand file tree
/
Copy pathutil.js
More file actions
86 lines (72 loc) · 2.69 KB
/
Copy pathutil.js
File metadata and controls
86 lines (72 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* @fileoverview Common utilities.
*/
"use strict";
//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
var PLUGIN_NAME_PREFIX = "eslint-plugin-";
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
/**
* Merges two objects together and assigns the result to the initial object. Can be used for shallow cloning.
* @param {Object} target of the cloning operation
* @param {Object} source object
* @returns {void}
*/
exports.mixin = function(target, source) {
Object.keys(source).forEach(function(key) {
target[key] = source[key];
});
};
/**
* Merges two config objects. This will not only add missing keys, but will also modify values to match.
* @param {Object} base config object
* @param {Object} custom config object. Overrides in this config object will take priority over base.
* @returns {Object} merged config object.
*/
exports.mergeConfigs = function mergeConfigs(base, custom) {
Object.keys(custom).forEach(function (key) {
var property = custom[key];
if (key === "plugins") {
if (!base[key]) {
base[key] = [];
}
property.forEach(function (plugin) {
// skip duplicates
if (base[key].indexOf(plugin) === -1) {
base[key].push(plugin);
}
});
return;
}
if (Array.isArray(base[key]) && !Array.isArray(property) && typeof property === "number") {
// assume that we are just overriding first attribute
base[key][0] = custom[key];
return;
}
if (typeof property === "object" && !Array.isArray(property)) {
// base[key] might not exist, so be careful with recursion here
base[key] = mergeConfigs(base[key] || {}, custom[key]);
} else {
base[key] = custom[key];
}
});
return base;
};
/**
* Removes the prefix `eslint-plugin-` from a plugin name.
* @param {string} pluginName The name of the plugin which may has the prefix.
* @returns {string} The name of the plugin without prefix.
*/
exports.removePluginPrefix = function removePluginPrefix(pluginName) {
var nameWithoutPrefix;
if (pluginName.indexOf(PLUGIN_NAME_PREFIX) === 0) {
nameWithoutPrefix = pluginName.substring(PLUGIN_NAME_PREFIX.length);
} else {
nameWithoutPrefix = pluginName;
}
return nameWithoutPrefix;
};
exports.PLUGIN_NAME_PREFIX = PLUGIN_NAME_PREFIX;