@jbrowse/jexl 2.3.1 → 3.0.2

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/LICENSE.txt +1 -0
  3. package/README.md +77 -85
  4. package/dist/Expression.d.ts +2 -12
  5. package/dist/Expression.js +5 -27
  6. package/dist/Expression.js.map +1 -1
  7. package/dist/Jexl.d.ts +6 -40
  8. package/dist/Jexl.js +7 -49
  9. package/dist/Jexl.js.map +1 -1
  10. package/dist/Lexer.js +3 -1
  11. package/dist/Lexer.js.map +1 -1
  12. package/dist/evaluator/Evaluator.d.ts +14 -55
  13. package/dist/evaluator/Evaluator.js +14 -73
  14. package/dist/evaluator/Evaluator.js.map +1 -1
  15. package/dist/evaluator/handlers.d.ts +19 -18
  16. package/dist/evaluator/handlers.js +54 -62
  17. package/dist/evaluator/handlers.js.map +1 -1
  18. package/dist/grammar.d.ts +3 -4
  19. package/dist/grammar.js +16 -39
  20. package/dist/grammar.js.map +1 -1
  21. package/dist/parser/Parser.js +16 -3
  22. package/dist/parser/Parser.js.map +1 -1
  23. package/dist/parser/states.js +7 -3
  24. package/dist/parser/states.js.map +1 -1
  25. package/esm/Expression.d.ts +2 -12
  26. package/esm/Expression.js +5 -27
  27. package/esm/Expression.js.map +1 -1
  28. package/esm/Jexl.d.ts +6 -40
  29. package/esm/Jexl.js +7 -49
  30. package/esm/Jexl.js.map +1 -1
  31. package/esm/Lexer.js +3 -1
  32. package/esm/Lexer.js.map +1 -1
  33. package/esm/evaluator/Evaluator.d.ts +14 -55
  34. package/esm/evaluator/Evaluator.js +14 -73
  35. package/esm/evaluator/Evaluator.js.map +1 -1
  36. package/esm/evaluator/handlers.d.ts +19 -18
  37. package/esm/evaluator/handlers.js +54 -62
  38. package/esm/evaluator/handlers.js.map +1 -1
  39. package/esm/grammar.d.ts +3 -4
  40. package/esm/grammar.js +16 -39
  41. package/esm/grammar.js.map +1 -1
  42. package/esm/parser/Parser.js +16 -3
  43. package/esm/parser/Parser.js.map +1 -1
  44. package/esm/parser/states.js +7 -3
  45. package/esm/parser/states.js.map +1 -1
  46. package/package.json +10 -8
  47. package/src/Expression.ts +5 -36
  48. package/src/Jexl.ts +7 -54
  49. package/src/Lexer.ts +3 -1
  50. package/src/evaluator/Evaluator.ts +14 -92
  51. package/src/evaluator/handlers.ts +54 -68
  52. package/src/grammar.ts +17 -38
  53. package/src/parser/Parser.ts +13 -3
  54. package/src/parser/states.ts +7 -3
  55. package/src/types.ts +4 -1
  56. package/dist/PromiseSync.d.ts +0 -13
  57. package/dist/PromiseSync.js +0 -80
  58. package/dist/PromiseSync.js.map +0 -1
  59. package/esm/PromiseSync.d.ts +0 -13
  60. package/esm/PromiseSync.js +0 -78
  61. package/esm/PromiseSync.js.map +0 -1
  62. package/src/PromiseSync.ts +0 -86
@@ -7,8 +7,7 @@ import type { AstNode } from '../types.ts'
7
7
  import type Evaluator from './Evaluator.ts'
8
8
 
9
9
  const poolNames: Record<string, string> = {
10
- functions: 'Jexl Function',
11
- transforms: 'Transform'
10
+ functions: 'Jexl Function'
12
11
  }
13
12
 
