Skip to content

Commit 293a852

Browse files
committed
fix: remove lodash
1 parent b1d3c4d commit 293a852

File tree

5 files changed

+62
-37
lines changed

5 files changed

+62
-37
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"build": "rimraf lib && tsc",
1111
"test": "jest",
1212
"lint": "eslint . --ext .js,.ts",
13+
"fmt": "eslint . --ext .js,.ts --fix",
1314
"semantic-release": "semantic-release",
1415
"commit": "git-cz"
1516
},
@@ -43,7 +44,6 @@
4344
"await-lock": "^2.0.1",
4445
"expression-eval": "^2.0.0",
4546
"ip": "^1.1.5",
46-
"lodash": "^4.17.15",
4747
"micromatch": "^4.0.2"
4848
},
4949
"files": [
@@ -64,7 +64,7 @@
6464
"husky": {
6565
"hooks": {
6666
"prepare-commit-msg": "exec < /dev/tty && git cz --hook || true",
67-
"pre-commit": "yarn lint && pretty-quick --staged"
67+
"pre-commit": "yarn fmt && pretty-quick --staged"
6868
}
6969
},
7070
"config": {

src/model/assertion.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
import * as rbac from '../rbac';
16-
import * as _ from 'lodash';
1716
import { logPrint } from '../log';
1817
import { PolicyOp } from './model';
1918

@@ -39,7 +38,7 @@ export class Assertion {
3938

4039
public async buildIncrementalRoleLinks(rm: rbac.RoleManager, op: PolicyOp, rules: string[][]): Promise<void> {
4140
this.rm = rm;
42-
const count = _.words(this.value, /_/g).length;
41+
const count = (this.value.match(/_/g) || []).length;
4342
if (count < 2) {
4443
throw new Error('the number of "_" in role definition should be at least 2');
4544
}
@@ -65,7 +64,7 @@ export class Assertion {
6564

6665
public async buildRoleLinks(rm: rbac.RoleManager): Promise<void> {
6766
this.rm = rm;
68-
const count = _.words(this.value, /_/g).length;
67+
const count = (this.value.match(/_/g) || []).length;
6968
if (count < 2) {
7069
throw new Error('the number of "_" in role definition should be at least 2');
7170
}

src/model/model.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import * as _ from 'lodash';
1615
import * as rbac from '../rbac';
1716
import * as util from '../util';
1817
import { Config, ConfigInterface } from '../config';
@@ -57,7 +56,7 @@ export class Model {
5756
return '';
5857
}
5958

60-
return _.toString(i);
59+
return i.toString();
6160
}
6261

6362
private loadSection(cfg: ConfigInterface, sec: string): void {
@@ -256,7 +255,7 @@ export class Model {
256255
if (!ast) {
257256
return false;
258257
}
259-
ast.policy = _.filter(ast.policy, r => !util.arrayEquals(rule, r));
258+
ast.policy = ast.policy.filter(r => !util.arrayEquals(rule, r));
260259
return true;
261260
}
262261

@@ -278,7 +277,7 @@ export class Model {
278277
}
279278

280279
for (const rule of rules) {
281-
ast.policy = _.filter(ast.policy, (r: string[]) => {
280+
ast.policy = ast.policy.filter((r: string[]) => {
282281
const equals = util.arrayEquals(rule, r);
283282
if (equals) {
284283
effects.push(r);

src/util/builtinOperators.ts

+24-18
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import * as rbac from '../rbac';
1616
import * as ip from 'ip';
17-
import * as _ from 'lodash';
1817
import { isMatch } from 'micromatch';
1918

2019
// regexMatch determines whether key1 matches the pattern of key2 in regular expression.
@@ -40,8 +39,9 @@ function keyMatch(key1: string, key2: string): boolean {
4039

4140
// keyMatchFunc is the wrapper for keyMatch.
4241
function keyMatchFunc(...args: any[]): boolean {
43-
const name1: string = _.toString(args[0]);
44-
const name2: string = _.toString(args[1]);
42+
const [arg0, arg1] = args;
43+
const name1: string = (arg0 || '').toString();
44+
const name2: string = (arg1 || '').toString();
4545

4646
return keyMatch(name1, name2);
4747
}
@@ -54,7 +54,7 @@ function keyMatch2(key1: string, key2: string): boolean {
5454

5555
const regexp = new RegExp(/(.*):[^/]+(.*)/g);
5656
for (;;) {
57-
if (!_.includes(key2, '/:')) {
57+
if (!key2.includes('/:')) {
5858
break;
5959
}
6060
key2 = key2.replace(regexp, '$1[^/]+$2');
@@ -65,8 +65,9 @@ function keyMatch2(key1: string, key2: string): boolean {
6565

6666
// keyMatch2Func is the wrapper for keyMatch2.
6767
function keyMatch2Func(...args: any[]): boolean {
68-
const name1: string = _.toString(args[0]);
69-
const name2: string = _.toString(args[1]);
68+
const [arg0, arg1] = args;
69+
const name1: string = (arg0 || '').toString();
70+
const name2: string = (arg1 || '').toString();
7071

7172
return keyMatch2(name1, name2);
7273
}
@@ -78,7 +79,7 @@ function keyMatch3(key1: string, key2: string): boolean {
7879

7980
const regexp = new RegExp(/(.*){[^/]+}(.*)/g);
8081
for (;;) {
81-
if (!_.includes(key2, '/{')) {
82+
if (!key2.includes('/{')) {
8283
break;
8384
}
8485
key2 = key2.replace(regexp, '$1[^/]+$2');
@@ -89,8 +90,9 @@ function keyMatch3(key1: string, key2: string): boolean {
8990

9091
// keyMatch3Func is the wrapper for keyMatch3.
9192
function keyMatch3Func(...args: any[]): boolean {
92-
const name1: string = _.toString(args[0]);
93-
const name2: string = _.toString(args[1]);
93+
const [arg0, arg1] = args;
94+
const name1: string = (arg0 || '').toString();
95+
const name2: string = (arg1 || '').toString();
9496

9597
return keyMatch3(name1, name2);
9698
}
@@ -166,16 +168,18 @@ function keyMatch4(key1: string, key2: string): boolean {
166168

167169
// keyMatch4Func is the wrapper for keyMatch4.
168170
function keyMatch4Func(...args: any[]): boolean {
169-
const name1: string = _.toString(args[0]);
170-
const name2: string = _.toString(args[1]);
171+
const [arg0, arg1] = args;
172+
const name1: string = (arg0 || '').toString();
173+
const name2: string = (arg1 || '').toString();
171174

172175
return keyMatch4(name1, name2);
173176
}
174177

175178
// regexMatchFunc is the wrapper for regexMatch.
176179
function regexMatchFunc(...args: any[]): boolean {
177-
const name1: string = _.toString(args[0]);
178-
const name2: string = _.toString(args[1]);
180+
const [arg0, arg1] = args;
181+
const name1: string = (arg0 || '').toString();
182+
const name2: string = (arg1 || '').toString();
179183

180184
return regexMatch(name1, name2);
181185
}
@@ -203,8 +207,9 @@ function ipMatch(ip1: string, ip2: string): boolean {
203207

204208
// ipMatchFunc is the wrapper for ipMatch.
205209
function ipMatchFunc(...args: any[]): boolean {
206-
const ip1: string = _.toString(args[0]);
207-
const ip2: string = _.toString(args[1]);
210+
const [arg0, arg1] = args;
211+
const ip1: string = (arg0 || '').toString();
212+
const ip2: string = (arg1 || '').toString();
208213

209214
return ipMatch(ip1, ip2);
210215
}
@@ -229,15 +234,16 @@ function globMatch(string: string, pattern: string): boolean {
229234
// generateGFunction is the factory method of the g(_, _) function.
230235
function generateGFunction(rm: rbac.RoleManager): any {
231236
return async function func(...args: any[]): Promise<boolean> {
232-
const name1: string = _.toString(args[0]);
233-
const name2: string = _.toString(args[1]);
237+
const [arg0, arg1] = args;
238+
const name1: string = (arg0 || '').toString();
239+
const name2: string = (arg1 || '').toString();
234240

235241
if (!rm) {
236242
return name1 === name2;
237243
} else if (args.length === 2) {
238244
return await rm.hasLink(name1, name2);
239245
} else {
240-
const domain: string = _.toString(args[2]);
246+
const domain: string = args[2].toString();
241247
return await rm.hasLink(name1, name2, domain);
242248
}
243249
};

src/util/util.ts

+31-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import * as _ from 'lodash';
1615
import * as fs from 'fs';
1716

1817
// escapeAssertion escapes the dots in the assertion,
@@ -26,37 +25,59 @@ function escapeAssertion(s: string): string {
2625
// removeComments removes the comments starting with # in the text.
2726
function removeComments(s: string): string {
2827
const pos = s.indexOf('#');
29-
return pos > -1 ? _.trim(s.slice(0, pos)) : s;
28+
return pos > -1 ? s.slice(0, pos).trim() : s;
3029
}
3130

3231
// arrayEquals determines whether two string arrays are identical.
33-
function arrayEquals(a: string[], b: string[]): boolean {
34-
return _.isEqual(a, b);
32+
function arrayEquals(a: string[] = [], b: string[] = []): boolean {
33+
const aLen = a.length;
34+
const bLen = b.length;
35+
if (aLen !== bLen) {
36+
return false;
37+
}
38+
39+
for (let i = 0; i < aLen; i++) {
40+
if (a[i] != b[i]) {
41+
return false;
42+
}
43+
}
44+
return true;
3545
}
3646

3747
// array2DEquals determines whether two 2-dimensional string arrays are identical.
38-
function array2DEquals(a: string[][], b: string[][]): boolean {
39-
return _.isEqual(a, b);
48+
function array2DEquals(a: string[][] = [], b: string[][] = []): boolean {
49+
const aLen = a.length;
50+
const bLen = a.length;
51+
if (aLen != bLen) {
52+
return false;
53+
}
54+
55+
for (let i = 0; i < aLen; i++) {
56+
if (!arrayEquals(a[i], b[i])) {
57+
return false;
58+
}
59+
}
60+
return true;
4061
}
4162

4263
// arrayRemoveDuplicates removes any duplicated elements in a string array.
4364
function arrayRemoveDuplicates(s: string[]): string[] {
44-
return _.uniq(s);
65+
return [...new Set(s)];
4566
}
4667

4768
// arrayToString gets a printable string for a string array.
4869
function arrayToString(a: string[]): string {
49-
return _.join(a, ', ');
70+
return a.join(', ');
5071
}
5172

5273
// paramsToString gets a printable string for variable number of parameters.
5374
function paramsToString(...v: string[]): string {
54-
return _.join(v, ', ');
75+
return v.join(', ');
5576
}
5677

5778
// setEquals determines whether two string sets are identical.
5879
function setEquals(a: string[], b: string[]): boolean {
59-
return _.isEqual(_.sortedUniq(a), _.sortedUniq(b));
80+
return arrayEquals(a.sort(), b.sort());
6081
}
6182

6283
// readFile return a promise for readFile.

0 commit comments

Comments
 (0)