Skip to content

Commit e71b40f

Browse files
nodeceZxilly
authored andcommitted
feat: add policyArrayToString and policyStringToArray to util
Signed-off-by: Zixuan Liu <nodeces@gmail.com>
1 parent 4585aac commit e71b40f

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

src/persist/helper.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
import { Model } from '../model';
2+
import { policyStringToArray } from '../util';
23

34
export class Helper {
45
public static loadPolicyLine(line: string, model: Model): void {
56
if (!line || line.trimStart().charAt(0) === '#') {
67
return;
78
}
89

9-
let tokens: any = undefined;
10-
11-
try {
12-
// eslint-disable-next-line @typescript-eslint/no-var-requires
13-
const parse = require('csv-parse/lib/sync');
14-
tokens = parse(line, {
15-
delimiter: ',',
16-
skip_empty_lines: true,
17-
trim: true,
18-
});
19-
} catch {
20-
throw new Error('Please add csv-parse to your dependency.');
21-
}
10+
const tokens = policyStringToArray(line);
2211

2312
if (!tokens || !tokens[0]) {
2413
return;

src/util/util.ts

+21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
// escapeAssertion escapes the dots in the assertion,
1616
// because the expression evaluation doesn't support such variable names.
17+
18+
import parse from 'csv-parse/lib/sync';
19+
1720
function escapeAssertion(s: string): string {
1821
s = s.replace(/r\./g, 'r_');
1922
s = s.replace(/p\./g, 'p_');
@@ -144,6 +147,22 @@ function deepCopy(obj: Array<any> | any): any {
144147
return newObj;
145148
}
146149

150+
function policyArrayToString(policy: string[]): string {
151+
return policy
152+
.map((n) => {
153+
return `"${(n === null ? '' : n.toString()).replace(/"/g, '""')}"`;
154+
})
155+
.join(',');
156+
}
157+
158+
function policyStringToArray(policy: string): string[][] {
159+
return parse(policy, {
160+
delimiter: ',',
161+
skip_empty_lines: true,
162+
trim: true,
163+
});
164+
}
165+
147166
export {
148167
escapeAssertion,
149168
removeComments,
@@ -159,4 +178,6 @@ export {
159178
generatorRunSync,
160179
generatorRunAsync,
161180
deepCopy,
181+
policyArrayToString,
182+
policyStringToArray,
162183
};

test/util.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,18 @@ test('test getEvalValue', () => {
155155
expect(util.arrayEquals(util.getEvalValue('eval(a) && eval(b) && a && b && c'), ['a', 'b']));
156156
expect(util.arrayEquals(util.getEvalValue('a && eval(a) && eval(b) && b && c'), ['a', 'b']));
157157
});
158+
159+
test('test policyStringToArray', () => {
160+
expect(util.policyStringToArray('p,alice,data1,read')).toEqual([['p', 'alice', 'data1', 'read']]);
161+
expect(util.policyStringToArray(`"p","alice","data1","read"`)).toEqual([['p', 'alice', 'data1', 'read']]);
162+
expect(util.policyStringToArray(`"p"," alice","data1 ","read"`)).toEqual([['p', ' alice', 'data1 ', 'read']]);
163+
expect(util.policyStringToArray(`p,alice,data1,read\np,bob,data1,write`)).toEqual([
164+
['p', 'alice', 'data1', 'read'],
165+
['p', 'bob', 'data1', 'write'],
166+
]);
167+
});
168+
169+
test('test policyArrayToString', () => {
170+
expect(util.policyArrayToString(['p', 'alice', 'data1', 'read'])).toEqual(`"p","alice","data1","read"`);
171+
expect(util.policyArrayToString(['p', 'alice ', ' data1', 'read'])).toEqual(`"p","alice "," data1","read"`);
172+
});

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"strictPropertyInitialization": false,
88
"declaration": true,
99
"downlevelIteration": true,
10-
"allowSyntheticDefaultImports": true
10+
"allowSyntheticDefaultImports": true,
11+
"esModuleInterop": true
1112
},
1213
"include": ["src/**/*", "test/utils.ts"]
1314
}

0 commit comments

Comments
 (0)