14
13
  /**
@@ -16,7 +15,7 @@ const poolNames: Record<string, string> = {
16
15
  * independently run through the evaluator.
17
16
  * @param {{type: 'ObjectLiteral', value: <{}>}} ast An expression tree with an
18
17
  * ObjectLiteral as the top node
19
- * @returns {Promise.<[]>} resolves to a map contained evaluated values.
18
+ * @returns {[]} an array of evaluated values.
20
19
  * @private
21
20
  */
22
21
  export function ArrayLiteral(this: Evaluator, ast: any) {
@@ -29,12 +28,12 @@ export function ArrayLiteral(this: Evaluator, ast: any) {
29
28
  * evaluators: `eval` is called with the left and right operands pre-evaluated.
30
29
  * `evalOnDemand`, if it exists, will be called with the left and right operands
31
30
  * each individually wrapped in an object with an "eval" function that returns
32
- * a promise with the resulting value. This allows the binary expression to
33
- * evaluate the operands conditionally.
31
+ * the resulting value. This allows the binary expression to evaluate the
32
+ * operands conditionally.
34
33
  * @param {{type: 'BinaryExpression', operator: <string>, left: {},
35
34
  * right: {}}} ast An expression tree with a BinaryExpression as the top
36
35
  * node
37
- * @returns {Promise<*>} resolves with the value of the BinaryExpression.
36
+ * @returns {*} the value of the BinaryExpression.
38
37
  * @private
39
38
  */
40
39
  export function BinaryExpression(this: Evaluator, ast: any) {
@@ -43,48 +42,45 @@ export function BinaryExpression(this: Evaluator, ast: any) {
43
42
  const wrap = (subAst: AstNode) => ({ eval: () => this.eval(subAst) })
44
43
  return grammarOp.evalOnDemand(wrap(ast.left), wrap(ast.right))
45
44
  }
46
- return this.Promise.all([this.eval(ast.left), this.eval(ast.right)]).then(
47
- (arr: any[]) => grammarOp.eval(arr[0], arr[1])
48
- )
45
+ const left = this.eval(ast.left)
46
+ const right = this.eval(ast.right)
47
+ return grammarOp.eval(left, right)
49
48
  }
50
49
 
51
50
  /**
52
51
  * Evaluates a ConditionalExpression node by first evaluating its test branch,
53
- * and resolving with the consequent branch if the test is truthy, or the
54
- * alternate branch if it is not. If there is no consequent branch, the test
55
- * result will be used instead.
52
+ * and returning the consequent branch if the test is truthy, or the alternate
53
+ * branch if it is not. If there is no consequent branch, the test result will
54
+ * be used instead.
56
55
  * @param {{type: 'ConditionalExpression', test: {}, consequent: {},
57
56
  * alternate: {}}} ast An expression tree with a ConditionalExpression as
58
57
  * the top node
59
58
  * @private
60
59
  */
61
60
  export function ConditionalExpression(this: Evaluator, ast: any) {
62
- return this.eval(ast.test).then((res: any) => {
63
- if (res) {
64
- if (ast.consequent) {
65
- return this.eval(ast.consequent)
66
- }
67
- return res
68
- }
69
- return this.eval(ast.alternate)
70
- })
61
+ const res = this.eval(ast.test)
62
+ if (res) {
63
+ return ast.consequent ? this.eval(ast.consequent) : res
64
+ }
65
+ return this.eval(ast.alternate)
71
66
  }
72
67
 
73
68
  /**
74
- * Evaluates a FilterExpression by applying it to the subject value.
69
+ * Evaluates a FilterExpression by applying bracket notation for array/object access.
70
+ * Note: Relative filtering (with leading dot) is not supported.
75
71
  * @param {{type: 'FilterExpression', relative: <boolean>, expr: {},
76
72
  * subject: {}}} ast An expression tree with a FilterExpression as the top
77
73
  * node
78
- * @returns {Promise<*>} resolves with the value of the FilterExpression.
74
+ * @returns {*} the value at the specified index/property.
79
75
  * @private
80
76
  */
81
77
  export function FilterExpression(this: Evaluator, ast: any) {
82
- return this.eval(ast.subject).then((subject: any) => {
83
- if (ast.relative) {
84
- return this._filterRelative(subject, ast.expr)
85
- }
86
- return this._filterStatic(subject, ast.expr)
87
- })
78
+ const subject = this.eval(ast.subject)
79
+ if (ast.relative) {
80
+ throw new Error('Relative filter expressions are not supported')
81
+ }
82
+ const index = this.eval(ast.expr)
83
+ return subject?.[index]
88
84
  }
89
85
 
90
86
  /**
@@ -93,8 +89,7 @@ export function FilterExpression(this: Evaluator, ast: any) {
93
89
  * constructed.
94
90
  * @param {{type: 'Identifier', value: <string>, [from]: {}}} ast An expression
95
91
  * tree with an Identifier as the top node
96
- * @returns {Promise<*>|*} either the identifier's value, or a Promise that
97
- * will resolve with the identifier's value.
92
+ * @returns {*} the identifier's value.
98
93
  * @private
99
94
  */
100
95
  export function Identifier(this: Evaluator, ast: any) {
@@ -102,13 +97,12 @@ export function Identifier(this: Evaluator, ast: any) {
102
97
  const contextSource = ast.relative ? this._relContext : this._context
103
98
  return contextSource[ast.value]
104
99
  }
105
- return this.eval(ast.from).then((context: any) => {
106
- if (context == null) {
107
- return undefined
108
- }
109
- const ctx = Array.isArray(context) ? context[0] : context
110
- return ctx?.[ast.value]
111
- })
100
+ const context = this.eval(ast.from)
101
+ if (context == null) {
102
+ return undefined
103
+ }
104
+ const ctx = Array.isArray(context) ? context[0] : context
105
+ return ctx?.[ast.value]
112
106
  }
113
107
 
114
108
  /**
@@ -127,23 +121,22 @@ export function Literal(this: Evaluator, ast: any) {
127
121
  * and concatenating all parts into a final string.
128
122
  * @param {{type: 'TemplateLiteral', parts: Array<{}>}} ast An expression
129
123
  * tree with a TemplateLiteral as the top node
130
- * @returns {Promise<string>} resolves with the final interpolated string
124
+ * @returns {string} the final interpolated string
131
125
  * @private
132
126
  */
133
127
  export function TemplateLiteral(this: Evaluator, ast: any) {
134
- const promises = ast.parts.map((part: any) => {
128
+ const values = ast.parts.map((part: any) => {
135
129
  if (part.type === 'static') {
136
- return this.Promise.resolve(part.value)
130
+ return part.value
137
131
  }
138
- return this.eval(part.value).then((result: any) => {
139
- if (result == null) {
140
- return ''
141
- }
142
- return String(result)
143
- })
132
+ const result = this.eval(part.value)
133
+ if (result == null) {
134
+ return ''
135
+ }
136
+ return String(result)
144
137
  })
145
138
 
146
- return this.Promise.all(promises).then((values: string[]) => values.join(''))
139
+ return values.join('')
147
140
  }
148
141
 
149
142
  /**
@@ -151,7 +144,7 @@ export function TemplateLiteral(this: Evaluator, ast: any) {
151
144
  * independently run through the evaluator.
152
145
  * @param {{type: 'ObjectLiteral', value: <{}>}} ast An expression tree with an
153
146
  * ObjectLiteral as the top node
154
- * @returns {Promise<{}>} resolves to a map contained evaluated values.
147
+ * @returns {{}} a map of evaluated values.
155
148
  * @private
156
149
  */
157
150
  export function ObjectLiteral(this: Evaluator, ast: any) {
@@ -163,8 +156,7 @@ export function ObjectLiteral(this: Evaluator, ast: any) {
163
156
  * function defined in one of the grammar's function pools.
164
157
  * @param {{type: 'FunctionCall', name: <string>}} ast An
165
158
  * expression tree with a FunctionCall as the top node
166
- * @returns {Promise<*>|*} the value of the function call, or a Promise that
167
- * will resolve with the resulting value.
159
+ * @returns {*} the value of the function call.
168
160
  * @private
169
161
  */
170
162
  export function FunctionCall(this: Evaluator, ast: any) {
@@ -177,7 +169,8 @@ export function FunctionCall(this: Evaluator, ast: any) {
177
169
  if (!func) {
178
170
  throw new Error(`${poolName} ${ast.name} is not defined.`)
179
171
  }
180
- return this.evalArray(ast.args || []).then((args: any[]) => func(...args))
172
+ const args = this.evalArray(ast.args || [])
173
+ return func(...args)
181
174
  }
182
175
 
183
176
  /**
@@ -185,13 +178,12 @@ export function FunctionCall(this: Evaluator, ast: any) {
185
178
  * operator's eval function.
186
179
  * @param {{type: 'UnaryExpression', operator: <string>, right: {}}} ast An
187
180
  * expression tree with a UnaryExpression as the top node
188
- * @returns {Promise<*>} resolves with the value of the UnaryExpression.
181
+ * @returns {*} the value of the UnaryExpression.
189
182
  * @constructor
190
183
  */
191
184
  export function UnaryExpression(this: Evaluator, ast: any) {
192
- return this.eval(ast.right).then((right: any) =>
193
- this._grammar.elements[ast.operator].eval(right)
194
- )
185
+ const right = this.eval(ast.right)
186
+ return this._grammar.elements[ast.operator].eval(right)
195
187
  }
196
188
 
197
189
  /**
@@ -200,17 +192,12 @@ export function UnaryExpression(this: Evaluator, ast: any) {
200
192
  */
201
193
  export function SequenceExpression(this: Evaluator, ast: any) {
202
194
  let lastValue: any
203
- let promise = this.Promise.resolve()
204
195
 
205
196
  for (const expr of ast.expressions) {
206
- promise = promise.then(() =>
207
- this.eval(expr).then((val: any) => {
208
- lastValue = val
209
- })
210
- )
197
+ lastValue = this.eval(expr)
211
198
  }
212
199
 
213
- return promise.then(() => lastValue)
200
+ return lastValue
214
201
  }
215
202
 
216
203
  /**
@@ -218,9 +205,8 @@ export function SequenceExpression(this: Evaluator, ast: any) {
218
205
  * and assigning it to the variable name on the left side.
219
206
  */
220
207
  export function AssignmentExpression(this: Evaluator, ast: any) {
221
- return this.eval(ast.right).then((value: any) => {
222
- const varName = ast.left.value
223
- this._context[varName] = value
224
- return value
225
- })
208
+ const value = this.eval(ast.right)
209
+ const varName = ast.left.value
210
+ this._context[varName] = value
211
+ return value
226
212
  }
package/src/grammar.ts CHANGED
@@ -9,7 +9,7 @@ export interface BinaryOp {
9
9
  type: 'binaryOp'
10
10
  precedence: number
11
11
  eval?: (left: any, right: any) => any
12
- evalOnDemand?: (left: { eval: () => Promise<any> }, right: { eval: () => Promise<any> }) => Promise<any>
12
+ evalOnDemand?: (left: { eval: () => any }, right: { eval: () => any }) => any
13
13
  }
14
14
 
15
15
  export interface UnaryOp {
@@ -27,7 +27,6 @@ export type GrammarElement = BinaryOp | UnaryOp | SimpleElement
27
27
  export interface Grammar {
28
28
  elements: Record<string, GrammarElement>
29
29
  functions: Record<string, (...args: any[]) => any>
30
- transforms: Record<string, (val: any, ...args: any[]) => any>
31
30
  }
32
31
 
33
32
  export const getGrammar = (): Grammar => ({
@@ -40,7 +39,6 @@ export const getGrammar = (): Grammar => ({
40
39
  '.': { type: 'dot' },
41
40
  '[': { type: 'openBracket' },
42
41
  ']': { type: 'closeBracket' },
43
- '|': { type: 'pipe' },
44
42
  '{': { type: 'openCurl' },
45
43
  '}': { type: 'closeCurl' },
46
44
  ':': { type: 'colon' },
@@ -118,20 +116,22 @@ export const getGrammar = (): Grammar => ({
118
116
  type: 'binaryOp',
119
117
  precedence: 10,
120
118
  evalOnDemand: (left, right) => {
121
- return left.eval().then((leftVal) => {
122
- if (!leftVal) {return leftVal}
123
- return right.eval()
124
- })
119
+ const leftVal = left.eval()
120
+ if (!leftVal) {
121
+ return leftVal
122
+ }
123
+ return right.eval()
125
124
  }
126
125
  },
127
126
  '||': {
128
127
  type: 'binaryOp',
129
128
  precedence: 10,
130
129
  evalOnDemand: (left, right) => {
131
- return left.eval().then((leftVal) => {
132
- if (leftVal) {return leftVal}
133
- return right.eval()
134
- })
130
+ const leftVal = left.eval()
131
+ if (leftVal) {
132
+ return leftVal
133
+ }
134
+ return right.eval()
135
135
  }
136
136
  },
137
137
  in: {
@@ -169,32 +169,11 @@ export const getGrammar = (): Grammar => ({
169
169
  * All of these are pre-evaluated to their actual values before calling
170
170
  * the function.
171
171
  *
172
- * The Jexl function should return either the transformed value, or
173
- * a Promises/A+ Promise object that resolves with the value and rejects
174
- * or throws only when an unrecoverable error occurs. Functions should
175
- * generally return undefined when they don't make sense to be used on the
176
- * given value type, rather than throw/reject. An error is only
177
- * appropriate when the function would normally return a value, but
178
- * cannot due to some other failure.
179
- */
180
- functions: {},
181
-
182
- /**
183
- * A map of transform names to transform functions. A transform function
184
- * takes one ore more arguemnts:
185
- *
186
- * - {*} val: A value to be transformed
187
- * - {*} ...args: A variable number of arguments passed to this transform.
188
- * All of these are pre-evaluated to their actual values before calling
189
- * the function.
190
- *
191
- * The transform function should return either the transformed value, or
192
- * a Promises/A+ Promise object that resolves with the value and rejects
193
- * or throws only when an unrecoverable error occurs. Transforms should
194
- * generally return undefined when they don't make sense to be used on the
195
- * given value type, rather than throw/reject. An error is only
196
- * appropriate when the transform would normally return a value, but
197
- * cannot due to some other failure.
172
+ * The Jexl function should return the resulting value, or throw when an
173
+ * unrecoverable error occurs. Functions should generally return undefined
174
+ * when they don't make sense to be used on the given value type, rather
175
+ * than throw. An error is only appropriate when the function would normally
176
+ * return a value, but cannot due to some other failure.
198
177
  */
199
- transforms: {}
178
+ functions: {}
200
179
  })
@@ -85,11 +85,16 @@ class Parser {
85
85
  const stopState = this._subParser!.addToken(token)
86
86
  if (stopState) {
87
87
  this._endSubExpression()
88
- if (this._parentStop) {
88
+ if (stopState === '_semicolon') {
89
+ handlers.semicolon.call(this)
90
+ } else if (this._parentStop) {
89
91
  return stopState
92
+ } else {
93
+ this._state = stopState
90
94
  }
91
- this._state = stopState
92
95
  }
96
+ } else if (token.type === 'semicolon' && this._stopMap[token.type]) {
97
+ return this._stopMap[token.type]
93
98
  } else if (state.tokenTypes?.[token.type]) {
94
99
  const typeOpts = state.tokenTypes[token.type]
95
100
  let handleFunc = (handlers as any)[token.type]
@@ -139,7 +144,9 @@ class Parser {
139
144
  }
140
145
 
141
146
  if (this._sequenceExpressions) {
142
- this._sequenceExpressions.push(this._tree!)
147
+ if (this._tree) {
148
+ this._sequenceExpressions.push(this._tree)
149
+ }
143
150
  const sequence: any = {
144
151
  type: 'SequenceExpression',
145
152
  expressions: this._sequenceExpressions
@@ -228,6 +235,9 @@ class Parser {
228
235
  this._parentStop = true
229
236
  endStates = this._stopMap
230
237
  }
238
+ if (states[this._state].completable && !endStates.semicolon) {
239
+ endStates = { ...endStates, semicolon: '_semicolon' }
240
+ }
231
241
  this._subParser = new Parser(this._grammar, this._lexer, exprStr, endStates)
232
242
  }
233
243
  }
@@ -72,6 +72,7 @@ export const states: States = {
72
72
  binaryOp: { toState: 'expectOperand' },
73
73
  pipe: { toState: 'expectTransform' },
74
74
  dot: { toState: 'traverse' },
75
+ openBracket: { toState: 'filter' },
75
76
  question: { toState: 'ternaryMid', handler: h.ternaryStart },
76
77
  semicolon: { handler: h.semicolon }
77
78
  },
@@ -101,7 +102,8 @@ export const states: States = {
101
102
  dot: { toState: 'traverse' },
102
103
  openBracket: { toState: 'filter' },
103
104
  pipe: { toState: 'expectTransform' },
104
- question: { toState: 'ternaryMid', handler: h.ternaryStart }
105
+ question: { toState: 'ternaryMid', handler: h.ternaryStart },
106
+ semicolon: { handler: h.semicolon }
105
107
  },
106
108
  completable: true
107
109
  },
@@ -111,7 +113,8 @@ export const states: States = {
111
113
  dot: { toState: 'traverse' },
112
114
  openBracket: { toState: 'filter' },
113
115
  pipe: { toState: 'expectTransform' },
114
- question: { toState: 'ternaryMid', handler: h.ternaryStart }
116
+ question: { toState: 'ternaryMid', handler: h.ternaryStart },
117
+ semicolon: { handler: h.semicolon }
115
118
  },
116
119
  completable: true
117
120
  },
@@ -122,7 +125,8 @@ export const states: States = {
122
125
  openBracket: { toState: 'filter' },
123
126
  openParen: { toState: 'argVal', handler: h.functionCall },
124
127
  pipe: { toState: 'expectTransform' },
125
- question: { toState: 'ternaryMid', handler: h.ternaryStart }
128
+ question: { toState: 'ternaryMid', handler: h.ternaryStart },
129
+ semicolon: { handler: h.semicolon }
126
130
  },
127
131
  completable: true
128
132
  },
package/src/types.ts CHANGED
@@ -104,4 +104,7 @@ export type AstNodeUnion =
104
104
  | SequenceExpression
105
105
  | AssignmentExpression
106
106
 
107
- export type NodeByType<T extends AstNodeUnion['type']> = Extract<AstNodeUnion, { type: T }>
107
+ export type NodeByType<T extends AstNodeUnion['type']> = Extract<
108
+ AstNodeUnion,
109
+ { type: T }
110
+ >
@@ -1,13 +0,0 @@
1
- declare class PromiseSync<T = unknown> {
2
- value?: T;
3
- error?: unknown;
4
- constructor(fn: (resolve: (val: T) => void, reject: (error: unknown) => void) => void);
5
- catch<TResult = never>(rejected: (error: unknown) => TResult): PromiseSync<T | TResult>;
6
- then<TResult1 = T, TResult2 = never>(resolved: (val: T) => TResult1, rejected?: (error: unknown) => TResult2): PromiseSync<TResult1 | TResult2>;
7
- _reject(error: unknown): void;
8
- _resolve(val: any): void;
9
- static all<T>(vals: (PromiseSync<T> | T)[]): PromiseSync<T[]>;
10
- static resolve<T>(val?: T): PromiseSync<T>;
11
- static reject(error: unknown): PromiseSync<never>;
12
- }
13
- export default PromiseSync;
@@ -1,80 +0,0 @@
1
- "use strict";
2
- /*
3
- * Jexl
4
- * Copyright 2020 Tom Shawver
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- class PromiseSync {
8
- constructor(fn) {
9
- fn(this._resolve.bind(this), this._reject.bind(this));
10
- }
11
- catch(rejected) {
12
- if (this.error) {
13
- try {
14
- this._resolve(rejected(this.error));
15
- }
16
- catch (e) {
17
- this._reject(e);
18
- }
19
- }
20
- return this;
21
- }
22
- // eslint-disable-next-line unicorn/no-thenable
23
- then(resolved, rejected) {
24
- if (!this.error) {
25
- try {
26
- this._resolve(resolved(this.value));
27
- }
28
- catch (e) {
29
- this._reject(e);
30
- }
31
- }
32
- if (rejected) {
33
- this.catch(rejected);
34
- }
35
- return this;
36
- }
37
- _reject(error) {
38
- this.value = undefined;
39
- this.error = error;
40
- }
41
- _resolve(val) {
42
- if (val instanceof PromiseSync) {
43
- if (val.error) {
44
- this._reject(val.error);
45
- }
46
- else {
47
- this._resolve(val.value);
48
- }
49
- }
50
- else {
51
- this.value = val;
52
- this.error = undefined;
53
- }
54
- }
55
- static all(vals) {
56
- return new PromiseSync((resolve) => {
57
- const resolved = vals.map((val) => {
58
- while (val instanceof PromiseSync) {
59
- if (val.error) {
60
- if (val.error instanceof Error) {
61
- throw val.error;
62
- }
63
- throw new Error(typeof val.error === 'string' ? val.error : JSON.stringify(val.error));
64
- }
65
- val = val.value;
66
- }
67
- return val;
68
- });
69
- resolve(resolved);
70
- });
71
- }
72
- static resolve(val) {
73
- return new PromiseSync((resolve) => { resolve(val); });
74
- }
75
- static reject(error) {
76
- return new PromiseSync((_, reject) => { reject(error); });
77
- }
78
- }
79
- exports.default = PromiseSync;
80
- //# sourceMappingURL=PromiseSync.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PromiseSync.js","sourceRoot":"","sources":["../src/PromiseSync.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAEH,MAAM,WAAW;IAIf,YAAY,EAAyE;QACnF,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IACvD,CAAC;IAED,KAAK,CAAkB,QAAqC;QAC1D,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,IAAI,CAAC;gBACH,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAQ,CAAC,CAAA;YAC5C,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;YACjB,CAAC;QACH,CAAC;QACD,OAAO,IAAW,CAAA;IACpB,CAAC;IAED,+CAA+C;IAC/C,IAAI,CACF,QAA8B,EAC9B,QAAuC;QAEvC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;YAChB,IAAI,CAAC;gBACH,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAU,CAAQ,CAAC,CAAA;YACjD,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;YACjB,CAAC;QACH,CAAC;QACD,IAAI,QAAQ,EAAE,CAAC;YAAA,IAAI,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;YACZ,CAAC,CAAC,CAAA;YACF,OAAO,CAAC,QAAQ,CAAC,CAAA;QACnB,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,CAAC,OAAO,CAAI,GAAO;QACvB,OAAO,IAAI,WAAW,CAAI,CAAC,OAAO,EAAE,EAAE,GAAG,OAAO,CAAC,GAAQ,CAAC,CAAA,CAAC,CAAC,CAAC,CAAA;IAC/D,CAAC;IAED,MAAM,CAAC,MAAM,CAAC,KAAc;QAC1B,OAAO,IAAI,WAAW,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,CAAA,CAAC,CAAC,CAAC,CAAA;IAC1D,CAAC;CACF;AAED,kBAAe,WAAW,CAAA"}
@@ -1,13 +0,0 @@
1
- declare class PromiseSync<T = unknown> {
2
- value?: T;
3
- error?: unknown;
4
- constructor(fn: (resolve: (val: T) => void, reject: (error: unknown) => void) => void);
5
- catch<TResult = never>(rejected: (error: unknown) => TResult): PromiseSync<T | TResult>;
6
- then<TResult1 = T, TResult2 = never>(resolved: (val: T) => TResult1, rejected?: (error: unknown) => TResult2): PromiseSync<TResult1 | TResult2>;
7
- _reject(error: unknown): void;
8
- _resolve(val: any): void;
9
- static all<T>(vals: (PromiseSync<T> | T)[]): PromiseSync<T[]>;
10
- static resolve<T>(val?: T): PromiseSync<T>;
11
- static reject(error: unknown): PromiseSync<never>;
12
- }
13
- export default PromiseSync;
@@ -1,78 +0,0 @@
1
- /*
2
- * Jexl
3
- * Copyright 2020 Tom Shawver
4
- */
5
- class PromiseSync {
6
- constructor(fn) {
7
- fn(this._resolve.bind(this), this._reject.bind(this));
8
- }
9
- catch(rejected) {
10
- if (this.error) {
11
- try {
12
- this._resolve(rejected(this.error));
13
- }
14
- catch (e) {
15
- this._reject(e);
16
- }
17
- }
18
- return this;
19
- }
20
- // eslint-disable-next-line unicorn/no-thenable
21
- then(resolved, rejected) {
22
- if (!this.error) {
23
- try {
24
- this._resolve(resolved(this.value));
25
- }
26
- catch (e) {
27
- this._reject(e);
28
- }
29
- }
30
- if (rejected) {
31
- this.catch(rejected);
32
- }
33
- return this;
34
- }
35
- _reject(error) {
36
- this.value = undefined;
37
- this.error = error;
38
- }
39
- _resolve(val) {
40
- if (val instanceof PromiseSync) {
41
- if (val.error) {
42
- this._reject(val.error);
43
- }
44
- else {
45
- this._resolve(val.value);
46
- }
47
- }
48
- else {
49
- this.value = val;
50
- this.error = undefined;
51
- }
52
- }
53
- static all(vals) {
54
- return new PromiseSync((resolve) => {
55
- const resolved = vals.map((val) => {
56
- while (val instanceof PromiseSync) {
57
- if (val.error) {
58
- if (val.error instanceof Error) {
59
- throw val.error;
60
- }
61
- throw new Error(typeof val.error === 'string' ? val.error : JSON.stringify(val.error));
62
- }
63
- val = val.value;
64
- }
65
- return val;
66
- });
67
- resolve(resolved);
68
- });
69
- }
70
- static resolve(val) {
71
- return new PromiseSync((resolve) => { resolve(val); });
72
- }
73
- static reject(error) {
74
- return new PromiseSync((_, reject) => { reject(error); });
75
- }
76
- }
77
- export default PromiseSync;
78
- //# sourceMappingURL=PromiseSync.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PromiseSync.js","sourceRoot":"","sources":["../src/PromiseSync.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW;IAIf,YAAY,EAAyE;QACnF,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IACvD,CAAC;IAED,KAAK,CAAkB,QAAqC;QAC1D,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,IAAI,CAAC;gBACH,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAQ,CAAC,CAAA;YAC5C,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,IAAI,CAAC,OAAO,CAAC,CAAC,C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