@celljs/core 3.1.1 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { ExpressionCompiler, JexlEngineProvider } from './expression-protocol';
1
+ import { ExpressionCompiler, ExpressionCompilerOptions, JexlEngineProvider } from './expression-protocol';
2
2
  import { Component, Autowired } from '../annotation';
3
3
 
4
4
  interface MiddleExpression {
@@ -17,15 +17,32 @@ export class ExpressionCompilerImpl implements ExpressionCompiler {
17
17
  @Autowired(JexlEngineProvider)
18
18
  protected readonly jexlEngineProvider: JexlEngineProvider<any>;
19
19
 
20
- compileSections(text: string): any[] {
21
- if (!text || text.indexOf(this.SPECIAL_CHAR) < 0) {
20
+ protected getSpecialChar(opitons: Required<ExpressionCompilerOptions>): string {
21
+ if (opitons.ignoreSpecialChar) {
22
+ return '';
23
+ }
24
+ return opitons.specialChar;
25
+ }
26
+
27
+ protected equalsSpecialChar(opitons: Required<ExpressionCompilerOptions>, char: string): boolean {
28
+ if (opitons.ignoreSpecialChar) {
29
+ return false;
30
+ }
31
+ return char === opitons.specialChar;
32
+ }
33
+
34
+ compileSections(text: string, options: ExpressionCompilerOptions): any[] {
35
+ const merged = {
36
+ ...{ escapeChar: this.ESCAPE_CHAR, specialChar: this.SPECIAL_CHAR, bracketBegin: this.BRACKET_BEGIN, bracketEnd: this.BRACKET_END, ...options }
37
+ } as Required<ExpressionCompilerOptions>;
38
+ if (!text || text.indexOf(`${this.getSpecialChar(merged)}${merged.bracketBegin}`) < 0) {
22
39
  return [];
23
40
  }
24
41
 
25
42
  const sections: any[] = [];
26
43
  let middleText: string | undefined = text;
27
44
  while (middleText) {
28
- const me: MiddleExpression | undefined = this.middleCompile(middleText);
45
+ const me: MiddleExpression | undefined = this.middleCompile(middleText, merged);
29
46
  if (!me) {
30
47
  sections.push(middleText);
31
48
  middleText = undefined;
@@ -38,19 +55,19 @@ export class ExpressionCompilerImpl implements ExpressionCompiler {
38
55
  return sections;
39
56
  }
40
57
 
41
- protected middleCompile(text: string): MiddleExpression | undefined {
58
+ protected middleCompile(text: string, options: Required<ExpressionCompilerOptions>): MiddleExpression | undefined {
42
59
  let me: MiddleExpression | undefined;
43
- if (text.startsWith('${{')) {
44
- me = this.nextMiddleExpression(text.substring(3), 2);
45
- } else if (text.startsWith('${')) {
46
- me = this.nextMiddleExpression(text.substring(2));
60
+ if (text.startsWith(`${this.getSpecialChar(options)}${options.bracketBegin}${options.bracketBegin}`)) {
61
+ me = this.nextMiddleExpression(text.substring(3), 2, options);
62
+ } else if (text.startsWith(`${this.getSpecialChar(options)}${options.bracketBegin}`)) {
63
+ me = this.nextMiddleExpression(text.substring(2), undefined, options);
47
64
  } else {
48
- me = this.nextString(text);
65
+ me = this.nextString(text, options);
49
66
  }
50
67
  return me;
51
68
  }
52
69
 
53
- protected nextMiddleExpression(text: string, bracketBeginCharNum = 1): MiddleExpression | undefined {
70
+ protected nextMiddleExpression(text: string, bracketBeginCharNum = 1, options: Required<ExpressionCompilerOptions>): MiddleExpression | undefined {
54
71
  let stringed = false;
55
72
  let escaped = false;
56
73
  let bracketBeginCharFound = 0;
@@ -64,7 +81,7 @@ export class ExpressionCompilerImpl implements ExpressionCompiler {
64
81
  section.push(c);
65
82
  continue;
66
83
  } else
67
- if (c === this.ESCAPE_CHAR) {
84
+ if (c === options.escapeChar) {
68
85
  escaped = true;
69
86
  continue;
70
87
  }
@@ -74,17 +91,17 @@ export class ExpressionCompilerImpl implements ExpressionCompiler {
74
91
  section.push(c);
75
92
  escaped = false;
76
93
  } else if (escaped) {
77
- if (this.SPECIAL_CHAR === c || this.BRACKET_BEGIN === c || this.BRACKET_END === c) {
94
+ if (this.equalsSpecialChar(options, c) || options.bracketBegin === c || options.bracketEnd === c) {
78
95
  section.push(c);
79
96
  } else {
80
- section.push(this.ESCAPE_CHAR);
97
+ section.push(options.escapeChar);
81
98
  section.push(c);
82
99
  }
83
100
  escaped = false;
84
- } else if (this.BRACKET_BEGIN === c) {
101
+ } else if (options.bracketBegin === c) {
85
102
  bracketBeginCharFound++;
86
103
  section.push(c);
87
- } else if (this.BRACKET_END === c) {
104
+ } else if (options.bracketEnd === c) {
88
105
  if (bracketBeginCharFound === 0 && bracketBeginCharNum === 1) {
89
106
  const jexlEngine = this.jexlEngineProvider.provide();
90
107
  const expression = jexlEngine.createExpression(section.join(''));
@@ -105,7 +122,7 @@ export class ExpressionCompilerImpl implements ExpressionCompiler {
105
122
  }
106
123
  }
107
124
 
108
- protected nextString(text: string): MiddleExpression {
125
+ protected nextString(text: string, options: Required<ExpressionCompilerOptions>): MiddleExpression {
109
126
  let escaped = false;
110
127
  let specialCharFound = false;
111
128
 
@@ -117,33 +134,33 @@ export class ExpressionCompilerImpl implements ExpressionCompiler {
117
134
  section.push(c);
118
135
  continue;
119
136
  } else
120
- if (c === this.ESCAPE_CHAR) {
137
+ if (c === options.escapeChar) {
121
138
  escaped = true;
122
139
  continue;
123
140
  }
124
141
  }
125
142
 
126
143
  if (escaped) {
127
- if (this.SPECIAL_CHAR === c || this.BRACKET_BEGIN === c || this.BRACKET_END === c) {
144
+ if (this.equalsSpecialChar(options, c) || options.bracketBegin === c || options.bracketEnd === c) {
128
145
  section.push(c);
129
146
  } else {
130
- section.push(this.ESCAPE_CHAR);
147
+ section.push(options.escapeChar);
131
148
  section.push(c);
132
149
  }
133
150
  escaped = false;
134
151
  } else
135
152
  if (specialCharFound) {
136
- if (this.BRACKET_BEGIN === c) {
153
+ if (options.bracketBegin === c) {
137
154
  const expression = section.join('');
138
155
  const nextText = text.substring(i - 1);
139
156
  return { expression, nextText };
140
157
  } else {
141
158
  specialCharFound = false;
142
- section.push(this.SPECIAL_CHAR);
159
+ section.push(options.specialChar);
143
160
  section.push(c);
144
161
  }
145
162
  } else {
146
- if (this.SPECIAL_CHAR === c) {
163
+ if (this.equalsSpecialChar(options, c)) {
147
164
  specialCharFound = true;
148
165
  } else {
149
166
  section.push(c);
@@ -1,5 +1,5 @@
1
1
  import { Component, Autowired } from '../annotation';
2
- import { JexlEngineProvider, ExpressionHandler, ExpressionContextProvider, ExpressionCompiler, ExpressionContext } from './expression-protocol';
2
+ import { JexlEngineProvider, ExpressionHandler, ExpressionContextProvider, ExpressionCompiler, ExpressionContext, ExpressionCompilerOptions } from './expression-protocol';
3
3
  import * as traverse from 'traverse';
4
4
 
5
5
  @Component(ExpressionHandler)
@@ -26,14 +26,14 @@ export class ExpressionHandlerImpl implements ExpressionHandler {
26
26
  return c;
27
27
  }
28
28
 
29
- handle(textOrObj: string | Object, ctx?: ExpressionContext): any {
29
+ handle(textOrObj: string | Object, ctx?: ExpressionContext, expressionCompilerOptions?: ExpressionCompilerOptions): any {
30
30
  if (typeof textOrObj === 'string') {
31
- return this.doHandle(textOrObj, ctx);
31
+ return this.doHandle(textOrObj, ctx, expressionCompilerOptions);
32
32
  } else {
33
33
  const self = this;
34
34
  traverse(textOrObj).forEach(function (value: any) {
35
35
  if (typeof value === 'string') {
36
- this.update(self.handle(value, ctx));
36
+ this.update(self.handle(value, ctx, expressionCompilerOptions));
37
37
  } else if (value && (value as any)._ignoreEl === true) {
38
38
  this.update(value, true);
39
39
  }
@@ -42,8 +42,8 @@ export class ExpressionHandlerImpl implements ExpressionHandler {
42
42
  }
43
43
  }
44
44
 
45
- protected doHandle(text: string, ctx?: ExpressionContext): any {
46
- const sections = this.expressionCompiler.compileSections(text);
45
+ protected doHandle(text: string, ctx?: ExpressionContext, expressionCompilerOptions?: ExpressionCompilerOptions): any {
46
+ const sections = this.expressionCompiler.compileSections(text, expressionCompilerOptions);
47
47
  if (sections.length > 0) {
48
48
  if (this.hasExpression(sections)) {
49
49
  const c = this.getContext(ctx);
@@ -5,12 +5,12 @@ export const ExpressionContextProvider = Symbol('ExpressionContextProvider');
5
5
  export const JexlEngineProvider = Symbol('JexlEngineProvider');
6
6
 
7
7
  export interface ExpressionCompiler {
8
- compileSections(text: string): any[]
8
+ compileSections(text: string, options?: ExpressionCompilerOptions): any[]
9
9
  }
10
10
 
11
11
  export interface ExpressionHandler {
12
12
 
13
- handle(text: string | Object, ctx?: ExpressionContext): any;
13
+ handle(text: string | Object, ctx?: ExpressionContext, expressionCompilerOptions?: ExpressionCompilerOptions): any;
14
14
 
15
15
  }
16
16
 
@@ -18,6 +18,14 @@ export interface ExpressionContext {
18
18
  [key: string]: any;
19
19
  }
20
20
 
21
+ export interface ExpressionCompilerOptions {
22
+ escapeChar?: string;
23
+ specialChar?: string;
24
+ bracketBegin?: string;
25
+ bracketEnd?: string;
26
+ ignoreSpecialChar?: boolean;
27
+ }
28
+
21
29
  export interface ContextInitializer {
22
30
 
23
31
  initialize(ctx: ExpressionContext): void;
@@ -0,0 +1,80 @@
1
+ import { IllegalArgumentError } from '../error';
2
+ import { Bytes } from './types';
3
+
4
+ /**
5
+ * Byte utility class.
6
+ */
7
+ export class ByteUtil {
8
+ private static isNode(): boolean {
9
+ return typeof Buffer !== 'undefined';
10
+ }
11
+
12
+ private static isBrowser(): boolean {
13
+ return typeof TextDecoder !== 'undefined';
14
+ }
15
+
16
+ static decode(bytes: Bytes): string {
17
+ if (!bytes) {
18
+ return '';
19
+ }
20
+ if (typeof bytes === 'string') {
21
+ return bytes;
22
+ }
23
+
24
+ if (this.isNode()) {
25
+ if (bytes instanceof Buffer || bytes instanceof Uint8Array) {
26
+ return Buffer.from(bytes).toString('utf8');
27
+ }
28
+ throw new IllegalArgumentError(
29
+ `Unexpected type ${bytes.constructor.name} in Node environment.`
30
+ );
31
+ }
32
+
33
+ if (this.isBrowser()) {
34
+ if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
35
+ return new TextDecoder('utf8').decode(bytes);
36
+ }
37
+ throw new IllegalArgumentError(
38
+ `Unexpected type ${(bytes as any).constructor.name} in Browser environment.`
39
+ );
40
+ }
41
+
42
+ throw new IllegalArgumentError(
43
+ 'Neither Buffer nor TextDecoder are available.'
44
+ );
45
+ }
46
+
47
+ static encodeBase64(bytes: Bytes): string {
48
+ if (!bytes) {
49
+ return '';
50
+ }
51
+
52
+ if (this.isNode()) {
53
+ if (typeof bytes === 'string') {
54
+ return Buffer.from(bytes, 'utf8').toString('base64');
55
+ }
56
+ if (bytes instanceof Buffer || bytes instanceof Uint8Array) {
57
+ return Buffer.from(bytes).toString('base64');
58
+ }
59
+ throw new IllegalArgumentError(
60
+ `Unexpected type ${bytes.constructor.name} in Node environment.`
61
+ );
62
+ }
63
+
64
+ if (this.isBrowser()) {
65
+ if (typeof bytes === 'string') {
66
+ return btoa(new TextEncoder().encode(bytes).reduce((data, byte) => data + String.fromCharCode(byte), ''));
67
+ }
68
+ if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
69
+ return btoa(String.fromCharCode(...new Uint8Array(bytes)));
70
+ }
71
+ throw new IllegalArgumentError(
72
+ `Unexpected type ${(bytes as any).constructor.name} in Browser environment.`
73
+ );
74
+ }
75
+
76
+ throw new IllegalArgumentError(
77
+ 'Neither Buffer nor TextEncoder are available.'
78
+ );
79
+ }
80
+ }
@@ -17,3 +17,4 @@ export * from './uuid';
17
17
  export * from './assert';
18
18
  export * from './mime-type-util';
19
19
  export * from './mime-type';
20
+ export * from './byte-util';
@@ -11,3 +11,7 @@ export interface Type<T> extends Function {
11
11
 
12
12
  export type Mutable<T> = { -readonly [P in keyof T]: T[P] };
13
13
 
14
+ /**
15
+ * A byte array type that can be used to represent binary data.
16
+ */
17
+ export type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;